예제 #1
0
        private MaterialFromObj createImplicitMaterial()
        {
            MaterialFromObj makeMaterial = new MaterialFromObj();

            materials.Add(makeMaterial);

            makeMaterial.name = "[ Implicit Material ]";

            return(makeMaterial);
        }
예제 #2
0
        private void parseOBJ(StreamReader sr)
        {
            MaterialFromObj currentMaterial = null;

            //Read the first line of text
            string line = sr.ReadLine();

            //Continue to read until you reach end of file
            while (line != null)
            {
                string[] tokens = line.Split(" ".ToArray(), 2);
                if (tokens.Length < 2)
                {
                    goto next_line;
                }

                string firstToken  = tokens[0];
                string lineContent = tokens[1];

                switch (firstToken)
                {
                case "#":       // Nothing to read, these are comments.
                    break;

                case "v":       // Vertex position
                    positions.Add(readVector3(lineContent, null));
                    break;

                case "vn":      // vertex normal direction vector
                    normals.Add(readVector3(lineContent, null));
                    break;

                case "vt":      // Vertex texcoordinate
                    texCoords.Add(readVector2_UV(lineContent, null));
                    break;

                case "f":       // Face
                    string[] values    = FilteredSplit(lineContent, null);
                    int      numPoints = values.Length;

                    Face face = new Face();
                    face.v_idx   = new Int16[numPoints];
                    face.n_idx   = new Int16[numPoints];
                    face.tex_idx = new Int16[numPoints];      // todo: how do outside clients know if there were texcoords or not?!?!

                    for (int i = 0; i < numPoints; i++)
                    {
                        string[] indexes = values[i].Split('/');        //  "loc_index[/tex_index[/normal_index]]"

                        int iPosition = (int.Parse(indexes[0]) - 1);    // adjust 1-based index
                        if (iPosition < 0)
                        {
                            iPosition += positions.Count + 1;
                        }                                                            // adjust negative indicies
                        face.v_idx[i] = (Int16)iPosition;

                        numIndices++;

                        if (indexes.Length > 1)
                        {
                            int iTexCoord = int.Parse(indexes[1]) - 1;     // adjust 1-based index
                            if (iTexCoord < 0)
                            {
                                iTexCoord += texCoords.Count + 1;
                            }                                                             // adjust negative indicies

                            face.tex_idx[i] = (Int16)iTexCoord;

                            if (indexes.Length > 2)
                            {
                                hasNormals = true;
                                int iNormal = int.Parse(indexes[2]) - 1;     // adjust 1 based index
                                if (iNormal < 0)
                                {
                                    iNormal += normals.Count + 1;
                                }                                                      // adjust negative indicies

                                face.n_idx[i] = (Int16)iNormal;
                            }
                        }
                    }
                    if (currentMaterial == null)
                    {
                        // no material in file, so create one
                        currentMaterial = createImplicitMaterial();
                    }
                    currentMaterial.faces.Add(face);
                    currentMaterial.nbrIndices += face.v_idx.Length;
                    numFaces++;
                    break;

                case "mtllib":      // load named material file
                    string mtlFile = lineContent;
                    parseMTL(mtlFile);
                    break;

                case "usemtl":      // use named material (from material file previously loaded)
                    bool found = false;

                    string matName = lineContent;

                    for (int i = 0; i < materials.Count; i++)
                    {
                        if (matName.Equals(materials[i].name))
                        {
                            found           = true;
                            currentMaterial = materials[i];
                        }
                    }

                    if (!found)
                    {
                        throw new Exception("Materials are already loaded so we should have it!");
                    }
                    break;
                }

next_line:
                //Read the next line
                line = sr.ReadLine();
            }

            //close the file
            sr.Close();
        }
