Example #1
0
        public void InitInstancedRender(AlignedCollisionObjectArray objects)
        {
            // Clear instance data
            foreach (ShapeData s in shapes.Values)
                s.InstanceDataList.Clear();
            removeList.Clear();

            int i = objects.Count - 1;
            for (; i >= 0; i--)
            {
                CollisionObject colObj = objects[i];

                Matrix transform;
                if (colObj is RigidBody)
                {
                    DefaultMotionState motionState = (colObj as RigidBody).MotionState as DefaultMotionState;
                    if (motionState != null)
                    {
                        // FIXME: doesn't work with ConvexHullShape?
                        transform = motionState.GraphicsWorldTrans;
                        //colObj.GetWorldTransform(out transform);
                    }
                    else
                    {
                        colObj.GetWorldTransform(out transform);
                    }
                }
                else if (colObj is SoftBody)
                {
                    if (demo.IsDebugDrawEnabled)
                        continue;
                    transform = BulletSharp.Matrix.Identity;
                }
                else
                {
                    colObj.GetWorldTransform(out transform);
                }
                InitInstanceData(colObj, colObj.CollisionShape, ref transform);
            }

            foreach (KeyValuePair<CollisionShape, ShapeData> sh in shapes)
            {
                ShapeData s = sh.Value;
                int instanceCount = s.InstanceDataList.Count;

                // Is the instance buffer the right size?
                if (s.InstanceDataBuffer.Description.SizeInBytes != instanceCount * InstanceData.SizeInBytes)
                {
                    // No, recreate it
                    s.InstanceDataBuffer.Dispose();

                    if (instanceCount == 0)
                    {
                        if (s.IndexBuffer != null)
                            s.IndexBuffer.Dispose();
                        s.VertexBuffer.Dispose();
                        removeList.Add(sh.Key);
                        continue;
                    }

                    instanceDataDesc.SizeInBytes = instanceCount * InstanceData.SizeInBytes;
                    s.InstanceDataBuffer = new Buffer(device, instanceDataDesc);
                    s.BufferBindings[1] = new VertexBufferBinding(s.InstanceDataBuffer, InstanceData.SizeInBytes, 0);
                }

                // Copy the instance data over to the instance buffer
                InstanceData[] instanceArray = s.InstanceDataListArray;
                if (instanceArray.Length != instanceCount)
                {
                    instanceArray = new InstanceData[instanceCount];
                    s.InstanceDataListArray = instanceArray;
                }
                s.InstanceDataList.CopyTo(instanceArray);

                using (var data = s.InstanceDataBuffer.Map(MapMode.WriteDiscard, global::SharpDX.Direct3D10.MapFlags.None))
                {
                    data.WriteRange(instanceArray);
                    s.InstanceDataBuffer.Unmap();
                }
            }

            if (removeList.Count != 0)
            {
                for (i = removeList.Count - 1; i >= 0; i--)
                {
                    shapes.Remove(removeList[i]);
                }
            }
        }
Example #2
0
        public void InitInstancedRender(AlignedCollisionObjectArray objects)
        {
            // Clear instance data
            foreach (ShapeData s in shapes.Values)
            {
                s.InstanceDataList.Clear();
            }

            int i = objects.Count - 1;

            for (; i >= 0; i--)
            {
                CollisionObject colObj = objects[i];

                Matrix transform;
                if (colObj is SoftBody)
                {
                    if (demo.IsDebugDrawEnabled)
                    {
                        continue;
                    }
                    transform = Matrix.Identity;
                }
                else
                {
                    colObj.GetWorldTransform(out transform);
                }
                InitInstanceData(colObj, colObj.CollisionShape, ref transform);
            }

            foreach (KeyValuePair <CollisionShape, ShapeData> sh in shapes)
            {
                ShapeData s             = sh.Value;
                int       instanceCount = s.InstanceDataList.Count;

                // Is the instance buffer the right size?
                if (s.InstanceDataBuffer.Description.SizeInBytes != instanceCount * InstanceData.SizeInBytes)
                {
                    // No, recreate it
                    s.InstanceDataBuffer.Dispose();

                    if (instanceCount == 0)
                    {
                        if (s.IndexBuffer != null)
                        {
                            s.IndexBuffer.Dispose();
                        }
                        s.VertexBuffer.Dispose();
                        removeList.Add(sh.Key);
                        continue;
                    }

                    instanceDataDesc.SizeInBytes = instanceCount * InstanceData.SizeInBytes;
                    s.InstanceDataBuffer         = new Buffer(device, instanceDataDesc);
                    s.BufferBindings[1]          = new VertexBufferBinding(s.InstanceDataBuffer, InstanceData.SizeInBytes, 0);
                }

                // Copy the instance data over to the instance buffer
                InstanceData[] instanceArray = s.InstanceDataListArray;
                if (instanceArray.Length != instanceCount)
                {
                    instanceArray           = new InstanceData[instanceCount];
                    s.InstanceDataListArray = instanceArray;
                }
                s.InstanceDataList.CopyTo(instanceArray);

                using (var data = s.InstanceDataBuffer.Map(MapMode.WriteDiscard, global::SharpDX.Direct3D10.MapFlags.None))
                {
                    data.WriteRange(instanceArray);
                    s.InstanceDataBuffer.Unmap();
                }
            }

            if (removeList.Count != 0)
            {
                for (i = removeList.Count - 1; i >= 0; i--)
                {
                    shapes.Remove(removeList[i]);
                }
                removeList.Clear();
            }
        }
Example #3
0
        public void InitInstancedRender(AlignedCollisionObjectArray objects)
        {
            // Clear instance data
            foreach (ShapeData s in shapes.Values)
                s.Instances.Clear();

            // Gather instance data
            int i = objects.Count - 1;
            for (; i >= 0; i--)
            {
                var colObj = objects[i];
                var shape = colObj.CollisionShape;

                if (shape.ShapeType == BroadphaseNativeType.SoftBodyShape)
                {
                    if (demo.IsDebugDrawEnabled)
                        continue;
                    InitSoftBodyInstance(colObj as SoftBody, shape);
                }
                else
                {
                    Matrix transform;
                    colObj.GetWorldTransform(out transform);
                    InitRigidBodyInstance(colObj, shape, ref transform);
                }
            }

            foreach (KeyValuePair<CollisionShape, ShapeData> sh in shapes)
            {
                ShapeData s = sh.Value;
                int instanceCount = s.Instances.Count;

                // Is the instance buffer the right size?
                if (s.InstanceDataBuffer.Description.SizeInBytes != instanceCount * InstanceData.SizeInBytes)
                {
                    // No, recreate it
                    s.InstanceDataBuffer.Dispose();

                    // Remember shapes that have no instances,
                    // shape is removed after iteration over shapes
                    if (instanceCount == 0)
                    {
                        if (s.IndexBuffer != null)
                            s.IndexBuffer.Dispose();
                        s.VertexBuffer.Dispose();
                        removeList.Add(sh.Key);
                        continue;
                    }

                    instanceDataDesc.SizeInBytes = instanceCount * InstanceData.SizeInBytes;
                    s.InstanceDataBuffer = new Buffer(device, instanceDataDesc);
                    s.BufferBindings[1] = new VertexBufferBinding(s.InstanceDataBuffer, InstanceData.SizeInBytes, 0);
                }

                // Copy the instance list over to the instance array
                InstanceData[] instanceArray = s.InstanceArray;
                if (instanceArray.Length != instanceCount)
                {
                    instanceArray = new InstanceData[instanceCount];
                    s.InstanceArray = instanceArray;
                }
                s.Instances.CopyTo(instanceArray);

                using (var data = s.InstanceDataBuffer.Map(MapMode.WriteDiscard, global::SharpDX.Direct3D10.MapFlags.None))
                {
                    data.WriteRange(instanceArray);
                    s.InstanceDataBuffer.Unmap();
                }
            }

            // Remove shapes that had no instances
            if (removeList.Count != 0)
            {
                foreach (var shape in removeList)
                {
                    shapes.Remove(shape);
                }
                removeList.Clear();
            }
        }
Example #4
0
        public void InitInstancedRender(AlignedCollisionObjectArray objects)
        {
            // Clear instance data
            foreach (ShapeData s in shapes.Values)
            {
                s.Instances.Clear();
            }

            // Gather instance data
            int i = objects.Count - 1;

            for (; i >= 0; i--)
            {
                var colObj = objects[i];
                var shape  = colObj.CollisionShape;

                if (shape.ShapeType == BroadphaseNativeType.SoftBodyShape)
                {
                    if (demo.IsDebugDrawEnabled)
                    {
                        continue;
                    }
                    InitSoftBodyInstance(colObj as SoftBody, shape);
                }
                else
                {
                    Matrix transform;
                    colObj.GetWorldTransform(out transform);
                    InitRigidBodyInstance(colObj, shape, ref transform);
                }
            }

            foreach (KeyValuePair <CollisionShape, ShapeData> sh in shapes)
            {
                ShapeData s             = sh.Value;
                int       instanceCount = s.Instances.Count;

                // Is the instance buffer the right size?
                if (s.InstanceDataBuffer.Description.SizeInBytes != instanceCount * InstanceData.SizeInBytes)
                {
                    // No, recreate it
                    s.InstanceDataBuffer.Dispose();

                    // Remember shapes that have no instances,
                    // shape is removed after iteration over shapes
                    if (instanceCount == 0)
                    {
                        if (s.IndexBuffer != null)
                        {
                            s.IndexBuffer.Dispose();
                        }
                        s.VertexBuffer.Dispose();
                        removeList.Add(sh.Key);
                        continue;
                    }

                    instanceDataDesc.SizeInBytes = instanceCount * InstanceData.SizeInBytes;
                    s.InstanceDataBuffer         = new Buffer(device, instanceDataDesc);
                    s.BufferBindings[1]          = new VertexBufferBinding(s.InstanceDataBuffer, InstanceData.SizeInBytes, 0);
                }

                // Copy the instance list over to the instance array
                InstanceData[] instanceArray = s.InstanceArray;
                if (instanceArray.Length != instanceCount)
                {
                    instanceArray   = new InstanceData[instanceCount];
                    s.InstanceArray = instanceArray;
                }
                s.Instances.CopyTo(instanceArray);

                using (var data = s.InstanceDataBuffer.Map(MapMode.WriteDiscard, global::SharpDX.Direct3D10.MapFlags.None))
                {
                    data.WriteRange(instanceArray);
                    s.InstanceDataBuffer.Unmap();
                }
            }

            // Remove shapes that had no instances
            if (removeList.Count != 0)
            {
                foreach (var shape in removeList)
                {
                    shapes.Remove(shape);
                }
                removeList.Clear();
            }
        }