Exemplo n.º 1
0
        public void ParserDealingWithFacesWithNormals_ShouldParseNormalDataCorrectly()
        {
            var file =
                @"v 0 1 0
v -1 0 0
v 1 0 0
vn -1 0 0
vn 1 0 0
vn 0 1 0
f 1//3 2//1 3//2
f 1/0/3 2/102/1 3/14/2";

            var parser = new ObjParser(file);

            parser.Parse();
            var g  = (object)parser.Groups[0] as Group;
            var t1 = (object)g[0] as SmoothTriangle;
            var t2 = (object)g[1] as SmoothTriangle;

            Assert.StrictEqual(t1.p1, parser.Vertices[0]);
            Assert.StrictEqual(t1.p2, parser.Vertices[1]);
            Assert.StrictEqual(t1.p3, parser.Vertices[2]);
            Assert.StrictEqual(t1.n1, parser.Normals[2]);
            Assert.StrictEqual(t1.n2, parser.Normals[0]);
            Assert.StrictEqual(t1.n3, parser.Normals[1]);
            //Assert.Equal(t2, t1);
        }
Exemplo n.º 2
0
        public void ParserWithMulitpleGroups_ShouldPutFacesIntoGroupsAndCorrectCalcBounds()
        {
            var file =
                @"v 0 0 0
v 0 0 4
v 0 4 0
v 0 4 4
v 4 0 0
v 4 4 0
v 4 0 4
v 4 4 4
g subgroup1
f 1 2 3
g subgroup2
f 1 2 4";

            var parser = new ObjParser(file);

            parser.Parse();
            parser.Normalize();
            var group = new Group();

            group.AddShapes(parser.Groups);
            Assert.Equal(new Point(-1, -1, -1), group.GetBounds().Min, PointComparer);
            Assert.Equal(new Point(-1, 1, 1), group.GetBounds().Max, PointComparer);
        }
Exemplo n.º 3
0
        public static void Main()
        {
            ObjParser parser = new ObjParser();

            string data = "";

            using (StreamReader str = new StreamReader("awp.obj"))
                data = str.ReadToEnd();

            Geometry geometry = parser.Parse(data);
            Bitmap   texture  = new Bitmap("texture.jpg");

            Mesh mesh = new Mesh(geometry, texture);

            MeshRenderer win = new MeshRenderer(1920, 1280, mesh);

            //settings go here
            win.CameraDistance += 10;
            win.AngleX         += 5;

            win.SavePath     = "test.jpg";
            win.CloseOnSaved = false; //will occupy a thread unil force finish

            win.Run(30);
        }
Exemplo n.º 4
0
        public void ParsingPolygons_ShouldBreakApartIntoTriangles()
        {
            var file =
                @"v -1 1 0
v -1 0 0
v 1 0 0
v 1 1 0
v 0 2 0
f 1 2 3 4 5";

            var parser = new ObjParser(file);

            parser.Parse();
            var      g  = (object)parser.Groups[0] as Group;
            Triangle t1 = (object)g[0] as Triangle;
            Triangle t2 = (object)g[1] as Triangle;
            Triangle t3 = (object)g[2] as Triangle;

            Assert.Equal(t1.p1, parser.Vertices[0]);
            Assert.Equal(t1.p2, parser.Vertices[1]);
            Assert.Equal(t1.p3, parser.Vertices[2]);
            Assert.Equal(t2.p1, parser.Vertices[0]);
            Assert.Equal(t2.p2, parser.Vertices[2]);
            Assert.Equal(t2.p3, parser.Vertices[3]);
            Assert.Equal(t3.p1, parser.Vertices[0]);
            Assert.Equal(t3.p2, parser.Vertices[3]);
            Assert.Equal(t3.p3, parser.Vertices[4]);
        }
Exemplo n.º 5
0
        public void ParsingFilesWithGroupNames_ShouldPutTrianglesInGivenGroups()
        {
            var file =
                @"v -1 1 0
v -1 0 0
v 1 0 0
v 1 1 0
g FirstGroup
f 1 2 3
g SecondGroup
f 1 3 4";

            var parser = new ObjParser(file);

            parser.Parse();
            var      g1 = (object)parser.Groups[0] as Group;
            var      g2 = (object)parser.Groups[1] as Group;
            Triangle t1 = (object)g1[0] as Triangle;
            Triangle t2 = (object)g2[0] as Triangle;

            Assert.Equal(t1.p1, parser.Vertices[0]);
            Assert.Equal(t1.p2, parser.Vertices[1]);
            Assert.Equal(t1.p3, parser.Vertices[2]);
            Assert.Equal(t2.p1, parser.Vertices[0]);
            Assert.Equal(t2.p2, parser.Vertices[2]);
            Assert.Equal(t2.p3, parser.Vertices[3]);
        }
