PDA

View Full Version : rotating object 90 degrees using a matrix and glMultMatrix(R);



TheBlackAdder
10-18-2005, 05:10 AM
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

Relic
10-18-2005, 05:47 AM
Math functions sin and cos need radians, you used degrees.

TheBlackAdder
10-18-2005, 05:54 AM
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...

Relic
10-18-2005, 06:22 AM
:eek:

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]

1 cos(theta) -sin(theta) 0
0 sin(theta) cos(theta) 0
0 0 0 1
0 0 0 missing?But you probably wanted

1 0 0 0
0 cos(theta) sin(theta) 0
0 -sin(theta) cos(theta) 0
0 0 0 1Be more careful.

TheBlackAdder
10-18-2005, 06:46 AM
yes of course hehe :)
Shame on me...It was the integer divide that made the inacurracy.
Thank you very much
:p