Eigen perspective projection matrix

c++, eigen, opengl-es-2.0

Solution

In your case, the simplest is to use the comma initializer syntax:

Eigen::Matrix4f pmat;
pmat << xScale, 0, 0, 0,
        0, yScale, 0, 0,
        0, 0, -(zFar+zNear)/(zFar-zNear), -1,
        0, 0, -2*zNear*zFar/(zFar-zNear), 0;

Problem

I'm trying to create a perspective projection matrix for OpenGL. I know how to do it with a `float[16]` but for consistencies sake I'd like to use an Eigen matrix. The formula is: ``` [ xScale 0 0 0 ] P = [ 0 yScale 0 0 ] [ 0 0 -(zFar+zNear)/(zFar-zNear) -2*zNear*zFar/(zFar-zNear) ] [ 0 0 -1 0 ] ``` Where: ``` yScale = cot(fovY/2) xScale = yScale/aspectRatio ``` Since the formula is column-major and c-arrays are defined row-major, you would define a `float[16]` matrix with: ``` float P[16] = { xScale, 0, 0, 0, 0, yScale, 0, 0, 0, 0, -(zFar+zNear)/(zFar-zNear), -1 0, 0, -2*zNear*zFar/(zFar-zNear), 0 }; ``` So how exactly would I create a matrix like this with Eigen? Would I use an `Eigen::Affine3f` or a `Eigen::Matrix4f`? Looking at the documentation, it's not apparent to me how to set individual cell values.

Original source