Best way to rotate an object

Member
Posts: 509
Joined: 2002.05
Post: #1
I know how to rotate everything in the world with a camera, but my animated model need to be able to rotate around itself when it turns in different directions, what is the best way to do this? Can openGL do this, or should I use matrixes? If matrixes, how do I do this, all I know about them is how to solve a double equation and I have only worked with 2x2 ones.
Quote this message in a reply
Member
Posts: 715
Joined: 2003.04
Post: #2
I think this is built in function of OpenGL.
Maybe Antack Source can help you?
Check the resources.
Quote this message in a reply
Mars_999
Unregistered
 
Post: #3
If you are referring to how to rotate the model? Then all you need to do is reset your matrix with glLoadIdentity() and then use glRotatef() and give the necessary parameters. I am not sure if this is what you are looking for but from what I read it is?
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #4
Ok, it looks like rotatef might work. I was afraid that rotatef would rotate EVERYTHING, how do I get it just to rotate a model and leave all the other polygons alone?

Sorry for the newbie question.
Quote this message in a reply
Mars_999
Unregistered
 
Post: #5
Use glLoadIdentity() to reset the matrix. I am unsure how you have OPENGL setup. But you might have to start looking into glPopMatrix() and glPushMatrix(). If you are using gluLookAt() I have had issues with trying to reset the matrix with glLoadIdentity() and do a glTranslatef() or glRotatef().
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #6
Ok, I think I have an idea of what you mean... tell me if this is right.

PHP Code:
glLoadIdentity();

glRotatef(50.0f,0.0f,1.0f,0.0f);
LOAD THE MODELS TRIANGLES HERE

glLoadIdentity
();

glRotatef(00.0f,1.0f,0.0f,0.0f);
LOAD THE REST OF THE MAP HERE TO BE ROTATED AROUND THE CAMERA


Do you get what i'm trying to do?
Quote this message in a reply
Mars_999
Unregistered
 
Post: #7
Quote:Originally posted by Jake
Ok, I think I have an idea of what you mean... tell me if this is right.

PHP Code:
glLoadIdentity();

glRotatef(50.0f,0.0f,1.0f,0.0f);
LOAD THE MODELS TRIANGLES HERE

glLoadIdentity
();

glRotatef(00.0f,1.0f,0.0f,0.0f);
LOAD THE REST OF THE MAP HERE TO BE ROTATED AROUND THE CAMERA


Do you get what i'm trying to do?


Unless you have a reason to rotate before you display the model I would do it this way.

Code:
glLoadIdentity();
glRotatef(angle, 0.0f, 1.0f, 0.0f);
//Load model
//if more than one model you will probably have to use glTranslatef() to keep them from being on top of each other.
glLoadIdentity();
glRotatef(angle2, 1.0, 0.0f, 0.0f);
//load the map here

Not sure but think that is what you want
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2003.02
Post: #8
i didn't think you had to call glLoadIdentity(); every time you rotated a new object.

if i had two objects i would do it like this

glLoadIdentity();

glPushMatrix();

glRotatef(...);
//draw

glPopMatrix();


glPushMatrix();

glRotatef(...);
//draw

glPopMatrix();

i thought that was how you do it but i could be mistaken.

laziness,impatience, and hubris
Quote this message in a reply
Mars_999
Unregistered
 
Post: #9
Quote:Originally posted by fat-t
i didn't think you had to call glLoadIdentity(); every time you rotated a new object.

if i had two objects i would do it like this

glLoadIdentity();

glPushMatrix();

glRotatef(...);
//draw

glPopMatrix();


glPushMatrix();

glRotatef(...);
//draw

glPopMatrix();

i thought that was how you do it but i could be mistaken.


I stated a few threads back about using glPushMatrix() glPopMatrix() to do this but decided to keep it simple for now. Beings he is learning from what I can tell.
Quote this message in a reply
Jesse
Unregistered
 
Post: #10
Once you are rendering more than one model, each with a different position and orientation, you will probably want to use glPushMatrix() and glPopMatrix().

glPushMatrix() basically 'saves' the current state of the modelview matrix so that you can change it and not lose what you had before. When you're done, glPopMatrix() returns the matrix to its former state.

Generally you'll want to set up your camera once at the beginning of each frame - this is where glLoadIdentity() comes into play, because you want to start from scratch. The code might look something like:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-camera.roll, 0, 0, 1);
glRotatef(-camera.pitch, 1, 0, 0);
glRotatef(-camera.yaw, 0, 1, 0);
glTranslatef(-camera.pos.x, -camera.pos.y, -camera.pos.z);

Or something like that - I think I got the order right.

Then when you render your models, you want to add the appropriate transformation to what's already there:

glPushMatrix();
glTranslatef(model.pos.x, model.pos.y, model.pos.z);
glRotatef(model.roll, 0, 0, 1);
glRotatef(model.pitch, 1, 0, 0);
glRotatef(model.yaw, 0, 1, 0);
model.Render();
glPopMatrix();

That's off the top of my head, so there are probably mistakes. But perhaps it will clarify the issue for you somewhat...
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #11
Thanks everyone! The glPushMatrix() and glPopMatrix() are exactly what I wanted! That will make it so easy to rotate my model AND the club its holding.

Another quick question, if I have a golf club my model is holding, and I want it to rotate around the top of the club (not the center like glRotate() does), is there someway to do that? Would I have to add a tiny triangle somewhere way above the top of the club, or can I specify the middle of the object I want rotated.

Actually... glRotate rotates the model around itself right, not 0,0,0?
Quote this message in a reply
Jesse
Unregistered
 
Post: #12
If your rotate a model with no translation, it will rotate around 0, 0, 0. If your model is built with 0, 0, 0 as its center, the model will rotate about its center.

In your case, you probably want to build the golf club model with 0, 0, 0 at the end rather than the center. That way, rotating it will rotate it as you want rather than spinning it around the center point.

When you add translation, it gets a little trickier 'cause you have to do it in the right order. One order will rotate the model around its local origin (which is what you want), and the other will rotate it around the global origin. In OpenGL, the transformation you specify last actually occurs first, which is kind of confusing. To rotate around the local origin, you want to rotate the model first, and then translate it. This means that in your OpenGL code, you should translate 'first' and then rotate.

I hope I got that all right...it still confuses me sometimes Wacko
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #13
Ok I think I understand everything... im glad I have 3 months to figure this out!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Cube auto-rotate sakrist 2 3,137 May 5, 2009 03:31 AM
Last Post: Ingemar
  rotate to focus on objects szgezu 1 2,083 Mar 4, 2006 03:59 AM
Last Post: SOUCHAN
  How does one rotate a scanned image in openGL? WhatMeWorry 16 8,824 Feb 2, 2005 09:25 PM
Last Post: arekkusu
  Move and Rotate camera SummerLand 2 3,370 Nov 25, 2002 10:53 PM
Last Post: Feanor
  rotate and translate opengl difficulties furballphat 2 4,321 May 24, 2002 05:19 PM
Last Post: furballphat