I am designing a camouflage distortion for a computer game which involves drawing the background over an object. To elaborate, the background is copied into a texture using glCopyTexImage2D, the object is drawn (currently a sprite) using the stencil buffer to mask out the object’s shape, and then the copied background is shunted a few pixels to make it appear slightly distorted before it is finally drawn over the object, using the stencil so it knows where to draw. The effect is quite nice, but I have run into a problem. When the object is fully visible (no camouflage), the frame rate is in the region of 400 with the rest of the objects drawn (currently only a mesh of triangles for terrain), but as soon as it cloaks, the frame rate plummets to 30. And if I start drawing multiple cloaked objects, it slows even more. Is there any way to improve performance here? I have tried using GL_RGB as a pixel format, but that has not helped too much.
Have you tried glCopyTexSubImage2D?
CopyTexSubImage() is certainly better than CopyTexImage().
Also, if you draw lots of cloaked objects, it might be faster to copy all of the screen to a texture, and use that same texture for all the cloaked objects. If there are typically only a few cloaked objects, you’re probably better off just copying the rectangle of the screen that the stencil will cover, plus the distorsion offset.
Copying the whole screen does tend to take up a lot of memory, but I can see it being an overall better method in that it is better for multiple cloaked objects and I do not have to worry about the texture changing size (if the object moves closer, etc.) I’ll experiment a bit.
Actually, it may not be the best solution, because if the same image is used for the cloaking overlay, it may cause problems if the cloaked object moves over another object.
Have you identified exactly what is causing the slow down? In my experience glCopyTexImage2D is quite fast.
You mentioned shuntting the pixels - how are you doing this?
You don’t specifically say how you blit the camo to the screen, but talk of stencils suggest perhaps either writepixels or applying say a fullscreen quad with the camo image on it, so it only draws into the unstenciled area.
If so have you considered using projective textures on the object itself, instead of using the stencil path?
Personally I would check which aspect of this camo effect is killing the framerate. I doubt it is glCopyTexImage2D. So try disabling the drawing of the camo, just do the glCopyTexImage2D see what speed you get etc. Also if you’re generating mipmaps from the camo that can be a bit of a hit.