PDA

View Full Version : Represent Rotate and Translate by one Translate



Luin
07-02-2000, 06:50 AM
Hi,
I can get my desired result by

--some transformations exist before next--
glRotatef(angle, 0.f, 1.f, 0.f);
glTranslatef(0.f, 0.f, 0.7f);
DrawObject;

Now, I want to represent shift of the origin point ((0,0,0,1)) of the above two transformations by one glTranslatef first in order to track the object position, and put glRotatef after that:

--some transformations exist before next--
glTranslatef(?, ?, ?);
glRotatef(angle, 0.f, 1.f, 0.f);
DrawObject;

I tried the next, but did not work

--some transformations exist before next--
glTranslatef(0.7f*(float)sin(angle), 0.f, 0.7f*(float)cos(angle));
glRotatef(angle, 0.f, 1.f, 0.f);
DrawObject;

What is the right expression in glTranslatef(?, ?, ?) ? Thank you in advance for any help.

Luin

[This message has been edited by Luin (edited 07-02-2000).]

SED
07-02-2000, 07:45 AM
Well for one thing you need to convert your angle to radians before passing it to sin() cos(). http://www.opengl.org/discussion_boards/ubb/smile.gif

#define PI 3.141596
float a = angle * (PI / 180);

cos(a)

or you can combine it ... cos(angle * (PI/180))

07-02-2000, 08:56 AM
As someone said, you must use radians. Also, the way GL multiplies matricies, you should put the order of translation/rotation backwards; i e start at where you draw, and put each operation above the drawing, going upwards.




Shape:: DrawSelf() {
glTranslatef(m_pos.x, m_pos.y, m_pos.z);
glRotatef(m_angle/180., 0., 1., 0.);
Render();
}


This will result in the thing being first rotated about its internal Y axis, and then positioned where you wish it. If you think about it, it makes sense, because you start
by rotating the viewer and translating the world. You can use glPushMatrix() to great effect if you do recursive structures:




Shape:: Draw() {
glPushMatrix();
DrawSelf();
glPopMatrix();
}


Of course, once you start performance tuning, you may wish to keep an aggregate translation matrix and just glLoadIdentity() and apply the (single) matrix for each item, instead. But save that for the optimization phase; no use confusing the issue while you're still getting your rendering up.

PS: dontcha hateit when some perfectly innocent code turns into smiley faces just because the method name happens to start with a "D"?

[This message has been edited by bgl (edited 07-02-2000).]

Luin
07-02-2000, 12:54 PM
SED and bgl, thank you for pointing out “radians.” It works now!

Matrix method suggestions seems helpful, too.

Have a nice day!

Luin