To main page | 3dengine.org
Camera position in space (OpenGL)
Generally there's no such thing as "
camera position" in OpenGL, basically you start at point 0,0,0 looking down negative z-axis. (X-axis goes right, Y-axis goes up).
To see the world from (1,0,0), which is 1 unit right on x axis, you should move the camera right 1 unit, but you can't, because there is no such thing as "camera" position, so you have to "move" the whole WORLD 1 unit left.
This is done due to the fact, that world drawing process is simply matrix multiplication. The point in space is multiplied by
projection matrix, by
modelview matrix and then converted to
screen space. So, in order to "see" the world from 1 unit RIGHT, you have to modify
modelview matrix so that it would move every pixel 1 unit LEFT.
Finding world coordinates of camera
Use gluUnProject call with
center of viewport and 0 as z-coordinate to find real-world center of the camera.
gluUnProject(
(viewport[2]-viewport[0])/2 , (viewport[3]-viewport[1])/2,
0.0,
model, proj, view,
&cx,&cy,&cz
)
cx,cy,cz would be the center of
image plane,
viewport is
viewport matrix (See "Reading").
Camera direction
Finding "where" the camera looks is basically the same - use gluUnProject with
center of viewport and 1.0 (instead of 0.0) as z-coordinate and use
vector subtraction with the camera center (previous paragraph) to find the vector of where the camera is looking.
gluUnProject(
(viewport[2]-viewport[0])/2 , (viewport[3]-viewport[1])/2,
1.0,
model, proj, view,
&cx1,&cy1,&cz1
)
The vector (cx1-cx, cy1-cy, cz1-cz) would be the camera direction. Where cx,cy,cz is camera center (see "Finding world coordinates of camera" above).
Another approach would be
extracting back vector from
modelview matrix and inverting it.