public SoftRigidDynamicsWorld(Dispatcher dispatcher, BroadphaseInterface pairCache,
			ConstraintSolver constraintSolver, CollisionConfiguration collisionConfiguration,
			SoftBodySolver softBodySolver)
            : base(IntPtr.Zero)
		{
            if (softBodySolver != null) {
                _softBodySolver = softBodySolver;
                _ownsSolver = false;
            } else {
                _softBodySolver = new DefaultSoftBodySolver();
                _ownsSolver = true;
            }

            _native = btSoftRigidDynamicsWorld_new2(dispatcher._native, pairCache._native,
                (constraintSolver != null) ? constraintSolver._native : IntPtr.Zero,
                collisionConfiguration._native, _softBodySolver._native);

            _collisionObjectArray = new AlignedCollisionObjectArray(btCollisionWorld_getCollisionObjectArray(_native), this);

            _broadphase = pairCache;
			_constraintSolver = constraintSolver;
			_dispatcher = dispatcher;
            _worldInfo = new SoftBodyWorldInfo(btSoftRigidDynamicsWorld_getWorldInfo(_native), true);
            _worldInfo.Dispatcher = dispatcher;
            _worldInfo.Broadphase = pairCache;
		}
        public SoftRigidDynamicsWorld(Dispatcher dispatcher, BroadphaseInterface pairCache, ConstraintSolver constraintSolver, CollisionConfiguration collisionConfiguration, SoftBodySolver softBodySolver)
            : base(IntPtr.Zero)
        {
            if (softBodySolver != null)
            {
                _softBodySolver = softBodySolver;
                _ownsSolver     = false;
            }
            else
            {
                _softBodySolver = new DefaultSoftBodySolver();
                _ownsSolver     = true;
            }

            _native = btSoftRigidDynamicsWorld_new2(dispatcher._native, pairCache._native,
                                                    (constraintSolver != null) ? constraintSolver._native : IntPtr.Zero,
                                                    collisionConfiguration._native, _softBodySolver._native);

            _collisionObjectArray = new AlignedCollisionObjectArray(btCollisionWorld_getCollisionObjectArray(_native), this);

            _dispatcher           = dispatcher;
            _broadphase           = pairCache;
            _constraintSolver     = constraintSolver;
            _worldInfo            = new SoftBodyWorldInfo(btSoftRigidDynamicsWorld_getWorldInfo(_native), true);
            _worldInfo.Dispatcher = dispatcher;
            _worldInfo.Broadphase = pairCache;
            _native2ManagedMap.Add(_native, this);
        }
Example #3
0
        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }

            if (World != null)
            {
                AlignedCollisionObjectArray objectArray = World.CollisionObjectArray;
                while (objectArray.Count != 0)
                {
                    int             objectIndex     = objectArray.Count - 1;
                    CollisionObject collisionObject = objectArray[objectIndex];
                    objectArray.RemoveAt(objectIndex);
                    collisionObject.Dispose();
                }

                World.Dispose();
                _dispatcher.Dispose();
                Broadphase.Dispose();
                _conf.Dispose();
            }

            _isDisposed = true;
        }
