Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Rotate in local coordinates

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    7

    Rotate in local coordinates

    Hi, I'm new OpenGL and I have a problem with rotations.
    I would like to draw a plane and rotate it in about its local axis.
    Here is my code. It draws a plane with its local coordinates. I would like to use the keys 'x', 'y', 'z' to rotate about corresponding axis. This doesn't work correctly. Could you please help me?

    Code :
    #include <GL/gl.h>                          
    #include <GL/glu.h>                         
    #include <GL/glut.h>                        
     
    GLfloat spin_x = 0;
    GLfloat spin_y = 0;
    GLfloat spin_z = 0;
     
    void init (void){
            glClearColor(0.5,0.5,0.5,0);
            glClearDepth(1.0);          
            glShadeModel(GL_FLAT);      
            glEnable(GL_DEPTH_TEST);    
    }                                   
     
    void display(void) {
            glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
            glColor3f (0.0, 0.0, 0.0);
            glLoadIdentity();         
            gluLookAt(10,10,10,0.0,0.0,0.0,0.0,1.0,0.0);
     
            glRotated(spin_x,1,0,0);
            glRotated(spin_y,0,1,0);
            glRotated(spin_z,0,0,1);
     
            // Plane
            glBegin(GL_POLYGON); 
                    glVertex3f(-1,0,-1);
                    glVertex3f(-1,0,1); 
                    glVertex3f(1,0,1);  
                    glVertex3f(1,0,-1); 
            glEnd();                    
     
            // Coordinates
            glColor3f(1,1,1);
            glBegin(GL_LINES);
                    glVertex3f(0,0,0);
                    glVertex3f(0,0,1);
                    glVertex3f(0,0,0);
                    glVertex3f(0,1,0);
                    glVertex3f(0,0,0);
                    glVertex3f(1,0,0);
            glEnd();                  
     
            glutSwapBuffers();
    }                         
     
    void reshape(int w, int h){
            glViewport(0,0,(GLsizei)w, (GLsizei) h);
            glMatrixMode(GL_PROJECTION);            
            glLoadIdentity();                       
            gluPerspective(30,1,1,200);             
            glMatrixMode(GL_MODELVIEW);             
    }                                               
     
    void keyboard(unsigned char key, int x, int y){
            switch(key){                           
                            case 'x': spin_x +=2;break;
                            case 'y': spin_y +=2;break;
                            case 'z': spin_z +=2;break;
            }
            glutPostRedisplay();
    }
     
    int main(int argc, char** argv){
      glutInit(&amp;argc, argv);
      glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
      glutInitWindowSize (500, 500);
      glutCreateWindow ("hello");
      init();
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
      glutKeyboardFunc(keyboard);
      glutMainLoop();
      return 0;
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Apr 2009
    Posts
    171

    Re: Rotate in local coordinates

    Make your drawing code sandwiched in

    glMatrixMode(POJE..);glPushMatrix();

    and
    glPopMatrix();
    glMatrixMode(MODEL...);
    which from
    glLoadIdentity to
    glEnd


    perhaps you not need gluLookAt for rotating the object
    and
    Try Mesa's demos
    or simple Devcpp codes(Windows)

    I hope it will work for you
    haven't made a game [img]<<GRAEMLIN_URL>>/frown.gif[/img]

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    7

    Re: Rotate in local coordinates

    Sandwiching the code as you said didn't help.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Apr 2009
    Posts
    171

    Re: Rotate in local coordinates

    is your display function look like
    Code :
    void display(void) {
            glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
            glColor3f (0.0, 0.0, 0.0);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();         
            gluLookAt(10,10,10,0.0,0.0,0.0,0.0,1.0,0.0);
     
    glPushMatrix();
     
            glRotated(spin_x,1,0,0);
            glRotated(spin_y,0,1,0);
            glRotated(spin_z,0,0,1);
     
            // Plane
            glBegin(GL_POLYGON); 
                    glVertex3f(-1,0,-1);
                    glVertex3f(-1,0,1); 
                    glVertex3f(1,0,1);  
                    glVertex3f(1,0,-1); 
            glEnd();                    
     
            // Coordinates
            glColor3f(1,1,1);
            glBegin(GL_LINES);
                    glVertex3f(0,0,0);
                    glVertex3f(0,0,1);
                    glVertex3f(0,0,0);
                    glVertex3f(0,1,0);
                    glVertex3f(0,0,0);
                    glVertex3f(1,0,0);
            glEnd();                  
    glPopMatrix();
     
              glMatrixMode(GL_MODELVIEW);
     
            glutSwapBuffers();
    }
    and reshape function look like
    Code :
    //most of reshape function look like this
    void reshape(int w, int h){
    GLfloat as = (GLfloat)w/(GLfloat)h;
    if(h==0)        h=1;
            glMatrixMode(GL_PROJECTION);            
            glLoadIdentity();                       
            gluPerspective(30.0f,as,1.0f,200.0f);             
            glMatrixMode(GL_MODELVIEW);             
    glViewport(0,0,w,h);
    }

    perhaps it must work???
    please take seriously that what is result glfunction 's
    parameter u used
    I hope forcely it must work(cause i had not tried it)
    haven't made a game [img]<<GRAEMLIN_URL>>/frown.gif[/img]

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    7

    Re: Rotate in local coordinates

    With glMatrixMode(GL_PROJECTION); at the beginning of the display function I can't see anything but background. When I change to GL_MODELVIEW the polygon and lines are displayd but do not rotate as I wish

  6. #6
    Senior Member Regular Contributor
    Join Date
    Apr 2009
    Posts
    171

    Re: Rotate in local coordinates

    i think remove LookAt call and put rotate command in that place as ireply that code please understand i m not using pc system i m on mobile cause my father taken that please apologies me for taking your time
    haven't made a game [img]<<GRAEMLIN_URL>>/frown.gif[/img]

  7. #7
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    7

    Re: Rotate in local coordinates

    It also doesn't work

  8. #8
    Senior Member Regular Contributor
    Join Date
    Apr 2009
    Posts
    171

    Re: Rotate in local coordinates

    void idle()glutPostRedisplay();
    and

    glutIdleFunc(idle);
    before MainLoop(.);
    haven't made a game [img]<<GRAEMLIN_URL>>/frown.gif[/img]

  9. #9
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    7

    Re: Rotate in local coordinates

    Well, the problem is not that I can't display the plane but that it doesn't rotete around its locl axis. It rotates about the world coordinates. I think I should change the rotations but I don't know how.

  10. #10
    Senior Member Regular Contributor
    Join Date
    Apr 2009
    Posts
    171

    Re: Rotate in local coordinates

    that means these 4 point not to be rotated but the plane to be rotated right??
    use glLoadIdentity();
    to remove these kind of transformation and rotation of points
    haven't made a game [img]<<GRAEMLIN_URL>>/frown.gif[/img]

Page 1 of 2 12 LastLast

Similar Threads

  1. Rotate object around its local coordinate system
    By Dannjell in forum OpenGL: General
    Replies: 4
    Last Post: 02-25-2014, 07:22 PM
  2. how to rotate object in local coordinate space?
    By therealremi in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-12-2007, 01:14 PM
  3. Converting LOCAL coordinates to WORLD coordinates - How do I do it?
    By VuurSnikkel in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 07-23-2002, 08:48 AM
  4. Transforming local coordinates to world coordinates.
    By VuurSnikkel in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 07-19-2002, 01:46 PM
  5. How do I rotate an object around the global axes instead of local?
    By blood.angel in forum OpenGL: Advanced Coding
    Replies: 9
    Last Post: 01-22-2002, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean