Hi guys…
hoping for some help here.
I’m new to openGL, but think i’m getting my head wrapped around the basics.
At its simplest, i want my app to have two OpenGLComponents.
Each has a (different) juce Image that gets drawn.
The whole purpose of this exercise is so I can get the rendering happening on a separate thread, to free up the GUI (that’s possible using OpenGL, right!?). I’m sure I"ll have some questions about that, sooner or later, but right now I’m more focussed on just drawing my images using OpenGL…
Ok, first question… I can map my Image to a texture… but do I need to bind it? If I bind both components to ‘texture 1’ are they essentially sharing a texture, or do they each get their own instance…?
Yes, I probably sound confused.
Using the code below, I’m able to draw textures to the screen in each component. Yay. But, sometimes component2’s image is getting drawn into component1, and vice versa… hence, my questions about binding textures.
here’s some sample code… any help greatly appreciated!
[code]
void MyOpenGLComp::MoveImageToTexture()
{
// This gets called periodically (when the Image changes), NOT just in newOpenGLContextCreated)
if(myImage)
{
// needed?
//glDeleteTextures( 1, &myGLTextureId );
// select our current texture
//glBindTexture( GL_TEXTURE_2D, myGLTextureId );
int stride, pixStride;
const void* pixels = myImage->lockPixelDataReadOnly (0, 0, myImage->getWidth(), myImage->getHeight(), stride, pixStride);
glTexImage2D (GL_TEXTURE_2D, 0, 4, myImage->getWidth(), myImage->getHeight(),
0, GL_BGR_EXT, //GL_RGB, // Juce seems to use BGR for its internal pixel format, so try using GL_BGR_EXT instead of GL_RGB
GL_UNSIGNED_BYTE, pixels);
myImage->releasePixelDataReadOnly (pixels);
}
}
void MyOpenGLComp::newOpenGLContextCreated()
{
glEnable (GL_TEXTURE_2D);
glEnable (GL_BLEND);
glShadeModel (GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); // what are these?
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
MoveImageToTexture();
glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void MyOpenGLComp::renderOpenGL()
{
if(!myImage || myImage->getWidth() <= 0)
return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0.0, getWidth(), 0.0, getHeight(), 0, 1);
// do i need to bind??
//glBindTexture( GL_TEXTURE_2D, myGLTextureId );
glBegin(GL_QUADS);
glTexCoord2i (0, 0); glVertex2f (0.0, getHeight()); // top left
glTexCoord2i (1, 0); glVertex2f (myImage->getWidth(), getHeight()); // top right
glTexCoord2i (1, 1); glVertex2f (myImage->getWidth(), 0.0); // bottom right
glTexCoord2i (0, 1); glVertex2f (0.0, 0.0); // bottom left
glEnd();
}[/code]