PDA

View Full Version : projection matrix



Pswin
08-30-2009, 02:49 PM
hi

at now i use gluPerspective for calculating projection matrix but at now i use OGL 3.2 and i want to calculate it manually.

i read some articles about it and i try to implement, but i dosnt get any result.

1. how can i calculate a projection matrix and save it in a float array

2. which is faster: ModelViewMatrix * ProjectionMatrix on gpu
or calculating this on cpu

zeoverlord
08-30-2009, 05:11 PM
1. look at http://www.flashbang.se/ (http://www.flashbang.se/index.php?id=63) among other places

2, technically on the cpu since you only have to do it once, but if you have separate projection and modelview matrices you might go with on the gpu since then you have more choices with your shader.

Ultimatly it really doesn't matter though since were fillrate limited anyway you choose.

thinks
08-31-2009, 12:28 PM
Very nice explanation of projection matrix is available here (http://www.songho.ca/opengl/gl_projectionmatrix.html)

d07.RiV
08-31-2009, 01:19 PM
MSDN help for glFrustum explains how the matrix is calculated, that might help.

Pswin
08-31-2009, 02:51 PM
why it dosent work?



float h = 1.0f/tanf(m_fFov / 2);


_elements[0][0] = h / m_fAspect;
_elements[0][1] = 0;
_elements[0][2] = 0;
_elements[0][3] = 0;

_elements[1][0] = 0;
_elements[1][1] = h;
_elements[1][2] = 0;
_elements[1][3] = 0;

_elements[2][0] = 0;
_elements[2][1] = 0;
_elements[2][2] = m_fFarZ / (m_fFarZ - m_fNearZ) ;
_elements[2][3] = 1.0;

_elements[3][0] = 0;
_elements[3][1] = 0;
_elements[3][2] = -m_fNearZ * m_fFarZ / (m_fFarZ - m_fNearZ);
_elements[3][3] = 0;


and my shader:


#version 150 core
in vec3 in_Position;
in vec3 in_Color;
out vec3 ex_Color;
uniform mat4 in_ProjectionMatrix;
uniform mat4 in_ModelViewMatrix;
void main(void)
{
gl_Position = in_ModelViewMatrix * in_ProjectionMatrix * vec4(in_Position, 1.0);
ex_Color = in_Color;
}

V-man
08-31-2009, 03:48 PM
That should be
gl_Position = in_ProjectionMatrix * in_ModelViewMatrix * vec4(in_Position, 1.0);

and in case anyone wants the source code
http://www.opengl.org/wiki/GluPerspective_code