noName
noName

Reputation: 130

Why does this projection matrix not work?

(Sorry for my english skills...)

I tryed to write my own shader with glsl #version 440 so I also use my own projection matrix but I only get this: output instead of that (the second one is without the projection matrix)

Does anyone know the reason for what's happening ?

Matrix definition :

// projectionMatrix is global defined
void Perspective(float fovy, float aspect, float near, float far)
{
  fovy = degree*90;
  float f;
  f = 1/tan(fovy/2);

  projectionMatrix[0] = f/aspect;   projectionMatrix[4] = 0;    projectionMatrix[8] = 0;            projectionMatrix[12] = 0;
  projectionMatrix[1] = 0;      projectionMatrix[5] = f;    projectionMatrix[9] = 0;            projectionMatrix[13] = 0;
  projectionMatrix[2] = 0;      projectionMatrix[6] = 0;    projectionMatrix[10] = (far+near)/(near-far);   projectionMatrix[14] = (2*far*near)/near-far;
  projectionMatrix[3] = 0;      projectionMatrix[7] = 0;    projectionMatrix[11] = -1;          projectionMatrix[15] = 1;

  gl

UniformMatrix4fv(posProjectionMatrix, 1, GL_TRUE, projectionMatrix);
}

Vertex shader :

#version 440

in vec4 vertexPosition;
in vec4 vertexColor;

out vec4 Color;

uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main() 
{
  gl_Position = projectionMatrix*vec4(vertexPosition.xyz, 1);
  Color = vertexColor;
}

//END 

If you need more code, just ask.

Upvotes: 2

Views: 750

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54592

There are a couple of errors in your projection matrix:

projectionMatrix[14] = (2*far*near)/near-far;

There are parentheses missing in the expression, it should be:

projectionMatrix[14] = (2*far*near)/(near-far);

The last element is also wrong:

projectionMatrix[15] = 1;

This matrix element needs to be 0:

projectionMatrix[15] = 0.0f;

Also, as @Nicol Bolas already pointed out in a now deleted answer, you're building the matrix in column major order, so you should not use the argument to glUniformMatrix4fv() that transposes the matrix as part of the upload. The correct call is:

glUniformMatrix4fv(posProjectionMatrix, 1, GL_FALSE, projectionMatrix);

Upvotes: 2

Related Questions