Esempio n. 1
1
        /*
		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;
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the scene.
        /// </summary>
        private void CreateScene()
        {
            //  Create OpenGL.
            gl.Create(RenderContextType.DIBSection, 800, 600, 32, null);

            //   Create the scene object.
            Scene = new Scene() { OpenGL = gl };

            //  Initialise the scene object.
            SharpGL.SceneGraph.Helpers.SceneHelper.InitialiseModelingScene(Scene);

            //  Create a sphere.
            SharpGL.SceneGraph.Quadrics.Sphere sphere = new SharpGL.SceneGraph.Quadrics.Sphere();

            //  Add it.
            Scene.SceneContainer.AddChild(sphere);

            //  Create the arcball camera.
            var camera = new ArcBallCamera()
            {
                Position = new Vertex(-10, -10, 10)
            };
            Camera = camera;
            Scene.CurrentCamera = camera;
        }
 /// <summary>
 /// Creates a new scene
 /// </summary>
 /// <param name="width">Width of the scene</param>
 /// <param name="height">Height of the scene</param>
 /// <param name="bitDepth">Color depth of the scene</param>
 /// <returns></returns>
 public virtual Scene Create(int width = Constants.Scene.MinimumWidth, int height = Constants.Scene.MinimumHeight, int bitDepth = Constants.Scene.MaximumColorDepth)
 {
     SceneArgumentValidator.ValidateArguments(width, height, bitDepth);
     _gl.Create(OpenGLVersion.OpenGL4_4, RenderContextType.DIBSection, width, height, bitDepth, null);
     var scene = new Scene { OpenGL = _gl };
     SharpGL.SceneGraph.Helpers.SceneHelper.InitialiseModelingScene(scene);
     return scene;
 }
Esempio n. 4
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;
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
 /// <summary>
 /// Saves the scene to the specified stream.
 /// </summary>
 /// <param name="scene">The scene.</param>
 /// <param name="path">The path.</param>
 /// <returns>
 /// True if saved correctly.
 /// </returns>
 public bool SaveData(Scene scene, string path)
 {
     try
     {
         using (FileStream stream = new FileStream(path, FileMode.Create))
         {
             XmlSerializer serializer = new XmlSerializer(typeof(Scene));
             serializer.Serialize(stream, scene);
             stream.Flush();
         }
     }
     catch (Exception e)
     {
         throw new Exception("Failed to load Scene from XML data.", e);
     }
        
     return true;
 }
Esempio n. 7
0
        /// <summary>
        /// Creates the scene.
        /// </summary>
        private void CreateScene()
        {
            //  Create the perspective camera.
            
            var camera = new ArcBallCamera()
            {
                Name = "Look at camera",
                Position = new Vertex(-10, -10, 10)
            };
            Camera = camera;
            /*
            //  Create the look at camera.
            var camera = new LookAtCamera()
            {
                Name = "Look at camera",
                Position = new Vertex(-10, -10, 10),
                Target = new Vertex(0, 0, 0),
                UpVector = new Vertex(0, 0, 1)
            };*/
            Camera = camera;

            //  Create OpenGL.
            gl.Create(RenderContextType.DIBSection, 800, 600, 32, null);

            //   Create the scene object.
            Scene = new Scene() { OpenGL = gl };

            //  Initialise the scene.
            SceneHelper.InitialiseModelingScene(Scene);
            Scene.CurrentCamera = camera;

            //  Set the polygon mode.
            openGlAttributes = (Scene.SceneContainer.Effects[0] as OpenGLAttributesEffect);
            openGlAttributes.PolygonAttributes.PolygonMode = PolygonMode.Lines;

            //  Set the attributes as a scene level effect.
            Scene.SceneContainer.AddEffect(openGlAttributes);
        }
Esempio n. 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);
        }
Esempio n. 9
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);
            }
        }
Esempio n. 10
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);
            }
        }
Esempio n. 11
0
        protected override object LoadData(Stream stream)
        {
            //	We use a binary reader for this data.
            BinaryReader reader = new BinaryReader(stream, System.Text.Encoding.ASCII);

            //	Create a scene.
            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 false;
            }

            //	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.Polygons.Add((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 scene;
        }
Esempio n. 12
0
        public bool SaveData(Scene scene, string path)
        {
 	        throw new NotImplementedException();
        }
