PDA

View Full Version : glRotate Question.

LostInTheWoods
08-07-2002, 03:47 AM
Ok, if i have a model that has a center of 0,0,0 and I want to rotate it 30 degrees around that center on the Y, I would do this

glRotatef(30, 0,1,0);

but if i move the object over to where the center is 0,4,0, and i STILL wanted to rotate around its center. What would i call to rotate it? The same glRotate as above??

I am currently moving my objects around the scene (tracking them, not PHYSICALY moving them). But i am physicaly moving there bounding sphere, so i always know where the center of the object is. Would i use that center as my rotation axis? Or would I use the original center as my rotation axis??

Or should I simply Rotate on the Original Center as my axis, and then translate??

Latrans
08-07-2002, 04:19 AM
OH MY GOD! From reading the previous thread you started, I don't think I even want to touch this one with you.

Find a good book like the OpenGL programmer's guide. It has a good chapter explaining how transformations work. Something that you obviously don't understand very well.

Gavin
08-07-2002, 04:34 AM
glPushMatrix()
glTranslatef(0.0, 4.0, 0.0)
glRotatef(30.0, 0.0, 1.0, 0.0)
glPopMatrix()

Deiussum
08-07-2002, 04:36 AM
It's all about order of operations. For instance...

glTranslate();
glRotate();

Gives different results than

glRotate();
glTranslate();

Because of the way matrix math is applied in OpenGL, you can think of it as though the last transformation function called is the first one actually executed on your object.

So... in the first case above, your object is rotated around it's central axis (assuming it's central axis is the same as the global axis), then moved.

The second example up there, the object is moved and then rotated around the global axis.

Now... if you have an object who's center is not quite where you want it, you can do a combination of transformations where you move it to the center, rotate, then move it back. Say for instance you want to rotate the model around an x-axis at the top of it, and the 0,0,0 point of the model is 2 units below the top. You would do something like so..

glTranslatef(0.0, 2.0, 0.0); //moves it back
glRotatef(ang, 1.0, 0.0, 0.0); // does the rotation
glTranslatef(0.0, -2.0, 0.0); // moves top of model to 0,0,0

Gavin
08-07-2002, 04:49 AM
Doh! 'its centre'. Sorry

nexusone
08-07-2002, 05:14 AM
Have you tried looking at any of the opengl tutor sites and the example programs?

nehe.gamedev.net is a good place to learn.

The best thing for you is to start with a simple program with one object on the screen, and play around making move around the screen.

You seem to think that the glRotate or Translate effect every scene created after it. but that is not the case with each new scene you start a zero again..

example rotating cube:

first render pass throuh display() routine

glPushMatrix();
glRotatef(cube_angle, 0, 1, 0); cube_angle = 15;
Draw_cube();
glPopMatrix();

Next scene render pass through display()
// we increase the angle
cube_angle = cube_angle + 15;
if (cube_angle > 360 ) cube_angle = 0; // check of angle going past 360.

glPushMatrix();
glRotatef(cube_angle, 0, 1, 0); cube_angle = 30;
Draw_cube();
glPopMatrix();

and we repeat the above over and over, increasing the angle each pass to give us a rotating cube

Originally posted by LostInTheWoods:
Ok, if i have a model that has a center of 0,0,0 and I want to rotate it 30 degrees around that center on the Y, I would do this

glRotatef(30, 0,1,0);

but if i move the object over to where the center is 0,4,0, and i STILL wanted to rotate around its center. What would i call to rotate it? The same glRotate as above??

I am currently moving my objects around the scene (tracking them, not PHYSICALY moving them). But i am physicaly moving there bounding sphere, so i always know where the center of the object is. Would i use that center as my rotation axis? Or would I use the original center as my rotation axis??

Or should I simply Rotate on the Original Center as my axis, and then translate??

LostInTheWoods
08-07-2002, 06:12 AM
Ok, Tell me if this is correct. I have rotated my camera/world with a call to glRotate, and glTranslate for the players view.

Now I have a box whos actual physical center is at 0,10,0 And i want to rotate that box around its center for 30 degrees about the x axis and then move it 4 more units in the y direction. I would do this.

glPushMatrix(); //Save the current view
glTranslatef(0,-10,0); //Set back to the origin.
glRotatef(30, 1,0,0); //Rotate about its 'new' center, aka the origin
glTranslatef(0,14,0); make up for the reverse move made earlier.

Edit: i forgot the last call should be
glPopMatrix(); // reset the matrix to the current view

[This message has been edited by LostInTheWoods (edited 08-07-2002).]

Gavin
08-07-2002, 06:44 AM
Try it! Now you have the general idea a bit of trial and error will do the world of good!

Deiussum
08-07-2002, 07:44 AM
As Gavin said, trial and error is a great thing. http://www.opengl.org/discussion_boards/ubb/smile.gif

One thing, your translates may be backwards based on your example. Think of it as the last transformation being the first to affect your object.

