PDA

View Full Version : I have a problem wit my projection matrix...



_NoName_
06-24-2016, 01:41 PM
(Sorry for my english skills...)

I tryed to write my own GLSL #version440 schader but when I add my projectionMatrix I get a black screen, I think because of any reason my matrix is 0 (when I use the vertex coords for the color the object is black (matrix is not added to the vertex but to the color....))

Matrix :
// projectionMatrix is 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[1] = 0; projectionMatrix[2] = 0; projectionMatrix[3] = 0;
projectionMatrix[4] = 0; projectionMatrix[5] = f; projectionMatrix[6] = 0; projectionMatrix[7] = 0;
projectionMatrix[8] = 0; projectionMatrix[9] = 0; projectionMatrix[10] = (far+near)/(near-far); projectionMatrix[11] = (2*far*near)/near-far;
projectionMatrix[12] = 0; projectionMatrix[13] = 0; projectionMatrix[14] = -1; projectionMatrix[15] = 1;

glUniformMatrix4fv(posProjectionMatrix, 1, GL_FALSE, 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, 10);
Color = vertexColor;
}

//END

Do anyone know why I get a black screen ??

GClements
06-24-2016, 03:08 PM
projectionMatrix[0] = f/aspect; projectionMatrix[1] = 0; projectionMatrix[2] = 0; projectionMatrix[3] = 0;
projectionMatrix[4] = 0; projectionMatrix[5] = f; projectionMatrix[6] = 0; projectionMatrix[7] = 0;
projectionMatrix[8] = 0; projectionMatrix[9] = 0; projectionMatrix[10] = (far+near)/(near-far); projectionMatrix[11] = (2*far*near)/near-far;
projectionMatrix[12] = 0; projectionMatrix[13] = 0; projectionMatrix[14] = -1; projectionMatrix[15] = 1;
glUniformMatrix4fv(posProjectionMatrix, 1, GL_FALSE, projectionMatrix);


First, the matrix needs to be transposed. The above would (almost) be typical for a perspective projection if the matrix was in row-major order, but OpenGL uses column-major order by default.

Second, index 15 should be 0, not 1. For a perspective projection, the W component should be strictly proportional to the Z component. Affine transformations (such as rotation, scale, translation and orthographic projection) simply copy the W component (i.e. the bottom row is [0 0 0 1]), but a perspective projection isn't an affine transformation.





gl_Position = projectionMatrix*vec4(vertexPosition.xyz, 10);


Why are you setting the W component to 10? This effectively scales vertexPosition by 1/10 (this only affects the computed depth value, so the effect is as if you scaled the near and far distances by 10).

_NoName_
06-25-2016, 04:35 AM
Now I get a strange result.
I don't know why but may it be that I miss something in the shader ??

_NoName_
06-25-2016, 04:40 AM
I changed the GL_FALSE to GL_TRUE, now I get a black screen.

MATRIX :

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

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

_NoName_
06-25-2016, 05:49 AM
I solved it, projectionMatrix[11] has to be (2*far*near)/(near-far) not (2*far*near)/near-far .