PDA

View Full Version : Help understanding the details of view transforms (camera problems)



Nick Nougat
09-27-2003, 08:25 AM
I'm having some problems concerning the exact way of realising cameras:
I'll list the facts I know and hope, that someone can tell me where I made a mistake:

In my 'engine', the location of each entity is described by a position vector and a quaternion for its rotation. Entity position is interpreted as relative to a fixed coordinate system.

My facts:
1) It is agreed that it should be possible to render the world from the point of view of any entity (i.e. making the entity the camera) simply by applying the translation and rotation of this entity differently (see below).

2) To render an entity at its position one first rotates, then translates the entity.
3) The view transformation (i.e. the camera transform) is the last transformation applied to the modelview matrix.
4) The camera transformation is different from normal translations: we rotate first, then translate by the negative position.
5) In the code, since I use the model of a fixed coordinate system, the order of the translations is reversed:
1. camera rotation
2. camera translation with negative position

3. push matrix
4. Entity_1 translation
5. Entity_1 rotation
6. pop matrix
7. ...and so on up to Entity_n

Problem:
--------
When I render, it seems as though the z-axis were inverted, moving forward moves backward, objects behind me are in front etc.
I calculated some simple examples of translations by hand and my method seems correct.
Do I have to invert the z-coordinates? If so, why? Or could my implementation of quaternions be wrong? (shouldn't be though).

Thanks in advance for your replies

Nick

john
09-27-2003, 06:14 PM
To render an entity at its position one first rotates, then translates the entity.

... well, yes and no. Rotation and then translation will give you a different transformation than translation then rotation, but that isn't to say that the latter isn't useful or appropriate in some instances. For the discussion of orientating an entity to look at something, then you would almost always, I think, rotate than translate. Just be aware that the alternative is also valid.


3) The view transformation (i.e. the camera transform) is the last transformation applied to the modelview matrix.

.. its the last transformation that IS applied, but since modifications to the modelview stack are post multiplied then you actually "apply it to the modelview matrix" first.


4) The camera transformation is different from normal translations: we rotate first, then translate by the negative position.

The camera transformation is designed to push the world "away" w.r.t. the camera, so in that sense you are applying the inverse transformation for a camera. By that I mean if you had two cases, one where you wanted an object at transformation M, and the second where you wanted the camera at the same position as the object, then you'd apply the inverse of M to the modelview matrix first. Yes, the inverse of translation is its "negative position", but the inverse of a rotation is its transpose (because it's orthogonal). But, ultimately, they're all transformations...


Do I have to invert the z-coordinates?

well, OpenGL's coordinate system is right handed: the camera looks down the negative Z axis. What is right handedness? If you poihnt your index finger of your rigth hand in the postive x directio (ie towards your right), then rotate your hand so your middle finger is pointing in the positive y direction--ie. upwards--and then extend your thumb, you'll find yoru thumb points in the positive z direction. If you try that with yrou left hand, you'll find the positive Z goes in the opposite direction.

If you do the maths yourself for an object that appears in front of the camera, then you should find that it has negative Z vertices. If you understand this (ie. this isn't the problem) then maybe you could post some camera extrinsics and object transformations in your parameterised form and the matricies you end up with.

I hope this helps

cheers,
jOhn

Nick Nougat
09-30-2003, 04:03 AM
That was exactly the information I needed.

I was somehow misinformed about the handedness of OpenGL and thought it was a left handed system... (why ever that was - I must have remebered something wrong)
Your post answered all my questions. Thanks a lot.
Now my camera code works perfectly.

Nick