예제 #1
0
        private void DrawMeshSubsetReflection(MeshSubset meshSubset)
        {
            // Set effect variables for this subset
            effect.Parameters["g_UseTextures"].SetValue(meshSubset.UseTextures);
            if (meshSubset.UseTextures)
            {
                // Set texture if needed
                if (!meshSubset.DiffuseMap.Equals(currentlySetDiffuseMap))
                {
                    effect.Parameters["g_DiffMap"].SetValue(meshSubset.DiffuseMap);
                    currentlySetDiffuseMap = meshSubset.DiffuseMap;
                }
            }
            effect.Parameters["g_DiffuseMaterial"].SetValue(meshSubset.DiffuseMaterial);
            effect.Parameters["g_SpecularMaterial"].SetValue(meshSubset.SpecularMaterial);

            // Set vertex buffer
            device.SetVertexBuffer(meshSubset.Vertices);

            // Draw
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                device.DrawPrimitives(PrimitiveType.TriangleList, 0, meshSubset.NumVertices / 3);
            }
        }
예제 #2
0
        public MeshSubset(MeshSubset meshSubset)
        {
            this.numVertices     = meshSubset.numVertices;
            this.vertexBuffer    = meshSubset.vertexBuffer;
            this.vertexPositions = meshSubset.vertexPositions;
            this.worldMatrix     = Matrix.Identity; // Do not copy world matrix

            this.useTextures = meshSubset.useTextures;
            this.diffuseMap  = meshSubset.diffuseMap;
            this.normalMap   = meshSubset.normalMap;
            this.specularMap = meshSubset.specularMap;

            this.reflectionMaterial = meshSubset.reflectionMaterial;
            this.diffuseMaterial    = meshSubset.diffuseMaterial;
            this.specularMaterial   = meshSubset.specularMaterial;

            this.techniqueName = meshSubset.techniqueName;

            this.aabb           = meshSubset.aabbModelSpace;
            this.aabbModelSpace = new BoundingBox(this.aabb.Min, this.aabb.Max);
        }
