Raycasting to the polygon level wiki: horrible efficiency?

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
Syniurge
Halfling
Posts: 40
Joined: Thu Aug 30, 2012 1:43 pm
Location: France
x 3

Raycasting to the polygon level wiki: horrible efficiency?

Post by Syniurge »

Code: Select all

[url]http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Raycasting+to+the+polygon+level[/url]
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? :shock:

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 :lol:
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Raycasting to the polygon level wiki: horrible efficienc

Post by Kojack »

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.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5299
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1280
Contact:

Re: Raycasting to the polygon level wiki: horrible efficienc

Post by dark_sylinc »

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.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Raycasting to the polygon level wiki: horrible efficienc

Post by Kojack »

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.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5299
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1280
Contact:

Re: Raycasting to the polygon level wiki: horrible efficienc

Post by dark_sylinc »

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).

There's no definitive answer, really.
Syniurge
Halfling
Posts: 40
Joined: Thu Aug 30, 2012 1:43 pm
Location: France
x 3

Re: Raycasting to the polygon level wiki: horrible efficienc

Post by Syniurge »

Doesn't Ogre keep a copy of vertex and index buffers in system memory by default?
http://www.ogre3d.org/docs/api/1.9/clas ... 2678284275

MeshPtr Ogre::MeshManager::load(...,
bool vertexBufferShadowed = true,
bool indexBufferShadowed = true
)

(...)

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)?
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5299
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1280
Contact:

Re: Raycasting to the polygon level wiki: horrible efficienc

Post by dark_sylinc »

Paste it somewhere for small review and then we'll see if it's ok.
Post Reply