Esempio n. 1
0
        private void drawMesh(RAMMesh mesh)
        {
            GraphicsDevice.RenderState.CullMode = CullMode.None;
            GraphicsDevice.VertexDeclaration    = tangentVertexDeclaration;
            var data = mesh.GetCoreData();

            for (int i = 0; i < data.Parts.Count; i++)
            {
                var part = data.Parts[i];
                renderGBufferShader.SetParameter("World", part.ObjectMatrix);
                if (part.MeshMaterial.DiffuseMap == null)
                {
                    renderGBufferShader.SetParameter("Texture", checkerTexture);
                }
                else
                {
                    renderGBufferShader.SetParameter("Texture", texturePool.LoadTexture(part.MeshMaterial.DiffuseMap));
                }

                GraphicsDevice.Vertices[0].SetSource(meshPartPool.GetVertexBuffer(part.MeshPart), 0,
                                                     TangentVertex.SizeInBytes);

                GraphicsDevice.Indices = meshPartPool.GetIndexBuffer(part.MeshPart);

                int numVertices =
                    part.MeshPart.GetGeometryData().GetSourceVector3(MeshPartGeometryData.Semantic.Position).Length;


                renderGBufferShader.RenderMultipass(delegate
                {
                    GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numVertices, 0, numVertices / 3);
                });
            }
        }
Esempio n. 2
0
        private void drawMeshShadowMap(RAMMesh mesh)
        {
            GraphicsDevice.RenderState.CullMode = CullMode.None;
            GraphicsDevice.VertexDeclaration    = tangentVertexDeclaration;
            var data = mesh.GetCoreData();

            for (int i = 0; i < data.Parts.Count; i++)
            {
                var part = data.Parts[i];
                shadowMapShader.SetParameter("g_matWorld", part.ObjectMatrix);

                GraphicsDevice.Vertices[0].SetSource(meshPartPool.GetVertexBuffer(part.MeshPart), 0,
                                                     TangentVertex.SizeInBytes);

                GraphicsDevice.Indices = meshPartPool.GetIndexBuffer(part.MeshPart);

                int numVertices =
                    part.MeshPart.GetGeometryData().GetSourceVector3(MeshPartGeometryData.Semantic.Position).Length;


                shadowMapShader.RenderMultipass(delegate
                {
                    GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numVertices, 0, numVertices / 3);
                });
            }
        }
        public static IMesh CreateSimpleTestMesh()
        {
            IMesh mesh;

            mesh = new RAMMesh();

            var part = new MeshCoreData.Part();

            part.ObjectMatrix = Matrix.Identity;
            part.MeshPart     = new RAMMeshPart();
            ((RAMMeshPart)part.MeshPart).SetGeometryData(MeshPartGeometryData.CreateTestSquare());

            var mat = new MeshCoreData.Material();

            mat.DiffuseMap = GetTestTexture();

            part.MeshMaterial = mat;
            mesh.GetCoreData().Parts.Add(part);

            return(mesh);
        }
        public RAMMesh CreateMesh(ObjImporter importer)
        {
            var mesh = new RAMMesh();
            //TODO: additional data

            /*var meshAdditionalData = new MeshAdditionalData();
             * meshAdditionalData.ImportedDate = DateTime.Now;
             * meshAdditionalData.ImporterDescription = "Imported by the OBJToRAMMeshConverter using the OBJImporter";*/

            var meshCoreData = mesh.GetCoreData();

            Dictionary <OBJMaterial, MeshCoreData.Material> materials = convertMaterials(importer);

            for (int i = 0; i < importer.Groups.Count; i++)
            {
                var group = importer.Groups[i];
                for (int j = 0; j < group.SubObjects.Count; j++)
                {
                    var sub = group.SubObjects[j];
                    if (sub.Faces.Count == 0)
                    {
                        continue;
                    }
                    if (sub.Material.Name == materialNamePhysicsBox)
                    {
                        continue;
                    }

                    convertSubObject(importer, sub, materials, mesh);
                }
            }

            convertCollisionData(importer, mesh);

            return(mesh);
        }
        private void createMeshPart(OBJGroup.SubObject subObject)
        {
            var part = interpreter.CreateMeshPart(subObject);

            mesh.GetCoreData().Parts.Add(part);
        }
        private void convertSubObjectRenderPart(RAMMesh mesh, OBJGroup.SubObject sub, ObjImporter importer, Dictionary <OBJMaterial, MeshCoreData.Material> materials)
        {
            if (sub.Faces.Count == 0)
            {
                return;
            }


            var meshCoreData = mesh.GetCoreData();

            Vector3[] positions = new Vector3[sub.Faces.Count * 3];
            Vector3[] normals   = new Vector3[sub.Faces.Count * 3];
            Vector2[] texcoords = new Vector2[sub.Faces.Count * 3];

            for (int k = 0; k < sub.Faces.Count; k++)
            {
                var face = sub.Faces[k];
                positions[k * 3 + 0] = importer.Vertices[face.V1.Position];
                positions[k * 3 + 1] = importer.Vertices[face.V2.Position];
                positions[k * 3 + 2] = importer.Vertices[face.V3.Position];

                normals[k * 3 + 0] = importer.Normals[face.V1.Normal];
                normals[k * 3 + 1] = importer.Normals[face.V2.Normal];
                normals[k * 3 + 2] = importer.Normals[face.V3.Normal];

                texcoords[k * 3 + 0] = new Vector2(importer.TexCoords[face.V1.TextureCoordinate].X, 1 - importer.TexCoords[face.V1.TextureCoordinate].Y);
                texcoords[k * 3 + 1] = new Vector2(importer.TexCoords[face.V2.TextureCoordinate].X, 1 - importer.TexCoords[face.V2.TextureCoordinate].Y);
                texcoords[k * 3 + 2] = new Vector2(importer.TexCoords[face.V3.TextureCoordinate].X, 1 - importer.TexCoords[face.V3.TextureCoordinate].Y);
            }


            TangentSolver solver   = new TangentSolver();
            var           tangents = solver.GenerateTangents(positions, normals, texcoords).Select(f => new Vector3(f.X, f.Y, f.Z)).ToArray();



            var positionsSource = new MeshPartGeometryData.Source();

            positionsSource.DataVector3 = positions;
            positionsSource.Semantic    = MeshPartGeometryData.Semantic.Position;
            var normalsSource = new MeshPartGeometryData.Source();

            normalsSource.DataVector3 = normals;
            normalsSource.Semantic    = MeshPartGeometryData.Semantic.Normal;
            var texcoordsSource = new MeshPartGeometryData.Source();

            texcoordsSource.DataVector2 = texcoords;
            texcoordsSource.Semantic    = MeshPartGeometryData.Semantic.Texcoord;
            var tangentsSource = new MeshPartGeometryData.Source();

            tangentsSource.DataVector3 = tangents;
            tangentsSource.Semantic    = MeshPartGeometryData.Semantic.Tangent;



            var part = new MeshCoreData.Part();

            part.MeshMaterial = materials[sub.Material];

            var meshPart = new RAMMeshPart();

            meshPart.GetGeometryData().Sources.Add(positionsSource);
            meshPart.GetGeometryData().Sources.Add(normalsSource);
            meshPart.GetGeometryData().Sources.Add(texcoordsSource);
            meshPart.GetGeometryData().Sources.Add(tangentsSource);

            part.MeshPart     = meshPart;
            part.ObjectMatrix = Matrix.Identity;

            meshCoreData.Parts.Add(part);
        }