Problem on overlapped region

there are two size rectangles. The small one is red and placed inside the big one (green). My problem is that two rectangles are overlapped and the colors sometimes show red and green if I rotated the camera. I would like to know how to do to prevent this problem. I want to show red color on smaller rectangle only. Thanks.

Can you post a screenshot of the behavior you’re getting? And what are you using for your graphics? Java2D, Slick, LWJGL, something else?

https://lh4.googleusercontent.com/-dLorCj-E2DM/UFuq3oMiA_I/AAAAAAAAAaU/IxWUgrTSQ-8/w497-h373/jogl_colorproblem.png

bigger rectangle is green and smaller is blue. When zooming or rotating the camera, the blue color will disappear sometimes. I would like the blue is solid on the surface of green rectangle.

It seems what you’re seeing is something sometimes called “z-buffer fighting”: There are different polygons that have the same z-values in the z-buffer, so it will become a matter of rounding differences deciding which block will become visible.

You could try to prevent this situation of the 2 blocks occupying the same space (i.e. just make the green block smaller so they don’t overlap anymore).
If the blue block is really a bit wider/deeper than the green one (which is just taller), you could try to improve the situation by moving the camera’s near-plane a bit forward (although this is not the best solution).

What are you trying to achieve by having two cubes in the same place? If it’s just for the colour difference then maybe you could just use a texture. :stuck_out_tongue:

erikd is right, use glPolygonOffset or use 2 quads with no overlap.

What are your z-values for the near plane and the far plane?

Thanks for all your replied. Two cubes are possible to move anywhere, so I cannot make different z positions. Is glPolygonOffset able to fix the problem? Any good example for using glPolygonOffset? Thanks.

All cubes are printed by order. Is possible the last order cube overlap the previous cube? For example, both cubs are overlapped in some surfaces. Green cube is printed first and blue cube is printed second. Can jogl allow to display blue cube color on overlapped surfaces only?