Saturday, July 9, 2022

Primer: 3D math - Matrices

Overview 
OpenGL uses matrix operations for a lot of purposes. For example, translations, scaling, rotations. Also computing of projection matrices, view matrices etc. The following discusses it in detail.

Details
Cartesian Coordinate System
OpenGL uses Right hand side coordinate system where +Z is pointing towards you.
In OpenGL, the position values of x,y,z coordinates are represented as doubles. The origin is centered at 0,0,0. The ranges of x,y,z axes goes from -1 to +1.  
For example, the left top corner would be represented as -1,+1,-1 and so on. In other words, a value 0.5 would be halfway between the origin and positive maximum of the axis. Similarly a value -0.5 would be halfway between the origin and negative maximum of the axis

Pitch, Yaw and Roll
A 3D object can rotate along all the three axes as shown below. 
Rotation along X Axis is called Pitch. Rotation along Y Axis is called Yaw. Rotation along Z Axis is called Roll

The following diagram shows how the positive rotation happens in Counter clockwise direction.
        Pitch(X Axis)                                    Yaw (Y Axis)                                  Roll (Z Axis)


Homogeneous coordinate system 
OpenGL uses 4x4 matrix used in homogeneous coordinate system as shown below.
Each coordinate is represented as [x,y,z,w].Where w=1 for the affine transformations such as  translations, scaling, rotations. W can be any floating point number in the range -1.0 to 1.0 for projection matrices. During rasterization x, y, z coordinates are computed as  x/w, y/w and z/w. This is known as perspective divide.

OpenGL uses columnar matrix multiplication as shown below.

Identity Matrix
A special matrix having all elements along diagonal as 1 as shown below. 
It is associative, I*M = M*I = M. It's used in the computation of rotation, translation and scaling as discussed below.
Affine transformations
Affine transformations are applied to 3D objects to perform operations such as Translation, Scaling or Rotation on the X,Y,Z axes. Note that  rotation happens independently  on three cardinal axes X, Y, and Z. This can be calculated from this link.

Unlike projective transformation which may distort the shape, affine transformations preserve Lines, Parallelism and Ratio of Distances. 
The following affine transformations are discussed.

Translation 
Translation matrix is used for moving around 3D objects.

Example:
Let's say there is a point [1,2,3] and want to translate by [4,5,6]. It can be accomplished as below. Resulting in  [5,7,9].


Scaling
Scaling operation can be performed to Shrinking or Expanding 3d objects.

Example:
Let's say there is a point [1,2, 3] and want to  scale by [2,2,2]. It can be accomplished as below. Resulting in  [2,4,6].


Pitch or Rotation on X axis
The following matrix represents rotation matrix on X axis.

Example:
Let's say there is a point [1,2, 3] and want to  rotate by 90 degrees in X axis. It can be accomplished as below. Resulting in  [1,-3,2].


Yaw or Rotation on Y axis
The following matrix represents rotation matrix on Y axis.

Example:
Let's say there is a point [1,2, 3] and want to  rotate by 90 degrees in Y axis. It can be accomplished as below. Resulting in  [3,2,-1].


Roll or Rotation on Z  axis
The following matrix represents rotation matrix on Z axis.

Example:
Let's say there is a point [1,2, 3] and want to  rotate by 90 degrees in Z axis. It can be accomplished as below. Resulting in  [-2,1,3].












No comments:

Post a Comment