Syafiq Kamarul Azman
Syafiq Kamarul Azman

Reputation: 862

OpenGL Teapot drawing error

I've been working on an object model but it seems that something is wrong. This is what I get when I draw a teapot:

Teapot

As you can see, background objects appear in foreground like the ring at the top. I've been working on everything I can changing things like the clipping plane and the the depth buffer but I just can't seem to get the model to appear solid. I have depth enabled along with cull face and I'm clearing the depth buffer and color buffer. I can't tell what's wrong.

Here is my code listing:

void displayLighting() {
    if ( specular )
        glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    else
        glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);

    glLightfv(GL_FRONT, GL_SHININESS, shininess);

    lightPos[0] = lightDist*sinf(lightAngle);
    lightPos[1] = lightDist; 
    lightPos[2] = lightDist*cosf(lightAngle);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos );
}

void drawTeapot() {
    float diffuseMaterial[] = { 1.f, 1.f, 1.f };
    glTranslatef(0.f, -0.f, -0.f);

    glShadeModel(GL_SMOOTH);

    glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
    glutSolidTeapot(20.0f);
}

void renderScene(void) {
    glClearColor(0.f,0.f,0.f,1.f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt( zDist*(sinf(yAngle)),zDist*(sinf(xAngle)),zDist*(cosf(xAngle))*(cosf(yAngle)),0,0,0,0,1,0 );

    displayLighting();

    drawTeapot();

    glutSwapBuffers();
}

void changeSize(int width, int height) {
    float ratio;

    ratio =  w * 1.0f / h;

    glMatrixMode(GL_PROJECTION);
    glViewport(0, 0, w, h);
    glLoadIdentity();
    gluPerspective(45.0f, ratio, 100.0f, 1000.0f);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv) {
// init GLUT and create window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(800,600);
    glutCreateWindow("Teapot");

    initLighting();
    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutIdleFunc(renderScene);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glutMainLoop();
}

I'm pretty sure it's a simple fix but I hope you guys can help out.

Upvotes: 1

Views: 1324

Answers (1)

user3146587
user3146587

Reputation: 4320

According to the manpage of glutSolidTeapot, the generated normals are oriented in the opposite direction than the expected one. As suggested, use glFrontFace(GL_CW) / glFrontFace(GL_CCW) to temporarily change the default front-facing orientation for faces.

Upvotes: 3

Related Questions