public void TriangleRenderKnot3()
        {            //scale and offset all verts.
            //flip verts since image top is 0,0


            var mesh = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/knot3/knot3.obj"));
            var tris = mesh.Triangles;

            mesh.VertexData = mesh.VertexData.Select(x => Vector4.Multiply(x, new Vector4(1.0f, 1.0f, 1.0f, 1.0f)))
                              //scale and offset.
                              .Select(x => Vector4.Multiply(Vector4.Add(x, new Vector4(5, 5, 0, 0)), 120)).ToArray();

            var renderer = new Triangle2dRenderer(1024, 768, new List <IEnumerable <TriangleFace> > {
                tris
            });

            renderer.VertexData = mesh.VertexData.Select(x => new Vector3(x.X, x.Y, x.Z)).ToArray();

            var image = new ppmImage(1024, 768, 255);

            image.Colors = renderer.Render();
            Assert.AreEqual(736261, image.Colors.Where(x => x == Color.Red).Count());

            System.IO.File.WriteAllBytes("../../../knotTest3.ppm", image.toByteArray());
        }
        public void MeshRenderPerspProjectionShader()
        {
            var cameraPos = new Vector3(0, 0, 4);
            var target    = new Vector3(0, 0, 0);
            var view      = Matrix4x4.CreateLookAt(cameraPos, target, Vector3.UnitY);
            var proj      = Matrix4x4.CreatePerspective(4, 3, 1, 10);
            var viewport  = MatrixExtensions.CreateViewPortMatrix(0, 0, 255, 640, 480);

            var mesh = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/knot3/knot3.obj"));

            var renderable = new Renderable <Mesh>(
                new Material()
            {
                Shader = new Base3dShader(view, proj, viewport)
            },
                mesh);
            var renderer = new Renderer3dGeneric <Mesh>(640, 480, Color.White,
                                                        new List <IEnumerable <Renderable <Mesh> > > {
                new List <Renderable <Mesh> > {
                    renderable
                }
            });

            var image = new ppmImage(640, 480, 255);

            image.Colors = renderer.Render();
            System.IO.File.WriteAllBytes("../../../perspectiveTest1.ppm", image.toByteArray());
            Assert.AreEqual(300249, image.Colors.Where(x => x == Color.White).Count());
        }
        public void lineRenderTeapot()
        {
            var lines = new List <List <(Vector2, Vector2, Color)> >();
            var verts = ObjFileLoader.LoadVertsFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/teapot.obj"))
                        .Select(x => Vector4.Add(x, new Vector4(5, 5, 0, 0))).ToArray();
            var tris = ObjFileLoader.LoadTrisFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/teapot.obj"));

            var trilines = tris.SelectMany(x =>

                                           new List <(Vector2, Vector2, Color)>()
            {
                (new Vector2(verts[x.vertIndexList[0] - 1].X * 50, verts[x.vertIndexList[0] - 1].Y * 50),
                 new Vector2(verts[x.vertIndexList[1] - 1].X * 50, verts[x.vertIndexList[1] - 1].Y * 50), Color.White),

                (new Vector2(verts[x.vertIndexList[1] - 1].X * 50, verts[x.vertIndexList[1] - 1].Y * 50),
                 new Vector2(verts[x.vertIndexList[2] - 1].X * 50, verts[x.vertIndexList[2] - 1].Y * 50), Color.White),

                (new Vector2(verts[x.vertIndexList[2] - 1].X * 50, verts[x.vertIndexList[2] - 1].Y * 50),
                 new Vector2(verts[x.vertIndexList[0] - 1].X * 50, verts[x.vertIndexList[0] - 1].Y * 50), Color.White),
            }
                                           );

            lines.Add(trilines.ToList());

            var renderer = new LineRenderer2d(640, 480, lines);

            var image = new ppmImage(640, 480, 255);

            image.Colors = renderer.Render();
            Assert.AreEqual(18702, image.Colors.Where(x => x == Color.White).Count());

            System.IO.File.WriteAllBytes("../../../linetest_teapot.ppm", image.toByteArray());
        }
        public void Prespective_Tex_Map()
        {
            var cameraPos = new Vector3(0, 0, 2);
            var target    = new Vector3(0, 0, 0);
            var width     = 1024;
            var height    = 1024;
            var view      = Matrix4x4.CreateLookAt(cameraPos, target, Vector3.UnitY);
            var proj      = Matrix4x4.CreatePerspective(4, 4, 1, 10);
            var viewport  = MatrixExtensions.CreateViewPortMatrix(0, 0, 255, width, height);

            var mesh       = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/knot3/knot3.obj"));
            var diffuseTex = new ppmImage("../../../../../textures/testTexture2.ppm");

            var renderable = new Renderable <Mesh>(
                new DiffuseMaterial()
            {
                Shader         = new TextureShader(view, proj, viewport),
                DiffuseTexture = new Texture2d(diffuseTex.Width, diffuseTex.Height, diffuseTex.Colors)
            },
                mesh);
            var renderer = new Renderer3dGeneric <Mesh>(width, height, Color.White,
                                                        new List <IEnumerable <Renderable <Mesh> > > {
                new List <Renderable <Mesh> > {
                    renderable
                }
            });

            var image = new ppmImage(width, height, 255);

            image.Colors = renderer.Render();
            System.IO.File.WriteAllBytes("../../../perspectiveTestDiffuse.ppm", image.toByteArray());
            Assert.AreEqual(847497, image.Colors.Where(x => x == Color.White).Count());
        }
        public void MeshRenderUVsAndTexture()
        {
            var mesh = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/knot3/knot3.obj"));
            var tris = mesh.Triangles;

            mesh.VertexData = mesh.VertexData.Select(x => Vector4.Multiply(x, new Vector4(1.0f, 1.0f, 1.0f, 1.0f)))
                              //scale and offset.
                              .Select(x => Vector4.Multiply(Vector4.Add(x, new Vector4(5, 5, 0, 0)), 120.0f)).ToArray();

            var ppm        = new ppmImage("../../../../../textures/testTexture2.ppm");
            var renderable = new Renderable <Mesh>(
                new DiffuseMaterial()
            {
                Shader         = new TextureShader(Matrix4x4.Identity, Matrix4x4.Identity, Matrix4x4.Identity),
                DiffuseTexture = new Texture2d(ppm.Width, ppm.Height, ppm.Colors)
            },
                mesh);
            var renderer = new Renderer2dGeneric <Mesh>(1024, 768, Color.Black, new List <IEnumerable <Renderable <Mesh> > > {
                new List <Renderable <Mesh> > {
                    renderable
                }
            });

            var image = new ppmImage(1024, 768, 255);

            image.Colors = renderer.Render();

            System.IO.File.WriteAllBytes("../../../ShaderRender2.ppm", image.toByteArray());
            //how many pixels are bluish...
            Assert.AreEqual(4053, image.Colors.Where(x => Utilities.ComputeSimpleColorDistance(x, Color.FromArgb(41, 92, 144)) < 50).Count());
        }
        public void MeshRendererCanRenderKnot_WithSimpleShaders()
        {
            var mesh = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/knot3/knot3.obj"));
            var tris = mesh.Triangles;

            mesh.VertexData = mesh.VertexData.Select(x => Vector4.Multiply(x, new Vector4(1.0f, 1.0f, 1.0f, 1.0f)))
                              //scale and offset.
                              .Select(x => Vector4.Multiply(Vector4.Add(x, new Vector4(5, 5, 0, 0)), 120.0f)).ToArray();


            var renderable = new Renderable <Mesh>(new Material()
            {
                Shader = new Shader()
            }, mesh);
            var renderer = new Renderer2dGeneric <Mesh>(1024, 768, Color.Black, new List <IEnumerable <Renderable <Mesh> > > {
                new List <Renderable <Mesh> > {
                    renderable
                }
            });

            var image = new ppmImage(1024, 768, 255);

            image.Colors = renderer.Render();
            Assert.AreEqual(736261, image.Colors.Where(x => x == Color.Black).Count());

            System.IO.File.WriteAllBytes("../../../ShaderRender1.ppm", image.toByteArray());
        }
        public void LoadPNGImageWithAlpha()
        {
            //load a ppm
            var png = PNGImage.LoadPNGFromPath("../../../../../textures/testTextureWithSomeAlpha.png");

            var ppm = new ppmImage(512, 512, 255);

            ppm.Colors = png.Colors;
            Assert.AreEqual(png.Colors.Where(x => x == Color.FromArgb(255, 216, 216, 216)).Count(), 11016);
            var savePath = "../../../../../testTextureWithSomeAlpha.ppm";

            System.IO.File.WriteAllBytes(savePath, ppm.toByteArray());
        }
