0
votes

I've recently begun learning OpenGL and as far as the communication with the GL and shaders go, I've gained a good competence, but I'm a little unclear regarding the coordinate system.

I'm using OpenGL ES 2.0, so I have to do all of the matrix management myself. I've come to understand that the following:

  1. In order to observe correct results multiply the matrices as model * view * projection (which makes perfect sense as matrix multiplication is not commutative).

  2. The model matrix represents the scale, rotation, translation, etc of the drawing primitive. Moving the primitive in 3D space involves transformations applied to this matrix.

  3. The view matrix represents the "camera" as many refer to it. Distance between the object and viewer as well as rotation of the eye around the object come from transformations to this matrix.

  4. Finally, the perspective matrix represents the 3D space that the scene exists in. I'm following some examples which mostly use the perspective method using the field of view and aspect ratio. This is where my confusion starts.

A primitive's coordinates are given as a float in the interval +-[0, 1]. But the perspective matrix represents a 3D space from near-z to far-z (which I've seen to be 0 and 100 respectively). I'm confused as to how the primitive's coordinates map into this space. Clearly my linear algebra skills aren't as strong as I'd like them to be in this case. Can anyone clarify how these points are mapped into this space?

For instance, many examples define a cube to be at +-(.5, .5, .5). I I use a perspective martrix of near-z = 0, far-z = 100, and the aspect ratio of the entire display, would this mean that the cube is always at .5 of each of these dimensions? What I mean is, if the perspective matrix works out to go from x=-5 to x=5, does an x coordinate end up being .5(-5) = -2.5?

1
"multiply the matrices as model * view * projection" If this is what you observe, it's because your matrices are transposed.Nicol Bolas

1 Answers

1
votes

To understand how the vertices map, you have to understand what open gl does to get the vertices in their final form, called "normalized device coordinate" space, which goes from z = -1 to z = 1.

First, the view-space vertex is multiplied by the projection matrix. Then, the vertex is divided by its 4th or "w" component. This is called the perspective divide

Knowing this information, you can then derive the formula. This page explains the math that goes into the projection matrix to map the view z coordinate to the projection-z coordinate. If you don't care about the derivation, you can scroll down to see the final result, which is:

zn = (-(f+n) * ze / (f-n) - 2*f*n/(f-n) ) / (-ze)

Where zn is z in normalized device coordinates, ze is z in view (or eye) coordinates, f is the far clip plane and n is the near clip plane.

If you examine the formula, you'll notice that if a vertex's z component is equal to n, it maps to -1. If it is equal to f, it maps to 1, in between, it follows a hyperbolic function to go from -1 to 1.