1
\$\begingroup\$

Suppose I had a 3D Cube on a 2D plane (screen). And I wanted to use the keypad to move and rotate it.

Without referring to a 3D Game Engine that could do this for me can you explain me the logic or link me to a tutorial on how to rotate the cube or change the camera view?

I tried to look online for a tutorial but its hard to exactly determine what is happening and its relevance to my scenario.

enter image description here

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You need to understand the traditional matrix stack that is associated with todays triangle rasterization pipeline. World-view-projection matrices, and how a world matrix can be built from a position, rotations, and scaling. I recommend this book, it has been very helpful for me. amazon.com/Math-Primer-Graphics-Development-Edition/dp/… \$\endgroup\$
    – Evan
    Aug 28 '13 at 14:48
  • \$\begingroup\$ A camera that rotates around an object is often called an "arcball". If you google that you will find plenty of tutorials. Here's one example. \$\endgroup\$ Aug 28 '13 at 17:36
  • 3
    \$\begingroup\$ "I tried to look online for a tutorial but its hard to exactly determine what is happening and its relevance to my scenario." Then stop trying to make it relevant to your scenario. Focus on understanding what it's trying to teach you. Once you understand, then you can apply that understanding to your scenario. \$\endgroup\$ Aug 29 '13 at 1:02
5
\$\begingroup\$

I'm assuming you know how basic matrix math works, so I'm not going to cover that in my answer.

What you eventually want to do is transform the vertices of the object you want to render from the virtual 3D world to the "real" 2D world (your monitor screen). You do that by multiplying the vertices of the object with a camera matrix (some people may use other terms for this, but it all comes down to basically the same thing). This camera matrix consists of two parts: an external (extrinsic) part and an internal (intrinsic) part.

The extrinsic part is a matrix that transforms the vertex from a 3D world coordinate system to a 3D camera coordinate system. Think this as positioning the camera inside your 3D world. Before the the transformation the vertex is viewed from the origin of the 3D world and after the transformation the vertex is viewed from the origin of the camera. So this is basically part of the answer to your question. If you want to move the camera, you manipulate the extrinsic parameter of the camera matrix (I will come back to this later).

The intrinsic part of the camera matrix is a matrix that transforms the camera coordinate system to an image coordinate system. So this is where the perspective projection takes place.

View Matrix

The extrinsic part of the camera matrix is often called the View matrix (World and Model matrices are related, but are not part of the view matrix). The view matrix is a 4X4 matrix that consists of a 3X3 rotation matrix and a 1x4 translation matrix (as seen below). The rotation matrix determines the orientation of the camera and the translation of the matrix describes the origin of the camera (so this is the camera's position in the virtual 3D world).

World to Camera

Projection Matrix

The intrincis part of the camera matrix is often called the Projection matrix. This is the matrix that does the actual projection from a 3D coordinate system to a 2D coordinate system. It projects everything in the camera's view frustum to a 2D plane (your monitor).

Camera to Screen

Now the perspective transformation is defined as follows:

Perspective transform

Where: Xim and Yim are your screen coordinates, Fx and Fy define the aspect ratio, S is a skew parameter and Xo and Yo define the focal length (please correct me if I'm wrong about this, my knowledge is kind of rusty for these parameters).

Vertex to screen

So in the end, if you want to transform a vertex coordinate to a screen coordinate you will end up with this:

Vertex to Screen

So, in the end if you want to rotate and move your camera, you need to need to modify the camera's View matrix.

P.S.: From a coding perspective: if you want to know how to construct the View matrix I refer you to this site, which does an excellent job of explaining how to create a LookAt matrix using OpenGL and C++.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.