예제 #3
0
        private void LoadMeshFromFile(GraphicsDevice device, ContentManager content, string meshFileName)
        {
            subsets = new List <MeshSubset>();

            int numMaterials;
            int numVertices;
            int numTriangles;

            MaterialData[] materials;

            StreamReader fileReader = new StreamReader(meshFileName);

            try
            {
                string   buffer;
                char[]   separator = { ' ' };
                string[] splitLine;

                // Read in file header information
                fileReader.ReadLine();

                // Num Materials
                buffer       = fileReader.ReadLine();
                splitLine    = buffer.Split(separator);
                numMaterials = int.Parse(splitLine[1]);

                // Num Vertices
                buffer      = fileReader.ReadLine();
                splitLine   = buffer.Split(separator);
                numVertices = int.Parse(splitLine[1]);

                // Num Triangles
                buffer       = fileReader.ReadLine();
                splitLine    = buffer.Split(separator);
                numTriangles = int.Parse(splitLine[1]);

                // Read in the material data
                fileReader.ReadLine();
                fileReader.ReadLine();

                materials = new MaterialData[numMaterials];
                int    materialIndex = 0;
                string texturePath   = @"Textures\";
                for (int i = 0; i < numMaterials; i++)
                {
                    materials[materialIndex] = new MaterialData();

                    // Diffuse Map
                    buffer = fileReader.ReadLine();
                    if (buffer != "NO_TEXTURE")
                    {
                        buffer = buffer.Remove(buffer.Length - 4); // Remove the file extension
                        materials[materialIndex].diffuseMap = content.Load <Texture2D>(texturePath + buffer);
                    }
                    else
                    {
                        // This material will render using material colors only
                        materials[materialIndex].useTextures = false;
                    }

                    // Specular Map
                    buffer = fileReader.ReadLine();
                    if (buffer != "NO_TEXTURE")
                    {
                        buffer = buffer.Remove(buffer.Length - 4); // Remove the file extension
                        materials[materialIndex].specularMap = content.Load <Texture2D>(texturePath + buffer);
                    }

                    // Normal Map
                    buffer = fileReader.ReadLine();
                    if (buffer != "NO_TEXTURE")
                    {
                        buffer = buffer.Remove(buffer.Length - 4); // Remove the file extension
                        materials[materialIndex].normalMap = content.Load <Texture2D>(texturePath + buffer);
                    }

                    // Reflectivity
                    buffer = fileReader.ReadLine();
                    string[] reflectivity = buffer.Split(' ');
                    materials[materialIndex].reflectionMaterial =
                        new Vector4(float.Parse(reflectivity[1], CultureInfo.InvariantCulture),
                                    float.Parse(reflectivity[2], CultureInfo.InvariantCulture),
                                    float.Parse(reflectivity[3], CultureInfo.InvariantCulture), 1.0f);

                    // Check for Material data
                    if (fileReader.Peek() == 'D')
                    {
                        // Read Diffuse Material
                        buffer = fileReader.ReadLine();
                        string[] data = buffer.Split(' ');
                        materials[materialIndex].diffuseMaterial =
                            new Vector4(float.Parse(data[1], CultureInfo.InvariantCulture),
                                        float.Parse(data[2], CultureInfo.InvariantCulture),
                                        float.Parse(data[3], CultureInfo.InvariantCulture), 1.0f);
                    }

                    // Check for Material data
                    if (fileReader.Peek() == 'S')
                    {
                        // Read Specular Material
                        buffer = fileReader.ReadLine();
                        string[] data = buffer.Split(' ');
                        materials[materialIndex].specularMaterial =
                            new Vector4(float.Parse(data[1], CultureInfo.InvariantCulture),
                                        float.Parse(data[2], CultureInfo.InvariantCulture),
                                        float.Parse(data[3], CultureInfo.InvariantCulture), 1.0f);
                    }

                    // Check for technique name
                    if (fileReader.Peek() == 'T')
                    {
                        // Read in the name of the technique
                        buffer = fileReader.ReadLine();
                        materials[materialIndex].techniqueName = buffer.Split(' ')[1];
                    }
                    else
                    {
                        // Use default technique name
                        materials[materialIndex].techniqueName = "MeshTech";
                    }

                    fileReader.ReadLine();
                    materialIndex++;
                }

                // Read in vertices
                fileReader.ReadLine();

                VertexPosTangNormTex[] vertices = new VertexPosTangNormTex[numVertices];
                for (int i = 0; i < numVertices; i++)
                {
                    // Position
                    buffer    = fileReader.ReadLine();
                    splitLine = buffer.Split(separator);

                    vertices[i].pos   = new Vector3();
                    vertices[i].pos.X = float.Parse(splitLine[1], CultureInfo.InvariantCulture);
                    vertices[i].pos.Y = float.Parse(splitLine[2], CultureInfo.InvariantCulture);
                    vertices[i].pos.Z = -float.Parse(splitLine[3], CultureInfo.InvariantCulture); // Invert Z-Coord

                    // Tangent
                    buffer    = fileReader.ReadLine();
                    splitLine = buffer.Split(separator);

                    vertices[i].tang   = new Vector3();
                    vertices[i].tang.X = float.Parse(splitLine[1], CultureInfo.InvariantCulture);
                    vertices[i].tang.Y = float.Parse(splitLine[2], CultureInfo.InvariantCulture);
                    vertices[i].tang.Z = -float.Parse(splitLine[3], CultureInfo.InvariantCulture); // Invert Z-Coord

                    // Normal
                    buffer    = fileReader.ReadLine();
                    splitLine = buffer.Split(separator);

                    vertices[i].norm   = new Vector3();
                    vertices[i].norm.X = float.Parse(splitLine[1], CultureInfo.InvariantCulture);
                    vertices[i].norm.Y = float.Parse(splitLine[2], CultureInfo.InvariantCulture);
                    vertices[i].norm.Z = -float.Parse(splitLine[3], CultureInfo.InvariantCulture); // Invert Z-Coord

                    // Texture Coordinates
                    buffer    = fileReader.ReadLine();
                    splitLine = buffer.Split(separator);

                    vertices[i].tex   = new Vector2();
                    vertices[i].tex.X = float.Parse(splitLine[1], CultureInfo.InvariantCulture);
                    vertices[i].tex.Y = float.Parse(splitLine[2], CultureInfo.InvariantCulture);

                    fileReader.ReadLine();
                }

                // Read in the indices
                fileReader.ReadLine();
                Vector4[] indices = new Vector4[numTriangles]; // XYZ- triangle indices, W- Subset index
                for (int i = 0; i < numTriangles; i++)
                {
                    buffer    = fileReader.ReadLine();
                    splitLine = buffer.Split(separator);

                    indices[i]   = new Vector4();
                    indices[i].X = float.Parse(splitLine[0], CultureInfo.InvariantCulture);
                    indices[i].Y = float.Parse(splitLine[1], CultureInfo.InvariantCulture);
                    indices[i].Z = float.Parse(splitLine[2], CultureInfo.InvariantCulture);
                    indices[i].W = float.Parse(splitLine[3], CultureInfo.InvariantCulture);
                }

                // Create the individual subsets
                int subsetID = (int)indices[0].W; // First
                int index    = -1;
                materialIndex = 0;
                for (int i = 0; i < numMaterials; i++)
                {
                    List <VertexPosTangNormTex> vertexList = new List <VertexPosTangNormTex>();

                    // Add all vertices used by this subset to the list
                    try
                    {
                        do
                        {
                            index++;

                            // Get the vertices for this primitive
                            VertexPosTangNormTex v0 = vertices[((int)indices[index].X)];
                            VertexPosTangNormTex v1 = vertices[((int)indices[index].Y)];
                            VertexPosTangNormTex v2 = vertices[((int)indices[index].Z)];
                            subsetID = (int)indices[index].W;

                            // Add the vertices to the list
                            vertexList.Add(v0);
                            vertexList.Add(v1);
                            vertexList.Add(v2);
                        } while(subsetID == (int)indices[index + 1].W);
                    }
                    catch (IndexOutOfRangeException e) {}

                    // Create the subset
                    MeshSubset subset = new MeshSubset(device, vertexList.ToArray());

                    // Set subset textures
                    subset.UseTextures        = materials[materialIndex].useTextures;
                    subset.DiffuseMap         = materials[materialIndex].diffuseMap;
                    subset.SpecularMap        = materials[materialIndex].specularMap;
                    subset.NormalMap          = materials[materialIndex].normalMap;
                    subset.ReflectionMaterial = materials[materialIndex].reflectionMaterial;
                    subset.DiffuseMaterial    = materials[materialIndex].diffuseMaterial;
                    subset.SpecularMaterial   = materials[materialIndex].specularMaterial;
                    subset.TechniqueName      = materials[materialIndex].techniqueName;
                    materialIndex++;

                    // Add the subset to the list
                    subsets.Add(subset);
                }
            }
            catch (IOException)
            {
            }
            finally
            {
                fileReader.Close();
            }
        }