Rendering to an offscreen buffer.

I’m doing a small 2D-game which constantly displays 64*45 number of tiles. Re-drawing those 2880 tiles every frame is too slow. The good thing is that about 95% of the tiles are static and do not need to updated every frame. So I basically want to render only the tiles that need to be rendered (the ones that are changed).

I tried to create an empty texture buffer of the size of my level and use glCopyTexSubImage2D() every time a tile needs to be rendered, but for some reason it didn’t work and just displayed a blank, white texture. I doing it somehow like this:

for each tile in level
 if tile needs to be rendered
   bind "gameimages" texture
   render tile as textured quad
   bind "buffer" texture
   GL.glCopyTexSubImage2D(GL.GL_TEXTURE_2D, 0, tile.x, tile.y, tile.x, tile.y, TILE_WIDTH, TILE_HEIGHT);
finally render the "buffer" texture

The most annoying thing in this method, is that I have to bind the “gameimages” texture for every tile again and again.
Am I doing something totally wrong?
What do you think about this method generally?

Could someone show a quick example code how I should do this. Any better ideas than using an extra buffer for this?

btw. Is it a good idea to generally have glClear() at the beginning of (lowest level) render()-method?


Looks like overkill.
If the texture is not working make sure you have the texture filters set correctly, mipmaps are default, no mipmaps downloaded means texunit is switched off. This needs to be set at least once per texture object because it’s texture object state and switched automaticall by following glBindTexture calls.

If you render double buffered with a pixelformat which has PFD_SWAP_COPY in the flags, you could update the changed tiles in the back buffer, swap-copy them to the display and so on.
This is not working if the back buffer is using a PFD_SWAP_EXCHANGE method. After a SwapBuffers the backbuffer contents are undefined.
You can also omit the swap buffers and use glCopyPixels fron back to front of the union-rectangle of changed tiles.
A faster method (than your glCopyTexSubImage) to save the backbuffer are the buffer_region extensions. Look at WGL_ARB_buffer_region extension here

You can also use a render to texture p-buffer to render in a non-visible unclipped buffer used directly for texturing, but this gets heavy handed.

Ok thanks a lot, I’ll try these.

Though I got the impression that the WGL_ARB_buffer_region is available only on Windows (all of these WGL_ extensions are?), so that’s not an option for me.

Hint: When using glCopyPixels instead of swap buffers, make sure you disable all operations which prohibit a simple color-to-color buffer copy, like depth test, blending, logic ops.