예제 #1
0
        public void TestDrawCamera()
        {
            var scene = new Scene();
            var cube  = new Cube()
            {
                Position = new Vector4()
                {
                    X = 0, Y = 0, Z = 1.25f, W = 1d
                }
            };

            scene.Objects = new List <Object3D>
            {
                new Pyramid()
                {
                    Position = new Vector4()
                    {
                        X = 0.7f, Y = 0, Z = 1.25f, W = 1d
                    }
                },
                cube,
                new Pyramid()
                {
                    Position = new Vector4()
                    {
                        X = -0.8f, Y = 0, Z = 3.5f, W = 1d
                    }, Mategial
                        = new Material {
                        DiffuseColor = new Vector4 {
                            X = 1, Y = 1, Z = 0, W = 1
                        }
                        }
                }
            };
            scene.LightSources = new List <LightSource> {
                new GlobalLightSource()
                {
                    Color = new Vector4(1, 0.9f, 0.65f, 1), Intensity = 1f, Position = new Vector4(1, 1, -1, 1)
                }
            };
            scene.AmbientColor = new Vector4(0, 0, 0.3f, 0);

            var form = new TestForm();

            form.ClientSize = new Size(320, 240);
            var t = new Timer();

            t.Interval = 60;
            t.Tick    += (object sender, EventArgs e) =>
            {
                cube.Rotation.Y += 0.07f;
                form.Refresh();
            };

            form.Paint += (object sender, PaintEventArgs e) =>
            {
                var renderer = new Renderer(form.ClientSize.Width, form.ClientSize.Height);
                renderer.Render(scene);

                e.Graphics.Clear(Color.Black);

                e.Graphics.DrawImage(renderer.FrameBuffer, 0, 0);
            };

            form.Load += (s, e) => { t.Enabled = true; };

            Application.Run(form);
        }
예제 #2
0
        public void DrawTriangles_SubpixelAccuracity()
        {
            var form = new TestForm();

            form.ClientSize = new Size(300, 300);

            var vertex1 = new Vector3f(1f, 1f, 0);
            var vertex2 = new Vector3f(5f, 1f, 0);
            var vertex3 = new Vector3f(5f, 5f, 0);

            form.Paint += (object sender, PaintEventArgs e) =>
            {
                e.Graphics.Clear(Color.Black);
                e.Graphics.SmoothingMode     = SmoothingMode.None;
                e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;

                Bitmap bmp     = new Bitmap(15, 15);
                var    zbuffer = new double[15 * 15];

                //Проверяем прямой порядок вершин по у
                Draw3D.SimpleRasterizeTriangle(vertex1, vertex2, vertex3, bmp, Color.Green, zbuffer);

                var imageScale = 20;

                DrawBitmap(e.Graphics, bmp, new Rectangle(0, 0, 300, 300));

                DrawGrid(new Pen(Color.Gray), e.Graphics, 15, 15, 300, 300);
                DrawBoundTriangle(new Pen(Color.Red), e.Graphics, vertex1, vertex2, vertex3, imageScale);
            };

            form.KeyDown += (object sender, KeyEventArgs e) =>
            {
                if (e.KeyCode == Keys.A)
                {
                    vertex1.X -= 0.05f;
                }
                if (e.KeyCode == Keys.D)
                {
                    vertex1.X += 0.05f;
                }

                if (e.KeyCode == Keys.W)
                {
                    vertex2.Y -= 0.05f;
                }
                if (e.KeyCode == Keys.S)
                {
                    vertex2.Y += 0.05f;
                }

                if (e.KeyCode == Keys.Left)
                {
                    vertex1.X -= 0.05f;
                    vertex2.X -= 0.05f;
                    vertex3.X -= 0.05f;
                }
                if (e.KeyCode == Keys.Right)
                {
                    vertex1.X += 0.05f;
                    vertex2.X += 0.05f;
                    vertex3.X += 0.05f;
                }

                if (e.KeyCode == Keys.Up)
                {
                    vertex1.Y -= 0.05f;
                    vertex2.Y -= 0.05f;
                    vertex3.Y -= 0.05f;
                }
                if (e.KeyCode == Keys.Down)
                {
                    vertex1.Y += 0.05f;
                    vertex2.Y += 0.05f;
                    vertex3.Y += 0.05f;
                }

                form.Refresh();
            };

            Application.Run(form);
        }