Matrix Notation

From Sunflow Wiki

Jump to: navigation, search

Contents

The Matrix In Sunflow

You can find the 4x4 matrix class in org.sunflow.math.Matrix4.

In Sunflow rotation matrices assume a right-handed convention and is used to represent general affine transformations in 3D. The bottom row of the matrix is assumed to be [0,0,0,1].

Matrix Notation

The 4x4 matrix:

m11 m12 m13 m14
m21 m22 m23 m24
m31 m32 m33 m34
m41 m42 m43 m44

In Sunflow, the 4x4 matrix can be written as a string in either row or column format:

Written in row format:

transform row m11 m12 m13 m14 m21 m22 m23 m24 m31 m32 m33 m34 m41 m42 m43 m44

Written in column format:

transform col m11 m21 m31 m41 m12 m22 m32 m42 m13 m23 m33 m43 m14 m24 m34 m44

Matrices in Sunflow Transforms (Example)

Here is a matrix of a cube in column major order, not rotated or scaled but translated to -4.6, 3.7, 5.8 in x y z world space (notice the first three 1.0 values are scale values):

transform col 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 -4.6 3.7 5.8 1.0

then rotated 75 degrees about the z axis

transform col 0.2588 -0.9659 0.0 0.0 0.9659 0.2588 0.0 0.0 0.0 0.0 1.0 0.0 -4.6 3.7 5.8 1.0

then scaled in x y and z by 1.3x

transform col 0.3364 -1.2557 0.0 0.0 1.2557 0.3364 0.0 0.0 0.0 0.0 1.3000 0.0 -4.6 3.7 5.8 1.0

Notice that the first value (which is both scale in x and cos theta for rotation in y and z) after rotation is 0.2588 (the cosine of 75 degrees) and after scaling is multipled by 1.3 which is 0.3364.

Matrix Components (Right Handed Rotation)

rotation in x =

1      0            0      0 
0 cos(theta)   -sin(theta) 0
0 sin(theta)   cos(theta)  0
0      0            0      1


rotation in y =

cos(theta)   0    sin(theta) 0
    0        1        0      0
-sin(theta)  0    cos(theta) 0
    0        0        0      1

rotation in z =

cos(theta)   -sin(theta) 0  0 
sin(theta)   cos(theta)  0  0
    0            0       1  0
    0            0       0  1

translation in x, y, z =

1 0 0 x
0 1 0 y
0 0 1 z
0 0 0 1

scale in x, y, z =

x 0 0 0
0 y 0 0
0 0 z 0
0 0 0 1