Example #8
0
        public void Prespective_Normal_Map_TinyRenderReferenceAnimation()
        {
            var cameraPos = new Vector3(-.6f, .6f, 3);
            var target    = new Vector3(0, 0, 0);
            var width     = 480;
            var height    = 480;
            var view      = Matrix4x4.CreateLookAt(cameraPos, target, Vector3.UnitY);
            var proj      = Matrix4x4.CreatePerspective(1, 1, 1, 10);
            var viewport  = MatrixExtensions.CreateViewPortMatrix(0, 0, 255, width, height);

            var mesh       = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo(Path.Combine(root, "geometry_models/tiny_renderer_sample_models/african_head.obj")));
            var diffuseTex = PNGImage.LoadPNGFromPath(Path.Combine(root, "geometry_models/tiny_renderer_sample_models/african_head_diffuse2.png"));
            var normalMap  = PNGImage.LoadPNGFromPath(Path.Combine(root, "geometry_models/tiny_renderer_sample_models/african_head_nm_tangent.png"));

            var lightvals = Enumerable.Range(0, 11).Select(x => (x / 5.0) - 1.0).ToList();

            var renderable = new Renderable <Mesh>(
                new NormalMaterial()
            {
                Shader = new NormalShader(view, proj, viewport)
                {
                    ambientCoef = 10, LightDirection = new Vector3(0, 0, 1)
                },
                DiffuseTexture = new Texture2d(diffuseTex.Width, diffuseTex.Height, diffuseTex.Colors),
                NormalMap      = new Texture2d(normalMap.Width, normalMap.Height, normalMap.Colors)
            },
                mesh);
            var renderer = new Renderer3dGeneric <Mesh>(width, height, Color.White,
                                                        new List <IEnumerable <Renderable <Mesh> > > {
                new List <Renderable <Mesh> > {
                    renderable
                }
            });

            for (var i = 0; i < lightvals.Count; i++)
            {
                var mat = Matrix4x4.CreateRotationY(0.174533f * 3f);
                var transformedLightDir = Vector3.Transform(renderable.material.Shader.LightDirection, Matrix4x4.Transpose(mat));
                renderable.material.Shader.LightDirection = transformedLightDir;
                var image = new ppmImage(width, height, 255);
                image.Colors = renderer.Render();
                image.Flip();

                var path = $"../../../animation1/perspectiveTestNormalHead{i}.ppm";
                System.IO.FileInfo file = new System.IO.FileInfo(path);
                file.Directory.Create();
                System.IO.File.WriteAllBytes(path, image.toByteArray());
            }
        }
