fghoussen
fghoussen

Reputation: 565

OpenGL: screen-to-world transformation and good use of glm::unProject

I have what I believed to be a basic need: from "2D position of the mouse on the screen", I need to get "the closest 3D point in the 3D world". Looks like ray-tracing common problematic (even if it's not mine).

I googled / read a lot: looks like the topic is messy and lots of things gets unfortunately quickly intricated. My initial problem / need involves lots of 3D points what I do not know (meshes or point cloud from the internet), so, it's impossible to understand what result you should expect! Thus, I decided to create simple shapes (triangle, quadrangle, cube) with points that I know (each coord of each point is 0.f or 0.5f in local frame), and, try to see if I can "recover" 3D point positions from the mouse cursor when I move it on the screen.

Note: all coord of all points of all shapes are known values like 0.f or 0.5f. For example, with the triangle:

float vertices[] = {
    -0.5f, -0.5f, 0.0f,
     0.5f, -0.5f, 0.0f,
     0.0f,  0.5f, 0.0f
};

What I do

I have a 3D OpenGL renderer where I added a GUI to have controls on the rendered scene

In picture I get

enter image description here

The camera is positioned at (0., 0., 0.) and looks "ahead" (front = -z as z-axis is positive from screen to me). The shape is positioned (using tx, ty, tz, rx, ry, rz) "in front of the camera" with tz = -5 (5 units following the front vector of the camera)

What I get

Triangle in initial setting

enter image description here

I have correct xpos and ypos in world frame but incorrect zpos = 0. (z-buffering is allowed). I expected zpos = -5 (as tz = -5).

Question: why zpos is incorrect?

If I do not use glm::unProject, I get outer space results

enter image description here

Question: why "back-projecting" by-hand doesn't return consistent results compared to glm::unProject? Is this logical? Arethey different operations? (I believed they should be equivalent but they are obviously not)

Triangle moved with translation

enter image description here

After translation of about tx = 0.5 I still get same coordinates (local frame) where I expected to have previous coord translated along x-axis. Not using glm::unProject returns oute-space results here too...

Question: why translation (applied by model - not view nor proj) is ignored?

Cube in initial setting

enter image description here

I get correct xpos, ypos and zpos?!... So why is this not working the same way with the "2D" triangle (which is "3D" one to me, so, they should behave the same)?

Cube moved with translation

enter image description here

Translated along ty this time seems to have no effect (still get same coordinates - local frame).

Question: like with triangle, why translation is ignored?

What I'd like to get

The main question is why the model transformation is ignored? If this is to be expected, I'd like to understand why. If there's a way to recover the "true" position of the shape in the world (including model transformation) from the position of the mouse cursor, I'd like to understand how.

Upvotes: 0

Views: 550

Answers (1)

fghoussen
fghoussen

Reputation: 565

Question: glm::unProject doc says Map the specified window coordinates (win.x, win.y, win.z) into object coordinates, but, I am not sure to understand what are object coordinates. Does object coordinates refers to local, world, view or clip space described here?

As I am new to OpenGL, I didn't get that object coordinates from glm::unProject doc is another way to refer to local space. Solution: pass view*model to glm::unProject and apply model again, or, pass view to glm::unProject as explained here: Screen Coordinates to World Coordinates.

This fixes all weird behaviors I observed.

Upvotes: 0

Related Questions