cube fracture function optimization

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
oringe
Posts: 41
Joined: Tue Jul 05, 2011 7:06 am
Location: San Francisco

cube fracture function optimization

Post by oringe »

So I've implemented a fracture function that simply sub-divides a cube into smaller cubes. Nothing too complex. The function takes (int s) parameter that is the side dimension of the number of cubes, thus s^3 would be the total number of cubes created.
I'm using Bullet Physics, and have implemented a custom MotionState that updates the Irrlicht nodes position and rotation to that of the rigid body.
My first iteration of the function simply created new Irr nodes for each sub-cube. Obviously that sucked up a lot of fps and was not ideal.
So my question is two parts,
1. What would be the best way to optimize the rendering of many cubes that are each animated by their own Bullet rigid body?
I'm working on a possible solution; that is to add each sub-cube manually to the mesh buffers. One per buffer. Then when the rigid body calls its MotionState, update the vertices of that buffer manually.
I've got a working MotionState that updates the position of the vertices, but
2. Is there a simple way to apply transforms to a MeshBuffer?
Currently I am updating the positions manually, I wonder if there is a better way to apply Bullet's btTransform to the mesh buffer?

Relevant code:

Code: Select all

class myMotionState : public btDefaultMotionState
    {
        myCube* cube;
        ...
        void setWorldTransform (const btTransform& centerOfMassWorldTrans)
        {
            if (cube->isFractured)
            {
                SMeshBuffer* buf = (SMeshBuffer*) m_cube->node->getMesh()->getMeshBuffer(0);
                btVector3 euler;
                k.QuaternionToEuler (centerOfMassWorldTrans.getRotation(), euler);
                vector3df rot = vector3df (euler[0], euler[1], euler[2]);
                matrix4 m; 
                m.setRotationDegrees (rot);
                for (u16 i=0; i<buf->Vertices.size(); i++)
                {
                    vector3df rotPos = m_cube->origVertices[i].Pos;
                    m.rotateVect (rotPos);
                    buf->Vertices[i].Pos = rotPos + newPos;
                }
                buf->recalculateBoundingBox();
            }
        }
"Logical groups of an IMesh need not be put into separate mesh buffers, but can be. Separately animated parts of the mesh must be put into separate mesh buffers. Some mesh buffer implementations have limitations on the number of vertices the buffer can hold. In that case, logical grouping can help. Moreover, the number of vertices should be optimized for the GPU upload, which often depends on the type of gfx card. Typial figures are 1000-10000 vertices per buffer."

Image
Last edited by oringe on Thu Dec 06, 2012 11:53 pm, edited 1 time in total.
codetiger
Posts: 103
Joined: Wed May 02, 2012 9:24 am
Location: Chennai, India
Contact:

Re: cube fracture function optimization

Post by codetiger »

The best way to render many (more than 200) mesh with low number of vertex attributes (less than 100), is to use instancing shaders. We tried using pseudo instancing in our program and could see good performance gain. You can also try other instancing methods.

Short desc on Instancing: Geometry Instancing allows you to reduce number of draw calls and amount of data transfer to GPU by sending mesh buffer once to GPU and drawing the same mesh many times.
IrrNaCl - Irrlicht Port for Google Chrome Native Client - Demo

Iyan 3D - Make your own 3d animation using your iOS Device
oringe
Posts: 41
Joined: Tue Jul 05, 2011 7:06 am
Location: San Francisco

xxx

Post by oringe »

xxx
Last edited by oringe on Thu Dec 06, 2012 11:54 pm, edited 1 time in total.
oringe
Posts: 41
Joined: Tue Jul 05, 2011 7:06 am
Location: San Francisco

Re: cube fracture function optimization

Post by oringe »

Thanks codetiger!
That's exactly the kind of advice I was looking for. Will definitely look into that : )
codetiger
Posts: 103
Joined: Wed May 02, 2012 9:24 am
Location: Chennai, India
Contact:

Re: cube fracture function optimization

Post by codetiger »

Your solutions is the straight forward way and called Batch rendering usually used for static objects. Howevery, it is CPU heavy and cannot be used in all cases. Thats why shader instancing has evolved. Shader instancing pushes the matrix multiplication part to the GPU.
IrrNaCl - Irrlicht Port for Google Chrome Native Client - Demo

Iyan 3D - Make your own 3d animation using your iOS Device
Post Reply