Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, some background. In 3D graphics, you need to worry about several vector spaces:</p> <ul> <li>Model space - these are usually the coordinates you specify to OpenGL</li> <li>World space - coordinates are specified with respect to some central point in the world.</li> <li>View space - coordinates are specified with respect to the camera</li> <li>Projection space - everything on the screen fits in the interval <code>[-1, +1]</code> in each dimension.</li> </ul> <p>Coordinates are specified homogeneously, so each vector has components <code>(x, y, z, w)</code>, where <code>w</code> is a scaling factor. You can obtain coordinates in 3-space as <code>(x/w, y/w, z/w)</code>. The scaling factor is needed for certain transformations like perspective projection that wouldn't be possible with non-homogenous coordinates.</p> <p>4x4 matrices are needed to transform coordinates from one vector space to another. You could have three different matrices:</p> <ul> <li>Model matrix (model to world)</li> <li>View matrix (world to view)</li> <li>Projection matrix (view to projection space)</li> </ul> <p>You would project a coordinate <code>C</code> onto the screen using the formula:</p> <pre><code>C' = P * V * M * C </code></pre> <p>OpenGL internally maintains two matrices: modelview (model and view multiplied together), and projection. There is also a texture matrix we won't worry about. When you call <code>glMatrixMode</code>, you are switching between these matrices. You replace the current matrix with the identity matrix using <code>glLoadIdentity</code>. You apply transformations to the current matrix with functions like <code>glTranslatef</code>, <code>glRotatef</code>, or <code>gluProjection</code>. Each of these functions just creates a 4x4 matrix the implements that specific transformation, then multiplies the current matrix by it. You can see what the transformation matrices are in the <a href="http://www.opengl.org/sdk/docs/man/" rel="noreferrer">OpenGL 2.1 reference pages</a>. </p> <hr> <p>Now for the actual answer. You need to maintain a 4x4 model matrix for each object in your scene. The model matrix will contain all the transformations needed to change model coordinates into world coordinates. For instance, every time you call <code>glTranslate</code>, you would update your model matrix:</p> <pre><code>T = [ 1 0 0 x ] [ 0 1 0 y ] [ 0 0 1 z ] [ 0 0 0 1 ] M' = M * T </code></pre> <p>You can then use your model matrix to get coordinates into world space (make sure they are homogenous coordinates first; just set <code>w = 1</code>, if they aren't):</p> <pre><code>V' = V * M </code></pre> <p>Since you are maintaining these transformations in parallel, you don't actually need to maintain the OpenGL modelview matrix anymore. You can pass in your model matrix as a uniform to your shaders. You can maintain your own view and projection matrices in similar ways. This is required in recent versions of OpenGL; all of the matrix handling functions are deprecated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload