• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

### #Actualmast4as

Posted 08 January 2013 - 03:06 PM

Hi Kevin,

I hope I am not too late to help you with your problem/question. You did right to watch on scratchapixel but you should look at this page in particular:

http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-4-geometry/conventions-again-row-major-vs-column-major-vector/

Basically knowing whether you use a right or left hand coordinate system is indeed important but in fact, in that particular case, it has little to do with whether you matrix is correct or not. What is important for constructing your matrix from the 3 vectors (up, right, forward) is whether you renderer uses row- or column-major notation for matrices. As mentioned in the page above if you use the row-major notation, your 3 vectors should be written vertically in the matrix and if you use column-major they should be written horizontally. In other words, assuming you have the vectors A B C (representing the right, top and forward vector from your camera coordinate system) you should write:

Ax Bx Cx
Ay By Cy
Az Bz Cz

If you use row-major notation. Now I will explain you why (there's a way an easy of checking how you should write the matrix if you can't remember it by heart). Remember that if you use row-major vector then the vector-matrix multiplication needs to be written as:

|Ax Ay Az|
V[x y z] M |Bx By Bz| [EDITED: changed from original post]
|Cx Cy Cz|

Where A is Right Vector, B is Up Vector and C is Forward Vector

Computing the multiplication can be done like that:

V'x = Vx * Ax + Vy * Bx + Vz * Cx
V'y = Vx * Ay + Vy * By + Vz * Cy [EDITED: changed from original post]
V'z = Vx * Az + Vy * Bz + Vz * Cz

EDITED FROM HERE:

I guess I was a bit too quick yesterday when I replied and my demonstration was actually wrong. Talking to the guys from work, they showed me a better way of checking that this is the right way of writing the matrix if you use a row-major notation for matrices. The default cartesian coordinate system should be like that:

Y (up 0, 1, 0)
^
|
|----> X (right 1, 0, 0)
/
/
Z (forward 1, 0, 0)

If you have a camera you would be looking down the z-axis. Now you can imagine that your camera is looking down the x-axis. Therefore the right vector for this camera becomes (0, 0, -1), the Up vector is still (0, 1, 0) and the Forward vector becomes the x-axis (1, 0, 0):

Up Right
^  /      \
| /        \
|/----> Fwd > camera
/
/

Now if you right these vectors as a matrix you get:
|Rx Ry Rz|   |0 0 -1|
M = |Ux Uy Yz| = |0 1 0|
|Fx Fy Fz|   |1 0 0|

This matrix is called the camera-to-world matrix.

The demonstration to check that this is correct is a bit complicated so I thought I would first quickly correct my original post to avoid leading you and other people on the wrong track. I will post the demonstration later today.

Sorry for having (probably) confused you (even more). Anyway, bottom line is that if you use row-major notation for your vectors and matrices (and assuming a right-hand coordinate system) as you mentioned, then this is the "proper" way of writing the camera-to-world matrix from the right, up and fwd vector.

One tip though. I am always (and I proved it) myself very confused about these things. I found scratchapixel very useful but I am also often using Maya to check the accuracy of my code. For instance in that case, you can create a default camera, rotate it around the y-axis by 90 degrees CCW, and check the resulting world matrix with the following mel code: getAttr camera.worldMatrix;

Because Maya uses row-major and uses the right hand coordinate system, if you use the same standard, then the result you get should mach your code and what you use.

### #4mast4as

Posted 08 January 2013 - 02:50 PM

Hi Kevin,

I hope I am not too late to help you with your problem/question. You did right to watch on scratchapixel but you should look at this page in particular:

http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-4-geometry/conventions-again-row-major-vs-column-major-vector/

Basically knowing whether you use a right or left hand coordinate system is indeed important but in fact, in that particular case, it has little to do with whether you matrix is correct or not. What is important for constructing your matrix from the 3 vectors (up, right, forward) is whether you renderer uses row- or column-major notation for matrices. As mentioned in the page above if you use the row-major notation, your 3 vectors should be written vertically in the matrix and if you use column-major they should be written horizontally. In other words, assuming you have the vectors A B C (representing the right, top and forward vector from your camera coordinate system) you should write:

