Exemplo n.º 1
0
        public static List<Mesh> GatherMeshes(GameObject gameObject, List<Material> materials, List<Matrix4f> worldTransforms, List<Shader> shaders)
        {
            List<Mesh> meshes = new List<Mesh>();
            if (worldTransforms == null)
                worldTransforms = new List<Matrix4f>();

            if (gameObject is Node)
            {
                GatherMeshes((Node)gameObject, meshes, materials, worldTransforms, shaders);
            }
            else if (gameObject is Geometry)
            {
                meshes.Add(((Geometry)gameObject).Mesh);
                Transform ttransform = new Transform();
                ttransform.SetTranslation(gameObject.GetUpdatedWorldTranslation());
                ttransform.SetRotation(gameObject.GetUpdatedWorldRotation());
                ttransform.SetScale(gameObject.GetUpdatedWorldScale());
                Matrix4f matrix = ttransform.GetMatrix();
                worldTransforms.Add(matrix);
                materials.Add(((Geometry)gameObject).Material);
                if (((Geometry)gameObject).GetShader() != null)
                     shaders.Add(((Geometry)gameObject).GetShader());
            }

            return meshes;
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
 private static void GatherMeshes(Node node, List<Mesh> meshes, List<Material> materials, List<Matrix4f> worldTransforms)
 {
     foreach (GameObject child in node.Children)
     {
         if (child is Node)
         {
             GatherMeshes((Node)child, meshes, materials, worldTransforms);
         }
         else if (child is Geometry)
         {
             meshes.Add(((Geometry)child).Mesh);
             materials.Add(((Geometry)child).Material);
             Transform ttransform = new Transform();
             ttransform.SetTranslation(child.GetUpdatedWorldTranslation());
             ttransform.SetRotation(child.GetUpdatedWorldRotation());
             ttransform.SetScale(child.GetUpdatedWorldScale());
             Matrix4f matrix = ttransform.GetMatrix();
             worldTransforms.Add(matrix);
         }
     }
 }