overdrawing triangles with different color

I have a complex scene of triangulated surfaces. To give feedback to the user I want to change the color of a set of triangles.

I want to avoid clearing the screen and completely redrawing it with the new colors (most triangles have not changed and therefore its a bit of a waste).

So I just draw the triangles with the changed color again on top of the ones that are already there. In principle that works fine, except that you can still see some triangles in the old color, apparantly from the z buffering, that happens to sort them the other way round than desired.

Any idea how to overcome this?

It gets more complicated when you overdraw something in a solid color with a translucent color, the effect is that you see the first drawn triangles through your translucency.

Is there maybe a way to “undraw” or erase a set of triangles?

Try mess with the stencil buffer some.

With HW spencil, and not that much triangles, you can draw each triangle with a unique stencil value. And when you want to redraw a triangle, just look up it’s value, and render the new one using the the stencil buffer, and depthtest and depthbufferupdate disabled.

If you got too many triangles, just disable the depthbufferupdate, and draw the triangle again with slightly changed coordinates. If you draw the polygon a little bit closer to the screen, it won’t get that much bigger (think there is a function for setting polygon offset, just look it up somewhere) and you will eliminate the z-fighting.

These methods does not apply to translucent triangles. If you want that feature too, you need to redraw the scene with proper sorting. But as long as you don’t have huge scenes that takes a while to render, what’s the problem with redrawing?

And yeah, if you render two identical triangles you can get z-fighting. If you are lucky, you can solve this with another depthtest function. Try something like GL_LEQUAL. It will draw the pixel if the pixels z-value is less OR EQUAL to the depthbuffer.