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=C60CEFC8DF3DAC53&resid=C60CEFC8DF3DAC53!109&authkey=APrCb8J7orXduls

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=C60CEFC8DF3DAC53&resid=C60CEFC8DF3DAC53!111&authkey=AC2D-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

layout(location = 0) in vec4 vertexPosition_modelspace;

uniform mat4 gProjectionMatrix;

uniform float gScale;

uniform mat4 gCameraMatrix;

void main(void)

{

vec4 scolumn1,scolumn2,scolumn3,scolumn4;

scolumn1.xyz=vec3(gScale,0.0,0.0);

scolumn1.w=0.0;

scolumn2.xyz=vec3(0.0,gScale,0.0);

scolumn2.w=0.0;

scolumn3.xyz=vec3(0.0,0.0,gScale);

scolumn3.w=0.0;

scolumn4.xyz=vec3(0.0,0.0,0.0);

scolumn4.w=1.0;

mat4 mScale;

mScale=mat4(

scolumn1,

scolumn2,

scolumn3,

scolumn4

);

gl_Position =gProjectionMatrixgCameraMatrixmScale* vertexPosition_modelspace;

}";

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=C60CEFC8DF3DAC53&resid=C60CEFC8DF3DAC53!110&authkey=ANlFA3dvZN-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 :slight_smile:

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 :wink:

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;