c++ - How do I find intersection of mouse click and a 3D mesh? -
in program i'm loading in 3d mesh view , interact with. user can rotate , scale view. using rotation matrix rotation , calling multmatrix rotate view, , scaling using glscalef. user can paint mesh, , why need translate mouse coordinates see if intersects mesh.
i've read http://www.opengl.org/resources/faq/technical/selection.htm , method use gluunproject @ near , far plane , subtracting, , have success it, when glulookat's position (0, 0, z) z can reasonable number. when move position (0, 1, z), goes haywire , returns intersection there empty space, , returns no intersection mesh underneath mouse.
this how i'm making ray mouse click scene:
float hx, hy; hx = mouse_x; hy = mouse_y; gldouble m1x,m1y,m1z,m2x,m2y,m2z; glint viewport[4]; gldouble projmatrix[16], mvmatrix[16]; glgetintegerv(gl_viewport,viewport); glgetdoublev(gl_modelview_matrix,mvmatrix); glgetdoublev(gl_projection_matrix,projmatrix); //unproject find actual coordinates gluunproject(hx,scrheight-hy,2.0,mvmatrix,projmatrix,viewport,&m1x,&m1y,&m1z); gluunproject(hx,scrheight-hy,10.0,mvmatrix,projmatrix,viewport,&m2x,&m2y,&m2z); gmvector3 dir = gmvector3(m2x,m2y,m2z) - gmvector3(m1x,m1y,m1z); dir.normalize(); gmvector3 point; bool intersected = findintersection(gmvector3(0,0,5), dir, point); i'm using glfrustum if makes difference.
the findintersection code long , i'm pretty confident works, won't post unless wants see it. gist of every face in mesh, find intersection between ray , plane, see if intersection point inside face.
i believe has camera's position , @ vector, don't know how, , them mouse clicks on mesh properly. can me this?
i haven't yet made rotation matrix or glscalef, can give me insight this? like, unproject account multmatrix , glscalef calls when calculating?
many thanks!
the solution raytracing. ray shoot defined through 2 points. first 1 origin of camera, second 1 mouse position projected on view plane in scene (the plane describe glfrustum). intersection of ray model point mouse click has hit model
Comments
Post a Comment