예제 #1
0
        /*
        protected override object LoadData(Stream stream)
        {

        }

        protected override bool SaveData(object data, Stream stream)
        {
            //	Haven't yet created this code.
            return false;
        }

        public override string[] FileTypes
        {

        }

        public override string Filter
        {

        }

        public override Type[] DataTypes
        {
            get {return new Type[] {typeof(Scene)};}
        }*/
        public Scene LoadData(string path)
        {
            //  Create a null scene.
            Scene scene = null;

            //  Open the file stream.
            using (var fileStream = new FileStream(path, FileMode.Open))
            {
                using (var reader = new BinaryReader(fileStream, System.Text.Encoding.ASCII))
                {
                    //	Create a new scene to load to.
                    scene = new Scene();

                    //	Peep the first chunk to make sure it's a 'main' chunk.
                    if (MAXChunkHeader.Peep(reader).type != ChunkType.CHUNK_MAIN)
                        return null;

                    //	The first chunk is always the main chunk, so read it.
                    MainChunk main = new MainChunk();
                    main.Read(scene, reader);
                }
            }

            //  Return the scene.
            return scene;
        }
예제 #2
0
        public Scene LoadData(string path)
        {
            //  Create the scene.
            Scene scene = null;

            //  Open a file stream.
            using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                //  Open a binary reader.
                using (var reader = new BinaryReader(fileStream))
                {
                    //	Create a scene.
                    scene = new Scene();

                    //	First, read the header.
                    CaligariFileHeader header = new CaligariFileHeader();
                    header.Read(reader);

                    //	Here we can make sure that it really is a Caligari File.
                    if (header.id != "Caligari " || header.dataMode != 'B')
                    {
                        System.Diagnostics.Debugger.Log(1, "File I/O", "File is not internally compatible.\n");
                        return null;
                    }

                    //	Now we go through the file, peeping at chunks.
                    while (true)
                    {
                        //	Peep at the next chunk.
                        string type = Peep(reader);

                        //	Check for every type of chunk.
                        if (type == "PolH")
                        {
                            //	Read a polygon into the scene.
                            PolygonChunk polyChunk = new PolygonChunk();
                            scene.SceneContainer.AddChild((Polygon)polyChunk.Read(reader));
                        }
                        else if (type == "END ")
                        {
                            //	It's the end of the file, so we may as well break.
                            break;
                        }
                        else
                        {
                            //	Well we don't know what type it is, so just read the generic chunk.
                            CaligariChunk chunk = new CaligariChunk();
                            chunk.Read(reader);
                        }

                    }
                }
            }

            //  Return the scene.
            return scene;
        }
예제 #3
0
        public override void ReadData(Scene scene, BinaryReader stream)
        {
            //	Note: A max face is three indices and
            //	a flag short.

            //	Read number of faces.
            short faceCount = 0;
            faceCount = stream.ReadInt16();

            //	Read each face and add it.
            for (short i = 0; i < faceCount; i++)
            {
                Face f = new Face();
                Index index = new Index(stream.ReadInt16());
                f.Indices.Add(index);
                index = new Index(stream.ReadInt16());
                f.Indices.Add(index);
                index = new Index(stream.ReadInt16());
                f.Indices.Add(index);
                stream.ReadInt16();
                faces.Add(f);
            }
        }
예제 #4
0
        public override void ReadData(Scene scene, BinaryReader stream)
        {
            do
            {
                //	Peep at the next chunk.
                MAXChunkHeader next = MAXChunkHeader.Peep(stream);

                //	If it's an Object Mesh, we can read that.
                if (next.type == ChunkType.CHUNK_OBJMESH)
                {
                    ObjectMeshChunk chunk = new ObjectMeshChunk();
                    chunk.Read(scene, stream);
                }
                else
                {
                    //	We don't know what this chunk is, so just read the generic one.
                    MAXChunk chunk = new MAXChunk();
                    chunk.Read(scene, stream);
                }

            } while (MoreChunks(stream));
        }
예제 #5
0
        public override void ReadData(Scene scene, BinaryReader stream)
        {
            //	Read number of vertices.
            short vertexCount = 0;
            vertexCount = stream.ReadInt16();

            //	Read each vertex and add it.
            for (short i = 0; i < vertexCount; i++)
            {
                Vertex v = new Vertex();
                v.X = stream.ReadSingle();
                v.Y = stream.ReadSingle();
                v.Z = stream.ReadSingle();
                vertices.Add(v);
            }
        }
예제 #6
0
        public override void ReadData(Scene scene, BinaryReader stream)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                    matrix[i,j] = stream.ReadSingle();
            }

            matrix.Transpose();
        }
예제 #7
0
        public override void ReadData(Scene scene, BinaryReader stream)
        {
            //	A triangle mesh is basicly a Polygon, so create it.
            Polygon poly = new Polygon();
            Matrix matrix = new Matrix(Matrix.Identity(4));

            do
            {
                //	Peep at the next chunk.
                MAXChunkHeader next = MAXChunkHeader.Peep(stream);

                if (next.type == ChunkType.CHUNK_VERTLIST)
                {
                    //	Read the vertices.
                    VertexListChunk chunk = new VertexListChunk();
                    chunk.Read(scene, stream);

                    //	Set them into the polygon.
                    poly.Vertices = chunk.vertices;
                }
                else if (next.type == ChunkType.CHUNK_FACELIST)
                {
                    //	Read the faces.
                    FaceListChunk chunk = new FaceListChunk();
                    chunk.Read(scene, stream);

                    //	Set them into the polygon.
                    poly.Faces = chunk.faces;
                }
                else if (next.type == ChunkType.CHUNK_MAPLIST)
                {
                    //	Read the uvs.
                    MapListChunk chunk = new MapListChunk();
                    chunk.Read(scene, stream);

                    //	Set them into the polygon.
                    poly.UVs = chunk.uvs;
                }
                else if (next.type == ChunkType.CHUNK_TRMATRIX)
                {
                    //	Here we just read the matrix (we'll use it later).
                    TrMatrixChunk chunk = new TrMatrixChunk();
                    chunk.Read(scene, stream);

                    matrix = chunk.matrix;
                }
                else
                {
                    //	We don't know what this chunk is, so just read the generic one.
                    MAXChunk chunk = new MAXChunk();
                    chunk.Read(scene, stream);
                }

            } while (MoreChunks(stream));

            //	Now we multiply each vertex by the matrix.
            for (int i = 0; i < poly.Vertices.Count; i++)
                poly.Vertices[i] *= matrix;

            //	Add the poly to the scene.
            scene.SceneContainer.AddChild(poly);
        }
예제 #8
0
        /// <summary>
        /// This function reads the chunk and bangs the data in it into the scene.
        /// </summary>
        /// <param name="stream">The file stream to read from.</param>
        /// <param name="scene">The scene to put data into.</param>
        public virtual void ReadData(Scene scene, BinaryReader stream)
        {
            //	This is the code that is executed when an unknown chunk is read.

            //	Advance the stream.
            stream.BaseStream.Seek(chunkHeader.dataBytes, System.IO.SeekOrigin.Current);
        }
예제 #9
0
        public virtual void Read(Scene scene, BinaryReader stream)
        {
            //	Set the start position.
            startPosition = stream.BaseStream.Position;

            //	Read the header.
            ReadHeader(stream);

            //	Read the data itself.
            ReadData(scene, stream);
        }
예제 #10
0
        public override void ReadData(Scene scene, BinaryReader stream)
        {
            //	Read number of uvs.
            short uvCount = 0;
            uvCount = stream.ReadInt16();

            //	Read each uv and add it.
            for (short i = 0; i < uvCount; i++)
            {
                UV uv = new UV();
                uv.U = stream.ReadSingle();
                uv.V = stream.ReadSingle();
                uvs.Add(uv);
            }
        }
예제 #11
0
 public bool SaveData(Scene scene, string path)
 {
     throw new NotImplementedException();
 }
예제 #12
0
        private void LoadMaterials(string path, Scene scene)
        {
            //  Create a stream reader.
            using (StreamReader reader = new StreamReader(path))
            {
                Material mtl = null;
                float alpha = 1;

                //  Read line by line.
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    line = line.Trim();

                    //  Skip any comments (lines that start with '#').
                    if (line.StartsWith("#"))
                        continue;

                    // newmatl indicates start of material definition.
                    if (line.StartsWith("newmtl"))
                    {
                        // Add new material to scene's assets.
                        mtl = new Material();
                        scene.Assets.Add(mtl);

                        // Name of material is on same line, immediately follows newmatl.
                        mtl.Name = ReadMaterialValue(line);

                        // Reset assumed alpha.
                        alpha = 1;
                    }

                    // Read properties of material.
                    if (mtl != null)
                    {
                        if (line.StartsWith("Ka"))
                            mtl.Ambient = ReadMaterialColor(line, alpha);
                        else if (line.StartsWith("Kd"))
                            mtl.Diffuse = ReadMaterialColor(line, alpha);
                        else if (line.StartsWith("Ks"))
                            mtl.Specular = ReadMaterialColor(line, alpha);
                        else if (line.StartsWith("Ns"))
                            mtl.Shininess = Convert.ToSingle(ReadMaterialValue(line));
                        else if (line.StartsWith("map_Ka") ||
                            line.StartsWith("map_Kd") ||
                            line.StartsWith("map_Ks"))
                        {
                            // Get texture map.
                            string textureFile = ReadMaterialValue(line);

                            // Check for existing textures.  Create if does not exist.
                            Texture theTexture = null;
                            var existingTextures = scene.Assets.Where(t => t is Texture && t.Name == textureFile);
                            if (existingTextures.Count() >= 1)
                                theTexture = existingTextures.FirstOrDefault() as Texture;
                            else
                            {
                                //  Does the texture file exist?
                                if (File.Exists(textureFile) == false)
                                {
                                    //  It doesn't, assume its in the same location
                                    //  as the obj file.
                                    textureFile = Path.Combine(Path.GetDirectoryName(path),
                                        Path.GetFileName(textureFile));
                                }

                                // Create/load texture.
                                theTexture = new Texture();
                                theTexture.Create(scene.OpenGL, textureFile);
                            }

                            // Set texture for material.
                            mtl.Texture = theTexture;
                        }
                        else if (line.StartsWith("d") || line.StartsWith("Tr"))
                        {
                            alpha = Convert.ToSingle(ReadMaterialValue(line));
                            SetAlphaForMaterial(mtl, alpha);
                        }
                        // TODO: Handle illumination mode (illum)
                    }
                }

            }
        }
