Search code examples
openglhaskellmatrixprojection

Perspective Projection Matrix in OpenGL appears black


I am attempting to implement a perspective projection transformation using Haskell and OpenGL.

My vertices are set up as follows:

  let vertices =
        [ -0.3,  0.3,  0, -- Front Top Left 0                         
           0.3,  0.3,  0, -- Front Top Right 1 
           0.3, -0.3,  0, -- Front Bottom Right 2             
          -0.3, -0.3,  0, -- Front Bottom Left  3 
           0.3, 0.3,   0, -- Back Top Left      4            
           0.3,   0,   0, -- Back Top Right     5               
           0.3,   0,   0, -- Back Bottom Right  6              
           0.3, 0.3,   0  -- Back Bottom Left   7  ]

and, using no transformation, or just the View transformation, everything appears as it should (a grey square).

The view transformation is

 view = [cos ty * cos tz,    cos tz * sin tx * sin ty - cos tx * sin tz,         cos tx * cos tz * sin ty + sin tx * sin tz,    -cx,
         cos ty * sin tz,    cos tx * cos tz + sin tx * sin ty * sin tz,    -1 * cos tz * sin tx + cos tx * sin ty * sin tz,    -cy,
         -1 * sin ty,        cos ty * sin tx,                                    cos tx * cos ty,                               -cz,
         0,                  0,                                                  0,      1]

where (cx, cy, cz) are the camera's position and (tx, ty, tz) are the cameras rotation about the axis in radian Euler angles.


The problems occurs when I apply the perspective transformation

perspective = [scale,     0,                        0,                     0,
                   0, scale,                        0,                     0,
                   0,     0, -1 * (far + near) / diff,                    -1,
                   0,     0,   -2 * near * far / diff,                     0]

where diff = far - near and scale = 1.0 / tan (fov / 2.0) and fov = 0.785.

My far and near planes are 10 and 0.01, respectively.

Once applied, using the following GLSL code, nothing appears (ie. only a black screen).

void main()
{
    Color = color;
    gl_Position = perspective * view * vec4(position, 1.0);
}

I have tried viewing the square from multiple different angles and positions, and with culling disabled.

What is wrong with my perspective projection matrix or the application of it?


Solution

  • This was solved by transposing the view matrix.