Exemplo n.º 6
0
        private void AddSponzaAtriumObjects()
        {
            ObjParser parser = new ObjParser();

            using (FileStream objStream = File.OpenRead(AssetHelper.GetPath("Models/SponzaAtrium/sponza.obj")))
            {
                ObjFile atriumFile = parser.Parse(objStream);
                MtlFile atriumMtls;
                using (FileStream mtlStream = File.OpenRead(AssetHelper.GetPath("Models/SponzaAtrium/sponza.mtl")))
                {
                    atriumMtls = new MtlParser().Parse(mtlStream);
                }

                foreach (ObjFile.MeshGroup group in atriumFile.MeshGroups)
                {
                    Vector3                scale               = new Vector3(0.1f);
                    ConstructedMeshInfo    mesh                = atriumFile.GetMesh(group);
                    MaterialDefinition     materialDef         = atriumMtls.Definitions[mesh.MaterialName];
                    ImageSharpTexture      overrideTextureData = null;
                    ImageSharpTexture      alphaTexture        = null;
                    MaterialPropsAndBuffer materialProps       = CommonMaterials.Brick;
                    if (materialDef.DiffuseTexture != null)
                    {
                        string texturePath = AssetHelper.GetPath("Models/SponzaAtrium/" + materialDef.DiffuseTexture);
                        overrideTextureData = LoadTexture(texturePath, true);
                    }
                    if (materialDef.AlphaMap != null)
                    {
                        string texturePath = AssetHelper.GetPath("Models/SponzaAtrium/" + materialDef.AlphaMap);
                        alphaTexture = LoadTexture(texturePath, false);
                    }
                    if (materialDef.Name.Contains("vase"))
                    {
                        materialProps = CommonMaterials.Vase;
                    }
                    if (group.Name == "sponza_117")
                    {
                        MirrorMesh.Plane = Plane.CreateFromVertices(
                            atriumFile.Positions[group.Faces[0].Vertex0.PositionIndex] * scale.X,
                            atriumFile.Positions[group.Faces[0].Vertex1.PositionIndex] * scale.Y,
                            atriumFile.Positions[group.Faces[0].Vertex2.PositionIndex] * scale.Z);
                        materialProps = CommonMaterials.Reflective;
                    }

                    AddTexturedMesh(
                        mesh,
                        overrideTextureData,
                        alphaTexture,
                        materialProps,
                        Vector3.Zero,
                        Quaternion.Identity,
                        scale,
                        group.Name);
                }
            }
        }
Exemplo n.º 7
0
        public void RenderBasicScene()
        {
            var          objFile  = modelPath.ToString() + "utah_teapot_hires.obj";
            FileStream   instream = File.OpenRead(objFile);
            StreamReader reader   = new StreamReader(instream);
            var          objData  = reader.ReadToEnd();

            var parser = new ObjParser(objData);

            parser.Parse();
            var teapot = new Group();

            teapot.AddShapes(parser.Groups);
            teapot.Divide(200);

            World world = new World();

            world.Shapes = new List <Shape> {
                teapot
            };

            // ======================================================
            // light sources
            // ======================================================

            world.Lights = new List <ILight> {
                new PointLight(new Point(-10, 10, -10), new Color(1, 1, 1))
            };

            // ======================================================
            // the camera
            // ======================================================

            Camera camera = new Camera(400, 300, Math.PI / 2);

            camera.Transform = Transformation.ViewTransform(
                new Point(-1.5, 1.5, 0),             // view from
                new Point(0, 0.5, 0),                // view to
                new Vector(0, 1, 0));                // vector up

            Canvas canvas = camera.Render(world);


            string filename = imagePath.ToString() + "SmoothTeapot.ppm";

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
            FileStream   stream = File.OpenWrite(filename);
            StreamWriter writer = new StreamWriter(stream);

            PpmWriter.WriteCanvasToPpm(writer, canvas);
            writer.Close();
        }