killercow
08-07-2002, 07:49 AM
I think it helps out a ton if you draw what you want to do on some paper. That way you'll see the effect of glRotate and glTranslate and stuff like that.

nexusone
08-08-2002, 06:43 AM
I think the order in which the translate/rotate work does tend to confuse people, since it is backward to normal though of how it should work..

On his example of an object with a center of 0,10,0.
To rotate the round the center and move it, would look something like this:

glPushMatrix()
glTranslate( 0, 14, 0); Location to move to.
glRotate( 15, 0, 1, 0); rotate;
glTranslate( 0, -10, 0); Move to center to rotate.
Draw_object();
glPopMatrix();

http://www.opengl.org/discussion_boards/ubb/smile.gif

Originally posted by Deiussum:
As Gavin said, trial and error is a great thing. http://www.opengl.org/discussion_boards/ubb/smile.gif

One thing, your translates may be backwards based on your example. Think of it as the last transformation being the first to affect your object.

[This message has been edited by nexusone (edited 08-08-2002).]

nickels
08-08-2002, 08:09 AM
Latrans,
Go away.

Go join Al-Queida or something.

lsdi
08-08-2002, 09:10 AM
I think we need some moderators in this forum.

Latrans
08-08-2002, 09:18 AM
Originally posted by nickels:
Latrans,
Go away.

Go join Al-Queida or something.

Why? Because I will say what other people only think? Too many people lately try to program stuff without being willing to actually try and spend the time it takes to find things out on their own. People like that are just one step above the people who do nothing but cut and paste other people's code.

dabeav
08-08-2002, 10:18 AM
OR, did you ever think that some people DO try stuff out on there own, but just might not get the whole concept. Hell, when i first started, i read the blue books chapter on Rotations, and translatoins, and matrixes, about 30 times, before i realized i had NO idea what i was doing.

So the natural thing to do is ASK someone. People are a fountain of knowlege, that can answer specific questions, books on the other hand are a fountain of knowledge, that MAY only answer a few of the questions.

Oh, and by the way, i take offence to stating that the people who ask for help (although some more than others) is just like copying other peoples work. The people that ask for help are the ones that know there limitations, but are trying to pass them. The people that copy, know they have but one limitation (the fact that they cant DO **** for there selves) but they DONT try to fix that.

Edit:

Wow theses posts are ******* edited, nice. Learned something new today.

[This message has been edited by dabeav (edited 08-08-2002).]

Latrans
08-08-2002, 11:54 AM
Yeah, ok... People who ask questions such as "why do I get the unresolved external _main" really tried hard to resolve it on their own. It's not like a search of these forms would turn up the solution. That would be too easy.

In this particular case, people tried explaining this concept to death in a previous thread to this guy. And he still doesn't get it. The people trying to explain it to him have far more patience than I...

[This message has been edited by Latrans (edited 08-08-2002).]

nexusone
08-08-2002, 12:03 PM
Most the time people don't get an answer to their queston is because they don't.

a: Give a good disciption of the problem
b: or ask the wrong question.
c: Ask question that have noting to do with openGL.

I think that question about "_main" is ask ton's of times and has been answer. But it goes back to my points, without the proper information from them it is hard to say a complete answer.

Originally posted by Latrans:
Yeah, ok... People who ask questions such as "why do I get the unresolved external _main" really tried hard to resolve it on their own. It's not like a search of these forms would turn up the solution. That would be too easy.

dabeav
08-08-2002, 12:47 PM
Also, some people (especialy people new to opengl) may not understand that there asking the questions incorrectly. They are looking at one thing, and asking waht makes sense to them, when the question may be COMPLEATLY missleading to the answerer.

nickels
08-09-2002, 07:30 AM
I just thought it was interesting that if you look at all of Mr. Latrans' posts, every single one is hostile.
Unusual.
There are lots of hostiles on this board, but I hadn't seen someone so consistently so. What is up? Something wrong? Tell me your pain....

lsdi
08-09-2002, 12:46 PM
No one have to answer questions here. If you are goin to answer some, be patient, and ANSWER the question, not make stupid comments. If you dont like begginers questions, theres a advanced forum here to you. If you dont like grammar errors and people from other places/country, get out of internet.

Latrans
08-09-2002, 07:41 PM
I have never insulted anyone for grammar errors or being from another country.

I have simply called people on their stupidity when I see it. (e.g. posting 3 exact same threads with the exact same message, within hours of each other.)

A lot of my "hostility" is something I think of more as "venting." I have a bit of a dual personality in that I also post under another name, under which I help people quite often. Sometimes I need to vent a little, though. Some people can just be so frustrating.

Jambolo
08-09-2002, 08:57 PM
You should go vent somewhere else. It's not welcome here. Beginners are clueless by definition. If you don't want to deal with clueless people, don't read the beginner board.

Latrans
08-10-2002, 11:33 AM
Whatever.

Procyon
08-10-2002, 11:48 AM
Hello.

