public static bool pixelIsInsideTriangle(int x, int y, Vector3[] triPoints)
        {
            var pt1        = triPoints[0];
            var pt2        = triPoints[1];
            var pt3        = triPoints[2];
            var barycenter = TriangleExtensions.BaryCoordinates2(x, y, pt1.ToVector2(), pt2.ToVector2(), pt3.ToVector2());

            //only in the triangle if coefs are all positive.

            if (barycenter.X < 0 || barycenter.X > 1f || barycenter.Y < 0 || barycenter.Y > 1f || barycenter.Z < 0 || barycenter.Z > 1f)
            {
                return(false);
            }
            return(true);
        }
        public static bool pixelIsInsideTriangle(int x, int y, TriangleFace triangle, Vector3[] vectors)
        {
            var pt1 = vectors[triangle.vertIndexList[0] - 1];
            var pt2 = vectors[triangle.vertIndexList[1] - 1];
            var pt3 = vectors[triangle.vertIndexList[2] - 1];

            var barycenter = TriangleExtensions.BaryCoordinates2(x, y, pt1.ToVector2(), pt2.ToVector2(), pt3.ToVector2());

            //only in the triangle if coefs are all positive.

            if (barycenter.X < 0 || barycenter.X >= 1f || barycenter.Y < 0 || barycenter.Y >= 1f || barycenter.Z < 0 || barycenter.Z >= 1f)
            {
                return(false);
            }
            return(true);
        }
        //TODO should probably be Vector4
        public static void drawTriangle(int triIndex, Vector3[] screenCords, Material material, double[] zbuffer, Color[] imageBuffer, int imageBufferWidth)
        {
            var minx = screenCords.Select(x => x.X).Min();
            var miny = screenCords.Select(x => x.Y).Min();
            var maxx = screenCords.Select(x => x.X).Max();
            var maxy = screenCords.Select(x => x.Y).Max();

            var A = screenCords[0];
            var B = screenCords[1];
            var C = screenCords[2];

            Enumerable.Range((int)minx, (int)(maxx - minx) + 2).ToList().ForEach(x =>
            {
                Enumerable.Range((int)miny, (int)(maxy - miny) + 2).ToList().ForEach(y =>
                {
                    var IsInsideTriangle = pixelIsInsideTriangle(x, y, screenCords);
                    var bary             = TriangleExtensions.BaryCoordinates2(x, y,
                                                                               A.ToVector2(), B.ToVector2(), C.ToVector2());
                    //compute the depth of current pixel.
                    var z = bary.X * A.Z + bary.Y * B.Z + bary.Z * C.Z;
                    if (IsInsideTriangle)
                    {
                        var flatIndex = imageBufferWidth * (int)y + (int)x;
                        //don't draw unless we are within bounds
                        //don't draw if something is already in the depth buffer for this pixel.
                        if (flatIndex < imageBuffer.Length && flatIndex > -1 /*&& z < depthBuffer[flatIndex]*/)
                        {
                            //only draw if nothing else is closer in the depth buffer and the shader does not ignore this pixel.
                            Color diffColor;
                            if (z < zbuffer[flatIndex] && material.Shader.FragmentToRaster(material, bary, ref diffColor))
                            {
                                imageBuffer[flatIndex] = diffColor;
                                zbuffer[flatIndex]     = z;
                            }
                        }
                    }
                });
            });
        }