Example #9
0
        public void LoadPPMImage()
        {
            //load a ppm
            var ppm = new ppmImage("../../../../../textures/testTexture2.ppm");
            //write it back out
            var savePath = System.IO.Path.GetTempFileName();

            System.IO.File.WriteAllBytes(savePath, ppm.toByteArray());
            //load it back
            var ppm2 = new ppmImage(savePath);

            Assert.AreEqual(ppm.Width, ppm2.Width);
            Assert.AreEqual(ppm.Height, ppm2.Height);
            Assert.AreEqual(ppm.maxColorValue, ppm2.maxColorValue);
            Assert.IsTrue(ppm.Colors.SequenceEqual(ppm2.Colors));

            //cleanup
            System.IO.File.Delete(savePath);
        }
        public void lineRender2dsimpleLines()
        {
            var lines = new List <List <(Vector2, Vector2, Color)> >()
            {
                new List <(Vector2, Vector2, Color)> {
                    (new Vector2(13, 20), new Vector2(80, 40), Color.White),
                    (new Vector2(20, 13), new Vector2(40, 80), Color.White),
                    (new Vector2(80, 40), new Vector2(80, 480), Color.White),
                    (new Vector2(0, 0), new Vector2(320, 240), Color.White)
                }
            };

            var renderer = new LineRenderer2d(640, 480, lines);

            var image = new ppmImage(640, 480, 255);

            image.Colors = renderer.Render();
            Assert.AreEqual(930, image.Colors.Where(x => x == Color.White).Count());
            System.IO.File.WriteAllBytes("../../../linetest.ppm", image.toByteArray());
        }
Example #11
0
        public void LoadComplexOBJTestAsMesh2()
        {            //scale and offset all verts.
            //flip verts since image top is 0,0


            var mesh = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/knot3/knot3.obj"));
            var tris = mesh.Triangles;

            mesh.VertexData = mesh.VertexData.Select(x => Vector4.Multiply(x, new Vector4(1.0f, 1.0f, 1.0f, 1.0f)))
                              //scale and offset.
                              .Select(x => Vector4.Multiply(Vector4.Add(x, new Vector4(5, 5, 0, 0)), 120)).ToArray();

            var verts    = mesh.VertexData;
            var lines    = new List <List <(Vector2, Vector2, Color)> >();
            var trilines = tris.SelectMany(x =>

                                           new List <(Vector2, Vector2, Color)>()
            {
                (new Vector2(verts[x.vertIndexList[0] - 1].X, verts[x.vertIndexList[0] - 1].Y),
                 new Vector2(verts[x.vertIndexList[1] - 1].X, verts[x.vertIndexList[1] - 1].Y), Color.White),

                (new Vector2(verts[x.vertIndexList[1] - 1].X, verts[x.vertIndexList[1] - 1].Y),
                 new Vector2(verts[x.vertIndexList[2] - 1].X, verts[x.vertIndexList[2] - 1].Y), Color.White),

                (new Vector2(verts[x.vertIndexList[2] - 1].X, verts[x.vertIndexList[2] - 1].Y),
                 new Vector2(verts[x.vertIndexList[0] - 1].X, verts[x.vertIndexList[0] - 1].Y), Color.White),
            }
                                           );

            lines.Add(trilines.ToList());

            var renderer = new LineRenderer2d(1024, 768, lines);

            var image = new ppmImage(1024, 768, 255);

            image.Colors = renderer.Render();
            Assert.AreEqual(38703, image.Colors.Where(x => x == Color.White).Count());

            System.IO.File.WriteAllBytes("../../../objLoaderTest2.ppm", image.toByteArray());
        }
        public void TriangleRenderTeapot2_usingMeshLoadOBJ()
        {
            var mesh = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/teapot.obj"));
            var tris = mesh.Triangles;

            mesh.VertexData = mesh.VertexData.Select(x => Vector4.Multiply(x, new Vector4(1.0f, -1.0f, 1.0f, 1.0f)))
                              //scale and offset.
                              .Select(x => Vector4.Multiply(Vector4.Add(x, new Vector4(5, 5, 0, 0)), 100)).ToArray();

            var renderer = new Triangle2dRenderer(1024, 768, new List <IEnumerable <TriangleFace> > {
                tris
            });

            renderer.VertexData = mesh.VertexData.Select(x => new Vector3(x.X, x.Y, x.Z)).ToArray();

            var image = new ppmImage(1024, 768, 255);

            image.Colors = renderer.Render();
            Assert.AreEqual(677784, image.Colors.Where(x => x == Color.Red).Count());

            System.IO.File.WriteAllBytes("../../../teapotTest2.ppm", image.toByteArray());
        }
        public void Prespective_Normal_Map_TinyRenderReference()
        {
            var cameraPos = new Vector3(-.6f, .6f, 3);
            var target    = new Vector3(0, 0, 0);
            var width     = 2048;
            var height    = 2048;
            var view      = Matrix4x4.CreateLookAt(cameraPos, target, Vector3.UnitY);
            var proj      = Matrix4x4.CreatePerspective(1, 1, 1, 10);
            var viewport  = MatrixExtensions.CreateViewPortMatrix(0, 0, 255, width, height);

            var mesh       = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo("../../../../../geometry_models/tiny_renderer_sample_models/african_head.obj"));
            var diffuseTex = PNGImage.LoadPNGFromPath("../../../../../geometry_models/tiny_renderer_sample_models/african_head_diffuse2.png");
            var normalMap  = PNGImage.LoadPNGFromPath("../../../../../geometry_models/tiny_renderer_sample_models/african_head_nm_tangent.png");

            var renderable = new Renderable <Mesh>(
                new NormalMaterial()
            {
                Shader = new NormalShader(view, proj, viewport)
                {
                    ambientCoef = 10, LightDirection = new Vector3(0, 0, 1)
                },
                DiffuseTexture = new Texture2d(diffuseTex.Width, diffuseTex.Height, diffuseTex.Colors),
                NormalMap      = new Texture2d(normalMap.Width, normalMap.Height, normalMap.Colors)
            },
                mesh);
            var renderer = new Renderer3dGeneric <Mesh>(width, height, Color.White,
                                                        new List <IEnumerable <Renderable <Mesh> > > {
                new List <Renderable <Mesh> > {
                    renderable
                }
            });

            var image = new ppmImage(width, height, 255);

            image.Colors = renderer.Render();
            System.IO.File.WriteAllBytes("../../../perspectiveTestNormalHead.ppm", image.toByteArray());
            Assert.AreEqual(3362439, image.Colors.Where(x => x == Color.White).Count());
        }
        public void RenderIsFlippedCorrectly()
        {
            var cameraPos = new Vector3(-.6f, .6f, 5);
            var target    = new Vector3(0, 0, 0);
            var width     = 2048;
            var height    = 2048;
            var view      = Matrix4x4.CreateLookAt(cameraPos, target, Vector3.UnitY);
            var proj      = Matrix4x4.CreatePerspective(1, 1, 1, 10);
            var viewport  = MatrixExtensions.CreateViewPortMatrix(0, 0, 255, width, height);

            var mesh       = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo(System.IO.Path.Combine(root, "geometry_models/head_asymmetric/head.OBJ")));
            var diffuseTex = PNGImage.LoadPNGFromPath(System.IO.Path.Combine(root, "geometry_models/head_asymmetric/head.png"));

            var renderable = new Renderable <Mesh>(
                new DiffuseMaterial()
            {
                Shader = new TextureShader(view, proj, viewport)
                {
                    LightDirection = new Vector3(0, 0, 1)
                },
                DiffuseTexture = new Texture2d(diffuseTex.Width, diffuseTex.Height, diffuseTex.Colors),
            },
                mesh);
            var renderer = new Renderer3dGeneric <Mesh>(width, height, Color.White,
                                                        new List <IEnumerable <Renderable <Mesh> > > {
                new List <Renderable <Mesh> > {
                    renderable
                }
            });

            var image = new ppmImage(width, height, 255);

            image.Colors = renderer.Render();
            image.Flip();
            System.IO.File.WriteAllBytes("../../../asymmetricHead.ppm", image.toByteArray());
            Assert.AreEqual(3528584, image.Colors.Where(x => x == Color.White).Count());
        }
        public void lineRender2dArrayedCircle()
        {
            var lines = new List <List <(Vector2, Vector2, Color)> >();


            var center = new Vector2(100, 300);
            var points = Enumerable.Range(0, 360).Select(x =>
                                                         new Vector2(
                                                             (float)(center.X + 100 * Math.Cos(x * (Math.PI / 180.0))),
                                                             (float)(center.Y + 100 * Math.Sin(x * (Math.PI / 180.0))))
                                                         );
            var pairs = points.Select(x => (center, x, Color.White));

            lines.Add(pairs.ToList());

            var renderer = new LineRenderer2d(640, 480, lines);

            var image = new ppmImage(640, 480, 255);

            image.Colors = renderer.Render();
            Assert.AreEqual(24390, image.Colors.Where(x => x == Color.White).Count());

            System.IO.File.WriteAllBytes("../../../linetest_circle.ppm", image.toByteArray());
        }
        public void Prespective_Normal_Map_ZbrushMando1()
        {
            var cameraPos = new Vector3(-2f, 1, 4f);
            var target    = new Vector3(0, 0, 0);
            var width     = 2048;
            var height    = 2048;
            var view      = Matrix4x4.CreateLookAt(cameraPos, target, Vector3.UnitY);
            var proj      = Matrix4x4.CreatePerspective(1, 1, 1, 10);
            var viewport  = MatrixExtensions.CreateViewPortMatrix(0, 0, 255, width, height);

            var mesh       = ObjFileLoader.LoadMeshFromObjAtPath(new System.IO.FileInfo(Path.Combine(root, "geometry_models/mandohead/mandoclean.OBJ")));
            var diffuseTex = PNGImage.LoadPNGFromPath(Path.Combine(root, "geometry_models/mandohead/mandocleandiffuse6.png"));
            var normalMap  = PNGImage.LoadPNGFromPath(Path.Combine(root, "geometry_models/mandohead/mandocleannormal1.png"));

            var renderable = new Renderable <Mesh>(
                new NormalMaterial()
            {
                Shader = new NormalShader(view, proj, viewport)
                {
                    ambientCoef = 30, LightDirection = new Vector3(0, 0, 1f)
                },
                DiffuseTexture = new Texture2d(diffuseTex.Width, diffuseTex.Height, diffuseTex.Colors),
                NormalMap      = new Texture2d(normalMap.Width, normalMap.Height, normalMap.Colors)
            },
                mesh);
            var renderer = new Renderer3dGeneric <Mesh>(width, height, Color.White,
                                                        new List <IEnumerable <Renderable <Mesh> > > {
                new List <Renderable <Mesh> > {
                    renderable
                }
            });

            var image = new ppmImage(width, height, 255);

            image.Colors = renderer.Render();
            image.Flip();
            System.IO.File.WriteAllBytes("../../../perspectiveTestNormalMandoonecolorfront.ppm", image.toByteArray());
            Assert.AreEqual(3248415, image.Colors.Where(x => x == Color.White).Count());
        }