Find the answer to your Linux question:
Results 1 to 2 of 2
I'm rather confused on a somewhat basic openGL program, as there is a rather irritating bug. The program isn't much, just a simple, rotating cube with lighting, but the top ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    26

    Question Lame OpenGL issue...

    I'm rather confused on a somewhat basic openGL program, as there is a rather irritating bug. The program isn't much, just a simple, rotating cube with lighting, but the top and bottom of the cube have a blank triangle. I went through all the vertices to see if that one was misplaced, but I don't think that is the problem. Could be, but I don't know. Anyways, here's the code:

    #include <iostream>
    #include <stdlib.h>
    #include <GL/glut.h>

    using namespace std;

    void handleKeypress(unsigned char key, int x, int y)
    {
    switch (key)
    {
    case 27:
    exit(0);
    }
    }

    void initRendering()
    {
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH);
    }

    void handleResize(int w, int h)
    {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
    }

    float angle;

    void drawScene()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //Lighting
    GLfloat ambientColor[] = {0.3, 0.3, 0.3, 1.0};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

    GLfloat lightColor0[]= {1.0,1.0,1.0,1.0};
    GLfloat lightPosition0[] = {-1.0,0.5,0.5,0.0};
    glLightfv(GL_LIGHT0,GL_DIFFUSE,lightColor0);
    glLightfv(GL_LIGHT0,GL_POSITION,lightPosition0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();

    glTranslatef(0.0,0.0,-5.0);
    glRotatef(angle, 1.0, 0.0, 0.0);
    glBegin(GL_QUADS);

    glColor3f(0.5, 0.5, 0.0);

    //Front
    glNormal3f(0.0, 0.0, 1.0);
    glVertex3f(-1.5, -1.0, 1.5);
    glVertex3f(1.5, -1.0, 1.5);
    glVertex3f(1.5, 1.0, 1.5);
    glVertex3f(-1.5, 1.0, 1.5);

    //Right
    glNormal3f(1.0, 0.0, 0.0);
    glVertex3f(1.5, -1.0, -1.5);
    glVertex3f(1.5, 1.0, -1.5);
    glVertex3f(1.5, 1.0, 1.5);
    glVertex3f(1.5, -1.0, 1.5);

    //Back
    glNormal3f(0.0, 0.0, -1.0);
    glVertex3f(-1.5, -1.0, -1.5);
    glVertex3f(-1.5, 1.0, -1.5);
    glVertex3f(1.5, 1.0, -1.5);
    glVertex3f(1.5, -1.0, -1.5);

    //Left
    glNormal3f(-1.0, 0.0, 0.0);
    glVertex3f(-1.5, -1.0, -1.5);
    glVertex3f(-1.5, -1.0, 1.5);
    glVertex3f(-1.5, 1.0, 1.5);
    glVertex3f(-1.5, 1.0, -1.5);

    //PROBLEM STARTS HERE (I think...)*******
    //Top
    glNormal3f(0.0,1.0,0.0);
    glVertex3f(-1.5,1.0,1.5);
    glVertex3f(1.5,1.0,1.5);
    glVertex3f(-1.5,1.0,-1.5);
    glVertex3f(1.5,1.0,-1.5);

    //Bottom
    glNormal3f(0.0,1.0,0.0);
    glVertex3f(-1.5,-1.0,1.5);
    glVertex3f(1.5,-1.0,1.5);
    glVertex3f(-1.5,-1.0,-1.5);
    glVertex3f(1.5,-1.0,-1.5);
    //AND (MIGHT) END HERE********

    glEnd();

    glPopMatrix();
    glutSwapBuffers();
    }

    void update(int value)
    {
    angle += 2.0;
    if (angle >= 360)
    angle = 0;

    glutPostRedisplay();

    glutTimerFunc(20, update, 0);
    }

    int main(int argc, char** argv)
    {
    //Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(1280, 800);

    //Create the window
    glutCreateWindow("OpenGL Test Window");
    initRendering();

    //Set handler functions
    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);

    glutTimerFunc(20, update, 0);

    glutMainLoop();
    return 0;
    }

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Looking at it casually, I see that the normal vectors are identical. Shouldn't they point in opposite directions?
    Debian GNU/Linux -- You know you want it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...