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 5 of 5

Thread: rotating object 90 degrees using a matrix and glMultMatrix(R);

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2005
    Posts
    13

    rotating object 90 degrees using a matrix and glMultMatrix(R);

    Hello, I need to rotate an object 90 degrees around the X-axis.

    Since I am very new to OpenGL I am still practicing, and therefor i don't want to use the glRotate function in this program I am doing at the moment.

    So I want to specify the Rotation matrix myself and then add it using glMultMatrix.

    I did the following:
    //The rotation matrix OGL style:

    GLfloat R[16]={1,0,0,0,cos(90),sin(90),0,0,-sin(90),cos(90),0,0,0,0,1}

    //In the display func:

    glPushMatrix();
    glMultMatrix(R);
    drawhouse();//this is the object i am rotating
    glPopMatrix();

    There is some rotation going on, but it is wrongly rotated. I cannot understand what I am doing wrong.
    I hope somebody can help me out here, thank you very much.

    Many regards the Black Adder

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,453

    Re: rotating object 90 degrees using a matrix and glMultMatrix(R);

    Math functions sin and cos need radians, you used degrees.

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2005
    Posts
    13

    Re: rotating object 90 degrees using a matrix and glMultMatrix(R);

    hmmm

    Now I am doing the following:
    #define PI 22/7

    theta=90*PI/180;
    GLfloat R[16]={1,0,0,0,cos(theta),sin(theta),0,0,-sin(theta),cos(theta),0,0,0,0,1}

    but alas, it still gives me wrong image...

    Also if i rotate about the z-axis:

    GLfloat Rz[16]={cos(theta),sin(theta),0.,0.,-sin(theta),cos(theta),0.,0.,0.,0.,1.,0.,0.,0.,1};

    it gives me a wrong rotation...

  4. #4
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,453

    Re: rotating object 90 degrees using a matrix and glMultMatrix(R);



    Integer division 22/7 gives 3.

    You wanted
    #define PI (22.0/7.0)
    or really
    #ifndef M_PI
    #define M_PI 3.141592653589793238462643383279502884197169399375 10
    #endif

    and

    double theta = 90.0 * M_PI / 180.0;

    Remember that OpenGL matrices are column-major.

    Your matrix built from R[16]
    Code :
    1 cos(theta) -sin(theta) 0
    0 sin(theta) cos(theta)  0
    0    0         0         1
    0    0         0       missing?
    But you probably wanted
    Code :
    1     0          0        0
    0  cos(theta) sin(theta)  0
    0 -sin(theta) cos(theta)  0
    0     0          0        1
    Be more careful.

  5. #5
    Junior Member Newbie
    Join Date
    Oct 2005
    Posts
    13

    Re: rotating object 90 degrees using a matrix and glMultMatrix(R);

    yes of course hehe
    Shame on me...It was the integer divide that made the inacurracy.
    Thank you very much

Posting Permissions

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