Getting the coordinates of a pixel


I am quite new to opengl. I would like to get the coordinates (x,y,z) of every pixels of the window plane.

Thank you

If you mean you want, after some rendering, retrieve the depth information calculated for each pixel, then use glReadPixels on GL_DEPTH_COMPONENT :

If I misunderstood, please explain better what you want.

Thank you for the quick reply!
However, this was not what I was looking for. I’ll try to explain better:

I must implement raytracing. To do so I need to create rays that goes from my camera and pass by each pixels of the window plane.
I have all the information of my camera (Upvector, position, etc). Basically, all I need is to get the coordinates of each pixels so I can create rays!

I don’t know if I made myself clear !

Anyone? I’m still struggling with this.

I think you want to do what he said, get the depth buffer data. If the data at location (x, y) is z, then the pixel coordinate is (x, y, z). Otherwise I can’t understand what you want to do.

Thank you Zyx_2000.

Here I’ll show some pseudo-code of what I am trying to do:

For each pixel
Construct ray from camera through pixel
Find first primitive hit by ray
Determine color at intersection point
Draw color

I’m able to do everything there exept for the line: Construct ray from camera through pixel.

I’m wondering how to pass from let’s say pixel [i][j] to coordinates (x,y,z) so i can get the direction of the ray that starts from the camera and pass through pixel[i][j].

Ah you only need something like gluUnProject :

Keep winZ at something like 1 or 0.5 and shoot a ray from camera position to objX,objY,objZ.

Thank you ZbuffeR, I think this is exactly what I need!
One more question though…
GLint gluUnProject( GLdouble winX,
GLdouble winY,
GLdouble winZ,
const GLdouble * model,
const GLdouble * proj,
const GLint * view,
GLdouble* objX,
GLdouble* objY,
GLdouble* objZ);

How do I get winX and winY? and what does winZ represent as winX and winY are coordinates in 2 dimensions?

I have the viewport dimension(x,y,width and height) available. But these are in pixels. How do I know how much width and height to associate to a pixel?

Maybe this sample of my code will be clearer:

for(int i=0;i<width;i++)
for(int j=0;j<height;j++)
Ray theRay;
double objX,objY,objZ;


for the first 2 members of function gluUnProject, how do I iterate on i and j so it covers all my viewplane?

Window x,y coordinates are in pixels, origin is lower left corner, and z is the value returned by glReadPixels for GL_DEPTH_COMPONENT.

So that should be i,j in your example.

I am still having problem getting this to work, but I think I’m close to the solution. So here is a bit of my code:

for(int i=0;i<Width;i++)
for(int j=0;j<Height;j++)

		Ray theRay;
		GLdouble z;
		glReadPixels (i,Height-j, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
		double objX,objY,objZ;
		gluUnProject(i,Height-j,z,c,p, view , &objX,&objY,&objZ);

		Vector3 direction = Vector3(objX,objY,objZ) - mPosition;
		ColorRGB pixel_color = scene.PerformRaySceneQuery(theRay);

the c and p matrix are respectively the CameraModelViewMatrix and PerspectiveProjectionMatrix. I know for sure that my intersection from ray with objects are good. The problem must be from the rays I am generating from the camera.

You should tell what is the problem you have now.

The problem is that I am not getting the expected results. The rendered image I am getting is not good. I am getting an Image with only the background color when there are objects directly in front of the camera.

Like I said, the function that looks for intersection and return the color of the intersection is definitely working since it was code given to me for this work.

So I am wondering, did I use the function glUnproject properly for what I am intending to do?