奇怪的输出:Opengl

时间:2015-02-20 18:34:15

标签: c opengl glut

我必须使用openGL和过剩来创建一个房间。从我在输出中看到的看起来左墙被绘制但我根本看不到前墙。我只需画2个墙,让相机指向2个墙相遇的角落。知道我做错了吗?

我的代码:

#include <gl/gl.h>
#include <gl/glut.h>

/*defining colors used in the room*/
GLfloat black[] = {0.0, 0.0, 0.0, 1.0};
GLfloat white[] = {1.0, 1.0, 1.0, 1.0};
GLfloat brown[] = {165.0/255.0, 42.0/255.0, 42.0/255.0, 1.0};

void drawFloor()
{
    glBegin(GL_POLYGON);
    glColor4fv(black);
    glVertex3f(0.0, 0.0, -3.0);
    glVertex3f(1.0, 0.0, -3.0);
    glVertex3f(2.0, 0.0, -3.0);
    glEnd();
}

void drawWalls()
{
    glBegin(GL_QUAD_STRIP);
    glColor4fv(white);
    glPolygonMode(GL_FRONT, GL_FILL);
    int i;
    //draw left wall
    for (i = 0; i <= 5; i++)
    {
        glVertex3f(0.0, 0.0, i);
        glVertex3f(0.0, 1.0, i);
    }
    //draw front wall
    for(i = 1; i <= 5; i++)
    {
        glVertex3f(i, 0, 5);
    }
    glEnd();
}

void display()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0,  1.0, 0.0);
    drawFloor();
    drawWalls();
    glFlush();
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat) w / (GLfloat) h, 1, 500);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void idleFunc()
{
    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("My room");
    glEnable(GL_DEPTH_TEST);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idleFunc);
    glutMainLoop();
}

0 个答案:

没有答案