PDA

View Full Version : Perspective Matrix



VirtualMan
01-28-2014, 07:42 PM
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%21109&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%21111&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 \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=C60CEFC8DF3DAC53&resid=C60CEFC8DF3DAC53%21110&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 :)

tdname
01-29-2014, 04:43 AM
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 ;)


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;