Esempio n. 13
0
        protected override object LoadData(Stream stream)
        {
            //	We use a binary reader.
            BinaryReader reader = new BinaryReader(stream, System.Text.Encoding.ASCII);

            //	Create a new scene to load to.
            Scene scene = new Scene();

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

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

            return scene;
        }
Esempio n. 14
0
        public override void ReadData(Scene scene, BinaryReader stream)
        {
            for(int i=0; i<3; i++)
            {
                for(int j=0; j<4; j++)
                    matrix.data[i][j] = stream.ReadSingle();
            }

            matrix.Transpose();
        }
 public void Init()
 {
     _scene = new SceneFactory().Create();
 }
Esempio n. 16
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);
                        float x = float.Parse(values[0]);
                        float y = float.Parse(values[1]);
                        
                        //  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);
                        values[0] = values[0].Replace(".", ",");
                        values[1] = values[1].Replace(".", ",");
                        values[2] = values[2].Replace(".", ",");

                        //  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);
                        values[0] = values[0].Replace(".", ",");
                        values[1] = values[1].Replace(".", ",");
                        values[2] = values[2].Replace(".", ",");

                        //  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;
        }
 /// <summary>
 /// Creates the new instance of the viewport.
 /// </summary>
 /// <param name="scene"></param>
 public SceneViewport(Scene scene)
 {
     Scene = scene;
     CurrentCamera = Scene.CurrentCamera;
     Navigation = new SceneViewportNavigation(this);
 }
Esempio n. 18
0
 public bool SaveData(Scene scene, string path)
 {
     throw new NotImplementedException("Cannot save to Caligari format");
 }
Esempio n. 19
0
        /// <summary>
        /// Saves the scene.
        /// </summary>
        /// <param name="scene">The scene.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public virtual bool SaveScene(Scene scene, string path)
        {
            //  Go through every format.
            var fileFormat = GetFormatForPath(path);
            if (fileFormat == null)
                return true;

            //  Load the scene.
            return fileFormat.SaveData(scene, path);
        }
Esempio n. 20
0
        public Scene LoadData(string path)
        { 
            char[] split = new char[] { ' '};

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

            //  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();

                        //  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"))
                        continue;

                    if (line.StartsWith("usemtl"))
                        continue;
                }
            }

            scene.SceneContainer.AddChild(polygon);

            return scene;
        }
Esempio n. 21
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 Block, we can read that.
                if(next.type == ChunkType.CHUNK_OBJBLOCK)
                {
                    ObjectBlockChunk chunk = new ObjectBlockChunk();
                    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));
        }
Esempio n. 22
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();

            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.Polygons.Add(poly);
        }
Esempio n. 23
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)                    	                    
                    }
                }

            }
        }
Esempio n. 24
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);
            }
        }
Esempio n. 25
0
		private void menuItemNew_Click(object sender, System.EventArgs e)
		{
			//	We need to reset the scene.
			Scene scene = new Scene();
			scene.Initialise(openGLCtrlScene.Width, openGLCtrlScene.Height, SceneType.HighQuality);
			openGLCtrlScene.Scene = scene;

			//	Reset the current selection and scenetree.
			PopulateSceneTree();
			propertyGridCurrent.SelectedObject = null;
		}
Esempio n. 26
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);
 }
Esempio n. 27
0
        public virtual Intersection Raytrace(Ray ray, Scene scene)
        {
            //	First we see if this ray intersects this polygon.
            Intersection intersect = TestIntersection(ray);

            //	If there wasn't an intersection, return.
            if(intersect.intersected == false)
                return intersect;

            //	There was an intersection, find the color of this point on the
            //	polygon.
            foreach(SharpGL.SceneGraph.Lights.Light light in scene.Lights)
            {
                if(light.On)
                {
                    //	Can we see this light? Cast a shadow ray.
                    Ray shadowRay = new Ray();
                    bool shadow = false;
                    shadowRay.origin = intersect.point;
                    shadowRay.direction = light.Translate - shadowRay.origin;

                    //	Test it with every polygon.
                    foreach(Polygon p in scene.Polygons)
                    {
                        if(p == this) continue;
                        Intersection shadowIntersect = p.TestIntersection(shadowRay);
                        if(shadowIntersect.intersected)
                        {
                            shadow = true;
                            break;
                        }
                    }

                    if(shadow == false)
                    {
                        //	Now find out what this light complements to our color.
                        float angle = light.Direction.ScalarProduct(intersect.normal);
                        ray.light += material.CalculateLighting(light, angle);
                        ray.light.Clamp();
                    }
                }
            }

            return intersect;
        }