[HowTo] Perspective Projection Matrix in OpenGL



HowTo - Programming

OpenGL


Here is a small function to create a perspective projection matrix. This function does not use OpenGL calls to initialize the matrix. This routine is nice because it can be used either with Direct3D (requires a little ogl to d3d matrix conversion – will be posted in another HowTo) or with the upcoming Larrabee too. And this function will be very useful to all coders that will need to produce OpenGL 3.1 compliant code because with OpenGL 3.1, you must have your own matrices functions!

void BuildPerspProjMat(float *m, float fov, float aspect, 
float znear, float zfar)
{
  float xymax = znear * tan(fov * PI_OVER_360);
  float ymin = -xymax;
  float xmin = -xymax;

  float width = xymax - xmin;
  float height = xymax - ymin;

  float depth = zfar - znear;
  float q = -(zfar + znear) / depth;
  float qn = -2 * (zfar * znear) / depth;

  float w = 2 * znear / width;
  w = w / aspect;
  float h = 2 * znear / height;

  m[0]  = w;
  m[1]  = 0;
  m[2]  = 0;
  m[3]  = 0;

  m[4]  = 0;
  m[5]  = h;
  m[6]  = 0;
  m[7]  = 0;

  m[8]  = 0;
  m[9]  = 0;
  m[10] = q;
  m[11] = -1;

  m[12] = 0;
  m[13] = 0;
  m[14] = qn;
  m[15] = 0;
}

and here is how to use it in an OpenGL 1 / OpenGL 2 code:

float m[16] = {0};
float fov=60.0f; // in degrees
float aspect=1.3333f;
float znear=1.0f;
float zfar=1000.0f;
BuildPerspProjMat(m, fov, aspect, znear, zfar);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(m);

// okay we can switch back to modelview mode
// for all other matrices
glMatrixMode(GL_MODELVIEW);
...

With a real OpenGL 3.0 code, we must use GLSL shaders and uniform variables to pass and exploit the transformation matrices:

glUseProgram(shaderId);
glUniformMatrix4fv("projMat", 1, GL_FALSE, m);
RenderObject();
glUseProgram(0);

[source]




Geeks3D.com

↑ Grab this Headline Animator