简体   繁体   English

重叠的矩形和opengl底部的剪切

[英]Overlaping rectangels and clipping at the bottom in opengl

I have to make some 3D figures using opengl (the older versions). 我必须使用opengl(旧版本)制作一些3D图形。 I created 3 GL_POLYGON rectangles that are connected to one another and which have different colors. 我创建了3个GL_POLYGON矩形,它们相互连接并且颜色不同。

My problem is that when the figure rotates the last color added (last added rectangle) is always above the other ones. 我的问题是,当图形旋转时,最后添加的颜色(最后添加的矩形)始终位于其他颜色之上。 For example the cyan one is above the pink and the yellow one, and the pink one is above the yellow one. 例如,青色的一种高于粉红色的一种,而黄色的一种高于粉红色的一种,而粉红色的一种高于黄色的一种。 I also noted some clipping at the bottom of the figure, which I think is caused by gluPerspective() . 我还注意到该图的底部有一些剪裁,我认为这是由gluPerspective()引起的。 What I'm trying to achieve is having the eye look from z+ to the center and the figure rotating around the y+ axis ( which I think I managed to do) and also to have the overlapping and clipping removed. 我要实现的目的是使眼睛从z +到中心,并且图形围绕y +轴旋转(我认为我设法做到了),并且还去除了重叠和修剪。

Any ideas why this happens and how to fix it? 有什么想法为什么会发生以及如何解决?

The code is bellow: 代码如下:

#include <GL/glfw.h>

int main()
{
int     width, height;
int     frame = 0;
bool    running = true;

glfwInit();

if( !glfwOpenWindow( 700, 800, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) )
{
    glfwTerminate();
    return 0;
}

glfwSetWindowTitle("GLFW Application");

while(running)
{
    frame++;

    glfwGetWindowSize( &width, &height );
    height = height > 0 ? height : 1;

    glViewport( 0, 0, width, height );

    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f );


    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    gluLookAt(0.0f, 0.0f, 40.0f,
            0.0f, 0.0f, 0.0f,
            0.0f, 1.0f, 0.0f );


    glRotatef(frame, 0.0f, 1.0f, 0.0f);


    glColor3ub(255,255,0);
    glBegin( GL_POLYGON );
      glVertex3f(5.0f, 0.0f, 0.0f);
      glVertex3f(0.0f, 0.0f, 0.0f);
      glVertex3f(0.0f, 10.0f, 0.0f);
      glVertex3f(5.0f, 10.0f, 0.0f);
    glEnd();
     glColor3ub(255,0,255);
    glBegin( GL_POLYGON );
      glVertex3f(5.0f, 0.0f, -2.0f);
      glVertex3f(0.0f, 0.0f, -2.0f);
      glVertex3f(0.0f, 10.0f, -2.0f);
      glVertex3f(5.0f, 10.0f, -2.0f);
    glEnd();
    glColor3ub(0,255,255);
    glBegin( GL_POLYGON );
      glVertex3f(5.0f, 0.0f, 0.0f);
      glVertex3f(5.0f, 0.0f, -2.0f);
      glVertex3f(5.0f, 10.0f, -2.0f);
      glVertex3f(5.0f, 10.0f, 0.0f);
    glEnd();

    glfwSwapBuffers();

    running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);}

glfwTerminate();

return 0;
}

我在您的代码中看不到深度功能,请尝试查找depth bufferGL_DEPTH_TEST ,如果未实现,请绘制顺序规则。

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

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