Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Cube doesn't rotate

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Location
    Italy
    Posts
    12

    Cube doesn't rotate

    Here I am trying to draw a cube with three faces.One face is red, one green and one blue.
    Then I rotate the cube in a way that I should see half of the red face and half of the green face, of 45 degrees around the y axis.I should see a half red and half green cube, but I only see the red face, that's the code:

    Code :
    #import <OpenGL/OpenGL.h>
    #import <GLUT/GLUT.h>
     
     
    int width=500, height=500, depth=500;
     
     
    void init()
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glEnable(GL_DEPTH_TEST);
        glViewport(0, 0, width, height);
        glOrtho(0, width, height, 0, 0, depth);
    }
     
     
    void display()
    {
        glClearColor(0.9, 0.9, 0.9, 0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBegin(GL_QUADS);
        gluLookAt(250, 250, 0, -250, 250, 250, 0, 1, 0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glRotatef(45, 0, 1, 0);
     
        // First face
        glColor4f(1, 0, 0, 0);
        glVertex3i(100, 100,0);
        glVertex3i(300, 100,0);
        glVertex3i(300, 300,0);
        glVertex3i(100, 300,0);
     
        // Second face
        glColor4f(0, 1, 0, 0);
        glVertex3i(300,100,0);
        glVertex3i(300,300,0);
        glVertex3i(300,100,300);
        glVertex3i(300,100,300);
     
        // Third face
        glColor4f(0, 0, 1, 0);
        glVertex3i(100, 100,300);
        glVertex3i(300, 100,300);
        glVertex3i(300, 300,300);
        glVertex3i(100, 300,300);
     
        glEnd();
        glFlush();
    }
     
     
    int main(int argc, char * argv[])
    {
        glutInit(&argc, argv);
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(width, height);
        glutCreateWindow("Test");
        glutDisplayFunc(display);
        init();
        glutMainLoop();
        return 0;
    }

    PS: Code outdated because my university is outdated

  2. #2
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    450
    You need to switch the positions of:
    Code :
    gluLookAt(250, 250, 0, -250, 250, 250, 0, 1, 0);
    and
    Code :
    glMatrixMode(GL_MODELVIEW);

    since the first time display is called you will still be in GL_PROJECTION matrix mode, so it will mess up your projection matrix.

    It should probably read:
    Code :
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(250, 250, 0, -250, 250, 250, 0, 1, 0);

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2012
    Location
    Italy
    Posts
    12
    I modified the code this way:

    Code :
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(250, 250, 0, -250, 250, 250, 0, 1, 0);
    glRotatef(45, 0, 1, 0);

    But I still see only a red face.
    Also, I really can't understand when to be in modelview mode and when in projection mode.When I rotate, translate and scale I am in modelview mode, but for the other functions, how to know if I must use them in mdoelview or projection mode?

  4. #4
    Junior Member Newbie
    Join Date
    Apr 2012
    Location
    Italy
    Posts
    12
    I reviewed the code and got this way to draw an entire cube:

    Code :
    #import <OpenGL/OpenGL.h>
    #import <GLUT/GLUT.h>
     
     
    int width=500, height=500, depth=500;
     
     
    void init()
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glEnable(GL_DEPTH_TEST);
        glViewport(0, 0, width, height);
        glOrtho(0, width, height, 0, -depth, depth);
    }
     
     
    void drawCube()
    {
        glBegin(GL_QUADS);
     
        glColor4f(1,0,0,0);
        glVertex3i(100, 100, 0);
        glVertex3i(300, 100, 0);
        glVertex3i(300, 300, 0);
        glVertex3i(100, 300, 0);
     
        glVertex3i(100, 100, 300);
        glVertex3i(300, 100, 300);
        glVertex3i(300, 300, 300);
        glVertex3i(100, 300, 300);
     
        glColor4f(0, 1, 0, 0);
        glVertex3i(100, 100, 0);
        glVertex3i(100, 100, 300);
        glVertex3i(100, 300, 300);
        glVertex3i(100, 300, 0);
     
        glVertex3i(300, 100, 0);
        glVertex3i(300, 100, 300);
        glVertex3i(300, 300, 300);
        glVertex3i(300, 300, 0);
     
        glColor4f(0, 0, 1, 0);
        glVertex3i(100, 100, 0);
        glVertex3i(100, 100, 300);
        glVertex3i(300, 100, 300);
        glVertex3i(300, 100, 0);
     
        glVertex3i(100, 300, 0);
        glVertex3i(100, 300, 300);
        glVertex3i(300, 300, 300);
        glVertex3i(300, 300, 0);
     
        glEnd();
    }
     
     
     
     
    void display()
    {
        glClearColor(0.9, 0.9, 0.9, 0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glRotatef(45, 1, 1, 0);
        drawCube();
        glLoadIdentity();
        glFlush();
    }
     
     
     
     
    int main(int argc, char * argv[])
    {
        glutInit(&argc, argv);
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(width, height);
        glutCreateWindow("Test");
        glutDisplayFunc(display);
        init();
        glutMainLoop();
        return 0;
    }

    And it does rotate.

Posting Permissions

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