Sunday 18 November 2012

Coordinate System transformation(OpenGL)

Hi,

This article mainly focuses on explaining the following:

0) Transforming between Coordinate Systems
1) Difference betweeen Eye Space and World Space.

These concepts confused me a lot, but thanks to Prof Ravi Ramamoorthi 's Lecture Series where the concepts are very well explained. I would like to just brief a little about the same and with more explanation. Hope this helps.

--------------------------------------------------------------------------------------------------------------
0) Transforming between Coordinate Systems:

So, let's begin! Generally speaking, a coordinate system (referring to Cartesian coordinate system) is a set of three perpendicular vectors and an origin and this system providing a way to address all points contained in the space.

Now, in the same space, there can be multiple Coordinate systems! This is where things get a little confusing(at least to me, it was). Now, let's take an example:



 Here, we have two coordinate systems here (CSa, CSb). Now, we can say that the origin of CSb is (0, 0, 0) w.r.t it's own coordinate system, AND (xO, yO, zO) w.r.t CSa. Now, let's consider an arbitrary point P.





Now, this point P can be described in two ways:
1) (xa, ya, za)  -> w.r.t CSa
2) (xb, yb, zb)  -> w.r.t CSb

Now, what we are interested in is, how a point represented in one coordinate system is transformed into another coordinate system.
So,  (xa, ya, za) -> (xb, yb, zb)  is the transformation what we need. We will shortly see how this can be achieved. Now, let's move onto how these concepts are used within OpenGL.

--------------------------------------------------------------------------------------------------------------

1) Difference betweeen Eye Space and World Space:

                In OpenGL, the modelview matrix transforms a vertex from object space to eye(view) space. The pipeline as shown in the Orange book, and more details added:



Fig (b) is the more detailed part of what is shown in the Orange Book(Fig (a)). Clearly, the ModelView matrix transforms a coordinate in object space to eye space. Now, revisiting the figure shown earlier, we have:



The tilted coordinate system is the camera(eye) space and the camera is at its origin. We have 3 vectors defining this space, (look, up, right). Now, notice that mentioning a random point say (1, 0, 0) here doesn't make sense unless we specify which coordinate system it belongs to. So, it could be (1, 0, 0) in world coordinate system or it could be (1, 0, 0) in eye coordinate system.

Now, the camera is located at some coordinate in world space (let's say this is camLoc) w.r.t the origin of the world. Thus, camera is located at camLoc in WorldSpace BUT, it is at (0, 0, 0) w.r.t EyeSpace. (similar as shown in CSa, CSb).

So, the view matrix basically does a transformation of a coordinate from world space to eye space. This can be done by building a ViewMatrix, or by simply using the glu function gluLookAt()
So, a point in world space Pw is transformed to eye space Pe by using the view matrix.

Now, there is an important point to be noted here. "moving a camera" is essentially the same as "moving the object" in reverse. They both modify the same matrix. Thus, OpenGL has it as just one matrix. How we visualize is more or less a personal choice, but the end result is that we update the same matrix. Thus, the Model and View Transforms are integrated into one -> the ModelView Matrix.

Interestingly, Direct3D still has two different matrices for the same: Direct3D matrices

Another interesting observation would be, the View Transformation can also be thought of as the camera being brought into WorldSpace (0, 0, 0). The other way to visualize the same would be, the WorldSpace brought into CameraSpace (0, 0, 0). But, both are the same.
--------------------------------------------------------------------------------------------------------------

I hope the pics have been helpful. Kindly let me know if there are mistakes in the article. I will correct them.