I'm having a really hard time moving away from all OpenGL 3.x deprecated functions. Currently I'm trying to get my own projection-, model- and view-matrices to my shaders. While there were no problems implementing any of the (for me) all new rendering architecture, it seems like I can't get the projection matrix right, not even if I use the gluPerspective/glFrustum code. No matter which calculation I use (there are numerous on the web), I end up with my object being either too small, too wide (as if I used the wrong aspect ratio) or both.
Here is how I currently calculate the projection matrix:
void initPerspective(double zNear, double zFar, double aspect){ double fw, fh; fh = Math.tan(fov / 360 * Math.PI) * zNear; fw = fh * aspect; initFrustum(-fw, fw, -fh, fh, zNear, zFar);}void initFrustum(double left, double right, double bottom, double top, float zNear, float zFar){ projectionMatrix.m00 = (float) (2 * zNear / (right - left)); projectionMatrix.m02 = (float) ((right + left) / (right - left)); projectionMatrix.m11 = (float) (2 * zNear / (top - bottom)); projectionMatrix.m12 = (float) ((top + bottom) / (top - bottom)); projectionMatrix.m22 = (float) (-(zFar + zNear) / (zFar - zNear)); projectionMatrix.m23 = (float) (-2 * zFar * zNear / (zFar - zNear)); projectionMatrix.m32 = -1; projectionMatrix.invert();}
The above code produces (surprinsingly ;)) the exact same matrix as gluPerspective does (I've compared them). However, not only is my object only 1/10 of its size, it is also too wide, as if it was scaled by 1.5 in width.
One more thing noteworthy, before the matrix is actually used in my shader, I put it to column major order.
Thanks for reading.
Klaus
Edit:
Almost forgot the shader code:
// fragment shader#version 150in vec3 ex_Color;out vec4 out_FragColor;void main(void){ out_FragColor = vec4(ex_Color, 1.0);}// vertex shader#version 150uniform mat4 uni_ProjMat;uniform mat4 uni_ViewMat;uniform mat4 uni_ModelMat;in vec3 in_Vertex;in vec3 in_Color;out vec3 ex_Color;void main(void){ mat4 trans = uni_ProjMat * uni_ViewMat * uni_ModelMat; //mat4 trans = uni_ViewMat * uni_ModelMat; //mat4 trans = uni_ModelMat; gl_Position = trans * vec4(in_Vertex, 1.0); ex_Color = in_Color;}