Skip to main content
added 1943 characters in body
Source Link
Maik Semder
  • 4.7k
  • 1
  • 27
  • 24

Edit: After reviewing your code, here is the list of the bugs, the most important ones are 3), that messed up all the rendering, and 4) the correction for the camera rotation code

  1. Off by one error in void setupGrid(), change "<=" to "<" in the while

    while(_i<GRID_SIZE){
    
  2. set the initial camera position to {0, 0, 5} in Initialize(), otherwise you wont see the boxes:

    player.camera.position.x=0;
    player.camera.position.y=0;
    player.camera.position.z=5;
    player.camera.rotation.x=0;
    player.camera.rotation.y=0;
    player.camera.rotation.z=0;
    
  3. in Reshape change 3rd param of gluPerspective to 1.0

    gluPerspective(45.0,(double)screenWidth/(double)screenHeight,1.0,CUBE_UNIT*100.0);
    
  4. Finally the correction of the camera rotation code in Keyboard2() like already written above

        void Keyboard2(int key,int x,int y){
            switch(key){
                case GLUT_KEY_F1:
                    if(!wireframe){
                        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
                        wireframe=TRUE;
                    }else{
                        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
                        wireframe=FALSE;
                    }
                    break;
                case LOOK_UP:
                    player.camera.rotation.x+=5.0;
                    break;
                case ROTATE_LEFT:
                    player.camera.rotation.y+=5.0;
                    break;
                case LOOK_DOWN:
                    player.camera.rotation.x-=5.0;
                    break;
                case ROTATE_RIGHT:
                    player.camera.rotation.y-=5.0;
                    break;
            };
            return;
        }
    

Edit: After reviewing your code, here is the list of the bugs, the most important ones are 3), that messed up all the rendering, and 4) the correction for the camera rotation code

  1. Off by one error in void setupGrid(), change "<=" to "<" in the while

    while(_i<GRID_SIZE){
    
  2. set the initial camera position to {0, 0, 5} in Initialize(), otherwise you wont see the boxes:

    player.camera.position.x=0;
    player.camera.position.y=0;
    player.camera.position.z=5;
    player.camera.rotation.x=0;
    player.camera.rotation.y=0;
    player.camera.rotation.z=0;
    
  3. in Reshape change 3rd param of gluPerspective to 1.0

    gluPerspective(45.0,(double)screenWidth/(double)screenHeight,1.0,CUBE_UNIT*100.0);
    
  4. Finally the correction of the camera rotation code in Keyboard2() like already written above

        void Keyboard2(int key,int x,int y){
            switch(key){
                case GLUT_KEY_F1:
                    if(!wireframe){
                        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
                        wireframe=TRUE;
                    }else{
                        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
                        wireframe=FALSE;
                    }
                    break;
                case LOOK_UP:
                    player.camera.rotation.x+=5.0;
                    break;
                case ROTATE_LEFT:
                    player.camera.rotation.y+=5.0;
                    break;
                case LOOK_DOWN:
                    player.camera.rotation.x-=5.0;
                    break;
                case ROTATE_RIGHT:
                    player.camera.rotation.y-=5.0;
                    break;
            };
            return;
        }
    
deleted 27 characters in body
Source Link
Maik Semder
  • 4.7k
  • 1
  • 27
  • 24

LOOK_UP and LOOK_DOWN must rotate around the x-axis, not around the y-axis.

ROTATE_RIGHT and ROTATE_LEFT must rotate around the y-axis, not around the x-axis.

Immediately rotating around 90 degrees or initializing your angles with 90 degrees will bring your system into a gimbal lockGimbal Lock state, resulting in weird rotations. You might want to use smaller angles and step-sizes for starters, particulary not 90 degrees or multiples of it.

Initialize all angles with 0 and start from there, don't use multiples of 90.:

    // init your angles at startup, i.e. main or camera constructor
    player.camera.rotation.x = 0.0;
    player.camera.rotation.y = 0.0;
    player.camera.rotation.z = 0.0;

    // change the step-size and rotation-axis like this:
    case LOOK_UP:
        player.camera.rotation.x+=5.0;
        break;
    case ROTATE_LEFT:
        player.camera.rotation.y-=5.0;
        break;
    case LOOK_DOWN:
        player.camera.rotation.x-=5.0;
        break;
    case ROTATE_RIGHT:
        player.camera.rotation.y+=5.0;
        break;

LOOK_UP and LOOK_DOWN must rotate around the x-axis, not around the y-axis.

ROTATE_RIGHT and ROTATE_LEFT must rotate around the y-axis, not around the x-axis.

Immediately rotating around 90 degrees or initializing your angles with 90 degrees will bring your system into a gimbal lock state, resulting in weird rotations. You might want to use smaller angles and step-sizes for starters, particulary not 90 degrees or multiples of it.

Initialize all angles with 0 and start from there, don't use multiples of 90.

    // init your angles at startup, i.e. main or camera constructor
    player.camera.rotation.x = 0.0;
    player.camera.rotation.y = 0.0;
    player.camera.rotation.z = 0.0;

    // change the step-size and rotation-axis like this:
    case LOOK_UP:
        player.camera.rotation.x+=5.0;
        break;
    case ROTATE_LEFT:
        player.camera.rotation.y-=5.0;
        break;
    case LOOK_DOWN:
        player.camera.rotation.x-=5.0;
        break;
    case ROTATE_RIGHT:
        player.camera.rotation.y+=5.0;
        break;

LOOK_UP and LOOK_DOWN must rotate around the x-axis, not around the y-axis.

ROTATE_RIGHT and ROTATE_LEFT must rotate around the y-axis, not around the x-axis.

Immediately rotating around 90 degrees or initializing your angles with 90 degrees will bring your system into a Gimbal Lock state, resulting in weird rotations. You might want to use smaller angles and step-sizes for starters, particulary not 90 degrees or multiples of it.

Initialize all angles with 0 and start from there:

    // init your angles at startup, i.e. main or camera constructor
    player.camera.rotation.x = 0.0;
    player.camera.rotation.y = 0.0;
    player.camera.rotation.z = 0.0;

    // change the step-size and rotation-axis like this:
    case LOOK_UP:
        player.camera.rotation.x+=5.0;
        break;
    case ROTATE_LEFT:
        player.camera.rotation.y-=5.0;
        break;
    case LOOK_DOWN:
        player.camera.rotation.x-=5.0;
        break;
    case ROTATE_RIGHT:
        player.camera.rotation.y+=5.0;
        break;
Source Link
Maik Semder
  • 4.7k
  • 1
  • 27
  • 24

LOOK_UP and LOOK_DOWN must rotate around the x-axis, not around the y-axis.

ROTATE_RIGHT and ROTATE_LEFT must rotate around the y-axis, not around the x-axis.

Immediately rotating around 90 degrees or initializing your angles with 90 degrees will bring your system into a gimbal lock state, resulting in weird rotations. You might want to use smaller angles and step-sizes for starters, particulary not 90 degrees or multiples of it.

Initialize all angles with 0 and start from there, don't use multiples of 90.

    // init your angles at startup, i.e. main or camera constructor
    player.camera.rotation.x = 0.0;
    player.camera.rotation.y = 0.0;
    player.camera.rotation.z = 0.0;

    // change the step-size and rotation-axis like this:
    case LOOK_UP:
        player.camera.rotation.x+=5.0;
        break;
    case ROTATE_LEFT:
        player.camera.rotation.y-=5.0;
        break;
    case LOOK_DOWN:
        player.camera.rotation.x-=5.0;
        break;
    case ROTATE_RIGHT:
        player.camera.rotation.y+=5.0;
        break;