Search code examples
c++openglglutcoordinate-systems

bug in opengl c++ code, probably related to cood system


I have put all the necessary files[temp link removed] if you need to have a look.

mavStar.exe is my program.

The function currently I‘m trying to debug is :

void drawOG() 
{
    int curr,right,back,bottom;
    //Does NOT draw the right most,back most,bottom most layer at the moment
    //Does NOT draw face between state 1 & 2
    for(int z=0;z+1 < occupancyGrid->Nz; z++){
        glPushMatrix();
        for(int y=0;y+1 < occupancyGrid->Ny; y++){
            glPushMatrix();
            for(int x=0;x+1 < occupancyGrid->Nx; x++){
                curr = occupancyGrid->M[x][y][z];
                right = occupancyGrid->M[x+1][y][z];
                back = occupancyGrid->M[x][y][z+1];
                bottom = occupancyGrid->M[x][y+1][z];
                drawCube(RIGHT_FACE,colorBetween(curr,right));
                drawCube(BACK_FACE,colorBetween(curr,back));
                drawCube(BOTTOM_FACE,colorBetween(curr,bottom));
                glTranslatef (HALF_VOXEL_SIZE*2, 0.0, 0.0);
            }
            glPopMatrix();
            glTranslatef (0.0, -HALF_VOXEL_SIZE*2, 0.0);
        }
        glPopMatrix();
        glTranslatef (0.0, 0.0, -HALF_VOXEL_SIZE*2);
    }

}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    //mouse tracking
    glRotatef(fYDiff, 1,0,0);
    glRotatef(fXDiff, 0,1,0);
    glRotatef(fZDiff, 0,0,1);

    glScalef(fScale, fScale, fScale);

    //draw model
    glMatrixMode(GL_MODELVIEW);
    drawOG();
    printOpenGLError();  // Check for OpenGL errors

    glutSwapBuffers();
}

Solution

  • There is a much easier way to draw the faces you want by using:

    glPushMatrix();
    
    glBegin(GL_QUADS);
    
    //Draw the 16 vertices with their normals.
    
    glEnd();
    
    glPopMatrix();
    

    For example if you want the front:

    glPushMatrix();
    
    glBegin(GL_QUADS);
    
    glColor3f(1.0f,1.0f,1.0f);
    glNormal3f(0.0,0.0,1.0);
    glVertex3f( position[0],  position[1], position[2]);
    
    
    glNormal3f(0.0,0.0,1.0);
    glVertex3f( position1[0], position1[1], position1[2]);
    
    
    glNormal3f(0.0,0.0,1.0);
    glVertex3f(position2[0], position2[1], position2[2]);
    
    
    glNormal3f(0.0,0.0,1.0);
    glVertex3f(position3[0],  position3[1], position3[2]);
    
    glEnd();
    
    glPopMatrix();
    

    Draw the faces on a piece of paper to figure out what values you need for position,position1,position2,position3,etc. Named them so for general purpose, it should be fairly easy to determine their coordinates.


    If you want to give it a touch of flexibility you can create a Cube class and render only the faces for witch you have set a flag to be on. By using a class you gain lots of control on how you want your render-able object to be displayed (color,position,scale, etc).