Skip to main content

Posts

Showing posts from January, 2011

What matrix gluLookAt generates? (3)

gluLookAt implementation in python Here is my gluLookAt matrix computation code by python (numpy) and its test code. This code cares rather comprehensiveness than efficiency, therefore, there are matrix multiplication and matrix transpose. You can remove this if you want to have more efficient code. The test compares the result of this implementation and the OpenGL's computation result with less than 1.0e-6 error. As far as I tested, the result is in the error threshold in Ubuntu 10.04, python 2.6, glx version 1.4 ----- # Copyright (C) 2010-2011 H. Yamauchi # under New (3-clause) BSD license # # get lookat matrix (gluLookAt compatible matrix) python, numpy # # \param[in]  _eye    eye point # \param[in]  _lookat lookat point # \param[in]  _up     up vector # \return 4x4 gluLookAt matrix def getLookAtMatrix(_eye, _lookat, _up):   ez = _eye - _lookat   ez = ez / numpy.linalg.norm(ez)   ex = numpy.cross(_up, ez)   ex = ex / numpy.linalg.norm(ex)   ey = numpy.cross

What matrix gluLookAt generates? (2) Why Gimbal lock happens?

gluLookAt matrix (why Gimbal lock happens?)  The camera posture is represented by a rotation matrix, and camera position is represented by a translation matrix. Because in OpenGL, the default camera position and posture are defined and we move this camera around in the program. The camera is at origin, look into the Z axis minus direction, and up direction is Y axis plus direction. A rotation matrix and a translation matrix look like the following. where, x : camera basis X axis, ``right'' in the figure, ``side'' in the Mesa program in the last blog entry y : camera basis Y axis, ``up'' in the figure, ``up'' in the Mesa program in the last blog entry z : camera basis z axis, ``Z'' in the figure, ``-forward'' in the Mesa program in the last blog entry e : camera position, ``eye'' in the figure, ``eyex, eyey, eyez'' in the Mesa program in the last blog entry In the Mesa program, eye , lookat , up are given to t

What matrix gluLookAt generates? (1)

gluLookAt function I use OpenGL API to draw a 3D scene. This API is often used in games. If you only use OpenGL world, you usually don't need to know what kind of matrices are created underneath the API. However, if you want to overlay the OpenGL scene, for instance, if you write a own renderer and still want to render the scene with OpenGL and own, it is sometimes useful to know what kind of matrices are computed in the OpenGL API. Therefore, this blog entry is quite specific. If you are not interested in such story, see you next time. I would like to talk about a matrix that is computed by gluLookAt function. gluLookAt puts a camera with a position and camera posture, e.g., which is up direction. Recently, I need to know how to generate this matrix by my own, however, I could not find a good page with a good explanation. At the end, I looked into the source code of Mesa 7.5.1 (Mesa-7.5.1/src/glu/sgi/libutil/project.c), OpenGL software implementation. The code is quite r