Skip to main content

History: Raycasting to the polygon level - Mogre

Preview of version: 12

This is a Mogre version of Raycasting to the polygon level as ported by Funguine (ketarax on #ogre3d) & Antont.

Here is the forum thread of the original code (C++).

Introducion

An Ogre ray query generally only detect bounding boxes (-AABBs), like you see on this image. With this code you can avoid this disadvantage.
Rays_and_BoundingBox_1a.gif

Important note

This code only returns correct results for polygons defined by rendertype OT_TRIANGLE_LIST. If a mesh contains other rendertypes (different triangle definition), lines or points, you will get wrong results or even a crash. For more details and improvement suggestions look here.

Code for initialization

Define this field (variable) in your class

Copy to clipboard
RaySceneQuery m_pray_scene_query;


Initialize it, for example in CreateScene()

Copy to clipboard
// create the ray scene query object m_pray_scene_query = sceneMgr.CreateRayQuery(new Ray(), SceneManager.WORLD_GEOMETRY_TYPE_MASK); if (null == m_pray_scene_query) { return false; } m_pray_scene_query.SetSortByDistance(true);

Method for raycast

Copy to clipboard
// raycast from a point in to the scene. // returns success or failure. // on success the point is returned in the result. public bool RaycastFromPoint(Vector3 point, Vector3 normal, ref Vector3 result, ref Vector3 resNormal) { // create the ray to test Ray ray = new Ray(point, normal); // check we are initialised if (m_pray_scene_query != null) { // create a query object m_pray_scene_query.Ray = ray; // execute the query, returns a vector of hits RaySceneQueryResult rayresult = m_pray_scene_query.Execute(); if (rayresult.Count <= 0) { // raycast did not hit an objects bounding box return false; } } else { return false; } // at this point we have raycast to a series of different objects bounding boxes. // we need to test these different objects to see which is the first polygon hit. // there are some minor optimizations (distance based) that mean we wont have to // check all of the objects most of the time, but the worst case scenario is that // we need to test every triangle of every object. float closest_distance = -1.0f; Vector3 closest_result = Vector3.ZERO; Vector3 vNormal = Vector3.ZERO; RaySceneQueryResult query_result = m_pray_scene_query.GetLastResults(); foreach (RaySceneQueryResultEntry this_result in query_result) { // stop checking if we have found a raycast hit that is closer // than all remaining entities if ((closest_distance >= 0.0f) && (closest_distance < this_result.distance)) { break; } // only check this result if its a hit against an entity if ((this_result.movable != null) && (this_result.movable.MovableType == "Entity")) { // get the entity to check Entity pentity = (Entity)this_result.movable; // mesh data to retrieve uint vertex_count = 0; uint index_count = 0; Vector3[] vertices = new Vector3[0]; UInt64[] indices = new UInt64[0]; // get the mesh information GetMeshInformation(pentity.GetMesh(), ref vertex_count, ref vertices, ref index_count, ref indices, pentity.ParentNode._getDerivedPosition(), // WorldPosition pentity.ParentNode._getDerivedOrientation(), // WorldOrientation pentity.ParentNode.GetScale()); int ncf = -1; // new_closest_found // test for hitting individual triangles on the mesh for (int i = 0; i < (int)index_count; i += 3) { // check for a hit against this triangle Pair<bool, float> hit = Mogre.Math.Intersects(ray, vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]], true, false); // if it was a hit check if its the closest if (hit.first) { if ((closest_distance < 0.0f) || (hit.second < closest_distance)) { // this is the closest so far, save it off closest_distance = hit.second; ncf = i; } } } if (ncf > -1) { closest_result = ray.GetPoint(closest_distance); // if you don't need the normal, comment this out; you'll save some CPU cycles. Vector3 v1 = vertices[indices[ncf]] - vertices[indices[ncf + 1]]; Vector3 v2 = vertices[indices[ncf + 2]] - vertices[indices[ncf + 1]]; vNormal = v1.CrossProduct(v2); } // free the verticies and indicies memory vertices = null; indices = null; } } // if we found a new closest raycast for this object, update the // closest_result before moving on to the next object. if (closest_distance >= 0.0f) { result = new Vector3(closest_result.x, closest_result.y, closest_result.z); resNormal = vNormal / vNormal.Normalise(); /* // this visualizes the 'result' position if (!sceneMgr.HasSceneNode("marker")) { SceneNode node = sceneMgr.CreateSceneNode("marker"); Entity ent = sceneMgr.CreateEntity("marker", "Cube.mesh"); node.AttachObject(ent); node.Position = result; node.Scale(0.25f, 0.25f, 0.25f); sceneMgr.RootSceneNode.AddChild(node); } else { sceneMgr.GetSceneNode("marker").Position = result; } */ // raycast success return true; } else { // raycast failed return false; } } // RayCastFromPoint

GetMeshInformation

This code is a port from RetrieveVertexData.

There are two alternative versions of GetMeshInformation(). One takes into account the animation state of an entity and the other one solves compiling errors for 64bit using GCC.

Copy to clipboard
// Get the mesh information for the given mesh. // Code found in Wiki: www.ogre3d.org/wiki/index.php/RetrieveVertexData public unsafe void GetMeshInformation(MeshPtr mesh, ref uint vertex_count, ref Vector3[] vertices, ref uint index_count, ref UInt64[] indices, Vector3 position, Quaternion orientation, Vector3 scale) { bool added_shared = false; uint current_offset = 0; uint shared_offset = 0; uint next_offset = 0; uint index_offset = 0; vertex_count = index_count = 0; // Calculate how many vertices and indices we're going to need for (ushort i = 0; i < mesh.NumSubMeshes; ++i) { SubMesh submesh = mesh.GetSubMesh(i); // We only need to add the shared vertices once if (submesh.useSharedVertices) { if (!added_shared) { vertex_count += mesh.sharedVertexData.vertexCount; added_shared = true; } } else { vertex_count += submesh.vertexData.vertexCount; } // Add the indices index_count += submesh.indexData.indexCount; } // Allocate space for the vertices and indices vertices = new Vector3[vertex_count]; indices = new UInt64[index_count]; added_shared = false; // Run through the submeshes again, adding the data into the arrays for (ushort i = 0; i < mesh.NumSubMeshes; ++i) { SubMesh submesh = mesh.GetSubMesh(i); VertexData vertex_data = submesh.useSharedVertices ? mesh.sharedVertexData : submesh.vertexData; if (!submesh.useSharedVertices || (submesh.useSharedVertices && !added_shared)) { if (submesh.useSharedVertices) { added_shared = true; shared_offset = current_offset; } VertexElement posElem = vertex_data.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.VES_POSITION); HardwareVertexBufferSharedPtr vbuf = vertex_data.vertexBufferBinding.GetBuffer(posElem.Source); byte* vertex = (byte*)vbuf.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY); float* pReal; // There is _no_ baseVertexPointerToElement() which takes an Ogre::Real or a double // as second argument. So make it float, to avoid trouble when Ogre::Real will // be comiled/typedefed as double: // Ogre::Real* pReal; for (int j = 0; j < vertex_data.vertexCount; ++j, vertex += vbuf.VertexSize) { posElem.BaseVertexPointerToElement(vertex, &pReal); Vector3 pt = new Vector3(pReal[0], pReal[1], pReal[2]); vertices[current_offset + j] = (orientation * (pt * scale)) + position; } vbuf.Unlock(); vbuf.Dispose(); next_offset += vertex_data.vertexCount; } IndexData index_data = submesh.indexData; uint numTris = index_data.indexCount / 3; HardwareIndexBufferSharedPtr ibuf = index_data.indexBuffer; // UNPORTED line of C++ code (because ibuf.IsNull() doesn't exist in C#) // if( ibuf.isNull() ) continue // need to check if index buffer is valid (which will be not if the mesh doesn't have triangles like a pointcloud) bool use32bitindexes = (ibuf.Type == HardwareIndexBuffer.IndexType.IT_32BIT); uint* pLong = (uint*)ibuf.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY); ushort* pShort = (ushort*)pLong; uint offset = submesh.useSharedVertices ? shared_offset : current_offset; if (use32bitindexes) { for (int k = 0; k < index_data.indexCount; ++k) { indices[index_offset++] = (UInt64)pLong[k] + (UInt64)offset; } } else { for (int k = 0; k < index_data.indexCount; ++k) { indices[index_offset++] = (UInt64)pShort[k] + (UInt64)offset; } } ibuf.Unlock(); ibuf.Dispose(); current_offset = next_offset; } } // GetMeshInformation


Alias: Raycasting_to_the_polygon_level_-_Mogre
Alias: Raycasting_to_the_polygon_level_%28Mogre%29

History

Information Version
Sun 15 of Jul, 2012 12:08 GMT-0000 Beauty added notes about Beautys improved polygon raycasting class 15
Mon 07 of May, 2012 16:10 GMT-0000 edoardo Ops, variable name was "mesh", not "meshPtr" 14
Mon 07 of May, 2012 16:09 GMT-0000 edoardo Added "meshPtr.Dispose();" to avoid memory corruption; also added comments above the previously added vbuff and ibuff Dispose() 13
Tue 03 of Apr, 2012 15:11 GMT-0000 Beauty added alias page name to avoid dead link 12
Wed 14 of Dec, 2011 15:36 GMT-0000 edoardo Updated the code of GetMeshInformation() as stated in this thread: http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=8374 (important changes are the adding of [vbuf|ibuf].Dispose()) 11
Wed 09 of Nov, 2011 13:41 GMT-0000 Beauty improved code comment 10
Thu 27 of Oct, 2011 13:36 GMT-0000 Beauty added "important note" about problems with other render types 9
Mon 24 of Oct, 2011 12:41 GMT-0000 Beauty added note + link for alternative version 8
Mon 24 of Oct, 2011 12:31 GMT-0000 Beauty copied all code comments from the C++ version 7
Fri 21 of Oct, 2011 14:38 GMT-0000 Beauty added note for necessary variable + updated code for Mogre 1.6 and newer 6
Mon 01 of Aug, 2011 00:30 GMT-0000 jacmoe 5
Wed 15 of Dec, 2010 12:47 GMT-0000 pyritie changing the ulong* to a uint* in the GetMeshInformation thing because it stops physx from crashing when you try to load a huge mesh 4
Tue 22 of Dec, 2009 00:37 GMT-0000 jacmoe 3
Tue 22 of Dec, 2009 00:36 GMT-0000 jacmoe 2
Mon 09 of Mar, 2009 21:47 GMT-0000 OgreWikiBot Robot: Cosmetic changes 1