OpenGL 3D Transformation: z-Aspect ratio

517 Views Asked by At

I'm doing 3D perspective projection in OpenGL (webgl), doing it myself with uniform-matrices. Everthing is working fine, but I have an aspect ration of 3:2 (600px x 400px) and this distorts all geometry rendered.

In 2D I used to fix this in the model matrix by dividing x and y through 1 / width and 1 / height respectively.

Now I also have z to worry about and I am pretty clueless how / where to transform z to not distort on my 3:2 aspect ratio.

The model matrix does not seem to offer any opportunity to do this and I don't know where / what to do in the projection matrix.

Edit: Projection Matrix:

@_pMatrix = [  
  1, 0.0, 0.0, 0.0,  
  0.0, 1, 0.0, 0.0,  
  0.0, 0.0, -(f + n) / (f - n), -1,  
  0.0, 0.0, -2.0 * n * f / (f - n), 0.0  
]  

Column major order

Edit 2: Weird distortions on n < 1 img

1

There are 1 best solutions below

8
On BEST ANSWER

You're missing the *left*, *right*, *top*, *bottom* in your projection matrix.

 2*n      
-----     0       0        0
 r-l

         2*n
  0     -----     0        0
         t-b

 r+l     t+b    -(f+n)
-----   -----   ------    -1
 r-l     t-b     f-n

                -2*f*n     0
  0       0     ------
                 f-n

If you define (r-l)/(t-b) = 3/2 such that the viewing volume is the appropriate size for your model, then you should be set.

Here are some slides describing the various projection matrices and how they're derived: http://www.cs.unm.edu/~angel/CS433/LECTURES/CS433_17.pdf

They're by Edward Angel, the author of Interactive Computer Graphics, which is where I got this matrix. Unfortunately the OpenGL Red Book doesn't seem to work through the math at all.