Exemplo n.º 8
0
        public void ObjLoad_Tests()
        {
            var objFile = Path.Combine(SampleRootPath, "model/model.obj");

            var objModel = ObjParser.Parse(objFile);

            Assert.NotNull(objModel);
            Assert.True(objModel.Groups.Count == 1);
            Assert.Equal(92, objModel.Positions.Count);
            Assert.Equal(19, objModel.Normals.Count);
            Assert.Equal(32, objModel.TextureCoords.Count);
        }
Exemplo n.º 9
0
        public void Parser_ShouldTrackMinAndMaxBoundsOfVertices()
        {
            var file =
                @"v 0 1 0
v -1 0 0
v 1 0 0";

            var parser = new ObjParser(file);

            parser.Parse();
            Assert.Equal(new Point(-1, 0, 0), parser.Min, PointComparer);
            Assert.Equal(new Point(1, 1, 0), parser.Max, PointComparer);
        }
Exemplo n.º 10
0
        public void ParseVertexNormals()
        {
            const string obj    = @"
vn 0 0 1
vn 0.707 0 -0.707
vn 1 2 3 ";
            var          parser = new ObjParser(obj);

            parser.Parse();
            Assert.AreEqual(parser.Normals[0], Direction(0, 0, 1));
            Assert.AreEqual(parser.Normals[1], Direction(0.707f, 0, -0.707f));
            Assert.AreEqual(parser.Normals[2], Direction(1, 2, 3));
        }
Exemplo n.º 11
0
        public void ObjParser_ShouldHandleVertexNormalRecords()
        {
            var file =
                @"vn 0 0 1
vn 0.707 0 -0.707
vn 1 2 3";

            var parser = new ObjParser(file);

            parser.Parse();
            Assert.StrictEqual(new Vector(0, 0, 1), parser.Normals[0]);
            Assert.StrictEqual(new Vector(0.707, 0, -0.707), parser.Normals[1]);
            Assert.StrictEqual(new Vector(1, 2, 3), parser.Normals[2]);
        }
Exemplo n.º 12
0
        public void IgnoreGibberish()
        {
            const string gibberisch = @"
Lorem ipsum, dolor sit amet consectetur adipisicing elit. 
Iusto id fuga accusamus quia! Alias quisquam ullam hic 
vero magnam sed.
";
            var          p          = new ObjParser(gibberisch);

            p.Parse();
            Assert.AreEqual(p.Vertices.Count, 0);
            Assert.AreEqual(p.Normals.Count, 0);
            Assert.AreEqual(p.Group.Count, 0);
        }
Exemplo n.º 13
0
        public void UnrecognizedLines_ShouldBeIgnoredAndCounted()
        {
            var gibberish =
                @"There was a young lady named Bright
who traveled much faster than light.
She set out one day
in a relative way,
and came back the previous night.";
            var parser = new ObjParser(gibberish);

            parser.Parse();
            Assert.Empty(parser.Vertices);
            Assert.Empty(parser.Normals);
        }
Exemplo n.º 14
0
 private void OpenFileButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         string[] fileLines = ObjFileReader.Execute();
         model = ObjParser.Parse(fileLines);
         model.diffuseTexture  = getBgr24BitmapDiffuse();
         model.normalsTexture  = getBgr24BitmapNormals();
         model.specularTexture = getBgr24BitmapSpecular();
     }
     catch
     {
         MessageBox.Show("Произошла ошибка!");
     }
 }
Exemplo n.º 15
0
        public void ParseVertices()
        {
            const string vertices = @"
v -1 1 0
v -1.000000  0.50000  0.000
v 1 0 0
v 1 1 0";
            var          parser   = new ObjParser(vertices);

            parser.Parse();
            Assert.AreEqual(parser.Vertices.Count, 4);
            Assert.AreEqual(parser.Vertices[0], Point(-1, 1, 0));
            Assert.AreEqual(parser.Vertices[1], Point(-1, .5f, 0));
            Assert.AreEqual(parser.Vertices[2], Point(1, 0, 0));
            Assert.AreEqual(parser.Vertices[3], Point(1, 1, 0));
        }
