Results 1 to 2 of 2

Thread: Rotation about Eye Co-ordinate's X and Y axis

  1. #1
    Junior Member Newbie
    Join Date
    May 2008
    Posts
    2

    Rotation about Eye Co-ordinate's X and Y axis

    Hi,
    I have drwan a cube at the point (1,1,-1). I want it rotate in eye co-ordinate X axis if I press key 'x' and it should rotate in eye co-ordinate Y axis when I press 'y'. I tried it following way but couldn't achieve the result.

    -----------------------------------------

    //press x to rotate about x axis press y to rotate about y axis.

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <GL/glut.h>
    #include <windows.h>

    #define PI_ 3.14159265358979323846

    GLuint theTorus;
    static GLfloat anglex =0.0, angley = 0.0;


    static void draw_cube(void)
    {

    glutWireCube(1.0); //,2.0,10,10);
    }

    static void init(void)
    {
    glShadeModel(GL_FLAT);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    }


    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0,1.0,1.0,1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(-5, 5, 10, 0, 0 , 0, 0, 1, 0);

    glBegin(GL_LINES);
    glColor3f(1.0,0.0,0.0);
    glVertex3f(0.0,0.0,0.0);
    glVertex3f(100.0,0.0,0.0);

    glColor3f(0.0,1.0,0.0);
    glVertex3f(0.0,0.0,0.0);
    glVertex3f(0.0,100.0,0.0);

    glColor3f(0.0,0.0,1.0);
    glVertex3f(0.0,0.0,0.0);
    glVertex3f(0.0,0.0,100.0);
    glEnd();


    glPushMatrix();
    glRotatef(anglex,1.0,0.0,0.0);
    glRotatef(angley,0.0,1.0,0.0);
    glTranslatef(1.0,1.0,-1.0);
    draw_cube();
    glPopMatrix();
    glFlush();
    }

    /* Handle window resize */
    void reshape(int w, int h)
    {
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(30, (GLfloat) w/(GLfloat) h, 1.0, 100.0);

    }

    /* Rotate about x-axis when "x" typed; rotate about y-axis
    when "y" typed; */

    void keyboard(unsigned char key, int x, int y)
    {
    switch (key) {
    case 'x':
    case 'X':
    //glPushMatrix();
    //glRotatef(30.0,1.0,0.0,0.0);
    anglex+=10.0;
    glutPostRedisplay();
    //glPopMatrix();
    break;
    case 'y':
    case 'Y':
    //glRotatef(30.,0.0,1.0,0.0);
    angley+=10.0;
    glutPostRedisplay();
    break;

    case 27:
    exit(0);
    break;
    }
    }

    int main(int argc, char **argv)
    {
    glutInitWindowSize(200, 200);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGB);
    glutCreateWindow(argv[0]);
    init();
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
    }

    -------------------------------

    Kindly help.

  2. #2
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Rotation about Eye Co-ordinate's X and Y axis

    Code :
    void keyboard(unsigned char key, int x, int y)
    {
    switch (key) {
    case 'x':
    case 'X':
    //glPushMatrix();
    //glRotatef(30.0,1.0,0.0,0.0);
    anglex+=10.0;
    glutPostRedisplay();
    //glPopMatrix();
    break;
    case 'y':
    case 'Y':
    //glRotatef(30.,0.0,1.0,0.0);
    angley+=10.0;
    glutPostRedisplay();
    break;
     
    case 27:
    exit(0);
    break;
    }
    }

    This is really bad! I advise you to just modify anglex and angley values in this function, redisplay if you want but not modify modelviw matrix here. (And I don't understand anything in this function , just looking at this arbitrary 30 rotation...

    Code :
     glPushMatrix();
    glRotatef(anglex,1.0,0.0,0.0);
    glRotatef(angley,0.0,1.0,0.0);
    glTranslatef(1.0,1.0,-1.0);
    draw_cube();
    glPopMatrix();

    If you want to rotate the cube around the camera X and Y axis, you have to take care of the camera position that you set with:

    Code :
     gluLookAt(-5, 5, 10, 0, 0 , 0, 0, 1, 0);

    I mean that you have to translate the scene from the oppsite of the camera position, so in your case from a vector ( 5, -5, -10)

    This gives:

    Code :
     glPushMatrix();
    glRotatef(anglex,1.0,0.0,0.0);
    glRotatef(angley,0.0,1.0,0.0);
    glTranslatef( 5, -5, -10); // go to camera space
    glTranslatef(1.0,1.0,-1.0);
    draw_cube();
    glPopMatrix();

    Note that your case, the camera view and vertical axis are no aligned with the world space (x,y,z) whereas you will compute these rotations in world space.

Similar Threads

  1. opengl co-ordinate system and axis range
    By dinesh17 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-20-2012, 02:47 AM
  2. Best co-ordinate system for axis-angle matrix
    By chethan in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-09-2012, 09:34 AM
  3. Rotation not on the 0,0,0 axis
    By tzano in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-03-2004, 12:55 PM
  4. changing axis of rotation and center of rotation in openGL performer
    By ravishashi in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 07-08-2003, 04:42 AM
  5. Rotation around two fixed rotation axis
    By Roland in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 03-06-2002, 12:07 PM

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