PDA

View Full Version : Projection Matrix problem



Aliii
09-25-2013, 03:58 AM
Im just moving to the modern OpenGL. I set up the view-matrix(without projection) and it works OK. I added a minimalist projection matrix and it also works fine.
GLfloat projection_matrix[16] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 };Then Ive tried to use a proper matrix and its not working out. That means, as I move the camera the movement and rotation seem like its mirrored.(I use a 90.0 perspective angle and 1.0 aspect ratio, to keep it simple.)
float n = 0.5, f = 100.0, w2 = 0.5, //width / 2 h2 = 0.5; //height / 2 GLfloat projection_matrix[16] = { n/w2, 0.0, 0.0, 0.0, 0.0, n/h2, 0.0, 0.0, 0.0, 0.0, -((f+n)/(f-n)), -1.0, 0.0, 0.0, -(2*f*n)/(f-n), 0.0 };I got it from here: http://www.songho.ca/opengl/gl_projectionmatrix.html...but its the same in the OGL Programming Guide 8th, so I guess it should be correct.This is the shader source BTW:
#version 330 corelayout(location = 0) in vec3 vertex_pos;uniform mat4 V_trans;uniform mat4 V_rot;uniform mat4 P;void main(){ vec4 pos_HOM = vec4( vertex_pos, 1); gl_Position = P * (V_rot * V_trans) * pos_HOM;} Im also wondering how can I set the near and far planes, ....as gluPerspective is for the "old" openGL I guess.Thanks!

Aliii
09-25-2013, 04:03 AM
Sorry, I copy-pasted the text and it got messed up. Strange

rombust
09-25-2013, 04:08 AM
Have a look at https://github.com/sphair/ClanLib/blob/master/Sources/API/Core/Math/mat4.h (header) https://github.com/sphair/ClanLib/blob/master/Sources/Core/Math/mat4.cpp (source) used for the http://clanlib.org/index.html ClanLib SDK
They generate the matrices in an OpenGL friendly way. Other libraries that do the same thing are freely available

GClements
09-25-2013, 07:19 AM
Sorry, I copy-pasted the text and it got messed up. Strange
Then edit it. Or if the edit window has expired, post a new message with legible code.

Aliii
09-25-2013, 08:20 AM
GLfloat projection_matrix[16] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 };...........
float n = 0.5, f = 100.0, w2 = 0.5, //width / 2 h2 = 0.5; //height / 2 GLfloat projection_matrix[16] = { n/w2, 0.0, 0.0, 0.0, 0.0, n/h2, 0.0, 0.0, 0.0, 0.0, -((f+n)/(f-n)), -1.0, 0.0, 0.0, -(2*f*n)/(f-n), 0.0 };........
#version 330 corelayout(location = 0) in vec3 vertex_pos;uniform mat4 V_trans;uniform mat4 V_rot;uniform mat4 P;void main(){ vec4 pos_HOM = vec4( vertex_pos, 1); gl_Position = P * (V_rot * V_trans) * pos_HOM;}

Aliii
09-25-2013, 08:21 AM
Its not working. I give up.