Exemplo n.º 16
0
        public void ParsingVertexRecordsWithMultipleSpacesBetweenRecords_ShouldReturnArrayOfPoints()
        {
            var file =
                @"v  -1  1  0
v  -1.0000  0.5000  0.0000
v  1  0  0
v  1  1  0";

            var parser = new ObjParser(file);

            parser.Parse();
            Assert.Equal(new Point(-1, 1, 0), parser.Vertices[0], PointComparer);
            Assert.Equal(new Point(-1, 0.5, 0), parser.Vertices[1], PointComparer);
            Assert.Equal(new Point(1, 0, 0), parser.Vertices[2], PointComparer);
            Assert.Equal(new Point(1, 1, 0), parser.Vertices[3], PointComparer);
        }
Exemplo n.º 17
0
        public void TriangulateFaces()
        {
            const string obj    = @"
v -1 1 0
v -1 0 0
v 1 0 0
v 1 1  0
v 0 2 0

f 1 2 3 4 5";
            var          parser = new ObjParser(obj);

            parser.Parse();
            Assert.AreEqual(parser.Vertices.Count, 5);
            Assert.AreEqual(parser.Group.Count, 3);
        }
Exemplo n.º 18
0
        public void ParseFaces()
        {
            const string obj    = @"
v -1 1 0
v -1 0 0
v 1 0 0
v 1 1  0

f 1 2 3
f 1 3 4";
            var          parser = new ObjParser(obj);

            parser.Parse();

            Assert.AreEqual(parser.Vertices.Count, 4);
            Assert.AreEqual(parser.Group.Count, 2);
        }
Exemplo n.º 19
0
        public void FaceWithNormals()
        {
            const string obj    = @"
v 0 1 0
v -1 0 0
v 1 0 0

vn -1 0 0
vn 1 0 0
vn 0 1 0

f 1//3 2//1 3//2
f 1/0/3 2/102/1 3/14/2";
            var          parser = new ObjParser(obj);

            parser.Parse();
            Assert.AreEqual(parser.Group.Count, 2);
        }
Exemplo n.º 20
0
        public void Parser_ShouldNormalizeVertexDataToFitAllModelsBetweenNegOneAndPosOne()
        {
            var file =
                @"v 0 0 0
v 0 0 4
v 0 4 0
v 0 4 4
v 4 0 0
v 4 4 0
v 4 0 4
v 4 4 4";

            var parser = new ObjParser(file);

            parser.Parse();
            parser.Normalize();
            Assert.Equal(new Point(-1, -1, -1), parser.Min, PointComparer);
            Assert.Equal(new Point(1, 1, 1), parser.Max, PointComparer);
        }
Exemplo n.º 21
0
        public void LoadingObjFile_ShouldConvertEverythingToGroup()
        {
            var file =
                @"v -1 1 0
v -1 0 0
v 1 0 0
v 1 1 0
g FirstGroup
f 1 2 3
g SecondGroup
f 1 3 4";

            var parser = new ObjParser(file);

            parser.Parse();
            var g = new Group();

            g.AddShapes(parser.Groups);
            Assert.True(true);
        }
Exemplo n.º 22
0
        private void AddSponzaAtriumObjects(CommandList cl)
        {
            ObjParser parser = new ObjParser();

            using (FileStream objStream = File.OpenRead(AssetHelper.GetPath("Models/SponzaAtrium/sponza.obj")))
            {
                ObjFile atriumFile = parser.Parse(objStream);
                MtlFile atriumMtls;
                using (FileStream mtlStream = File.OpenRead(AssetHelper.GetPath("Models/SponzaAtrium/sponza.mtl")))
                {
                    atriumMtls = new MtlParser().Parse(mtlStream);
                }

                foreach (ObjFile.MeshGroup group in atriumFile.MeshGroups)
                {
                    ConstructedMeshInfo    mesh                = atriumFile.GetMesh(group);
                    MaterialDefinition     materialDef         = atriumMtls.Definitions[mesh.MaterialName];
                    ImageSharpTexture      overrideTextureData = null;
                    ImageSharpTexture      alphaTexture        = null;
                    MaterialPropsAndBuffer materialProps       = CommonMaterials.Brick;
                    if (materialDef.DiffuseTexture != null)
                    {
                        string texturePath = AssetHelper.GetPath("Models/SponzaAtrium/" + materialDef.DiffuseTexture);
                        overrideTextureData = LoadTexture(texturePath, true);
                    }
                    if (materialDef.AlphaMap != null)
                    {
                        string texturePath = AssetHelper.GetPath("Models/SponzaAtrium/" + materialDef.AlphaMap);
                        alphaTexture = LoadTexture(texturePath, false);
                    }
                    if (materialDef.Name.Contains("vase"))
                    {
                        materialProps = CommonMaterials.Vase;
                    }

                    AddTexturedMesh(cl, mesh, overrideTextureData, alphaTexture, materialProps, Vector3.Zero, Quaternion.Identity, new Vector3(0.1f));
                }
            }
        }
