# OpenGL matrix rotation/transformation

This topic is 4230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, ive been getting to grips with 3d matrix rotations and transformations. im planning to use a 3x4 matrix in an implimentation of opengl for my nintendo ds. at the moment i have an object with x,y and z coords represented by variables: with the rotation matrix do i multiply these coordinates by the rotation matrix or the actual vertices of the said object? ive read that u multiply by the objects coordinates, however if i multiply the 3vector for its position by the matrix, how will it rotate the object? would it just not move the object with unexpected results?

##### Share on other sites
how do i multiply a matrix by the coordinates of the vertices of my model? especially if my model is fairly complex, it will be a long procedure and take up alot of cpu time

##### Share on other sites
can anybody help me?

##### Share on other sites
Hi...

I had a similar problem some time ago, and what I did for example, for implementing Rotation in the Z axis was:

//Z-rotation-"matrix"
new_x = x * cos(angle) - y * sin(angle);
new_y = x * sin(angle) + y * cos(angle);

(Please correct me if I had any mistake in that formula)

This article really helped me when I had that problem, I think it may help you too. =)

http://www.gamedev.net/reference/articles/article415.asp

##### Share on other sites
Hey!
You're very right that you can't rotate an object by manipulating it's position - you have no choice but to do it on all vertices. luckily the OpenGL API (and possibly you hardware, but I really don't know about the DS) can handle that for you: you are multiplying all your vertices by the two OpenGL matrices anyways. What is typically done is just manipulating the GL_MODELVIEW matrix with your rotation. Write back if you need further help doing that.

##### Share on other sites
oh right, i think i get it now. how would i implement it? would i somehow put the modelview matrix on the stack and then multiply it with my rotation matrix? cheers guys for the input! :)

##### Share on other sites
you, you could do that, but you don't really need to use the stack :) only if you want a way to undo the change to the modelview matrix

##### Share on other sites
cheers for the help! so after i create my rotation matrix, how do i actually multiply the modelview matrix by it?

##### Share on other sites
glMultMatrix - or you can just have opengl create the matrix for you using glRotate

##### Share on other sites
cheers ill try it out now. the reason im not using glRotate is because i need rotations and translations on my objects local axis (unless im missing a way to do this!). ill get back and post my results :)

##### Share on other sites
Just multiply all the transformation matrix to get the final rotation and translation matrix.

Although you should remember that openGL uses a stach based matrix system. So if you are going to do the transforms yourself, reverse the process you are doing with OGL.

IE.
GL:
Translate
Rotate

Bruteforce:
Rotate
Translate

Here's an old tute I made that explains the concept. Didn't use OpenGL at that time. :*)

http://rel.betterwebber.com/mytutes/3dtutes/chapter4/chapter4.htm

Note. It's in BASIC but really understandable.

##### Share on other sites
it still seems like im going wrong somewhere :( (its a really slow day for me...lack of sleep!)

ok simply put: in the main loop, i load my 3d object in the normal way (with a set of glVertex commands, normals, texture coords e.t.c). i create a transformation matrix (a 4x4 array). i create an if, which says if left pad is pressed, then increment the certain part of the array that i want incremented (for rotation, translation e.t.c). now i have my matrix which i want to multiply with the gl_matrixmode matrix (i think im gettin something wrong here) with the glMultMatrix command to change the vertex's positions and directions? all this is so i can rotate and translate along the objects own axis.

sorry to keep reposting, im just finding the actual coding kinda confusing

##### Share on other sites
wow musta bored everyone ;)

##### Share on other sites
you can use glRotate and glTranslate to do transformations around any axis. Basically this is because a rotation around a different axis is just the same as a rotation around an axis through the origin combined with a translation to 'fix' the move. It's not too hard to calculate this 'fix'.. just write down your equations as a combination of a 3x3 rotational matrix and an offset vector.
It's kinda hard to explain this if you don't have a math background tho. Alternatively you could just multiply the matrices in the other order. That should give you want you need. You need to use glGet and glLoadMatrix and multiply your own matrix so it's the left operand, not the right.
I know this can all be very confusing, just keep it up ;)

##### Share on other sites
Why dont you post some of your code?

why use glMultMatrix() ?. You can get your effects with a simple glPushMatrix() and glPopMatrix() group.

Like this

for each object glPushMatrix();  glTranslate();  glRotate(); glPopMatrix();loop

##### Share on other sites
cheers for all the support guys!

LtJax: cool, i didnt know you could! do you mind explaining how to calculate this fix? i dont mean to be long, its just that the ds cannot do float point math well (no fpu chip) so i think the glRotate function in "openGL" for the ds uses fixed point math and i dont want to go creating my own method with glMultMatrix if im using floats. dont worry about the maths part, im doing a maths and comp sci degree so i "should" understand everything you say :)

FireNet: the reason i didnt want to use glRotate and glTranslate is because i didnt know you could use them along the objects local axis. but LtJax just told me that i could, so i probably will :)

##### Share on other sites
it seems there is a bug in the ds implementation of opengl, or at least it works differently. as a simple test, i called glMatrixMode(GL_MODELVIEW), loaded an identity matrix and pushed it into the modelview stack when it first loads. this should push the called identity matrix ontop of the identity matrix that is already there, and have no effect, however the model moved into a really weird position, and moving the camera had no effect. if im following opengl so far, what i did shouldn't have caused a problem. even pushing my own created matrix ontop didnt give me the results i wanted.

for this reason i think im gonna go with LtJax's method of using glRotate and glTranslate, as the actual operation is already implemented in a way i cant understand (since my test didnt even work). could you quickly explain to me how to go about it? cheers :)

p.s. sorry to keep bugging :(