View Full Version : rotation and translation

joeyx

04-15-2001, 11:45 PM

Hi all,

I'm really new to opengl and i really hope someone can enlighten me on the below:

1. What is a polygon mesh? http://www.opengl.org/discussion_boards/ubb/frown.gif

2. how can i control the thickness of a polygon?

3. I'm quite confused on the glTranslate. I read a book on glTranslate saying "it translates for x,y,z not to x,y,z" what exactly does this means? http://www.opengl.org/discussion_boards/ubb/frown.gif

4. why is it that whenever i does a rotation on my polygon, the other end of the polygon runs out of the screen? (think if i understand translate, maybe i can figure this out? http://www.opengl.org/discussion_boards/ubb/smile.gif ).

Thanx in advance.

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

ffish

04-16-2001, 01:03 AM

Joe,

1. What is a polygon mesh?

Exactly what is sounds like, a mesh of polygons. http://www.opengl.org/discussion_boards/ubb/smile.gif It can be a collection of individual triangles/quads/generic polygons or more commonly a collection of triangle strips. Think along the lines of a terrain in a game. Most would be implemented as meshes of triangles.

2. how can i control the thickness of a polygon?

You don't. Polygons like triangles are infinitesimally thin, like in mathematics. Of course, you can build a thick polygon, like a cube, from several thin ones. There is one rendering mode (glBegin(GL_POLYGON)) that can build arbitrary polygons, but they must be convex, and it is better to use triangles anyway.

3. I'm quite confused on the glTranslate. I read a book on glTranslate saying "it translates for x,y,z not to x,y,z" what exactly does this means?

Dunno. glTranslate*(TYPE x, TYPE y, TYPE z) multiplies the current (modelview) matrix by a translation matrix. It has the same effect as moving an object on screen by x, y and z. For example, if you have an object at (0, 0, 0) and you call glTranslatef(2.0f, 0.0f, 0.0f); the object will then be at the position (2, 0, 0). If you follow that with a call to glTranslatef(0.0f, 1.0f, 0.0f); the object will be at the point (2.0f, 1.0f, 0.0f). Is that what you mean? glTranslate() translates relative to the current position, not to an absolute position.

4. why is it that whenever i does a rotation on my polygon, the other end of the polygon runs out of the screen? (think if i understand translate, maybe i can figure this out?).

I'm guessing you're maybe translating first, then rotating second? What do you want to do, rotate a polygon on the spot (around its axis) at a specific location? Or rotate around a specific location, like a planet in orbit? Here's some pseudocode for a rotating planet orbiting some central sun:

glRotatef(orbit_amount, x_orbit, y_orbit, z_orbit);

glTranslatef(planet_x, planet_y, planet_z);

glRotatef(rotation_amount, x_rotation, y_rotation, z_rotation);

DrawPlanet();

I think that's right. Someone will correct me if it's not. I am always confused (like a lot of people) with OpenGL's transformation order. I learnt linear algebra as part of my undergrad course and the mathematical order is the opposite to OpenGL's. I hope this is right: I usually try to think of the transformations in the reverse order to which they are performed. So in the above example, reading from bottom to top, you draw the planet, rotate it on the spot, translate it outwards into its orbit, then rotate around the orbit.

Anyway, the best way to learn is to experiment with simple examples until you understand what happens.

Hope that helped http://www.opengl.org/discussion_boards/ubb/smile.gif

vrempire

04-16-2001, 01:19 AM

I dunno the correct way of saying this..

but, I 'll try...

1.Polygon mesh is something like the collection of polygons. As we know, polygon is the collection of vertices. Usually, polygonal mesh is interconnected (share common vertices).

2.Thickness of polygon? Ermm..if the polygon is a cube such as glutSolidCube(), then you can scale it width, height and depth using glScale().

3.Well, lets say that, a point is at (2,3,5).

case 1:If "translate for (4,4,4)" then the new position of that point is (2+4, 3+4, 5+4), that is (6,7,9).

case 2:If "translate to (4,4,4) then the new position of that point is (4,4,4), which is translate for (4-2, 4-3, 4-5),that is(2,1,-1).

4. let me think about it first , wokey...

Deiussum

04-16-2001, 04:41 AM

ffish,

The order of your translate/rotate is wrong. http://www.opengl.org/discussion_boards/ubb/smile.gif OpenGL doesn't do things in the opposite way of linear algebra, it POST multiplies the current matrix by the new matrix.

For instance assume current matrix is C, translation matrix is T, Rotation matrix is R, identity matrix is I.

glLoadIdentity(); // C = I

glTranslate(); // C = C*T

glRotate(); // C = C*R

so now you basically have I*T*R. Becuase of the properties of matrix multiplication, the last thing there is basically what is done first. (If you are thinking in terms of world coordinates.)

So in your example, your planet would be rotated, then moved. So it basically is just rotating about it's own axis.

ffish

04-16-2001, 10:43 PM

Deiussum,

You had me worried there for a moment http://www.opengl.org/discussion_boards/ubb/redface.gif I knew I should never post without testing something myself first http://www.opengl.org/discussion_boards/ubb/smile.gif

Anyway, I tested it with a simple app and I was right http://www.opengl.org/discussion_boards/ubb/tongue.gif (at least in my own head). What I meant by this example was to have a planet rotating on its own axis, and this rotating planet orbiting some central planet (or sun). An extract from my code:

glCallList(cube1); // Draw the central "planet".

glRotatef(orbit_amount, 0.0f, 1.0f, 0.0f); // Rotate around the orbit by some amount.

glTranslatef(5.0f, 0.0f, 0.0f); // Translate the rotating planet out to its orbit path.

glRotatef(rotation_amount, 0.0f, 1.0f, 0.0f); // Rotate the orbiting planet by some amount on its own axis.

glCallList(cube1); // Draw the rotating, orbiting "planet".

and in my idle function I'm updating rotation_amount and orbit_amount.

Thanks for keeping me on my toes though. I rely on you guys for a lot of information http://www.opengl.org/discussion_boards/ubb/smile.gif

joeyx

04-16-2001, 11:03 PM

Originally posted by ffish:

Joe,

glRotatef(orbit_amount, x_orbit, y_orbit, z_orbit);

glTranslatef(planet_x, planet_y, planet_z);

glRotatef(rotation_amount, x_rotation, y_rotation, z_rotation);

DrawPlanet();

I think that's right. Someone will correct me if it's not. I am always confused (like a lot of people) with OpenGL's transformation order. I learnt linear algebra as part of my undergrad course and the mathematical order is the opposite to OpenGL's. I hope this is right: I usually try to think of the transformations in the reverse order to which they are performed. So in the above example, reading from bottom to top, you draw the planet, rotate it on the spot, translate it outwards into its orbit, then rotate around the orbit.

Anyway, the best way to learn is to experiment with simple examples until you understand what happens.

Hope that helped http://www.opengl.org/discussion_boards/ubb/smile.gif

ffish,

I tried a simple example on my own with a pyramid, however, the pyramid still rotates out of the whole window http://www.opengl.org/discussion_boards/ubb/frown.gif. What is wrong http://www.opengl.org/discussion_boards/ubb/frown.gif. Mmmm, i did a translate then a rotation, but what i really want is to have my pyramid at some pt and rotation about it's own axis. This is so tough :`(

Then i was exploring gluLookAt(eyex, eyey, eyez, objx, objy, objz, ...), so if i were to set the objx, objy and objz, my pyramid will be able to rotate abt that (x,y,z) pt?

L http://www.opengl.org/discussion_boards/ubb/frown.gif

Gavin

04-17-2001, 01:06 AM

I assume his answer to no. 4 is his projection/view as the rotation/translation may be causing a part of the poly to move out of this?

gav

ffish

04-17-2001, 01:53 AM

joeyx,

I remember that it is a steep learning curve but it gets much easier http://www.opengl.org/discussion_boards/ubb/smile.gif I will e-mail you an example source file that does what I said in my post. It's a really quick messy job (10 minutes) so ignore the poor coding practices. It requires GLUT but that's easily downloaded and set up if you don't have it.

I'm guessing that your pyramid problems come from you not defining your pyramid around the origin. If you know you will rotate an object around its own axis, its best to define it around the origin. For example, a cube will have corners at (-1, -1, -1) and (1, 1, 1).

Try to sketch out an object either in your mind or on paper before you code it if you're unsure. Also, check out the tutorials on http://nehe.gamedev.net/opengl.asp

As to gluLookAt(), I don't use it myself. It's just a concatenation of glTranslate() & glRotate() calls anyway (which is what I use).

E-mail me if you have problems with my example code.

Above all, have fun with OpenGL http://www.opengl.org/discussion_boards/ubb/smile.gif

joeyx

04-17-2001, 01:58 AM

Thanx ffish.

My problem is i want to rotate the obj abt a pt and the obj should not run all over the screen http://www.opengl.org/discussion_boards/ubb/frown.gif

I have one object, say a prism, has a length 30 and it spans into the z-plane. When it rotates, the back end of the prism becomes too close to the screen and i dun want that. I want it to be able to rotate at the center of the drawn prism. But i still can't figure out how to do it http://www.opengl.org/discussion_boards/ubb/frown.gif.

Sigh...

Thanx for all ur help. http://www.opengl.org/discussion_boards/ubb/smile.gif

ffish

04-17-2001, 02:25 AM

Start your render function with a call to:

const GLfloat ZOffset(-some_large_value);

glTranslatef(0.0f, 0.0f, ZOffset);

to move the objects further away from the screen so you should be able to see them. Also, you can send your code to me to have a look at if you like (or post it here).

Deiussum

04-17-2001, 07:53 AM

ffish,

Ahhh... I must have missed the first glRotate call there. I thought you were just demonstrating a planet rotating around the sun so I had just looked at the last two transformations.

Deiussum

04-17-2001, 07:59 AM

joeyx,

It sounds like you might be saying that the origin of the object isn't at the center of the object. If you are unable to change the model data to correct this, then one way to fix it would be to use a glTranslate to move the model so that it's origin is where you want the center to be.

For instance say you want the object's center to be (0,0,1) instead of (0,0,0). You could do something like so

glTranslate(); // move it where you want it in the world

glRotate(); //with however you want it to rotate about itself

glTranslatef(0,0,-1);

Michael Steinberg

04-17-2001, 08:09 AM

The trick behind all is just to do it all reversed.

If you would normally move the object to it's new center and then movi it to it's position and then rotate it around it's center, then do it all flipped with opengl calls.

karamian

04-21-2001, 11:10 PM

Ok, I think I'll be able to help you on this one. This involves a bit of mathematics.

The following are some 3D Transformations, let's start by Translation, than we will do Scaling, and Rotation.

Translation

===========

[x'] = [1 0 0 dx][x]

[y'] = [0 1 0 dy][y]

[z'] = [0 0 1 dz][z]

[1 ] = [0 0 0 1 ][1]

Scaling

=======

[x'] = [sx 0 0 0][x]

[y'] = [0 sy 0 0][y]

[z'] = [0 0 sz 0][z]

[1 ] = [0 0 0 1 ][1]

Rotation

========

Before going on, Rotation is a bit complicated in a 3D world. Let's consider one axis at a time.

Rotation on X-Axis (Pitch)

[1 0 0 0][x]

[0 cos(@) -sin(@) 0][y]

[0 sin(@) +cos(@) 0][z]

[0 0 0 1][1]

Rotation on Z-Axis (Roll)

[cos(@) -sin(@) 0 0][x]

[sin(@) +cos(@) 0 0][y]

[ 0 0 1 0][z]

[ 0 0 0 1][1]

Rotation on Y-Axis (Yaw)

[+cos(@) 0 sin(@) 0][x]

[ 0 1 0 0][y]

[-sin(@) 0 cos(@) 0][z]

[ 0 0 0 1][1]

Now consider rotating a cube by @ angle about an axis defined by two end points (0,0,0) and (x,y,z)

Here are the steps to do so:

1) Rotate on Y-Axis such that the rotation axis is on the YZ-plane

2) Rotate on X-Axis such that the rotation axis coincides with the Z-Axis

3) Rotate on Z-Axis by the desired angle @

4) Undo step (2)

5) Undo step (1)

in the program you would multiply the matrices in the reverse order, that is:

M5*M4*M3*M2*M1 = you desired transformation

I hope this helps, I know that it is kind of hard to read this, but if you would like more information on it let me know and I might be able to explain it better.

Sincerely,

Vahe Karamian www.karamian.com (http://www.karamian.com)

bobert

04-23-2001, 11:32 AM

um, duh, the edges are being near-plane clipped, resulting in the cornners vanishing on rotation! :P

-bob

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.