コード例 #1
0
 public PhysicsObject AddDynamicObject(float mass, Matrix4x4 transform, Collider col, Vector3?inertia = null)
 {
     if (mass < float.Epsilon)
     {
         throw new Exception("Mass must be non-zero");
     }
     BM.Vector3 localInertia;
     if (inertia != null)
     {
         localInertia = inertia.Value.Cast();
     }
     else
     {
         col.BtShape.CalculateLocalInertia(mass, out localInertia);
     }
     using (var rbInfo = new RigidBodyConstructionInfo(mass,
                                                       new DefaultMotionState(transform.Cast()),
                                                       col.BtShape, localInertia))
     {
         var body = new RigidBody(rbInfo);
         body.SetDamping(0, 0);
         body.Restitution = 1f;
         var phys = new PhysicsObject(body, col)
         {
             Static = false
         };
         phys.UpdateProperties();
         body.UserObject = phys;
         btWorld.AddRigidBody(body);
         objects.Add(phys);
         dynamicObjects.Add(phys);
         return(phys);
     }
 }
コード例 #2
0
 public IEnumerable <BoundingBox> GetBoxes(Matrix4x4 transform)
 {
     foreach (var shape in btCompound.ChildList)
     {
         BM.Vector3 min, max;
         shape.ChildShape.GetAabb(shape.Transform * transform.Cast(), out min, out max);
         yield return(new BoundingBox(min.Cast(), max.Cast()));
     }
 }
        public override void Render(Mesh mesh, Camera camera, Matrix4x4[] matrices)
        {
            Bind();
            VertexArray.Bind();

            PositionBuffer.Bind();
            PositionBuffer.SetData(mesh.Attributes["POSITION"].BufferData);

            NormalBuffer.Bind();
            NormalBuffer.SetData(mesh.Attributes["NORMAL"].BufferData);

            ColorBuffer.Bind();
            ColorBuffer.SetData(mesh.Attributes["COLOR_0"].BufferData);

            MatrixBuffer.Bind();
            MatrixBuffer.SetData(matrices);

            Vector3 lightInvDir = new Vector3(0.5f, 2, 2);

            // Compute the MVP matrix from the light's point of view
            Matrix4x4 depthProjectionMatrix = Matrix4x4.CreateOrthographicOffCenter(-10, 10, -10, 10, -10, 20);
            Matrix4x4 depthViewMatrix       = Matrix4x4.CreateLookAt(lightInvDir, new Vector3(0, 0, 0), Vector3.UnitY);
            Matrix4x4 depthModelMatrix      = Matrix4x4.Identity;;
            Matrix4x4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;

            var projectionCast         = camera.GetComponent <CameraComponent>().Projection().Cast();
            var projectionViewLocation = GL.GetUniformLocation(ProgramId, "uViewProjection");

            GL.ProgramUniformMatrix4(ProgramId, projectionViewLocation, false, ref projectionCast);

            var depthProjectionCast         = depthMVP.Cast();
            var depthProjectionViewLocation = GL.GetUniformLocation(ProgramId, "uDepthMVP");

            GL.ProgramUniformMatrix4(ProgramId, depthProjectionViewLocation, false, ref depthProjectionCast);

            Matrix4x4 biasMatrix = new Matrix4x4(
                0.5f, 0.0f, 0.0f, 0.0f,
                0.0f, 0.5f, 0.0f, 0.0f,
                0.0f, 0.0f, 0.5f, 0.0f,
                0.5f, 0.5f, 0.5f, 1.0f
                );
            Matrix4x4 depthBiasMVP = biasMatrix * depthMVP;

            var depthBiasCast     = depthBiasMVP.Cast();
            var depthBiasLocation = GL.GetUniformLocation(ProgramId, "uDepthBias");

            GL.ProgramUniformMatrix4(ProgramId, depthBiasLocation, false, ref depthBiasCast);

            var colorLocation = GL.GetUniformLocation(ProgramId, "uColor");
            var colorCast     = Color.CastRendering();

            GL.ProgramUniform3(ProgramId, colorLocation, ref colorCast);

            GL.DrawElementsInstancedBaseInstance(PrimitiveType.Triangles, mesh.Attributes["INDEX"].BufferData.Length / 2, DrawElementsType.UnsignedShort, mesh.Attributes["INDEX"].BufferData, matrices.Length, 0);
        }
コード例 #4
0
 public PhysicsObject AddStaticObject(Matrix4x4 transform, Collider col)
 {
     using (var rbInfo = new RigidBodyConstructionInfo(0,
                                                       new DefaultMotionState(transform.Cast()),
                                                       col.BtShape, BM.Vector3.Zero)) {
         var body = new RigidBody(rbInfo);
         body.Restitution = 1;
         var phys = new PhysicsObject(body, col)
         {
             Static = true
         };
         phys.UpdateProperties();
         body.UserObject = phys;
         btWorld.AddRigidBody(body);
         objects.Add(phys);
         return(phys);
     }
 }
コード例 #5
0
        public void UpdatePart(object tag, Matrix4x4 localTransform)
        {
            var tr = localTransform.Cast();

            foreach (var part in children)
            {
                if (part.Tag == tag)
                {
                    if (part.CurrentTransform == localTransform)
                    {
                        return;
                    }
                    part.CurrentTransform = localTransform;
                    for (int i = part.Index; i < (part.Index + part.Count); i++)
                    {
                        btCompound.UpdateChildTransform(i, tr, false);
                    }
                    break;
                }
            }
        }
コード例 #6
0
        public void AddPart(uint meshId, Matrix4x4 localTransform, object tag, int suridx = 0)
        {
            var sur = surs[suridx];

            if (!sur.HasShape(meshId))
            {
                return;
            }
            var hulls = sur.GetShape(meshId);
            var pt    = new CollisionPart()
            {
                Tag = tag, Index = currentIndex, Count = hulls.Length
            };
            var tr = localTransform.Cast();

            foreach (var h in hulls)
            {
                btCompound.AddChildShape(tr, h);
            }
            currentIndex += hulls.Length;
            children.Add(pt);
        }
コード例 #7
0
 public void SetTransform(Matrix4x4 transform)
 {
     Transform = transform;
     RigidBody.WorldTransform = transform.Cast();
     Position = Vector3.Transform(Vector3.Zero, Transform);
 }