• 13
• 10
• 27
• 9
• 9

# Depth Vector from a window coordinate

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

## Recommended Posts

Hi all - I've got a problem, and I can't think of a decent way around it. I basically need to find the intersection of a ray from the position of the mouse in windows coordinates to a given plane in world coordinates. What I know so far is that if the Mouse is located at 650, 200, I can use gluUnProject() to convert this to a world coordinate, no problem. I know the equation of the plane of which I am trying to intersect the mouse with. What I am missing, is the direction of the ray from the mouse position, through the world, to attempt to find an intersection with the plane. I know how to determine the direction of a ray originating at the centre of the screen, but as I am using a perspective view, I cannot determine the correct values for anything other than the window's centre point. Any help would be much appreciated, Thans in advance.

##### Share on other sites
Unproject the cursor at both the near and far clip planes. This gives you the two endpoints of the 3d line representing the position of the cursor in your world.

Enigma

##### Share on other sites
Quote:
 Original post by EnigmaUnproject the cursor at both the near and far clip planes. This gives you the two endpoints of the 3d line representing the position of the cursor in your world.Enigma

EDIT: read the next post by Kalidor for the actual solution, and ignore my rambling.

Just in case Enigma's answer is a bit too concise for your taste, an algorithm (it's more of an implementation actually, but who cares):
1. Call gluUnProject using the regular projection and modelview matrices to get the ray origin (ray_org).
2. Push projection matrix. Load identity. Call your gluPerspective with the same parameters as your actual projection except for the 'near' value. Use 'near + somevalue' (e.g. 'near + 1.0' or 'far' as Enigma suggested) as a near value.
3. Call gluUnProject again using the new Projection matrix and the same modelview as before to get the ray end (ray_end). Pop projection matrix.
4. Ray origin is ray_org, ray direction is ray_end - ray_org.

Tom

[Edited by - dimebolt on October 5, 2005 9:12:04 AM]

##### Share on other sites
Quote:
 Original post by dimeboltJust in case Enigma's answer is a bit too concise for your taste, an algorithm (it's more of an implementation actually, but who cares):1. Call gluUnProject using the regular projection and modelview matrices to get the ray origin (ray_org). 2. Push projection matrix. Load identity. Call your gluPerspective with the same parameters as your actual projection except for the 'near' value. Use 'near + somevalue' (e.g. 'near + 1.0' or 'far' as Enigma suggested) as a near value. 3. Call gluUnProject again using the new Projection matrix and the same modelview as before to get the ray end (ray_end). Pop projection matrix.4. Ray origin is ray_org, ray direction is ray_end - ray_org.Tom
You don't need to do that. You can just call gluUnProject twice using the same matrices and viewport, once with the winz parameter set to 0.0 (the near plane) and then again with it set to 1.0 (the far plane).
gluUnProject(winx, winy, 0.0, mv, p, view, &startX, &startY, &startZ);gluUnProject(winx, winy, 1.0, mv, p, view, &endX, &endY, &endZ);

##### Share on other sites
Quote:
Original post by Kalidor
Quote:
 Original post by dimebolt* ignorant bla *
You don't need to do that. You can just call gluUnProject twice using the same matrices and viewport, once with the winz parameter set to 0.0 (the near plane) and then again with it set to 1.0 (the far plane).
gluUnProject(winx, winy, 0.0, mv, p, view, &startX, &startY, &startZ);gluUnProject(winx, winy, 1.0, mv, p, view, &endX, &endY, &endZ);

Ah, ok. Replying to a topic about a function you never actually used is not too smart I guess. I didn't realize it actually had that many parameters.

Tom

##### Share on other sites
the start is where the camera is eg
gluLookAt( campos.x, campos.y, campos.z, .. )
so u dont need to calculate it

##### Share on other sites
thanks a lot! that's done the trick :)

##### Share on other sites
two questions :
1)to get the mv, p, view , i should pop them using glget ? or is there any other way around ?
2)after getting the ray , it would be in world space coordinates , but the models are rotated and translated , so how do i know the actual coordinates of a rotated and translated pixel ?
or how to calculate a new ray rotated and translated in the opposite direction (to compensate for the model shift) ?

##### Share on other sites
Quote:
 Original post by egNuKetwo questions :1)to get the mv, p, view , i should pop them using glget ? or is there any other way around ?2)after getting the ray , it would be in world space coordinates , but the models are rotated and translated , so how do i know the actual coordinates of a rotated and translated pixel ?or how to calculate a new ray rotated and translated in the opposite direction (to compensate for the model shift) ?

It would probably have been better to continue your question in your original post, rather than resurrecting this old thread.

1) Anyway, you probably should use glGet to obtain modelview and projection matrix (note that glGet does not pop the stack, it just reads it. Another option would be to keep track of them yourself, but that is not likely what you want to do. You can obtain viewport info through glGet as well.

2) With this method you use the modelview matrix so the ray will not be in world space but in model space. Basically, this means you will need to do the ray calaculation for every model that can be mouse-picked. Note that you should glGet the modelview matrix after all the transformations for a specific model have been performed.

Tom