Ax Bx Cx
Ay By Cy
Az Bz Cz

If you use row-major notation. Now I will explain you why (there's a way an easy of checking how you should write the matrix if you can't remember it by heart). Remember that if you use row-major vector then the vector-matrix multiplication needs to be written as:

|Ax Ay Az|
V[x y z] M |Bx By Bz| [EDITED: changed from original post]
|Cx Cy Cz|

Where A is Right Vector, B is Up Vector and C is Forward Vector

Computing the multiplication can be done like that:

V'x = Vx * Ax + Vy * Bx + Vz * Cx
V'y = Vx * Ay + Vy * By + Vz * Cy [EDITED: changed from original post]
V'z = Vx * Az + Vy * Bz + Vz * Cz

EDITED FROM HERE:

I guess I was a bit too quick yesterday when I replied and my demonstration was actually wrong. Talking to the guys from work, they showed me a better way of checking that this is the right way of writing the matrix if you use a row-major notation for matrices. The default cartesian coordinate system should be like that:

Y (up 0, 1, 0)

^

|

|----> X (right 1, 0, 0)

/

/

Z (forward 1, 0, 0)

If you have a camera you would be looking down the z-axis. Now you can imagine that your camera is looking down the x-axis. Therefore the right vector for this camera becomes (0, 0, -1), the Up vector is still (0, 1, 0) and the Forward vector becomes the x-axis (1, 0, 0):

Up  Right

^  /        \

| /          \

|/----> Fwd   > camera

/

/

Now if you right these vectors as a matrix you get:

|Rx Ry Rz|   |0 0 -1|

M = |Ux Uy Yz| = |0 1  0|

|Fx Fy Fz|   |1 0  0|

This matrix is called the camera-to-world matrix.

The demonstration to check that this is correct is a bit complicated so I thought I would first quickly correct my original post to avoid leading you and other people on the wrong track. I will post the demonstration later today.

Sorry for having (probably) confused you (even more). Anyway, bottom line is that if you use column major notation for your vectors and matrices (and assuming a right-hand coordinate system) as you mentioned, then this is the "right" way of writing the camera-to-world matrix from the right, up and fwd vector.

One tip though. I am always (and I proved it) myself very confused about these things. I found scratchapixel very useful but I am also often using Maya to check the accuracy of my code. For instance in that case, you can create a default camera, rotate it around the y-axis by 90 degrees CCW, and check the resulting world matrix with the following mel code: getAttr camera.worldMatrix;

Because Maya uses row-major and uses the right hand coordinate system, if you use the same standard, then the result you get should mach your code and what you use.

### #3mast4as

Posted 07 January 2013 - 02:18 PM

Hi Kevin,

I hope I am not too late to help you with your problem/question. You did right to watch on scratchapixel but you should look at this page in particular:

http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-4-geometry/conventions-again-row-major-vs-column-major-vector/

Basically knowing whether you use a right or left hand coordinate system is indeed important but in fact, in that particular case, it has little to do with whether you matrix is correct or not. What is important for constructing your matrix from the 3 vectors (up, right, forward) is whether you renderer uses row- or column-major notation for matrices. As mentioned in the page above if you use the row-major notation, your 3 vectors should be written vertically in the matrix and if you use column-major they should be written horizontally. In other words, assuming you have the vectors A B C (representing the right, top and forward vector from your camera coordinate system) you should write:

Ax Bx Cx
Ay By Cy
Az Bz Cz

If you use row-major notation. Now I will explain you why (there's a way an easy of checking how you should write the matrix if you can't remember it by heart). Remember that if you use row-major vector then the vector-matrix multiplication needs to be written as:

|Ax Bx Cx|
V[x y z] M |Ay By Cy|
|Az Bz Cz|

Computing the multiplication can be done like that:

V'x = Vx * Ax + Vy * Ay + Vz * Az
V'y = Vx * Bx + Vy * By + Vz * Bz
V'z = Vx * Cx + Vy * Cy + Vz * Cz

Okay so that doesn't help you much maybe. But imagine now that you actually want to transform a point using this technique. And to make it simple imagine you just want to translate a point along the x-axis. If you use row-major matrices the translation vector is located on the 13th, 14th and 16th coefficient of the matrix:

