# Camera position

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

## Recommended Posts

I am trying to translate a mouse click into ingame coordinates. If anyone knows of a simple way to do this, please ignore the rest of my post. The problem is slightly simplified since I am always looking at the point (0,0,0) from some known angle. For now, let us assume that I am always somewhere on the z axis; I can generalise from there, I do believe. First question : When the Red Book speaks of the camera position, is this to be understood as the position of my physical eyes, or of my screen? Or perhaps something else again? And if it is my eyes, do we know where my screen is?

##### Share on other sites
The easiest (not necessarily the best) would be to read back the depth under your cursor and use gluUnproject to get the 3d world corrdinate from it. A better way would be to generate the ray from the near plane (mouse position) to the far plane and manually work out the intersections.

To get the ray points I belive you can also use gluUnproject just by passing in 0 (near) and 1 (far) for the depths instead of the readback depth.

Hope that helps

##### Share on other sites
Hmm, gluUnProject looks like just what I need; thanks! I don't quite understand the 'window z' coordinate, though. You seemed to imply that I could get it from reading back the depth under my cursor; how do you do this? Perhaps with glReadPixels? I don't quite understand, however, how the depth coordinate maps to the z coordinate, in the general case where you aren't necessarily looking down the z axis. Could you explain this, please?

##### Share on other sites
Quote:
 Original post by King of MenHmm, gluUnProject looks like just what I need; thanks! I don't quite understand the 'window z' coordinate, though. You seemed to imply that I could get it from reading back the depth under my cursor; how do you do this? Perhaps with glReadPixels? I don't quite understand, however, how the depth coordinate maps to the z coordinate, in the general case where you aren't necessarily looking down the z axis. Could you explain this, please?

Yup to read back the depth coordinate you should use glReadPixels, something like the following
double depth;glReadPixels(mouse_x, mouse_y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &depth)
The depth (z) in the depth buffer isn't the z coordinate of the world. Easiest way to think about it would be, the depth being the distance from the eye to the visible pixel. The depth buffer is used to perform hidden surface removal when rendering.

This will probably explain it better.

Hope that helps

##### Share on other sites
Ok, I understand that now, thanks. :) I think I won't use glReadPixel, as there's no guarantee I'm actually drawing anything in the pixel clicked on; but zero should do me, I think. A point plus a ray, I can do intersection from that.

Getting some weirdness, though : When I call glGetIntegerv in my mouse-response method to get my viewport, it gives back all zeroes. When I call it in my init method, though, it gives sensible values. Weird. Could it be a threading issue? I don't see how you can possibly have a race condition here, though. Let me see what happens in my drawing method.

##### Share on other sites
Oh well, no worries, I'll just cache the values from the drawing method. Thanks for helping me. :)

##### Share on other sites
The only place it would fail is if you call glGet... in a glBegin/glEnd block. Maybe just track your glViewport calls and use glGetError to make sure you aren't causing any. Of course if you don't change your window/viewport size then caching them will work.

1. 1
2. 2
3. 3
4. 4
frob
14
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632168
• Total Posts
3004544

×