Example #4
0
        public bool StepSimulation(float timeStep)
        {
            simTimeStep += timeStep;

            if (simThread != null)
            {
                return(false);
            }

            simThread = new Thread(new ParameterizedThreadStart(delegate(object t)
            {
                float s = (float)t;
                if (s > 1.0f / 60)
                {
                    s = 1.0f / 60;
                }

                bulletWorld.StepSimulation(s);

                IrrlichtLime.Core.Matrix m = new IrrlichtLime.Core.Matrix();
                AlignedCollisionObjectArray collObjects = bulletWorld.CollisionObjectArray;
                for (int i = collObjects.Count - 1; i >= 0; i--)
                {
                    CollisionObject collObject = collObjects[i];
                    if (collObject.IsStaticObject || !collObject.IsActive)
                    {
                        continue;
                    }

                    m.SetElementArray(collObject.WorldTransform.ToArray());

                    SceneNode n = collObject.UserObject as SceneNode;
                    n.Position  = m.Translation;
                    n.Rotation  = m.Rotation;

                    if (m.Translation.Y < -40000)
                    {
                        n.SceneManager.AddToDeletionQueue(n);
                        bulletWorld.RemoveCollisionObject(collObject);
                        collObject.Dispose();
                    }
                }

                simThread = null;
            }));

            simThread.Start(simTimeStep);
            simTimeStep = 0;

            return(true);
        }
        /// <summary>
        /// gRally
        /// </summary>
        /// <param name="worldCreated"></param>
        /// <param name="dispatcher"></param>
        /// <param name="pairCache"></param>
        /// <param name="constraintSolver"></param>
        public SoftRigidDynamicsWorld(IntPtr worldCreated, Dispatcher dispatcher, BroadphaseInterface pairCache, ConstraintSolver constraintSolver)
            : base(IntPtr.Zero)
        {
            _softBodySolver = new DefaultSoftBodySolver();
            _ownsSolver     = true;
            _native         = worldCreated;

            _collisionObjectArray = new AlignedCollisionObjectArray(btCollisionWorld_getCollisionObjectArray(_native), this);

            _dispatcher           = dispatcher;
            _broadphase           = pairCache;
            _constraintSolver     = constraintSolver;
            _worldInfo            = new SoftBodyWorldInfo(btSoftRigidDynamicsWorld_getWorldInfo(_native), true);
            _worldInfo.Dispatcher = dispatcher;
            _worldInfo.Broadphase = pairCache;
            _native2ManagedMap.Add(_native, this);
        }
Example #6
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]);
                }
            }
        }
        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);

                DataBox db = device.ImmediateContext.MapSubresource(s.InstanceDataBuffer, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None);
                SharpDX.Utilities.Write(db.DataPointer, instanceArray, 0, instanceArray.Length);
                device.ImmediateContext.UnmapSubresource(s.InstanceDataBuffer, 0);
            }

            if (removeList.Count != 0)
            {
                for (i = removeList.Count - 1; i >= 0; i--)
                {
                    shapes.Remove(removeList[i]);
                }
                removeList.Clear();
            }
        }
Example #8
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 #9
0
        public void InitInstancedRender()
        {
            // Clear instance data
            foreach (ShapeData s in shapes.Values)
            {
                s.Instances.Clear();
            }

            // Gather instance data
            AlignedCollisionObjectArray objects = demo.PhysicsContext.World.CollisionObjectArray;
            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;
                var       instanceArray = s.InstanceArray;

                // Is the instance buffer the right size?
                if (instanceArray.Length != instanceCount)
                {
                    // 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);

                    instanceArray   = new InstanceData[instanceCount];
                    s.InstanceArray = instanceArray;
                }

                // Copy the instance list over to the instance array
                s.Instances.CopyTo(instanceArray);

                DataBox db = device.ImmediateContext.MapSubresource(s.InstanceDataBuffer, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None);
                SharpDX.Utilities.Write(db.DataPointer, instanceArray, 0, instanceArray.Length);
                device.ImmediateContext.UnmapSubresource(s.InstanceDataBuffer, 0);
            }

            // Remove shapes that had no instances
            if (removeList.Count != 0)
            {
                foreach (var shape in removeList)
                {
                    shapes.Remove(shape);
                }
                removeList.Clear();
            }
        }
