Exemplo n.º 1
0
        private void ParseGeometries(IEnumerable <XElement> geometryNodes, IEnumerable <XElement> visualSceneNodes)
        {
            int i = 0;

            foreach (var geometry in geometryNodes.Elements("geometry"))
            {
                String geometryId = geometry.Attribute("id").Value;
                Dictionary <String, float[]> floatArrays      = new Dictionary <string, float[]>();
                Dictionary <String, String>  positionToVertex = new Dictionary <string, string>();
                Dictionary <String, String>  normalToVertex   = new Dictionary <string, string>();

                //Get the transformation
                Matrix4 transformationMatrix = Matrix4.Identity;

                foreach (var node in visualSceneNodes.Elements("node"))
                {
                    if (node.Element("instance_geometry") != null && node.Element("instance_geometry").Attribute("url").Value.Replace("#", "").Equals(geometryId))
                    {
                        if (node.Element("matrix") != null)
                        {
                            transformationMatrix = ParseMatrix(node.Element("matrix").Value);
                            Matrix4 scaleMatrix = Matrix4.Scale(scale);
                            transformationMatrix = Matrix4.Mult(transformationMatrix, scaleMatrix);
                        }
                        else
                        {
                            Matrix4 rotationMatrix    = Matrix4.Identity;
                            Matrix4 translationMatrix = Matrix4.Identity;
                            Matrix4 scaleMatrix       = Matrix4.Identity;

                            //Rotation
                            translationMatrix    = Matrix4.CreateRotationX((float)-Math.PI / 2);
                            transformationMatrix = Matrix4.Mult(transformationMatrix, translationMatrix);
                            foreach (var rotation in node.Elements("rotate"))
                            {
                                if (rotation.Attribute("sid").Value.Equals("rotateX"))
                                {
                                    Matrix4 rotationX = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(float.Parse(rotation.Value)));
                                    rotationMatrix = Matrix4.Mult(rotationMatrix, rotationX);
                                }
                                else if (rotation.Attribute("sid").Value.Equals("rotateY"))
                                {
                                    Matrix4 rotationY = Matrix4.CreateRotationY(MathHelper.DegreesToRadians(float.Parse(rotation.Value)));
                                    rotationMatrix = Matrix4.Mult(rotationMatrix, rotationY);
                                }
                                else if (rotation.Attribute("sid").Value.Equals("rotateZ"))
                                {
                                    Matrix4 rotationZ = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(float.Parse(rotation.Value)));
                                    rotationMatrix = Matrix4.Mult(rotationMatrix, rotationZ);
                                }
                                transformationMatrix = Matrix4.Mult(transformationMatrix, rotationMatrix);
                            }
                            //Translation
                            if (node.Element("translation") != null)
                            {
                                transformationMatrix.Translation = new Vector4(ParseVector(node.Element("translation").Value));
                            }
                            //Scaling
                            if (node.Element("scale") != null)
                            {
                                scaleMatrix          = Matrix4.CreateScaling(ParseVector(node.Element("scale").Value));
                                transformationMatrix = Matrix4.Mult(transformationMatrix, scaleMatrix);
                            }
                        }
                    }
                }

                //Parse the Float Arrays
                foreach (var source in geometry.Element("mesh").Elements("source"))
                {
                    String  key    = source.Attribute("id").Value.Replace("#", "");
                    float[] values = ParseFloatArray(source.Element("float_array").Value);
                    floatArrays.Add(key, values);
                }
                //Get the vertices
                foreach (var source in geometry.Element("mesh").Elements("vertices"))
                {
                    String vertexID = source.Attribute("id").Value.Replace("#", "");
                    foreach (var element in source.Elements("input"))
                    {
                        if (element.Attribute("semantic").Value.Equals("POSITION"))
                        {
                            String positionId = element.Attribute("source").Value.Replace("#", "");
                            positionToVertex.Add(vertexID, positionId);
                        }
                        if (element.Attribute("semantic").Value.Equals("NORMAL"))
                        {
                            String normalId = element.Attribute("source").Value.Replace("#", "");
                            normalToVertex.Add(vertexID, normalId);
                        }
                    }
                }
                //Parse the Triangle

                #region TriangleMesh

                if (geometry.Element("mesh").Elements("triangles").Count() > 0)
                {
                    foreach (var source in geometry.Element("mesh").Elements("triangles"))
                    {
                        int    triangleCount = Convert.ToInt32(source.Attribute("count").Value);
                        String materialName  = source.Attribute("material").Value.Replace("-material", "");
                        //Parse Everything
                        IEnumerable <string> vertexArrayNames = from item in source.Elements("input")
                                                                where item.Attribute("semantic").Value == "VERTEX"
                                                                select item.Attribute("source").Value.Remove(0, 1);

                        IEnumerable <string> vertexArrayOffsets = from item in source.Elements("input")
                                                                  where item.Attribute("semantic").Value == "VERTEX"
                                                                  select item.Attribute("offset").Value;

                        IEnumerable <string> normalArrayNames = from item in source.Elements("input")
                                                                where item.Attribute("semantic").Value == "NORMAL"
                                                                select item.Attribute("source").Value.Remove(0, 1);

                        IEnumerable <string> normalArrayOffsets = from item in source.Elements("input")
                                                                  where item.Attribute("semantic").Value == "NORMAL"
                                                                  select item.Attribute("offset").Value;

                        IEnumerable <string> texCordArrayNames = from item in source.Elements("input")
                                                                 where item.Attribute("semantic").Value == "TEXCOORD"
                                                                 select item.Attribute("source").Value.Remove(0, 1);

                        IEnumerable <string> texCordArrayOffsets = from item in source.Elements("input")
                                                                   where item.Attribute("semantic").Value == "TEXCOORD"
                                                                   select item.Attribute("offset").Value;

                        String normalArrayName = normalArrayNames.FirstOrDefault();
                        if (!string.IsNullOrEmpty(normalArrayName))
                        {
                            normalArrayName = normalArrayName.Replace("#", "");
                        }
                        String vertexArrayName = vertexArrayNames.FirstOrDefault();
                        if (!string.IsNullOrEmpty(vertexArrayName))
                        {
                            vertexArrayName = vertexArrayName.Replace("#", "");
                        }
                        String texCordArrayName = texCordArrayNames.FirstOrDefault();
                        if (!string.IsNullOrEmpty(texCordArrayName))
                        {
                            texCordArrayName = texCordArrayName.Replace("#", "");
                        }

                        int normalOffset   = -1;
                        int vertexOffset   = -1;
                        int texCoordOffset = -1;

                        float[] normals   = null;
                        float[] vertices  = null;
                        float[] texCoords = null;

                        int[]  vertexIndices   = null;
                        int[]  normalIndices   = null;
                        int[]  texCoordIndices = null;
                        String indices         = source.Element("p").Value;

                        int count = 0;
                        count += vertexArrayNames.Count();
                        count += normalArrayNames.Count();
                        count += texCordArrayNames.Count();

                        if (!string.IsNullOrEmpty(normalArrayName))
                        {
                            int.TryParse(normalArrayOffsets.First(), out normalOffset);
                            normals = floatArrays[normalArrayName];
                        }
                        if (!string.IsNullOrEmpty(vertexArrayName))
                        {
                            int.TryParse(vertexArrayOffsets.First(), out vertexOffset);
                            vertices = floatArrays[positionToVertex[vertexArrayName]];
                        }
                        if (!string.IsNullOrEmpty(texCordArrayName))
                        {
                            int.TryParse(texCordArrayOffsets.First(), out texCoordOffset);
                            texCoords = floatArrays[texCordArrayName];
                        }
                        if (!string.IsNullOrEmpty(normalArrayName))
                        {
                            normalIndices = ParseTriangleIndices(indices, normalOffset, count, triangleCount);
                        }
                        if (!string.IsNullOrEmpty(vertexArrayName))
                        {
                            vertexIndices = ParseTriangleIndices(indices, vertexOffset, count, triangleCount);
                        }
                        if (!string.IsNullOrEmpty(texCordArrayName))
                        {
                            texCoordIndices = ParseTriangleIndices(indices, texCoordOffset, count, triangleCount);
                        }
                        //Create the Mesh
                        Material mat;

                        mat = Materials[materialName];

                        Mesh           m   = new Mesh(vertices, vertexIndices, normals, normalIndices, texCoords, texCoordIndices, mat);
                        BspAccelerator acc = new BspAccelerator();
                        acc.Construct(m);
                        Instance instance = new Instance(acc, transformationMatrix);
                        Meshes.Add(geometryId + "_" + i, instance);
                        i++;
                    }
                }

                #endregion
            }
        }
