public static void line3(int x0, int y0, int x1, int y1, Image2D image, ColorRGB color) { bool steep = false; if (Math.Abs(x0 - x1) < Math.Abs(y0 - y1)) { swap(ref x0, ref y0); swap(ref x1, ref y1); steep = true; } if (x0 > x1) { swap(ref x0, ref x1); swap(ref y0, ref y1); } for (int x = x0; x <= x1; x++) { float t = (x - x0) / (float)(x1 - x0); int y = Convert.ToInt32(y0 * (1 - t) + y1 * t); if (steep) { image.setPixel(y, x, color); } else { image.setPixel(x, y, color); } } }
private void button5_Click(object sender, EventArgs e) { Image2D imageLines = new Image2D(200, 200); int type = Convert.ToInt32(textBox3.Text); switch (type) { case 1: line = ImageProcessor.line1; break; case 2: line = ImageProcessor.line2; break; case 3: line = ImageProcessor.line3; break; case 4: line = ImageProcessor.line4; break; } for (int i = 0; i < 13; i++) { double alpha = 2 * i * Math.PI / 13; line(100, 100, Convert.ToInt32(100 + 95 * Math.Cos(alpha)), Convert.ToInt32(100 + 95 * Math.Sin(alpha)), imageLines, new ColorRGB(255, 0, 0)); } Bitmap resultImageLines = ImageProcessor.Image2DtoBitmap(imageLines); pictureBox1.Image = resultImageLines; }
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); } } } } }
private void button2_Click(object sender, EventArgs e) { int height = Convert.ToInt32(textBox1.Text); int widht = Convert.ToInt32(textBox2.Text); Image2D whiteImage = new Image2D(widht, height); int[,] white = new int[widht, 255]; for (int i = 0; i < widht; i++) { for (int j = 0; j < height; j++) { white[i, j] = 255; } } for (int i = 0; i < widht; i++) { for (int j = 0; j < height; j++) { whiteImage.setPixel(i, j, new ColorRGB(white[i, j], white[i, j], white[i, j])); } } Bitmap resultBlackImage = ImageProcessor.Image2DtoBitmap(whiteImage); pictureBox1.Image = resultBlackImage; }
public static void line2(int x0, int y0, int x1, int y1, Image2D image, ColorRGB color) { for (int x = x0; x <= x1; x++) { float t = (x - x0) / (float)(x1 - x0); int y = Convert.ToInt32(y0 * (1 - t) + y1 * t); image.setPixel(x, y, color); } }
public static void line1(int x0, int y0, int x1, int y1, Image2D image, ColorRGB color) { for (float t = 0.0F; t < 1.0F; t += 0.01F) { int x = Convert.ToInt32(x0 * (1 - t) + x1 * t); int y = Convert.ToInt32(y0 * (1 - t) + y1 * t); image.setPixel(x, y, color); } }
private void rawPoints(object sender, EventArgs e) { Image2D pointsImage = new Image2D(1000, 1000); foreach (Point3D temp in points) { pointsImage.setPixel(temp.X, temp.Y, new ColorRGB(255, 0, 0)); } Bitmap image = ImageProcessor.Image2DtoBitmap(pointsImage); pictureBox1.Image = image; }
public static Bitmap Image2DtoBitmap(Image2D image2D) { Bitmap result = new Bitmap(image2D.Width, image2D.Height); for (int x = 0; x < image2D.Width; x++) { for (int y = 0; y < image2D.Height; y++) { result.SetPixel(x, y, ImageProcessor.ColorRGBtoColor(image2D.getColor(x, y))); } } return(result); }
private void rawTriangles(object sender, EventArgs e) { Image2D polygonsImage = new Image2D(1000, 1000); Random random = new Random(); foreach (Polygon pol in polygons) { ImageProcessor.rawTriangle(pol, polygonsImage, new ColorRGB(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255))); } Bitmap image = ImageProcessor.Image2DtoBitmap(polygonsImage); pictureBox1.Image = image; }
private void rawPolygons(object sender, EventArgs e) { Image2D polygonsImage = new Image2D(1700, 1500); line = ImageProcessor.line4; foreach (Polygon pol in polygons) { line(pol[0].X, pol[0].Y, pol[1].X, pol[1].Y, polygonsImage, new ColorRGB(0, 0, 255)); line(pol[0].X, pol[0].Y, pol[2].X, pol[2].Y, polygonsImage, new ColorRGB(0, 0, 255)); line(pol[1].X, pol[1].Y, pol[2].X, pol[2].Y, polygonsImage, new ColorRGB(0, 0, 255)); } Bitmap image = ImageProcessor.Image2DtoBitmap(polygonsImage); pictureBox1.Image = image; }
private void button3_Click(object sender, EventArgs e) { int height = Convert.ToInt32(textBox1.Text); int widht = Convert.ToInt32(textBox2.Text); Image2D redImage = new Image2D(widht, height); for (int i = 0; i < widht; i++) { for (int j = 0; j < height; j++) { redImage.setPixel(i, j, new ColorRGB(255, 0, 0)); } } Bitmap resultBlackImage = ImageProcessor.Image2DtoBitmap(redImage); pictureBox1.Image = resultBlackImage; }
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; }
private void basicLighting(object sender, EventArgs e) { Image2D polygonsImage = new Image2D(1000, 1000); foreach (Polygon pol in polygons) { if (MatrixUtil.cosDirectionEarthNormal(pol) >= 0) { continue; } ImageProcessor.rawTriangle(pol, polygonsImage, new ColorRGB((int)Math.Abs(MatrixUtil.cosDirectionEarthNormal(pol) * 255), 0, 0)); } Bitmap image = ImageProcessor.Image2DtoBitmap(polygonsImage); pictureBox1.Image = image; }
public static void line4(int x0, int y0, int x1, int y1, Image2D image, ColorRGB color) { bool steep = false; if (Math.Abs(x0 - x1) < Math.Abs(y0 - y1)) { swap(ref x0, ref y0); swap(ref x1, ref y1); steep = true; } if (x0 > x1) { swap(ref x0, ref x1); swap(ref y0, ref y1); } int dx = x1 - x0; int dy = y1 - y0; float derror = Math.Abs(dy / (float)dx); float error = 0; int y = y0; for (int x = x0; x <= x1; x++) { if (steep) { image.setPixel(y, x, color); } else { image.setPixel(x, y, color); } error += derror; if (error > 0.5) { y += (y1 > y0 ? 1 : -1); error -= 1.0F; } } }
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)); } } } } }