Example #1
0
 public override void UpdateWorldBoundingBox()
 {
     if (worldBoundingBox != null)
         if (mesh != null)
             worldBoundingBox = mesh.CreateBoundingBox(GetWorldMatrix());
 }
        private Shape CreateShape()
        {
            Shape shape = null;
            boundingBox = new BoundingBox();
            List<Geometry> geom = RenderUtil.GatherGeometry(GameObject);
            List<Mesh> meshes = new List<Mesh>();
            List<Matrix4f> matrices = new List<Matrix4f>();
            Vector3f mytrans = GameObject.GetUpdatedWorldTranslation();
            if (GameObject is Geometry)
            {
                Geometry g_obj = (Geometry)GameObject;

                Mesh m = g_obj.Mesh;
                meshes.Add(m);
                Transform ttransform = new Transform();
                ttransform.SetTranslation(g_obj.GetUpdatedWorldTranslation());
                ttransform.SetRotation(g_obj.GetUpdatedWorldRotation());
                ttransform.SetScale(g_obj.GetUpdatedWorldScale());
                Matrix4f matrix = ttransform.GetMatrix();
                BoundingBox tmpBB = m.CreateBoundingBox();
                matrices.Add(matrix);
                boundingBox.Extend(tmpBB);
            }
            for (int i = 0; i < geom.Count; i++)
            {
                Geometry g = geom[i];
                if (g != GameObject)
                {
                    Mesh m = g.Mesh;
                    meshes.Add(m);
                    Transform ttransform = new Transform();
                    ttransform.SetTranslation(g.GetUpdatedWorldTranslation().Subtract(mytrans));
                    ttransform.SetRotation(g.GetUpdatedWorldRotation());
                    ttransform.SetScale(g.GetUpdatedWorldScale());
                    Matrix4f matrix = ttransform.GetMatrix();
                    BoundingBox tmpBB = m.CreateBoundingBox(matrix);
                    matrices.Add(matrix);
                    boundingBox.Extend(tmpBB);
                }
            }
            if (physicsShape == PhysicsWorld.PhysicsShape.StaticMesh)
            {
                List<JVector> jvec = new List<JVector>();
                List<TriangleVertexIndices> tv = new List<TriangleVertexIndices>();
                List<Vector3f> vertexPositions = new List<Vector3f>();

                for (int m = 0; m < meshes.Count; m++)
                {
                    for (int v = 0; v < meshes[m].vertices.Count; v++)
                    {
                        Vector3f myvec = meshes[m].vertices[v].GetPosition().Multiply(matrices[m]);
                        jvec.Add(new JVector(myvec.x, myvec.y, myvec.z));
                    }
                    for (int i = 0; i < meshes[m].indices.Count; i += 3)
                    {
                        tv.Add(new TriangleVertexIndices(meshes[m].indices[i + 2], meshes[m].indices[i + 1], meshes[m].indices[i]));
                    }
                }
                Octree oct = new Octree(jvec, tv);
                TriangleMeshShape trimesh = new TriangleMeshShape(oct);

                shape = trimesh;
                oct = null;
                jvec.Clear();
                tv.Clear();
            }
            else if (physicsShape == PhysicsWorld.PhysicsShape.ConvexMesh)
            {
                List<JVector> jvec = new List<JVector>();
                List<Vector3f> vertexPositions = new List<Vector3f>();

                for (int m = 0; m < meshes.Count; m++)
                {
                    for (int v = 0; v < meshes[m].indices.Count; v++)
                    {
                        Vector3f vec = meshes[m].vertices[meshes[m].indices[v]].GetPosition().Multiply(matrices[m]);
                        jvec.Add(new JVector(vec.x, vec.y, vec.z));
                    }
                }

                ConvexHullShape hullShape = new ConvexHullShape(jvec);
                shape = hullShape;
            }
            else if (physicsShape == PhysicsWorld.PhysicsShape.Box)
            {
               /* Mesh boxMesh = MeshFactory.CreateCube(boundingBox.Min, boundingBox.Max);
                List<JVector> jvec = new List<JVector>();
                List<TriangleVertexIndices> tv = new List<TriangleVertexIndices>();
                for (int v = 0; v < boxMesh.vertices.Count; v++)
                {
                    Vector3f myvec = boxMesh.vertices[v].GetPosition();
                    jvec.Add(new JVector(myvec.x, myvec.y, myvec.z));
                }
                for (int i = 0; i < boxMesh.indices.Count; i += 3)
                {
                    tv.Add(new TriangleVertexIndices(boxMesh.indices[i], boxMesh.indices[i + 1], boxMesh.indices[i + 2]));
                }

                Octree oct = new Octree(jvec, tv);
                TriangleMeshShape trimesh = new TriangleMeshShape(oct);*/
                shape = new BoxShape(boundingBox.Extent.x, boundingBox.Extent.y, boundingBox.Extent.z);
            //    shape = trimesh;
            //    tv.Clear();
             //   jvec.Clear();
             //   oct = null;
             //   boxMesh = null;
            }
            meshes.Clear();
            matrices.Clear();
            geom.Clear();
            return shape;
        }
Example #3
0
 public override BoundingBox GetWorldBoundingBox()
 {
     if (worldBoundingBox == null)
     {
         worldBoundingBox = new BoundingBox();
         UpdateWorldBoundingBox();
     }
     return worldBoundingBox;
 }
Example #4
0
 public override BoundingBox GetLocalBoundingBox()
 {
     if (localBoundingBox == null)
     {
         localBoundingBox = new BoundingBox();
         UpdateLocalBoundingBox();
     }
     return localBoundingBox;
 }
Example #5
0
 public BoundingBox Extend(BoundingBox b)
 {
     return Extend(b.Min).Extend(b.Max);
 }
Example #6
0
 public static void RenderBoundingBox(BoundingBox boundingBox)
 {
     float offset = 0.1f;
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Min.y - offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Min.y - offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Max.y + offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Max.y + offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Max.y + offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Max.y + offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Min.y - offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Min.y - offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Min.y - offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Max.y + offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Max.y + offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Max.y + offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Min.y - offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Min.y - offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Max.z + offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Min.y - offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Max.y + offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Max.x + offset, boundingBox.Min.y - offset, boundingBox.Min.z - offset);
     RenderManager.Renderer.DrawVertex(boundingBox.Min.x - offset, boundingBox.Max.y + offset, boundingBox.Min.z - offset);
 }
        private void RenderBoundingBox(BoundingBox boundingBox)
        {
            GL.Enable(EnableCap.PolygonOffsetLine);
            GL.PolygonOffset(15.0f, 100.0f);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(cam.ViewMatrix.GetInvertedValues());
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(cam.ProjectionMatrix.GetInvertedValues());
            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);

            GL.LineWidth(1.5f);
            GL.Begin(PrimitiveType.Triangles);

            GL.Color3(1.0f, 0.0f, 0.0f);
            RenderUtil.RenderBoundingBox(boundingBox);

            GL.End();
            GL.Disable(EnableCap.PolygonOffsetLine);
        }