There's something fundamental I'm not doing right, because I'm having this problem in GLUT, OpenGL, and OpenCL. The basic idea is I'm using OpenGL for 2D raster graphics by rendering a quad with a texture. I use glTexSubImage2D to update the texture during Idle, and then during redraw I draw the quad with texture coordinates.
Everything works fine unless I combine updating the texture with Window resize. (I can resize the window with no updates, or see my updates without resizing the window.) When I try both, as soon as a resize occurs, the texture rapidly fragments into rectangles and garbage throughout memory, as if the texture is lost. (The artifacts are not simple ones as you'd expect with incorrect pitch, etc.)
I'll describe what I'm doing in GLUT, since it's the easiest, and I'm hoping someone can catch an error. This is in Mac OS-X Snow Leopard 10.6.3 / Xcode. I am VERY GRATEFUL for any help, as this has help me up for almost 2 years with no solution.
(1) Initial setup before glutMainLoop(); I make a buffer for the texture. It's not a power of 2 size, but I've tried power of 2 sizes and got the same result:
unsigned char* pucMyBuffer = malloc(w * h * 4);
2) Then I set up the openGL context for it as so:
// Create a new texture!
glEnable( GL_TEXTURE_2D ); // Allow texturing!
// This is needed!
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
w,h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pucMyBuffer);
// Try to get Alpha to work:
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3) After entering glutMainLoop, I draw the window like this. Currently w and h are both 1.0 -> it depends whether I want
to always fill the window with the texture or make the texture
per pixel accurate:
glColor4f(1.0, 1.0, 1.0, fGlobalAlpha);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1,0); glVertex2f(w, 0);
glTexCoord2f(1,1); glVertex2f(w, h);
glTexCoord2f(0, 1); glVertex2f(0, h);
4) I handle a resize like this. Again, the option depends
on whether I want constant pixels or view
//glViewport( 0, 0, w, h );
glViewport( 0, 0, 1920, 1200 );
NOTE THAT EVERYTHING SO FAR WORKS PERFECTLY!
5) And on the Idle call, I do this to keep the texture changing:
(Note that the hard coded sizes were just part of my attempts to debug what was going on)
// Do a loaded update:
glTexSubImage2D(GL_TEXTURE_2D, 0, // MIP level
0,0, // SrcX, SrcY
1920,1200, // W,H
GL_RGBA, GL_UNSIGNED_BYTE, pucMyBuffer);
This last step works as long as I don't resize, but it I resize after changing even 1 byte of the buffer, the display goes haywire.
Any ideas are appreciated more than I can say. I am close to having to abandon the use of OpenGl after over a year of trying because I can find no programmer that knows anything about it.