Get normal with DXEngine raycast - Printable Version
+- AB4D Forum (https://forum.ab4d.com)
+-- Forum: Products Forums (https://forum.ab4d.com/forumdisplay.php?fid=4)
+--- Forum: Ab3d.DXEngine (https://forum.ab4d.com/forumdisplay.php?fid=11)
+--- Thread: Get normal with DXEngine raycast (/showthread.php?tid=4219)
Get normal with DXEngine raycast - janovrom - 10-09-2020
I'd like to ask how can I get a normal of hit point using DXEngine raycast.
I can obtain the hit result by using:
DXRayHitTestResult rayHitResult = DXScene.GetClosestHitObject(pickRay, rootNode);
It is possible to get rayHitResult.HitPosition and rayHitResult.TriangleIndex. But to what does the rayHitResult.TriangleIndex point to? MeshBase that can be accessed from rayHitResult.HitSceneNode does only have a buffer. I can cast it to SimpleMesh<PositionNormal> (or any other that I will use). Here it is possible to access IndexBufferArray.
The issue I have is that the TriangleIndex is not divisible by 3 (I expect that the IndexBufferArray is a triangle soup). So it can't be start index of the triangle. Maybe it's triangle strip?
TLDR: How can I get a normal from DXRayHitTestResult? Attached code is based on assumptions made above.
Edit: If the VertexBufferArray is accessed using directly t0, t1, and t2, it produces correct results half of a time, but can be outside of array range.
RE: Get normal with DXEngine raycast - abenedik - 10-12-2020
TriangleIndex is an index of a triangle and not an index of a TriangleIndice. You can get the index in the TriangleIndices collection or in IndexBuffer by multiplying TriangleIndex by 3 - for example, triangle indices for the 3rd triangle (in this case TriangleIndex is 2) start at TriangleIndice with index 6.
You should change your calculation of t0 ... t2 to: