View Full Version : Getting the coordinates of a pixel

10-18-2010, 05:22 AM

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

10-18-2010, 05:28 AM
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.

10-18-2010, 05:41 AM
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 !

10-20-2010, 10:57 AM
Anyone? I'm still struggling with this.

10-21-2010, 05:46 AM
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.

10-21-2010, 10:31 AM
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].

10-21-2010, 12:17 PM
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.

10-21-2010, 05:19 PM
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?

10-22-2010, 12:01 AM
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.

10-25-2010, 02:45 PM
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, &amp;z);

double objX,objY,objZ;
gluUnProject(i,Height-j,z,c,p, view , &amp;objX,&amp;objY,&amp;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.

10-26-2010, 03:00 AM
You should tell *what* is the problem you have now.

10-26-2010, 08:37 AM
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?