PDA

View Full Version : [OpenGL 3] Representing an object in the 3D space



Brainer
26-12-2010, 08:44 PM
Hello all.

Ok, Christmas's over and after being away and without an Internet connection (I had tried though), I finally had some time to think about my 3D engine, because it's pleading for a re-write right now.

The first thing I want to rebuild is my entity class. Not everything - of course. The matter I'm struggling with is representing an object in the 3D space. What I mean by that is:
a) positioning in at some point
b) adding rotations
c) scaling
What's the correct way of doing that? I've seen numerous codes and examples and I'm thinking about using vectors for positioning (that's rather obvious) and quaternions for rotations. The latter is the meritum of my post - I read about orientation, direction - what are those? What's the difference and how do I make use of them in my 3D engine?

Can you please enlighten me? :)

chronozphere
26-12-2010, 11:41 PM
I would highly recommend the book: 3d math primer for graphics and game development. :) It has a great chapter about "orientation" where it discusses the different methods to represent orientation and all their pro's and cons. It basicly comes down to the following:

First of all, orientation is more than a direction. If you can say where an object is heading, you still need to define "up" to be able to visualize the object. For example, if you say an airplane is flying north, it could do so normally, but also upside-down. :)

You can represent orientation in the following ways:

Rotation Matrices: You can store orientation into a rotation matrix. The advantage is that its easy to work with as a programmer, because all graphics API's use matrices to represent transformations. Inverting a transformation is also easy, because you only have to transpose that matrix (given that it does not contain any (non-uniform) scaling transform). The downside is that it consumes more memory ( 3x3xsizeOf(Single) ) and that it's hard to read for humans.
Euler angles: Consists of three angles X, Y and Z which all describe rotations about the local-space axes of the object. The advantage is that the format is very human readable and does not consume much memory. The downside is that they are harder to work with. The order of rotations matter, and there are problems like "gimbal lock" which are nasty to work around. Also, there is no easy way to interpolate two orientations.
Quaternions: These are actually very nice to represent orientations with. The main problem is that they are mysterious and hard to understand, but if you just pick existing code, you won't have many problems. They also don't consume alot of memory, and you can do nice things with them (like adding them up, interpolation etc). You don't have the gimbal lock problem here. :)
Axis-angle: These are actually very similar to quaternions, and are a bit easier to understad, but slightly harder to work with. Not used often anymore.

Aside from orientation, scaling also provides us with some challenges. It's easy if you only want non-uniform scaling ( single scale factor for all dimentions ). However, non-uniform scaling might get you into trouble, especially when you want to work with scene-graphs where transformations can be inherited from the upper nodes. I've read about this and it can be done, but it comes with a performance cost. For more info, you can check Dave eberly's book: "Game engine architecture" (The scene-graph/transformations part).

Hope this helps you to take the next step. Your question is actually rather vague, so I thought I should just dump some nice info here in the hope that it covers some of your dilemma's. :)

Merry christmas! ;)

User137
27-12-2010, 02:51 PM
A rotation matrix actually only holds 2 vectors you need to know when we assume the model is not scaled: direction and up-vector. Up-vector is propably the "orientation" you mean. A character might look in the right direction but renderer might think hes upside down. You can get the left-vector with crossproduct of direction and up vectors.

However, OpenGL assumes that matrices are 4x4 so for rendering objects you would have to rebuild the matrices per frame if the data is separated. This can slow things down a little, so some engines propably save the full 4x4 with position included for objects. It's exactly the position-vector in the matrix that i'm most confused about always. I'm at the moment unsure wether glMultMatrix causes the object to move to that coordinate before or after the rotation (guessing after).