|Ax Bx Cx 0|
P[x y z] M |Ay By Cy 0|
|Az Bz Cz 0|
|Tx Ty Tz 1|

So imagine that you translate your camera along the x-axis to the right by 1 unit. Therefore your "right vector" is:

// translation along the x-axis thus Tx = 1
Right Vector   = Rx Ry Rz Tx -> 1 0 0 1
Up Vector      = Ux Uy Uz Ty -> 0 1 0 0

// no translation along the y-axis thus Ty = 0
Forward Vector = Fx Fy Fx Tz -> 0 0 1 0

// no translation along the z-axis thus Tz = 0

If you put them in the matrix form:

|Rx Ux Fx 0|                  |1 0 0 0|
P[x y z] M |Ry Uy Fy 0| ==> P[x y z] * M |0 1 0 0|
|Rz Uz Fz 0|                  |0 0 1 0|
|Tx Ty Tz 1|                  |1 0 0 1|

Now to know that this is the right way of putting the vector R, U and F in the matrix if you multiply the point 0 0 0 by the matrix then you should get 1 0 0 (since the matrix is supposed to translate your point by 1 unit towards the right along the x-axis). So just compute and verify that it's actually the case:

P'x = 0 * 1 + 0 * 0 + 0 * 0 + 1 = 1
P'y = 0 * 0 + 0 * 1 + 0 * 0 + 0 = 0
P'z = 0 * 0 + 0 * 0 + 0 * 1 + 0 = 0

Hope it helps a bit. This should be maybe clearer on scratchapixel indeed. I would be you I would contact them and asked them questions when you don't know or can't find information on their website. I work with them and know they are keen on improving their website, etc.

Hope I didn't make things more messy in your mind though ;-)

### #2mast4as

Posted 07 January 2013 - 02:13 PM

Hi Kevin,

I hope I am not too late to help you with your problem/question. You did right to watch on scratchapixel but you should look at this page in particular:

http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-4-geometry/conventions-again-row-major-vs-column-major-vector/

Basically knowing whether you use a right or left hand coordinate system is indeed important but this is in fact in that particular case, little to do with whether you matrix is correct or not. What is important for constructing your matrix from the 3 vectors (up, right, forward) is whether you renderer uses row- or column-major notation for your matrices. As mentioned in the page above if you use row-major your 3 vectors should written vertically in the matrix and if you use column-major they should be written horizontally. In other words, assuming you have the vectors A B C (representing the right, top and forward vector from your camera coordinate system) you should write:

Ax Bx Cx
Ay By Cy
Az Bz Cz

If you use row-major notation. Now I will explain you why (there's a way an easy of checking how you should write the matrix if you can't remember it by heart). Remember that if you use row-major vector then the vector-matrix multiplication needs to be written as:

|Ax Bx Cx|
V[x y z] M |Ay By Cy|
|Az Bz Cz|

Computing the multiplication can be done like that:

V'x = Vx * Ax + Vy * Ay + Vz * Az
V'y = Vx * Bx + Vy * By + Vz * Bz
V'z = Vx * Cx + Vy * Cy + Vz * Cz

Okay so that doesn't help you much maybe. But imagine now that you actually want to transform a point using this technique. And to make it simple imagine you just want to translate a point along the x-axis. If you use row-major matrices the translation vector is located on the 13th, 14th and 16th coefficient of the matrix:

|Ax Bx Cx 0|
P[x y z] M |Ay By Cy 0|
|Az Bz Cz 0|
|Tx Ty Tz 1|

So imagine that you translate your camera along the x-axis to the right by 1 unit. Therefore your "right vector" is:

Right Vector   = Rx Ry Rz Tx -> 1 0 0 1
Up Vector      = Ux Uy Uz Ty -> 0 1 0 0

// no translation along the y-axis thus Ty = 0
Forward Vector = Fx Fy Fx Tz -> 0 0 1 0

// no translation along the z-axis thus Tz = 0

If you put them in the matrix form:

|Rx Ux Fx 0|                  |1 0 0 0|
P[x y z] M |Ry Uy Fy 0| ==> P[x y z] * M |0 1 0 0|
|Rz Uz Fz 0|                  |0 0 1 0|
|Tx Ty Tz 1|                  |1 0 0 1|

Now to know that this is the right way of putting the vector R, U and F in the matrix if you multiply the point 0 0 0 by the matrix then you should get 1 0 0 (since the matrix is supposed to translate your point by 1 unit towards the right along the x-axis).

P'x = 0 * 1 + 0 * 0 + 0 * 0 + 1 = 1
P'y = 0 * 0 + 0 * 1 + 0 * 0 + 0 = 0
P'z = 0 * 0 + 0 * 0 + 0 * 1 + 0 = 0

Hope I helped a bit. This should be clearer on scratch indeed. I would be you I would contact them and asked them questions when you don't know or can't find information on their website. I work with them and know they are keen on improving their website, etc.

Hope I didn't make things more messy in your mind though ;-)

### #1mast4as

Posted 07 January 2013 - 02:10 PM

Hi Kevin,

I hope I am not too late to help you with your problem/question. You did right to watch on scratchapixel but you should look at this page in particular:

http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-4-geometry/conventions-again-row-major-vs-column-major-vector/

Basically knowing whether you use a right or left hand coordinate system is indeed important but this is in fact in that particular case, little to do with whether you matrix is correct or not. What is important for constructing your matrix from the 3 vectors (up, right, forward) is whether you renderer uses row- or column-major notation for your matrices. As mentioned in the page above if you use row-major your 3 vectors should written vertically in the matrix and if you use column-major they should be written horizontally. In other words, assuming you have the vectors A B C (representing the right, top and forward vector from your camera coordinate system) you should write:

Ax Bx Cx

Ay By Cy

Az Bz Cz

If you use row-major notation. Now I will explain you why (there's a way an easy of checking how you should write the matrix if you can't remember it by heart). Remember that if you use row-major vector then the vector-matrix multiplication needs to be written as:

|Ax Bx Cx|

V[x y z] M  |Ay By Cy|

|Az Bz Cz|

Computing the multiplication can be done like that:

V'x = Vx * Ax + Vy * Ay + Vz * Az

V'y = Vx * Bx + Vy * By + Vz * Bz

V'z = Vx * Cx + Vy * Cy + Vz * Cz

Okay so that doesn't help you much maybe. But imagine now that you actually want to transform a point using this technique. And to make it simple imagine you just want to translate a point along the x-axis. If you use row-major matrices the translation vector is located on the 13th, 14th and 16th coefficient of the matrix:

|Ax Bx Cx 0|

P[x y z] M  |Ay By Cy 0|

|Az Bz Cz 0|

|Tx Ty Tz 1|

So imagine that you translate your camera along the x-axis to the right by 1 unit. Therefore your "right vector" is:

Right Vector = Rx Ry Rz Tx -> 1 0 0 1
Up Vector = Ux Uy Uz Ty -> 0 1 0 0 (no translation along the y-axis thus Ty = 0)

Forward Vector = Fx Fy Fx Tz -> 0 0 1 0

If you put them in the matrix form:

|Rx Ux Fx 0|                  |1 0 0 0|

P[x y z] M  |Ry Uy Fy 0| ==> P[x y z] * M |0 1 0 0|

|Rz Uz Fz 0|                  |0 0 1 0|

|Tx Ty Tz 1|                  |1 0 0 1|

Now to know that this is the right way of putting the vector R, U and F in the matrix if you multiply the point 0 0 0 by the matrix then you should get 1 0 0 (since the matrix is supposed to translate your point by 1 unit towards the right along the x-axis).

P'x = 0 * 1 + 0 * 0 + 0 * 0 + 1 = 1

P'y = 0 * 0 + 0 * 1 + 0 * 0 + 0 = 0

P'z = 0 * 0 + 0 * 0 + 0 * 1 + 0 = 0

Hope I helped a bit. This should be clearer on scratch indeed. I would be you I would contact them and asked them questions when you don't know or can't find information on their website. I work with them and know they are keen on improving their website, etc.

Hope I didn't make things more messy in your mind though ;-)

PARTNERS