# Going from transform matrix + depth to world coordinates

This topic is 5396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am working on a project that works like this: take a very complex scene and render it in parallel on a large cluster, then send the rendered images (pixel colors + depth buffer) to a client machine. The client machine takes the pixels + depth and puts it on a plane. The plane is texture mapped, and it is also broken up into an 85 x 85 grid so that at each vertex I can put a specific depth. The images that were rendered are all 256x256. The idea is that I want to read the depth buffer and reproject the x,y,z coordinates at the vertices back into world space. I have the transformation matrix and its inverse, and I'm trying something like this: glPushMatrix(); // ... //apply the same transformations // ... glGetDoublev(GL_PROJECTION_MATRIX, (double*)matrix); glPopMatrix(); invertMatrix(matrix, mat_inv); // loop over all vertices for (i = 0; i < m_order.cv; i++) { // get the window space (pixel) coordinates int vindex = m_order.vertex; int x = vindex % 256; int y = vindex / 256; // z is just the depth read at that pixel double z = v_depth[y][x]; // create a new point in homogeneous coordinates double pos[] = { (x - 127.5) / 127.5f, (y - 127.5) / 127.5f, z, 1.0f}; // find the original point by multiplying homogenous coordinates by the inverse of the transformation matrix double newpos[4]; matmul(mat_inv, pos, newpos); // divide by the perspective (w) for (j = 0; j < 3; j++) { newpos[j] /= newpos[3]; } verts.x = (float)newpos[0]; verts.y = (float)newpos[1]; verts.z = (float)newpos[2]; } I'm getting something which is close, but the z values are too large. Has anyone done something like this before that can maybe point out a flaw in my logic or something simple I'm just not thinking about? Thanks

• 13
• 18
• 29
• 11