/// <summary> /// Draw a (filled) triangle to the framebuffer /// </summary> /// <remarks>http://www-users.mat.uni.torun.pl/~wrona/3d_tutor/tri_fillers.html</remarks> /// <param name="vertices"></param> /// <param name="color"></param> /// <param name="fill"></param> public void DrawTriangle(Model model, Triangle triangle) { Vertex[] sortedv = new Vertex[3]; triangle.Vertices.CopyTo(sortedv, 0); Array.Sort(sortedv, delegate(Vertex v1, Vertex v2) { return v1.Projected.Y.CompareTo(v2.Projected.Y); }); Vector3 a = sortedv[0].Projected; Vector3 b = sortedv[1].Projected; Vector3 c = sortedv[2].Projected; double dx1 = (b.Y - a.Y > 0) ? (double)(b.X - a.X) / (double)(b.Y - a.Y) : 0; double dx2 = (c.Y - a.Y > 0) ? (double)(c.X - a.X) / (double)(c.Y - a.Y) : 0; double dx3 = (c.Y - b.Y > 0) ? (double)(c.X - b.X) / (double)(c.Y - b.Y) : 0; double ex = (double)a.X; double sx = (double)a.X; double sy = (double)a.Y; for (; sy <= b.Y; sy++, sx += dx1, ex += dx2) { DrawTriangleScanline(sx, ex, sy, model.LineColor, triangle.ShadedFillColor); } sx = b.X; for (; sy <= c.Y; sy++, sx += dx3, ex += dx2) { DrawTriangleScanline(sx, ex, sy, model.LineColor, triangle.ShadedFillColor); } }
public Triangle(Vertex a, Vertex b, Vertex c) { Vertices = new Vertex[3] { a, b, c }; }