Ejemplo n.º 1
0
        private void Form1_Shown(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            var model = ModelFiles.GetAfricanHeadModel();

            //var model = ModelFiles.GetDiabloModel();

            sw.Stop();

            var renderer = new ModelRenderer(new ShadowShaderRenderer(model, pictureBox1));

            //var renderer = new ModelRenderer(new NoToneShaderRenderer(model, pictureBox1));
            //var renderer = new ModelRenderer(new ToonShaderRenderer(model, pictureBox1));
            //var renderer = new ModelRenderer(new NormalMapShaderRenderer(model, pictureBox1));
            renderer.Render();

            InfoViewer.ShowDiagnosticInfo
                ($"Loading Time: {sw.Elapsed.TotalMilliseconds:f2} ms", new PointF(10, 10), Graphics.FromImage(pictureBox1.Image));

            InfoViewer.ShowDiagnosticInfo
                ($"Triangles Count: {model.FacesV.Count}", new PointF(10, 50), Graphics.FromImage(pictureBox1.Image));

            InfoViewer.ShowDiagnosticInfo
                ($"Resolution: {pictureBox1.Width} X {pictureBox1.Height}", new PointF(10, 70), Graphics.FromImage(pictureBox1.Image));
        }
Ejemplo n.º 2
0
        public void Render()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            DirectBitmap bitmap = new DirectBitmap(width, height);

            for (int i = 0; i < z_buffer.Length; i++)
            {
                z_buffer[i] = -float.MaxValue;
            }

            Rasterizer rasterizer = new Rasterizer();

            //// start of picture render by normal map shader ////
            var nm_shader = new NormalMappingShader(model, ViewPort, Projection, ModelView, light_dir);

            nm_shader.uniform_m   = Projection * ModelView;
            nm_shader.uniform_mit = (Projection * ModelView).Inverse().Transpose();

            for (int i = 0; i < model.FacesV.Count; i++)
            {
                Vector4[] screen_coords = new Vector4[3];
                for (int j = 0; j < model.FacesV[i].Length; j++)
                {
                    screen_coords[j] = nm_shader.Vertex(i, j);
                }

                rasterizer.Triangle(screen_coords, nm_shader, bitmap, pictureBox, z_buffer);
            }
            //// end of picture render by normal map shader ////

            sw.Stop();

            bitmap.Bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);

            InfoViewer.ShowDiagnosticInfo
                ($"Render Time: {sw.Elapsed.TotalMilliseconds:f2} ms", new PointF(10, 30), Graphics.FromImage(bitmap.Bitmap));

            // output final render
            pictureBox.Image = bitmap.Bitmap;
        }
Ejemplo n.º 3
0
        public void Render()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            DirectBitmap bitmap = new DirectBitmap(width, height);

            Rasterizer rasterizer = new Rasterizer();

            //// start of picture render by toon shader ////
            var toon_shader = new GouraudShader(model, ViewPort, Projection, ModelView, light_dir);

            for (int i = 0; i < model.FacesV.Count; i++)
            {
                Vector4[] screen_coords = new Vector4[3];
                for (int j = 0; j < model.FacesV[i].Length; j++)
                {
                    screen_coords[j] = toon_shader.Vertex(i, j);
                }

                rasterizer.Triangle(screen_coords, toon_shader, bitmap, pictureBox, z_buffer);
            }
            //// end of picture render by toon shader ////

            sw.Stop();

            model.Texture.Dispose();
            bitmap.Bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);

            InfoViewer.ShowDiagnosticInfo
                ($"Render Time: {sw.Elapsed.TotalMilliseconds:f2} ms", new PointF(10, 30), Graphics.FromImage(bitmap.Bitmap));

            // output final render
            pictureBox.Image = bitmap.Bitmap;
        }
Ejemplo n.º 4
0
        public void Render()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            DirectBitmap bitmap = new DirectBitmap(width, height);

            Rasterizer rasterizer = new Rasterizer();

            //// start of shadow buffer rendering
            Matrix4x4 SB_ModelView  = Matrix4x4Extensions.LookAt(light_dir, center, up);
            Matrix4x4 SB_Projection = Matrix4x4.Identity;
            Matrix4x4 ViewPort      = Matrix4x4Extensions.Viewport(width / 8, height / 8, width * 3 / 4, height * 3 / 4);

            SB_Projection.M43 = 0;

            var shader = new DepthShader(model, ViewPort, SB_Projection, SB_ModelView);

            for (int i = 0; i < model.FacesV.Count; i++)
            {
                Vector4[] screen_coords = new Vector4[3];
                for (int j = 0; j < model.FacesV[i].Length; j++)
                {
                    screen_coords[j] = shader.Vertex(i, j);
                }

                rasterizer.Triangle(screen_coords, shader, bitmap, pictureBox, shadow_buffer, true);
            }
            //// end of shadow buffer rendering

            //// start of picture render by shadow shader ////
            Matrix4x4 M = ViewPort * SB_Projection * SB_ModelView;

            Matrix4x4 ModelView  = Matrix4x4Extensions.LookAt(eye, center, up);
            Matrix4x4 Projection = Matrix4x4.Identity;

            Projection.M43 = -1.0f / (eye - center).Norm();

            var sh_shader = new ShadowShader(model, ViewPort, Projection, ModelView, light_dir, shadow_buffer, width);

            sh_shader.uniform_m        = Projection * ModelView;
            sh_shader.uniform_mit      = sh_shader.uniform_m.Inverse().Transpose();
            sh_shader.uniform_m_shadow = M * (ViewPort * Projection * ModelView).Inverse();

            for (int i = 0; i < model.FacesV.Count; i++)
            {
                Vector4[] screen_coords = new Vector4[3];
                for (int j = 0; j < model.FacesV[i].Length; j++)
                {
                    screen_coords[j] = sh_shader.Vertex(i, j);
                }

                rasterizer.Triangle(screen_coords, sh_shader, bitmap, pictureBox, z_buffer);
            }
            //// end of picture render by shadow shader ////

            sw.Stop();

            model.Texture.Dispose();
            bitmap.Bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);

            InfoViewer.ShowDiagnosticInfo
                ($"Render Time: {sw.Elapsed.TotalMilliseconds:f2} ms", new PointF(10, 30), Graphics.FromImage(bitmap.Bitmap));

            // output final render
            pictureBox.Image = bitmap.Bitmap;
        }