Exemplo n.º 2
0
        public KdTreeScene()
        {
            FileName = "Assignment2_KdTree.jpg";
            //scene = new Scene { BackgroundColor = Color.Black };
            Integrator = (IIntegrator)Activator.CreateInstance(Constants.Integrator);



            Camera.FieldOfViewX = 60f;
            Camera.FieldOfViewY = 60f;
            Camera.ScreenWidth  = 512;
            Camera.ScreenHeight = 512;
            Camera.Eye          = new Vector4(0, 0, 2, 1);
            Camera.Up           = new Vector4(0, 1, 0, 1);
            Camera.LookAt       = new Vector4(0, 0, 0, 1);
            Camera.PreProcess();

            Film = new Film(Camera.ScreenWidth, Camera.ScreenHeight);

            Plane p1 = new Plane(1f, new Vector3(0, 1, 0))
            {
                Name     = "P1",
                Material = new LambertMaterial(new Color(0f, 0.8f, 0.8f))
            };
            Plane p2 = new Plane(1f, new Vector3(0, 0, 1))
            {
                Name     = "P2",
                Material = new LambertMaterial(new Color(0.3f, 0.8f, 0.8f))
            };
            Plane p3 = new Plane(1f, new Vector3(-1, 0, 0))
            {
                Name     = "P3",
                Material = new LambertMaterial(new Color(1.0f, 0.8f, 0.8f))
            };
            Plane p4 = new Plane(1f, new Vector3(1, 0, 0))
            {
                Name     = "P4",
                Material = new LambertMaterial(new Color(0f, 0.8f, 0f))
            };
            Plane p5 = new Plane(1f, new Vector3(0, -1, 0))
            {
                Name     = "P5",
                Material = new LambertMaterial(new Color(0.8f, 0.8f, 0.8f))
            };

            Mesh mesh = new Mesh()
            {
                Material = new LambertMaterial(new Color(0.5f, 0.5f, 0.5f))
            };

            mesh.CreateMeshFromObjectFile("./geometries/buddha.obj", 1.0f);

            BspAccelerator tree = new BspAccelerator();

            tree.Construct(mesh);

            //Matrix4 m1 = Matrix4.Scale(0.5f);
            //m1.Translation = new Vector4(0, -0.25f, 0, 1);


            Matrix4 m2 = Matrix4.Scale(0.5f);

            m2.Translation = new Vector4(0, 0.25f, 0, 1);



            Instance i1 = new Instance(tree, Matrix4.Identity);

            //Instance i2 = new Instance(tree, m2);

            /*
             * Instance i1 = new Instance(mesh, m1);
             * Instance i2 = new Instance(mesh, m2);
             */
            Objects = new IntersectableList();

            Objects.Add(p1);
            Objects.Add(p2);
            Objects.Add(p3);
            Objects.Add(p4);
            Objects.Add(p5);
            Objects.Add(i1);
            //scene.IntersectableList.Objects.Add(i2);

            Lights = new List <ILight>();

            ILight light  = new PointLight(new Vector3(0.0f, 0.8f, 0.8f), new Color(.7f, .7f, .7f));
            ILight light2 = new PointLight(new Vector3(-0.8f, 0.2f, 1f), new Color(.5f, .5f, .5f));

            Lights.Add(light);
            Lights.Add(light2);
        }