# How to create a perspective field of view matrix?

Hey, hello. I am learning a little of WebGL (OpenGL ES 2.0) through the http://learningwebgl.com tutorials, but it uses a third party library to create the View, Projection and transformation matrices (glutils.js). Well, I don't really like the idea of depending on a non standard library, so I am trying to learn how to create these matrices by myself.

I've already managed to understand most of it, and although I can't really tell why these matrices work if someone asks me, at least I know how to build them when I need to. Now I have my own lib to work with 4x4 matrices. I can create translation, scale, rotation and the lookAt matrix, but, I am having a hard time finding out how to create the perspective matrix. Anyone could help me out?

Here is my lib: http://pastebin.com/csMi9GuC

• You should totally learn about Matrix mathematics, it will make your life in 3D land that much easier. It would also make it obvious why it is a 4x4 matrix instead of a 3x3 one that you might expect in a setting with only three dimensions. – Robert Massaioli Mar 24 '11 at 0:46

## 1 Answer

Very well. That is fine approach. Sadly lots of game programmers do not understand their matrices, so don't worry about it now. Personally i use directx documentation as my personal reference if i'm not sure.

There is perspective matrix.

• What does that "cot" function does? in yScale = cot(fovY/2) – Delta Mar 23 '11 at 22:47
• Figured it out, it's cotangent (1 / tangent). It works, thank you! – Delta Mar 23 '11 at 23:00
• Don't forget to transpose that DirectX matrix for use in OpenGL – Steve H Mar 24 '11 at 1:35
• I have no idea why, but if I transpose it then it doesn't work, I also thought that I was supposed to transpose it, but it works fine the way it is on the msdn page. – Delta Mar 24 '11 at 2:38
• OpenGL expects a right-handed, column-major matrix. DirectX spits out a left-handed, row-major matrix. You would need to transpose the matrix twice (once to fix the 'handedness' and once to fix the 'majorness'). Transposing it a second time cancels out the first transpose. Using this logic, the way DirectX uses a matrix in memory is the same as OpenGL's matrix. – MarkP Aug 26 '11 at 14:14