예제 #13
0
 public bool SaveData(Scene scene, string path)
 {
     throw new NotImplementedException("The SaveData method has not been implemented for .obj files.");
     //return SaveData(scene, scene.SceneContainer, path);
 }
예제 #14
0
        public Scene LoadData(string path)
        {
            char[] split = new char[] { ' ' };

            //  Create a scene and polygon.
            Scene scene = new Scene();
            Polygon polygon = new Polygon();

            string mtlName = null;

            //  Create a stream reader.
            using (StreamReader reader = new StreamReader(path))
            {
                //  Read line by line.
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    //  Skip any comments (lines that start with '#').
                    if (line.StartsWith("#"))
                        continue;

                    //  Do we have a texture coordinate?
                    if (line.StartsWith("vt"))
                    {
                        //  Get the texture coord strings.
                        string[] values = line.Substring(3).Split(split, StringSplitOptions.RemoveEmptyEntries);

                        //  Parse texture coordinates.
                        float u = float.Parse(values[0]);
                        float v = float.Parse(values[1]);

                        //  Add the texture coordinate.
                        polygon.UVs.Add(new UV(u, v));

                        continue;
                    }

                    //  Do we have a normal coordinate?
                    if (line.StartsWith("vn"))
                    {
                        //  Get the normal coord strings.
                        string[] values = line.Substring(3).Split(split, StringSplitOptions.RemoveEmptyEntries);

                        //  Parse normal coordinates.
                        float x = float.Parse(values[0]);
                        float y = float.Parse(values[1]);
                        float z = float.Parse(values[2]);

                        //  Add the normal.
                        polygon.Normals.Add(new Vertex(x, y, z));

                        continue;
                    }

                    //  Do we have a vertex?
                    if (line.StartsWith("v"))
                    {
                        //  Get the vertex coord strings.
                        string[] values = line.Substring(2).Split(split, StringSplitOptions.RemoveEmptyEntries);

                        //  Parse vertex coordinates.
                        float x = float.Parse(values[0]);
                        float y = float.Parse(values[1]);
                        float z = float.Parse(values[2]);

                        //   Add the vertices.
                        polygon.Vertices.Add(new Vertex(x, y, z));

                        continue;
                    }

                    //  Do we have a face?
                    if (line.StartsWith("f"))
                    {
                        Face face = new Face();

                        if (!String.IsNullOrWhiteSpace(mtlName))
                            face.Material = scene.Assets.Where(t => t.Name == mtlName).FirstOrDefault() as Material;

                        //  Get the face indices
                        string[] indices = line.Substring(2).Split(split,
                            StringSplitOptions.RemoveEmptyEntries);

                        //  Add each index.
                        foreach (var index in indices)
                        {
                            //  Split the parts.
                            string[] parts = index.Split(new char[] { '/' }, StringSplitOptions.None);

                            //  Add each part.
                            face.Indices.Add(new Index(
                                (parts.Length > 0 && parts[0].Length > 0) ? int.Parse(parts[0]) - 1 : -1,
                                (parts.Length > 1 && parts[1].Length > 0) ? int.Parse(parts[1]) - 1 : -1,
                                (parts.Length > 2 && parts[2].Length > 0) ? int.Parse(parts[2]) - 1 : -1));
                        }

                        //  Add the face.
                        polygon.Faces.Add(face);

                        continue;
                    }

                    if (line.StartsWith("mtllib"))
                    {
                        // Set current directory in case a relative path to material file is used.
                        Environment.CurrentDirectory = Path.GetDirectoryName(path);

                        // Load materials file.
                        string mtlPath = ReadMaterialValue(line);
                        LoadMaterials(mtlPath, scene);
                    }

                    if (line.StartsWith("usemtl"))
                        mtlName = ReadMaterialValue(line);
                }
            }

            scene.SceneContainer.AddChild(polygon);

            return scene;
        }
예제 #15
0
 public bool SaveData(Scene scene, string path)
 {
     throw new NotImplementedException("Cannot save to Caligari format");
 }