Results 1 to 2 of 2

Thread: Perspective Matrix

  1. #1
    Newbie Newbie
    Join Date
    Oct 2013
    Posts
    2

    Perspective Matrix

    Hi everyone,
    I have a very weird situation. The image below show the correct rendered image which my opengl with GLSL should draw

    https://skydrive.live.com/embed?cid=...PrCb8J7orXduls

    however, this is the problem:

    when I add perspective projection matrix and my camera matrix to my GLSL, this will be the rendered problem


    https://skydrive.live.com/embed?cid=...C2D-Dt7g7lf44w



    the following is my projection GLSL code: ( I got this from song ha)

    float tangent = tanf(m_persProj.FOV /2 * DEG2RAD); // tangent of half fovY
    float height = m_persProj.zNear * tangent; // half height of near plane
    float width = height * (m_persProj.Width/ m_persProj.Height ) ; // half width of near plane

    m[0] = 2 * m_persProj.zNear / (m_persProj.Width- (-m_persProj.Width));
    m[2] = (m_persProj.Width + (-m_persProj.Width)) / (m_persProj.Width - (-m_persProj.Width));
    m[5] = 2 * m_persProj.zNear / (m_persProj.Height- (-m_persProj.Height));
    m[6] = (m_persProj.Height + (-m_persProj.Height)) / (m_persProj.Height -(-m_persProj.Height));
    m[10] = -(m_persProj.zFar + m_persProj.zNear) / (m_persProj.zFar - m_persProj.zNear);
    m[11] = -(2 * m_persProj.zFar* m_persProj.zNear) / (m_persProj.zFar - m_persProj.zNear);
    m[14] = -1;
    m[15] = 0;
    .
    .
    In my main GLSL code :


    #version 330 core \n\
    layout(location = 0) in vec4 vertexPosition_modelspace; \n\
    uniform mat4 gProjectionMatrix; \n\
    uniform float gScale; \n\
    uniform mat4 gCameraMatrix; \n\
    void main(void) \n\
    { \n\
    vec4 scolumn1,scolumn2,scolumn3,scolumn4; \n\
    scolumn1.xyz=vec3(gScale,0.0,0.0); \n\
    scolumn1.w=0.0; \n\
    scolumn2.xyz=vec3(0.0,gScale,0.0); \n\
    scolumn2.w=0.0; \n\
    scolumn3.xyz=vec3(0.0,0.0,gScale); \n\
    scolumn3.w=0.0; \n\
    scolumn4.xyz=vec3(0.0,0.0,0.0); \n\
    scolumn4.w=1.0; \n\
    mat4 mScale; \n\
    mScale=mat4( \n\
    scolumn1, \n\
    scolumn2, \n\
    scolumn3, \n\
    scolumn4 \n\
    ); \n\
    gl_Position =gProjectionMatrix*gCameraMatrix*mScale* vertexPosition_modelspace; \n\
    }";

    Later I substitute my projection code using :
    glUniformMatrix4fv(locProjMatrix , 1, GL_FALSE, p->GetTrans());

    If I do not use any camera matrix or projection matrix , this is my output:
    https://skydrive.live.com/embed?cid=...NlFA3dvZN-2WsY

    as you can see from the image ALL the MESH are rendered at the same coordinate.

    does anyone has any idea how to solve this?

    Thank you in advance

  2. #2
    Member Contributor
    Join Date
    Nov 2011
    Posts
    51
    Please check your "vertexPosition_modelspace" generator in your application, something could be wrong in it!

    Here is my working Perspective matrix generator, I hope this helps you to compareit with yours

    Code :
    procedure MakePerspectiveMatrixColumnOrder(var a : TeMatrixArrayOfFloats; fieldOfView, aspectRatio, zNear, zFar : TeMatrixFloat);
    const PI_OVER_360 = 0.0087266;
    var xymax, ymin, xmin, width, height, depth, q, qn, w, h : TeMatrixFloat;
    begin
      xymax := zNear * tan(fieldOfView * PI_OVER_360);
      ymin := -xymax;
      xmin := -xymax;
     
      width := xymax - xmin;
      height := xymax - ymin;
     
      depth := zfar - znear;
      q := -(zfar + znear) / depth;
      qn := -2 * (zfar * znear) / depth;
     
      w := 2 * znear / width;
      w := w / aspectRatio;
      h := 2 * znear / height;
     
      a[0]  := w;
      a[1]  := 0;
      a[2]  := 0;
      a[3]  := 0;
     
      a[4]  := 0;
      a[5]  := h;
      a[6]  := 0;
      a[7]  := 0;
     
      a[8]  := 0;
      a[9]  := 0;
      a[10] := q;
      a[11] := -1;
     
      a[12] := 0;
      a[13] := 0;
      a[14] := qn;
      a[15] := 0;
    end;

Similar Threads

  1. Replies: 1
    Last Post: 07-20-2014, 03:29 PM
  2. 3x3 perspective projection matrix
    By myk45 in forum OpenGL: Basic Coding
    Replies: 12
    Last Post: 09-19-2012, 06:21 AM
  3. How to use perspective matrix in openinventor?
    By hughnxy in forum OpenGL: General
    Replies: 0
    Last Post: 03-21-2008, 09:26 PM
  4. Perspective matrix
    By mr_coolio in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 01-29-2003, 09:02 AM
  5. Perspective Matrix
    By The Wolf in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 11-30-2000, 05:45 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