2

This is a topic that I come across often online, but most websites do a poor job properly explaining this. I'm currently creating my own Camera class as part of a 3D renderer built from scratch (how better to understand what happens, right?). But I've hit a snag with creating the World and View matrices for the camera.

As I understand it, the world matrix of a camera is essentially the matrix that places the camera where in needs to be in world space; which is only necessary when you need to render something in its position and according to its orientation. The View matrix, on the other hand, is the matrix that places the camera from that position to the origin of world space, facing along the z axis in one direction or another (negative for right-handed, positive for left-handed, I believe). Am I correct so far?

Given a matrix with its position defined as m_Eye and a lookat defined as m_LookAt, how do I generate the world matrix? More importantly, how do I generate the view matrix without having to perform an expensive inverse operations? I know that the rotation element's inverse is equal to its transpose, so I'm thinking that will factor into it. Regardless, this is the code I have been tinkering with. As an aside, I'm using a right-handed coordinate system.

The following code should generate the appropriate local coordinate axis:

m_W = AlgebraHelper::Normalize(m_Eye - m_LookAt);
m_U = AlgebraHelper::Normalize(m_Up.Cross(m_W));
m_V = m_W.Cross(m_U);

The following code is what I have, so far, for generating the World matrix (note, I also work with row-based matrices, so m_12 indicates the first row and second column):

Matrix4 matrix = Matrix4Identity;
matrix.m_11 = m_U.m_X;
matrix.m_12 = m_U.m_Y;
matrix.m_13 = m_U.m_Z;

matrix.m_21 = m_V.m_X;
matrix.m_22 = m_V.m_Y;
matrix.m_23 = m_V.m_Z;

matrix.m_31 = m_W.m_X;
matrix.m_32 = m_W.m_Y;
matrix.m_33 = m_W.m_Z;

matrix.m_41 = m_Eye.m_X;
matrix.m_42 = m_Eye.m_Y;
matrix.m_43 = m_Eye.m_Z;

Is this a good way to calculate the World matrix and, subsequently, how do I extract the View matrix?

Thank you in advance for any help in the matter.

Renegade Vile
  • 157
  • 10
  • 1
    There are countless tutorials that explain it well, from [this one](http://arcsynthesis.org/gltut) to The Red Book. – Bartek Banachewicz Apr 24 '13 at 14:38
  • Ah, thank you, I'll give it a look-see. I hadn't dug this up in any google search, so it may offer some new angles. What sources I had found were too varying and incomplete to give the big picture or simply use various different conventions making it impossible to grasp what's being done from one tutorial to the next. – Renegade Vile Apr 24 '13 at 16:54
  • @RenegadeVile Did you ever find anything that explained this well? – CodyBugstein Nov 14 '13 at 15:19
  • look here http://stackoverflow.com/a/21100338/2521214 ... camera matrix is (view coordinate system transform matrix)^-1 * (perspective matrix), world matrix does not exist. instead use matrix for each object on the scene – Spektre Jan 14 '14 at 10:43

0 Answers0