简体   繁体   中英

Overlaping rectangels and clipping at the bottom in opengl

I have to make some 3D figures using opengl (the older versions). I created 3 GL_POLYGON rectangles that are connected to one another and which have different colors.

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() . 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.

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 ,如果未实现,请绘制顺序规则。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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