Example #10
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];

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

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

                if (s.InstanceDataList.Count == 0)
                {
                    removeList.Add(sh.Key);
                }

                /*
                 * // Is the instance buffer the right size?
                 * if (s.InstanceDataBuffer.Description.SizeInBytes != s.InstanceDataList.Count * InstanceData.SizeInBytes)
                 * {
                 *  // No, recreate it
                 *  s.InstanceDataBuffer.Dispose();
                 *
                 *  if (s.InstanceDataList.Count == 0)
                 *  {
                 *      if (s.IndexBuffer != null)
                 *          s.IndexBuffer.Dispose();
                 *      s.VertexBuffer.Dispose();
                 *      removeList.Add(sh.Key);
                 *      continue;
                 *  }
                 *
                 *  instanceDataDesc.SizeInBytes = s.InstanceDataList.Count * 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
                 * using (var data = s.InstanceDataBuffer.Map(MapMode.WriteDiscard))
                 * {
                 *  data.WriteRange(s.InstanceDataList.ToArray());
                 *  s.InstanceDataBuffer.Unmap();
                 * }
                 */
            }

            if (removeList.Count != 0)
            {
                for (i = removeList.Count - 1; i >= 0; i--)
                {
                    shapes.Remove(removeList[i]);
                }
                removeList.Clear();
            }
        }
		internal CollisionWorld(IntPtr native)
		{
            if (native == IntPtr.Zero)
            {
                return;
            }
			_native = native;
            _collisionObjectArray = new AlignedCollisionObjectArray(btCollisionWorld_getCollisionObjectArray(native), this);
		}
Example #12
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];

                BulletSharp.Matrix transform;
                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;

                if (s.InstanceDataList.Count == 0)
                {
                    removeList.Add(sh.Key);
                }

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

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

                    instanceDataDesc.SizeInBytes = s.InstanceDataList.Count * 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
                using (var data = s.InstanceDataBuffer.Map(MapMode.WriteDiscard))
                {
                    data.WriteRange(s.InstanceDataList.ToArray());
                    s.InstanceDataBuffer.Unmap();
                }
                */
            }

            if (removeList.Count != 0)
            {
                for (i = removeList.Count - 1; i >= 0; i--)
                {
                    shapes.Remove(removeList[i]);
                }
            }
        }
        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);

                DataBox db = device.ImmediateContext.MapSubresource(s.InstanceDataBuffer, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None);
                SharpDX.Utilities.Write(db.DataPointer, instanceArray, 0, instanceArray.Length);
                device.ImmediateContext.UnmapSubresource(s.InstanceDataBuffer, 0);
            }

            if (removeList.Count != 0)
            {
                for (i = removeList.Count - 1; i >= 0; i--)
                {
                    shapes.Remove(removeList[i]);
                }
                removeList.Clear();
            }
        }
Example #14
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;

                if (s.Instances.Count == 0)
                {
                    removeList.Add(sh.Key);
                }

                /*
                 * // 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 data over to the instance buffer
                 * using (var data = s.InstanceDataBuffer.Map(MapMode.WriteDiscard))
                 * {
                 *  data.WriteRange(s.Instances.ToArray());
                 *  s.InstanceDataBuffer.Unmap();
                 * }
                 */
            }

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

            AlignedCollisionObjectArray objects = demo.PhysicsContext.World.CollisionObjectArray;
            int i = objects.Count - 1;

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

                Matrix transform;
                if (colObj is SoftBody)
                {
                    transform = Matrix.Identity;
                }
                else
                {
                    ((colObj as RigidBody).MotionState as DefaultMotionState).GetWorldTransform(out transform);
                }
                InitInstanceData(colObj, colObj.CollisionShape, ref transform);
            }

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

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

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

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

                // Copy the instance data over to the instance buffer
                using (var data = s.InstanceDataBuffer.Map(MapMode.WriteDiscard))
                {
                    data.WriteRange(s.InstanceDataList.ToArray());
                    s.InstanceDataBuffer.Unmap();
                }
            }

            if (removeList.Count != 0)
            {
                for (i = removeList.Count - 1; i >= 0; i--)
                {
                    shapes.Remove(removeList[i]);
                }
            }
        }
Example #16
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]);
                }
            }
        }