Why are every snippets of code in that page making a copy of the mesh vertices and applying the world transformation onto them then checking if the ray intersects with the copies of the faces?
Wouldn't it be incomparably faster to just apply the inverse world transformation onto the ray and check if the ray intersects with the faces without making any copy (just locking the vertex and index buffers in read-only)?
I'm asking in case I'm missing something before.. fixing that page which has been around for ages
The code for getting the vertex data wasn't (iirc) originally made for real time raycasting. It was for extracting mesh data to give to a physics engine to create mesh collision objects. Adding meshes to the physics scene as statics (most physics engines don't like triangle soup meshes that can move around) is easiest if you bake in their position/orientation/scale.
That code was then used for the raycasting example, but it's really not efficient for it.
- transform each vertex rather than the ray
- allocate and delete two buffers every raycast
- instead of extracting the mesh data to a memory buffer every time, just do the ray intersect directly on the data (so no copy step).
So the GetMeshInformation function itself is fine, it's just not suited to this particular usage.
I had to use this snippet a couple weeks ago, and wondered the same thing.
My best guess is that position may be in a multisource vertex buffer, with a complex vertex buffer format, so the copy simplifies the process to just get a contigous chunk of position data for the raycasting code to use.
True, the buffers could be complex, but the code still has to iterate through them to extract the position data, so it should just do the raycast within that iteration code rather than as a second step.
The wiki presents the code in a relatively separate way and educationally.
There's no way to optimize without knowing its usage. If the snippet is going to be used for educational use, it's perfect. If you need to raycast in real time every frame with many objects, on NUMA devices you should cache the vertex position data on CPU once then provide those buffers on every call (memcpy once, keep the buffers externally).
If it's a UMA architecture, the best approach is probably reading directly from GPU (but without the memcpy).
vertexBufferShadowed If true, the vertex buffers will be shadowed by system memory copies for faster read access indexBufferShadowed If true, the index buffers will be shadowed by system memory copies for faster read access
But you're right, the first snippet is very straightforward thanks to GetMeshInformation().
Would it be ok to replace the adapted versions 1&2 and the ManualObject version by my optimized version (covering ManualObjects too)?