I am a stupid person. I need help with the following.

1) How to use these message boards?
2) What is difference between WinMain() and main()?
3) What is better, C or C++?
4) What is difference between the two?
5) What is better, OpenGL or DirectX?
6) I get problems with undefined WINGDIAPI in gl.h. What gives?
7) Can someone help me fix my simple syntax errors?
8) I get linker errors that say undefined reference to all the OpenGL functions I use. I do not understand. I included gl.h.
9) I don't know anything at all about 3d math, but I want to program OpenGL. How to do this w/o learning math?
10) I just started programming and I want to do OpenGL. Where to start?

08-10-2002, 02:22 PM
Latrans/Procyon, why you gotta h8 on me so much?

Seriously though, I have a tough problem. Gonna need all yer guys help for this.

void main (void)
{
int arraySize; cin >> arraySize;
int array[arraySize];
}

This program gives me strange compiler errors! I've been programming for a full month, so this isn't a regular problem or else I would have solved it. HELP!

08-11-2002, 10:29 AM
If everyone read the redbook we'd have no beginners.

Cowboy
08-11-2002, 11:08 AM
Here you have links to OpenGL books online.

You find "The Official Reference Document for OpenGL, Release 1" (The Blue Book) at http://ask.ii.uib.no/ebt-bin/nph-dweb/dynaweb/SGI_Developer/OpenGL_RM/.

And you find "OpenGL Programming Guide" (The Red Book) at http://ask.ii.uib.no/ebt-bin/nph-dweb/dynaweb/SGI_Developer/OpenGL_PG/.

Happy Hacking.
Dont forget to eat and sleep.
Cowboy

dabeav
08-11-2002, 03:59 PM
Stupid Person, I cant tell if your being fesecious or not, but if you are legit, the answer to your question is that what you are doing is illegal. You have to use the new operator to declare a (on the fly) array like that.

void main()
{
int *ArrayWanted; //Used to create the new array.

int ArraySize; //Size of array needed

cin >> ArraySize; //Read in the size

ArrayWanted = new int[ArraySize]; //Create the new array

//use the new array here

delete []ArrayWanted; // Return the memory to the heap.
}

rixed
08-12-2002, 03:54 AM
StupidPerson and dabeav, seams to me that you can initialize your array like you did, otherwise you should try another compiler. At least this works without any problem with GCC.
in pure C, however, you can't initialize an array with variable size, but GCC compile it fine anyway.

What was the point, agin ?? :-)

A027298
08-12-2002, 09:19 AM
@rixed: A compiler shouldn't compile code like that, cause it's wrong. The array is dynamic (cause you don't know the size during compile time) and therefore memory has to be allocated from the heap.

rixed
08-13-2002, 01:16 AM
it certainly is. When you ask GCC (or G++) to be pedantic, it complains about such declarations.
But in C++, as you can define variables anywhere in the code, you can suppose that memory on the heap or stack for those variables are actually allocated when the variables are declared, so that there are no technical reasons why variable sized arrays should not work.
Thats why most compiler should be able to compile it.

The reason why C++ doesn't allow it is perhaps for the compiler should choose to allocate local variables the C way, that is allocating the whole memory needed for local variables in the stack so that there is only one allocation/desalocation when you enter/leave the block. The syntax of C++ suggests a more general behavior, even if its not in the specs.

rhmazza
08-13-2002, 02:58 AM
Deiussum, (if you're still there... the conversation has degenerated a bit...) you mentioned thinking about order of matrix operations in reverse... which is an interesting way to think about it (and maybe the correct way, if there is one) but it started playing with my head nonetheless because I'd been thinking about it differently. For a long time.

Tell me if this is wrong and/or what you think of it:
I've been thinking of matrix operations in order, but as acting on the coordinate system.

so that
glRotate(180, 0, 1, 0);
glTranslate(5, 0, 0);

In my head rotates the whole coordinate system 180 degrees around Y, so the translate still put it 5 units down +X, except that +X happens to now be in the direction -X used to.

So it looks the same as thinking in reverse, that you translate +5 down X, then rotate 180 degrees around the World's Y axis.

I'd certainly like to know if I'm wrong in my thinking about this, even if it works. Or maybe they're the same exact thing. It's early and my brain's not completely functioning yet.

Deiussum
08-13-2002, 04:35 AM
rhmazza,

Yes, your way of thinking about it is also correct. It is just looking at it from a different perspective. For myself, I find thinking about the main coordinate system as being fixed a bit more intuitive, though it means I have to think of the transformations in reverse. When I start thinking of the coordinate system as being moved, my head starts to spin. http://www.opengl.org/discussion_boards/ubb/smile.gif

rhmazza
08-13-2002, 05:03 AM
Thanks, that clears things up a bit. It's funny that I'd never heard or thought of thinking about it in reverse. I agree, it's a more intuitive way of thinking about it! I'm sure it'll help me out a bit!