-1

我决定手动制作一个四面体(4 个三角形)并旋转它。它有点工作,唯一的问题是其中一个三角形从未被绘制。在我的示例代码中,首先绘制前三角形(红色),但是当我渲染它时,它永远不会出现。而是出现了左三角形(白色)和右三角形(蓝色)的一半。但是,当我更改代码并将前三角形的三个顶点放在其他三角形的顶点之后时,我确实得到了前三角形。然而,其他两个三角形之一消失了。我究竟做错了什么?

#include <stdio.h>
#include <stdlib.h>
#include <GLUT/glut.h>
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>

float posx = 0;
float posy = 0;

void drawLines(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //THIS ROTATES THE WHOLE SCENCE
    /*glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(posx, 0.0f, 1.0f, 0.0f);*/

    //THIS ROTATES ONLY THE OBJECTS INSIDE THE GLPUSHMATRIX AND GLPOPMATRIX FUNCTIONS
    glPushMatrix();
    glRotatef(posx, 0.0f, 1.0f, 0.0f);

    //TETAEDRON BY HAND
    glBegin(GL_TRIANGLES);

    //front triangle
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    glVertex3f(0.0f, 5.0f, 0.0f);
    glVertex3f( -5.0f, -5.0f, 0.0f);
    glVertex3f( 5.0f,  -5.0f, 0.0f);

    //right side triangle
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glVertex3f( 5.0f,  -5.0f, 0.0f);
    glVertex3f(0.0f, 5.0f, 0.0f);
    glVertex3f( 0.0f,  -5.0f, -5.0f);

    //left side triangle
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    glVertex3f( -5.0f, -5.0f, 0.0f);
    glVertex3f(0.0f, 5.0f, 0.0f);
    glVertex3f( 0.0f,  -5.0f, -5.0f);

    //bottom triangle
    glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
    glVertex3f( -5.0f, -5.0f, 0.0f);
    glVertex3f( 5.0f,  -5.0f, 0.0f);
    glVertex3f( 0.0f,  -5.0f, -5.0f);

    glEnd();

    //glutSolidTeapot(5.0);
    glPopMatrix();
    glutWireTeapot(5.0);
    glFlush();
}

void init()
{
    /*glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);*/
    glClearColor(0, 0, 0, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -10.0f, 10.0f);//range of x,y,z axis
    //gluOrtho2D(0.0f, 500.0f, 500.0f, 0.0f);//setup the camera in 2d mode

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 'd':posx+=10;
            printf("derecha %f\n",posx);
            break;
        case 'a':posx-=10;
            printf("izquierda\n");
            break;
        case 'w':posy-=10;
            printf("arriba\n");
            break;
        case 's':posy+=10;
            printf("abajo\n");
            break;
     }

 //glMatrixMode(GL_MODELVIEW);
 //glLoadIdentity();
 //glViewport(100.0f, 100.0f, 500.0f, 500.0f);
 //glTranslatef(posx, posy, 0.0);
 if(posx>=360)
     posx = 0;
 //glRotatef(posx, 0.0f, 1.0f, 0.0f);
}

int main(int argc, const char * argv[])
{

    // insert code here...

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    glutInitWindowPosition(400, 200);
    glutInitWindowSize(500, 500);
    glutCreateWindow("OpenGLTest");

    init();

    glutDisplayFunc(drawLines);
    glutIdleFunc(drawLines);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 1;
}
4

1 回答 1

2

您需要启用深度测试。init()目前它在您的函数中被注释掉。

于 2013-07-11T06:00:44.170 回答