I’m working on a 2D game and am using OpenGL to draw the screen from sprites converted into textures. Everything works fine, except that it takes quite a lot of time to draw the entire screen every refresh as I’m drawing a bunch of textured quads (I’m mimicking a 80x50 console output, so am drawing 4000 quads - one for each “character” on the “console”). I want to employ dirty rectangles and only redraw the parts of the screen that have changed. I figured I could just use glCopyTexImage2D to copy the screen into a few power of 2 textures, but I just can’t get it to work. Here’s a code snippet:
// Getting the pixels from the upper left 512x512 part of the screen: glReadBuffer(GL_Front); glBindTexture(GL_TEXTURE_2D, ScreenTextureID[1, 1]); glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 512, 512, 0); // Clear the screen: glClear(GL_COLOR_BUFFER_BIT); // Drawing the saved screen texture back to the screen: glBindTexture(GL_TEXTURE_2D, ScreenTextureID[1, 1]); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex3f(0, 0, 0.0); glTexCoord2f(1, 0); glVertex3f(512, 0, 0); glTexCoord2f(1, 1); glVertex3f(512, 512, 0); glTexCoord2f(0, 1); glVertex3f(0, 512, 0); glEnd();
This just draws a flickering image with some parts of the correct image mirrored and drawn twice. I’ve tried drawing another image and that works fine, so the error seems to be in the part of my code that saves the screen pixel information into the texture, not in the part that draws it back.