Exemplo n.º 23
0
        public void ParsingTriangleFaces_ShouldReturnGroupsAndVetices()
        {
            var file =
                @"v -1 1 0
v -1 0 0
v 1 0 0
v 1 1 0
f 1 2 3
f 1 3 4";

            var parser = new ObjParser(file);

            parser.Parse();
            var      g  = (object)parser.Groups[0] as Group;
            Triangle t1 = (object)g[0] as Triangle;
            Triangle t2 = (object)g[1] as Triangle;

            Assert.Equal(t1.p1, parser.Vertices[0]);
            Assert.Equal(t1.p2, parser.Vertices[1]);
            Assert.Equal(t1.p3, parser.Vertices[2]);
            Assert.Equal(t2.p1, parser.Vertices[0]);
            Assert.Equal(t2.p2, parser.Vertices[2]);
            Assert.Equal(t2.p3, parser.Vertices[3]);
        }
Exemplo n.º 24
0
        public ObjFile Deserialize(Stream stream)
        {
            var parser = new ObjParser();

            return(parser.Parse(stream));
        }
Exemplo n.º 25
0
        public void Init()
        {
            var parser = new ObjParser(GetObjFile(), GetMtlFile());

            _result = parser.Parse();
        }
Exemplo n.º 26
0
        public (World, Camera) Setup(int width, int height, double fov)
        {
            // ======================================================
            // the camera
            // ======================================================

            var camera = new Camera(width, height, fov)
            {
                Transform = Transformation.ViewTransform(
                    new Point(0, 0.5, -2),            // view from
                    new Point(0, 0, 0),               // view to
                    new Vector(0, 1, 0)),             // vector up

                ProgressMonitor = new ParallelConsoleProgressMonitor(height),
            };

            // ======================================================
            // light sources
            // ======================================================

            var light = new PointLight(
                new Point(5, 5, -5),
                new Color(1, 1, 1)
                );

            // ======================================================
            // describe the elements of the scene
            // ======================================================

            var objFile = "/Users/rhagan/VSCode Projects/RayTracer/RayTracer.Program/Scenes/Models/teapot-low.obj";
            //var objFile = "/Users/ryan.hagan/Documents/VSCode Proejects/RayTracer/RayTracer.Program/Scenes/teapot.obj";
            FileStream   instream = File.OpenRead(objFile);
            StreamReader reader   = new StreamReader(instream);
            var          objData  = reader.ReadToEnd();
            var          parser   = new ObjParser(objData);

            parser.Parse();

            var teapot = new Group();

            teapot.Transform = Transformation.Rotation_x(-Math.PI / 2);
            var material = new Material()
            {
                Color      = new Color(0.9, 0.9, 1),
                Ambient    = 0.1,
                Diffuse    = 0.6,
                Specular   = 0.4,
                Shininess  = 5,
                Reflective = 0.1,
            };

            teapot.AddShapes(parser.Groups);
            teapot.SetMaterial(material);

            var boundingBoxMaterial = new Material()
            {
                Color           = new Color(1, 1, 0),
                Ambient         = 0.2,
                Diffuse         = 0.0,
                Specular        = 0.0,
                Shininess       = 0,
                Reflective      = 0.0,
                Transparency    = 0.8,
                RefractiveIndex = 1,
            };
            var box = new Cube(teapot.GetBounds().Min, teapot.GetBounds().Max)
            {
                Material = boundingBoxMaterial,
                HitBy    = RayType.Primary,
            };

            var floor = new Plane()
            {
                Material = new Material()
                {
                    Pattern  = new Checkers(Color.White, Color.Black),
                    Ambient  = 0.9,
                    Diffuse  = 0.2,
                    Specular = 0.0,
                },
                Transform = Transformation.Translation(0, -1, 0),
            };

            World world = new World();

            world.Shapes = new List <Shape> {
                floor, teapot, box
            };
            world.Lights = new List <ILight> {
                light
            };

            return(world, camera);
        }