private void Guru(object sender, EventArgs e) { Image2D polygonsImage = new Image2D(1000, 1000); ZBuffer zBuffer = new ZBuffer(1000, 1000); foreach (Polygon pol in polygons) { if (MatrixUtil.cosDirectionEarthNormal(pol) >= 0) { continue; } ImageProcessor.rawTriangleWithGuruAndZBuffer(pol, polygonsImage, zBuffer); } Bitmap image = ImageProcessor.Image2DtoBitmap(polygonsImage); pictureBox1.Image = image; }
public static void rawTriangleWithGuruAndZBuffer(Polygon polygon, Image2D image2D, ZBuffer zBuffer) { int xMin = Math.Min(Math.Min(polygon[0].X, polygon[1].X), polygon[2].X) < 0 ? 0 : Math.Min(Math.Min(polygon[0].X, polygon[1].X), polygon[2].X); int xMax = Math.Max(Math.Max(polygon[0].X, polygon[1].X), polygon[2].X) > image2D.Width ? image2D.Width : Math.Max(Math.Max(polygon[0].X, polygon[1].X), polygon[2].X); int yMin = Math.Min(Math.Min(polygon[0].Y, polygon[1].Y), polygon[2].Y) < 0 ? 0 : Math.Min(Math.Min(polygon[0].Y, polygon[1].Y), polygon[2].Y); int yMax = Math.Max(Math.Max(polygon[0].Y, polygon[1].Y), polygon[2].Y) > image2D.Height ? image2D.Height : Math.Max(Math.Max(polygon[0].Y, polygon[1].Y), polygon[2].Y); BarycentricPoint barycentricPoint = new BarycentricPoint(polygon); double l0 = MatrixUtil.cosDirectionEarthNormal(polygon.Norms[0]); double l1 = MatrixUtil.cosDirectionEarthNormal(polygon.Norms[1]); double l2 = MatrixUtil.cosDirectionEarthNormal(polygon.Norms[2]); for (int x = xMin; x < xMax; x++) { for (int y = yMin; y < yMax; y++) { barycentricPoint.calculateLambds(new Point3D(x, y)); if (barycentricPoint.Lambda0 >= 0 && barycentricPoint.Lambda1 >= 0 && barycentricPoint.Lambda2 >= 0) { double z = barycentricPoint.Lambda0 * polygon[0].Z + barycentricPoint.Lambda1 * polygon[1].Z + barycentricPoint.Lambda2 * polygon[2].Z; if (z < zBuffer.getZBuffer(x, y)) { int brightness = (int)Math.Abs((255 * (barycentricPoint.Lambda0 * l0 + barycentricPoint.Lambda1 * l1 + barycentricPoint.Lambda2 * l2))); zBuffer.setZBuffer(x, y, z); image2D.setPixel(x, y, new ColorRGB(brightness, brightness, brightness)); } } } } }
public static void rawTriangleWithZBuffer(Polygon polygon, Image2D image2D, ColorRGB colorRgb, ZBuffer zBuffer) { int xMin = Math.Min(Math.Min(polygon[0].X, polygon[1].X), polygon[2].X) < 0 ? 0 : Math.Min(Math.Min(polygon[0].X, polygon[1].X), polygon[2].X); int xMax = Math.Max(Math.Max(polygon[0].X, polygon[1].X), polygon[2].X) > image2D.Width ? image2D.Width : Math.Max(Math.Max(polygon[0].X, polygon[1].X), polygon[2].X); int yMin = Math.Min(Math.Min(polygon[0].Y, polygon[1].Y), polygon[2].Y) < 0 ? 0 : Math.Min(Math.Min(polygon[0].Y, polygon[1].Y), polygon[2].Y); int yMax = Math.Max(Math.Max(polygon[0].Y, polygon[1].Y), polygon[2].Y) > image2D.Height ? image2D.Height : Math.Max(Math.Max(polygon[0].Y, polygon[1].Y), polygon[2].Y); BarycentricPoint barycentricPoint = new BarycentricPoint(polygon); for (int x = xMin; x < xMax; x++) { for (int y = yMin; y < yMax; y++) { barycentricPoint.calculateLambds(new Point3D(x, y)); if (barycentricPoint.Lambda0 >= 0 && barycentricPoint.Lambda1 >= 0 && barycentricPoint.Lambda2 >= 0) { double z = barycentricPoint.Lambda0 * polygon[0].Z + barycentricPoint.Lambda1 * polygon[1].Z + barycentricPoint.Lambda2 * polygon[2].Z; if (z < zBuffer.getZBuffer(x, y)) { zBuffer.setZBuffer(x, y, z); image2D.setPixel(x, y, colorRgb); } } } } }