예제 #3
0
        /// <summary>
        /// This method is used to load information stored in .mtl files referenced by the .obj file.
        /// </summary>
        /// <param name="d3ddevice"></param>
        /// <param name="file"></param>

        public void parseMTL(string file)
        {
            MaterialFromObj parseMaterial = new MaterialFromObj();

            bool first = true;

            // ask the delegate to open the material file for us..
            StreamReader sr = new StreamReader(this.opendelegate(file));

            //Read the first line of text
            string line = sr.ReadLine();

            //Continue to read until you reach end of file
            while (line != null)
            {
                string[] tokens = line.Split(" ".ToArray(), 2);
                if (tokens.Length < 2)
                {
                    goto next_line;
                }

                string firstToken  = tokens[0];
                string lineContent = tokens[1];


                switch (firstToken)
                {
                case "#":
                    // Nothing to read, these are comments.
                    break;

                case "newmtl":      // create new named material
                    if (!first)
                    {
                        materials.Add(parseMaterial);
                        parseMaterial = new MaterialFromObj();
                    }
                    first = false;
                    parseMaterial.name = lineContent;
                    break;

                case "Ka":     // ambient color
                    parseMaterial.vAmbient   = readVector3(lineContent, null);
                    parseMaterial.hasAmbient = true;
                    break;

                case "Kd":     // diffuse color
                    parseMaterial.vDiffuse   = readVector3(lineContent, null);
                    parseMaterial.hasDiffuse = true;
                    break;

                case "Ks":     // specular color (weighted by Ns)
                    parseMaterial.vSpecular   = readVector3(lineContent, null);
                    parseMaterial.hasSpecular = true;
                    break;

                case "Ns":     // specular color weight
                    parseMaterial.vSpecularWeight = float.Parse(lineContent);
                    break;

                case "d":
                case "Tr":     // transparency / dissolve (i.e. alpha)
                    parseMaterial.fTransparency   = float.Parse(lineContent);
                    parseMaterial.hasTransparency = true;
                    break;

                case "illum":     // illumination mode
                    parseMaterial.hasIlluminationMode = true;
                    parseMaterial.illuminationMode    = (WffObjIlluminationMode)int.Parse(lineContent);
                    break;

                case "map_Kd":     // diffuse color map
                    parseMaterial.diffuseTextureResourceName = lineContent;
                    break;

                case "map_Ka":     // ambient color map
                    parseMaterial.ambientTextureResourceName = lineContent;
                    break;

                case "map_Ks":     // specular color map
                    parseMaterial.specularTextureResourceName = lineContent;
                    break;

                case "map_d":     // alpha mask map
                    parseMaterial.alphaMaskTextureResourceName = lineContent;
                    parseMaterial.hasTransparency = true;
                    break;

                case "bump":
                case "map_bump":     // bump map
                    parseMaterial.bumpTextureResourceName = lineContent;
                    break;
                }

next_line:
                //Read the next line
                line = sr.ReadLine();
            }
            materials.Add(parseMaterial);

            //close the file
            sr.Close();
        }
        /// <summary>
        /// This method is used to load information stored in .mtl files referenced by the .obj file.
        /// </summary>
        /// <param name="d3ddevice"></param>
        /// <param name="file"></param>
        public void parseMTL(string file)
        {
            MaterialFromObj parseMaterial = new MaterialFromObj();

            bool first = true;

            // ask the delegate to open the material file for us..
            StreamReader sr = new StreamReader(this.opendelegate(file));

            //Read the first line of text
            string line = sr.ReadLine();

            //Continue to read until you reach end of file
            while (line != null) {
                string[] tokens = line.Split(" ".ToArray(), 2);
                if (tokens.Length < 2) {
                    goto next_line;
                }

                string firstToken = tokens[0];
                string lineContent = tokens[1];

                switch (firstToken) {
                    case "#":
                        // Nothing to read, these are comments.
                        break;
                    case "newmtl":  // create new named material
                        if (!first) {
                            materials.Add(parseMaterial);
                            parseMaterial = new MaterialFromObj();
                        }
                        first = false;
                        parseMaterial.name = lineContent;
                        break;
                    case "Ka": // ambient color
                        parseMaterial.vAmbient = readVector3(lineContent, null);
                        parseMaterial.hasAmbient = true;
                        break;
                    case "Kd": // diffuse color
                        parseMaterial.vDiffuse = readVector3(lineContent, null);
                        parseMaterial.hasDiffuse = true;
                        break;
                    case "Ks": // specular color (weighted by Ns)
                        parseMaterial.vSpecular = readVector3(lineContent,null);
                        parseMaterial.hasSpecular = true;
                        break;
                    case "Ns": // specular color weight
                        parseMaterial.vSpecularWeight = float.Parse(lineContent);
                        break;
                    case "d":
                    case "Tr": // transparency / dissolve (i.e. alpha)
                        parseMaterial.fTransparency = float.Parse(lineContent);
                        parseMaterial.hasTransparency = true;
                        break;
                    case "illum": // illumination mode
                        parseMaterial.hasIlluminationMode = true;
                        parseMaterial.illuminationMode = (WffObjIlluminationMode) int.Parse(lineContent);
                        break;
                    case "map_Kd": // diffuse color map
                        parseMaterial.diffuseTextureResourceName = lineContent;
                        break;
                    case "map_Ka": // ambient color map
                        parseMaterial.ambientTextureResourceName = lineContent;
                        break;
                    case "map_Ks": // specular color map
                        parseMaterial.specularTextureResourceName = lineContent;
                        break;
                    case "map_d": // alpha mask map
                        parseMaterial.alphaMaskTextureResourceName = lineContent;
                        parseMaterial.hasTransparency = true;
                        break;
                    case "bump":
                    case "map_bump": // bump map
                        parseMaterial.bumpTextureResourceName = lineContent;
                        break;
                }

            next_line:
                //Read the next line
                line = sr.ReadLine();
            }
            materials.Add(parseMaterial);

            //close the file
            sr.Close();
        }
        private MaterialFromObj createImplicitMaterial()
        {
            MaterialFromObj makeMaterial = new MaterialFromObj();
            materials.Add(makeMaterial);

            makeMaterial.name = "[ Implicit Material ]";

            return makeMaterial;
        }