David Doria

02-24-2009, 02:38 PM

I am trying to get the depth at of the 3d point that was projected to every pixel. I am reading the depth buffer, then using gluUnProject to get the 3d location at each pixel, then finding the distance from the 3d location to the camera. However, it is producing the following result:

http://rpi.edu/~doriad/cube.jpg

On the left is a cube rendered with perspective projection. The right is the output of my code. As you can see, there are bizarre lines through the whole thing, but the overall shape is right which leads me to believe I am close to doing it right. Does anyone see a problem with this code?

void ReadDepthBuffer()

{

float depths[640*480];

glReadPixels(0, 0, 640, 480, GL_DEPTH_COMPONENT, GL_FLOAT, depths);

WriteDepthFile("buffer.txt", depths);

}

void WriteDepthFile(const string &Filename, float* depths)

{

ofstream fout(Filename.c_str());

double ModelView[16];

glGetDoublev(GL_MODELVIEW_MATRIX, ModelView);

double Projection[16];

glGetDoublev(GL_PROJECTION_MATRIX, Projection);

GLint Viewport[4];

glGetIntegerv(GL_VIEWPORT, Viewport);

double x;

double y;

double z;

vnl_double_3 Eye(EyeX, EyeY, EyeZ);

for(unsigned int im_x = 0; im_x < Width; im_x++)

{

for(unsigned int im_y = 0; im_y < Height; im_y++)

{

double CurrentDepth = depths[Height*im_x + im_y];

bool success = gluUnProject(static_cast<double>(im_x), static_cast<double>(im_y), CurrentDepth, ModelView, Projection, Viewport, &x, &y, &z);

vnl_double_3 ModelCoord(x, y, z);

double Dist = (ModelCoord - Eye).magnitude();

if(!success)

Dist = 0.0;

fout << im_x << " " << im_y << " " << Dist << endl;

}

}

fout.close();

}

Thanks,

Dave

http://rpi.edu/~doriad/cube.jpg

On the left is a cube rendered with perspective projection. The right is the output of my code. As you can see, there are bizarre lines through the whole thing, but the overall shape is right which leads me to believe I am close to doing it right. Does anyone see a problem with this code?

void ReadDepthBuffer()

{

float depths[640*480];

glReadPixels(0, 0, 640, 480, GL_DEPTH_COMPONENT, GL_FLOAT, depths);

WriteDepthFile("buffer.txt", depths);

}

void WriteDepthFile(const string &Filename, float* depths)

{

ofstream fout(Filename.c_str());

double ModelView[16];

glGetDoublev(GL_MODELVIEW_MATRIX, ModelView);

double Projection[16];

glGetDoublev(GL_PROJECTION_MATRIX, Projection);

GLint Viewport[4];

glGetIntegerv(GL_VIEWPORT, Viewport);

double x;

double y;

double z;

vnl_double_3 Eye(EyeX, EyeY, EyeZ);

for(unsigned int im_x = 0; im_x < Width; im_x++)

{

for(unsigned int im_y = 0; im_y < Height; im_y++)

{

double CurrentDepth = depths[Height*im_x + im_y];

bool success = gluUnProject(static_cast<double>(im_x), static_cast<double>(im_y), CurrentDepth, ModelView, Projection, Viewport, &x, &y, &z);

vnl_double_3 ModelCoord(x, y, z);

double Dist = (ModelCoord - Eye).magnitude();

if(!success)

Dist = 0.0;

fout << im_x << " " << im_y << " " << Dist << endl;

}

}

fout.close();

}

Thanks,

Dave