Exemplo n.º 1
0
 private void Form1_Load(object sender, EventArgs e)
 {
     tester.INIT();
     backbuffer = new byte[img.Width, img.Height, 3];
     tester[0]  = new Utils.Vec(new double[] { 20, 20, 0 });
     tester[1]  = new Utils.Vec(new double[] { 170, 70, 127 });
     tester[2]  = new Utils.Vec(new double[] { 70, 170, 255 });
     GameLoop();
 }
Exemplo n.º 2
0
            public void Fill()
            {
                Utils.Vec p1 = this[0];
                Utils.Vec p2 = this[1];
                Utils.Vec p3 = this[2];

                Utils.Vec t = p3;

                if (p3.y < p2.y)
                {
                    p3 = p2;
                    p2 = t;
                }
                if (p2.y < p1.y)
                {
                    t  = p2;
                    p2 = p1;
                    p1 = t;
                }
                if (p3.y < p2.y)
                {
                    t  = p3;
                    p3 = p2;
                    p2 = t;
                }

                double q = 0;
                double p = 0;

                double dy1 = p2.y - p1.y;
                double dy2 = p3.y - p1.y;
                double dy3 = p3.y - p2.y;

                if (p1.y != p2.y)
                {
                    for (int y = (int)p1.y; y <= (int)p2.y; y++)
                    {
                        double ay1 = (y - p1.y) / dy1;
                        double ay2 = (y - p1.y) / dy2;

                        double x1 = (1 - ay1) * p1.x + ay1 * p2.x;
                        double x2 = (1 - ay2) * p1.x + ay2 * p3.x;

                        q = Math.Min(x1, x2);
                        p = Math.Max(x1, x2);

                        x1 = q;
                        x2 = p;

                        double z1 = (1 - ay1) * p1.z + ay1 * p2.z;
                        double z2 = (1 - ay2) * p1.z + ay2 * p3.z;

                        double dx = x2 - x1;

                        for (int x = (int)x1; x <= (int)x2; x++)
                        {
                            double ax = (x - x1) * (1 / dx);

                            double z = 255 - ((1 - ax) * z1 + ax * z2);

                            backbuffer[x, y, 0] = (byte)z;
                            backbuffer[x, y, 1] = (byte)z;
                            backbuffer[x, y, 2] = (byte)z;
                        }
                    }
                }
                if (p2.y != p3.y)
                {
                    for (int y = (int)p2.y; y <= (int)p3.y; y++)
                    {
                        double ay1 = (y - p2.y) / dy3;
                        double ay2 = (y - p1.y) / dy2;

                        double x1 = (1 - ay1) * p2.x + ay1 * p3.x;
                        double x2 = (1 - ay2) * p1.x + ay2 * p3.x;

                        q = Math.Min(x1, x2);
                        p = Math.Max(x1, x2);

                        x1 = q;
                        x2 = p;

                        double z1 = (1 - ay1) * p2.z + ay1 * p3.z;
                        double z2 = (1 - ay2) * p1.z + ay2 * p3.z;

                        double dx = x2 - x1;

                        for (int x = (int)x1; x <= (int)x2; x++)
                        {
                            double ax = (x - x1) * (1 / dx);

                            double z = 255 - ((1 - ax) * z1 + ax * z2);

                            backbuffer[x, y, 0] = (byte)z;
                            backbuffer[x, y, 1] = (byte)z;
                            backbuffer[x, y, 2] = (byte)z;
                        }
                    }
                }
            }