Results 1 to 5 of 5

Thread: I have a problem wit my projection matrix...

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2016
    Posts
    19

    I have a problem wit my projection matrix...

    (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 ??

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,103
    Quote Originally Posted by _NoName_ View Post
    Code :
      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.

    Quote Originally Posted by _NoName_ View Post
    Code :
      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).

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2016
    Posts
    19

    Now I get a strange result...

    Now I get a strange result.
    I don't know why but may it be that I miss something in the shader ??

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2016
    Posts
    19

    Black screen

    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);
    }

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2016
    Posts
    19

    Problem solved

    I solved it, projectionMatrix[11] has to be (2*far*near)/(near-far) not (2*far*near)/near-far .

Similar Threads

  1. Replies: 0
    Last Post: 09-16-2015, 03:27 PM
  2. Projection Matrix problem
    By Aliii in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 09-25-2013, 08:21 AM
  3. Projection matrix, arbitrary projection plane
    By omdown in forum OpenGL: General
    Replies: 0
    Last Post: 04-01-2010, 07:26 AM
  4. Can concatenate projection matrix with viewport transformation matrix?
    By MelvinEng in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 07-07-2002, 10:31 PM
  5. Custom projection matrix (UV problem??)
    By hemicube in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 12-05-2000, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean