Saturday, March 7, 2009

The Z-buffer equation

I found an explanation for how OpenGL calculates z values here. It's handy if you want to do your own custom rendering in shaders and match OpenGLs depths for other objects.

Here's a function I wrote to compute it, based on the given distance:

float getDepth(float dist) {
float zNear = 0.1, zFar = 1000; // from main.cpp
float a = zFar / (zFar - zNear);
float b = zFar * zNear / (zNear - zFar);
float z = dist;
return a + b / z;
}

No comments: