Render in Opengl. Overlapping Objects
I'm having trouble rendering overlapping objects in OpenGL.
I am calling to functions that draw objects so that if I call drawB () and then drawA (), A is above B and B may be hidden by A.
Everything works fine until it happens that A is a line (GL_LINES) and B is a polygon (GL_POLYGON). If this happens, B, which should be placed at the bottom, is drawn over A.
At this moment I have disabled GL_DEPTH_TEST. I tried enabling it, I tried glDepthMask and glPolygonOffset but none has worked. I assume that I have been using them wrong. Any ideas how can I draw this objects in the correct order?
**Edit: Apparently the problem was that i wasn't clearing the GL_COLOR_BUFFER_BIT, but i'm not clearing as a matter of speed. I have to find a way to without erasing the gl_color_buffer_bit, draw the objects in order **
It has an ortographic projection defined by glOrtho(r.left(),r.right(),r.bottom(),r.top(),0,2);
When you call the function, background objects are drawn, then foregrounds. I want that the background objects are drawn belows foregrounds.
The idea is that i have two layouts, one for foreground and one for background. First every object in the background draws itself. Then every object in the foreground do the same. Every object has a Display List wich transform it's own coordinate system acoording their needs. The idea of that display lists is to to allow object nesting, each object with his own coordinate system, in spite of their parent coordinate system.
In a moment i have in background a set of objects that does
glBegin (GL_POLYGON) ;
for (angle = 0; angle<PI * 2;angle+=1){
glVertex2f (X + sin(angle) * Radius, Y + cos(angle) * Radius);
}
glEnd () ;
the another object, in the foreground layout does
glBegin(GL_LINES);
glVertex2f(m_x[m_currentPositionP1],m_y[m_currentPositionP1]);
glVertex2f(m_x[m_currentPositionP],m_y[m_currentPositionP]);
glEnd();
B开发者_高级运维ut the lines appear hidden by the polygon
glVertex2f
gives good results as long as you draw back-to-front.
Since you want a different order, you need to specify the Z coordinate to determine which is on top. glVertex3f
ftw.
精彩评论