I need to plot a set of 3D points (could be curve/fonts) on surface of a mesh such that they can be retrieved later, so rendering points on a texture and attaching the texture does not work. The points will be drawed/edited by mouse clicks. This is for CAD purposes so precision is important.
How can I get 3D vertices in mesh local coordinates from 2D mouse position?
I am using OpenGL for the rendering part with perspective projection created using
glm::perspective()
with:FOV = 45.0f aspect ratio = 16 : 9 zNear = 0.1f zFar = 100.0f triangulated mesh
Is it possible to do Ray-Triangle Intersection calculations in the object space?
Does the camera Position (Origin) have to be World Space?
what you need is (Assuming old api OpenGL default notations):
FOVx,FOVy,znear
Model*View
matrix<-1,+1>
rangeYou can do this like this:
Yes you can compute this in mesh local coordinates and yes in such case you need Ray in the same coordinates.
Here simple C++/old api OpenGL/VCL example:
The only imnportant thing is function
ray_pick
which returns your 3D point based on mouse 2D position and actual inverse of ModelView matrix...Here preview:
I render yellow cross at the found 3D position as you can see its in direct contact to surface (as half of its line are below surface).
On top of usual stuff I used mine libs: gl_simple.h for the OpenGL context creation and GLSL_math.h instead of GLM for vector and matrix math. But the GL context can be created anyhow and you can use what you have for the math too (I think even the syntax is the same as GLM as they tried to mimic GLSL too...)
Looks like for aspects 1:1 this works perfectly, and in rectangular aspects its slightly imprecise the further away from screen center you get (most likely because I used raw
gluPerspective
which has some imprecise terms in it, or I missed some correction while ray creation but I doubt that)In case you do not need high precision (not your case as CAD/CAM needs as high precision as you can get) You can get rid of the ray mesh intersection and directly pick depth buffer at mouse position and compute the resulting point from that (no need for mesh).
For more info see related QAs: