Last active
April 16, 2020 19:43
-
-
Save triangle-soup/5880024da248edf438266de96ffef065 to your computer and use it in GitHub Desktop.
modo-ray-hit-test-three-ways
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // This is not a complete implementation, but rather code snippets. | |
| // Tool packet offsets are defined elsewhere. | |
| // | |
| CLxUser_VectorStack vector_stack (tool_vector); | |
| if (! vector_stack.test ()) | |
| { | |
| return LXe_FALSE; | |
| } | |
| // | |
| // 1. Using CLxUser_Raycast | |
| // | |
| CLxUser_RaycastPacket raycast_packet {}; | |
| if (! vector_stack.ReadObject (raycast_packet_offset_, raycast_packet)) | |
| { | |
| return LXe_FALSE; | |
| } | |
| auto screen_event = static_cast<LXpToolScreenEvent*>(vector_stack.Read (screen_event_packet_offset_)); | |
| if (! screen_event) | |
| { | |
| return LXe_FALSE; | |
| } | |
| LXtHitPolygon hit_polygon {}; | |
| raycast_packet.HitPolygon (vector_stack, LXf_LAYER_ACTIVE, screen_event->fcx, screen_event->fcy, &hit_polygon); | |
| // | |
| // 2. Using LXpToolHitEvent | |
| // | |
| auto hit_event = static_cast<LXpToolHitEvent*>(vector_stack.Read (hitevent_packet_offset_)); | |
| if (! hit_event) | |
| { | |
| return LXe_FALSE; | |
| } | |
| if (! hit_event->hit.pol) | |
| { | |
| return LXe_FALSE; | |
| } | |
| // | |
| // 3. Using CLxUser_View3D and CLxUser_Surface(not shown; must account for item transforms) | |
| // I don't use modo's macro maths in practice. | |
| // | |
| CLxUser_View3DportService view_service {}; | |
| const int view_index_current = view_service.Current (); | |
| CLxUser_View3D view {}; | |
| view_service.View (view_index_current, view); | |
| if (! view.test ()) | |
| { | |
| return LXe_FALSE; | |
| } | |
| auto screen_event = static_cast<LXpToolScreenEvent*>(vector_stack.Read (screen_event_packet_offset_)); | |
| if (! screen_event) | |
| { | |
| return LXe_FALSE; | |
| } | |
| const int pos_flags {0}; // alternatives: LXi_VPTO3D_SNAP; LXi_VPTO3D_WORK | |
| LXtVector point_from_screen {}; | |
| if (LXx_FAIL (view.To3D (screen_event->fcx, screen_event->fcy, point_from_screen, pos_flags))) | |
| { | |
| return LXe_FALSE; | |
| } | |
| LXtVector ray_direction {}; | |
| const double dist_to_point_from_ray_origin = view.EyeVector (point_from_screen, ray_direction); | |
| LXtVector ray_origin {}; | |
| LXx_VCPY (ray_origin, point_from_screen); | |
| LXx_VADDS (ray_origin, ray_direction, dist_to_point_from_ray_origin * -1.0); | |
| // pass ray to function that uses CLxUser_Surface::RayCast | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment