To main page | 3dengine.org

Center of Viewport


Center of viewport (or camera center) is X,Y coordinates right in the middle of current screen space in OpenGL can be found as:
(viewport[2]-viewport[0])/2 , (viewport[3]-viewport[1])/2
where viewport is the viewport matrix that can be loaded via glGetIntegerv call.

Why not (Width of window / 2) ?

There are some times that center of window isn't really center of "virtual camera", like when you are drawing multiple views of the same scene - there might be 2,3,4 or more "camera centers", each in the middle of it's respective viewport.

3D world coordinates

In real math,
C(x,y,z) = -R' * t , where ' is transpose
but remember about column major ordering. R is rotation matrix (modelview's upper left 3x3 matrix, transposed), t is translation 3x1 matrix (in terms of right-up-back vectors).

3D coordinates of current camera center in PyOpenGL

1 way:
gluUnProject( (viewport[2]-viewport[0])/2 , (viewport[3]-viewport[1])/2,  
    .... )
Another:
import numpy
buffer = glGetDoublev(GL_MODELVIEW_MATRIX)  # [1]
cXYZ = -1 * numpy.mat(buffer[:3,:3]) * numpy.mat(buffer[3,:3]).T
[1] Avoid glGetFloat in Python