简体   繁体   English

在Opengl中渲染。 重叠对象

[英]Render in Opengl. Overlapping Objects

I'm having trouble rendering overlapping objects in OpenGL. 我在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. 我正在调用绘制对象的函数,以便如果我先调用drawB()然后再调用drawA(),则A在B之上,而B可能被A隐藏。

Everything works fine until it happens that A is a line (GL_LINES) and B is a polygon (GL_POLYGON). 一切正常,直到发生A是一条线(GL_LINES)和B是一条多边形(GL_POLYGON)为止。 If this happens, B, which should be placed at the bottom, is drawn over A. 如果发生这种情况,应将B放置在A的底部,该B应该放在底部。

At this moment I have disabled GL_DEPTH_TEST. 目前,我已禁用GL_DEPTH_TEST。 I tried enabling it, I tried glDepthMask and glPolygonOffset but none has worked. 我尝试启用它,尝试了glDepthMask和glPolygonOffset,但没有一个起作用。 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. **编辑:显然问题是我没有清除GL_COLOR_BUFFER_BIT,但是由于速度问题我没有清除。 I have to find a way to without erasing the gl_color_buffer_bit, draw the objects in order ** 我必须找到一种方法,而不会擦除gl_color_buffer_bit,按顺序绘制对象**

It has an ortographic projection defined by glOrtho(r.left(),r.right(),r.bottom(),r.top(),0,2); 它具有由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();

But the lines appear hidden by the polygon 但是线条看起来被多边形隐藏了

glVertex2f gives good results as long as you draw back-to-front. 只要从后往前绘制, glVertex2f产生良好的效果。

Since you want a different order, you need to specify the Z coordinate to determine which is on top. 由于需要不同的顺序,因此需要指定Z坐标以确定哪个在最上面。 glVertex3f ftw. glVertex3f ftw。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM