Esempio n. 1
0
 void DrawTriangle(Point p1, Point p2, Point p3, WorkScene scene, Color color)
 {
     if (p1.y > p2.y)
     {
         Swap(ref p1, ref p2);
     }
     if (p1.y > p3.y)
     {
         Swap(ref p1, ref p3);
     }
     if (p2.y > p3.y)
     {
         Swap(ref p2, ref p3);
     }
     if (!scene.fill)
     {
         DrawLine(p1.x, p1.y, p2.x, p2.y, scene, color);
         DrawLine(p2.x, p2.y, p3.x, p3.y, scene, color);
         DrawLine(p3.x, p3.y, p1.x, p1.y, scene, color);
     }
     else
     {
         int total_height = (int)(p3.y - p1.y);
         for (int i = 0; i < total_height; i++)
         {
             bool  second_half    = i > p2.y - p1.y || p2.y == p1.y;
             int   segment_height = second_half ? (int)(p3.y - p2.y) : (int)(p2.y - p1.y);
             float alpha          = (float)i / total_height;
             float beta           = (float)(i - (second_half ? p2.y - p1.y : 0)) / segment_height;
             Point A = p1 + (p3 - p1) * alpha;
             Point B = second_half ? p2 + (p3 - p2) * beta : p1 + (p2 - p1) * beta;
             if (A.x > B.x)
             {
                 Swap(ref A, ref B);
             }
             for (int j = (int)A.x; j <= B.x; j++)
             {
                 double phi = B.x == A.x ? 1 : (j - A.x) / (B.x - A.x);
                 Point  P   = new Point(A) + new Point(B - A) * phi;
                 P.x = j; P.y = p1.y + i;
                 int idx = (int)(P.x + P.y * scene.bmp.Width);
                 if (P.x >= scene.bmp.Width || P.x < 0 || P.y >= scene.bmp.Height || P.y < 0 || new Line(scene.camera.eye, P).Length < scene.camera.near || new Line(scene.camera.eye, P).Length > scene.camera.far)
                 {
                     continue;
                 }
                 try
                 {
                     if (scene.zBuf[idx] < P.z)
                     {
                         scene.zBuf[idx] = (int)P.z;
                         scene.bmp.SetPixel((int)P.x, (int)P.y, color);
                     }
                 }
                 catch { }
             }
         }
     }
 }
Esempio n. 2
0
        void DrawLine(Point p1, Point p2, WorkScene scene, Color color)
        {
            double x1 = p1.x;
            double y1 = p1.y;
            double x2 = p2.x;
            double y2 = p2.y;

            bool steep = false;

            if (Math.Abs(x1 - x2) < Math.Abs(y1 - y2))
            {
                Swap(ref x1, ref y1);
                Swap(ref x2, ref y2);
                steep = true;
            }
            if (x1 > x2)
            {
                Swap(ref x1, ref x2);
                Swap(ref y1, ref y2);
            }
            int dx      = (int)(x2 - x1);
            int dy      = (int)(y2 - y1);
            int derror2 = Math.Abs(dy) * 2;
            int error2  = 0;
            int y       = (int)y1;

            for (int x = (int)x1; x <= x2; x++)
            {
                if (steep)
                {
                    try
                    {
                        scene.bmp.SetPixel(y, x, color);
                    }
                    catch { }
                }
                else
                {
                    try
                    {
                        scene.bmp.SetPixel(x, y, color);
                    }
                    catch { }
                }
                error2 += derror2;

                if (error2 > dx)
                {
                    y      += (y2 > y1 ? 1 : -1);
                    error2 -= dx * 2;
                }
            }
        }
Esempio n. 3
0
 void DrawTriangle(Triangle triangle, WorkScene scene)
 {
     DrawTriangle(triangle.Points[0], triangle.Points[1], triangle.Points[2], scene, triangle.Color);
 }
Esempio n. 4
0
 public MainForm()
 {
     InitializeComponent();
     scene = new WorkScene(PBox.Width, PBox.Height);
     Redraw();
 }
Esempio n. 5
0
 void DrawLine(double x1, double y1, double x2, double y2, WorkScene scene, Color color)
 {
     DrawLine(new Point(x1, y1, 0), new Point(x2, y2, 0), scene, color);
 }