Get camera position in opengl
I know in opengl that the camera is not moving, but the model is moving around it. Well, I need the camera position in MODELVIEW
... Yes, I also know there are a lot of topics in it, but I tried everything they do to get the camera coordinates, but it always returns (0, 0, 0)
(the actual camera position).
This is what I am trying:
GLfloat mdl[16];
float camera_org[3];
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glGetFloatv(GL_MODELVIEW_MATRIX, mdl);
camera_org[0] = -(mdl[0] * mdl[12] + mdl[1] * mdl[13] + mdl[2] * mdl[14]);
camera_org[1] = -(mdl[4] * mdl[12] + mdl[5] * mdl[13] + mdl[6] * mdl[14]);
camera_org[2] = -(mdl[8] * mdl[12] + mdl[9] * mdl[13] + mdl[10] * mdl[14]);
also tried this (as said in some sections):
camera_org[0] = mdl[12];
camera_org[0] = mdl[13];
camera_org[0] = mdl[14];
both of them give me the same result (0,0,0)
, can anyone tell me what i am doing wrong?
+3
source to share
1 answer
I found an answer for this:
int viewport[4];
// get matrixs and viewport:
glGetDoublev( GL_MODELVIEW_MATRIX, matModelView );
glGetDoublev( GL_PROJECTION_MATRIX, matProjection );
glGetIntegerv( GL_VIEWPORT, viewport );
gluUnProject( (viewport[2]-viewport[0])/2 , (viewport[3]-viewport[1])/2,
0.0, matModelView, matProjection, viewport,
&camera_pos[0],&camera_pos[1],&camera_pos[2]);
this will give you the coordinates of the camera in the scene.
+3
source to share