public static IEnumerable <Pixel> GetPixels(int x1, int y1, float z1, int x2, int y2, float z2, int width, int height, Bgr24Bitmap bitmap, ZBuffer zBuf, Color color) { List <Pixel> list = new List <Pixel>(); int deltaX = Math.Abs(x2 - x1); int deltaY = Math.Abs(y2 - y1); float deltaZ = Math.Abs(z2 - z1); int signX = x1 < x2 ? 1 : -1; int signY = y1 < y2 ? 1 : -1; float signZ = z1 < z2 ? 1 : -1; float curZ = z1; float z3 = deltaZ / deltaY; int error = deltaX - deltaY; list.Add(new Pixel(x2, y2, z2)); if (x2 > 0 && x2 < zBuf.Width && y2 > 0 && y2 < zBuf.Height) { if (z2 <= zBuf[x2, y2]) { //if (z2 > 0 && z2 < 1) //{ // zBuf[x2, y2] = z2; // bitmap[x2, y2] = Color.FromRgb(33, 105, 72); //new Vector4(0, 0, 255, 255); //} zBuf[x2, y2] = z2; bitmap[x2, y2] = color; //Color.FromRgb(0, 0, 255); //new Vector4(0, 0, 255, 255); } } while (x1 != x2 || y1 != y2) { list.Add(new Pixel(x1, y1, curZ)); if (x1 > 0 && x1 < zBuf.Width && y1 > 0 && y1 < zBuf.Height) { if (curZ <= zBuf[(int)x1, (int)y1]) { //if (curZ > 0 && curZ < 1) //{ // zBuf[(int)x1, (int)y1] = curZ; //!!!! curZ????? point2.Z // bitmap[(int)x1, (int)y1] = Color.FromRgb(0, 0, 255); //} zBuf[(int)x1, (int)y1] = curZ; //!!!! curZ????? point2.Z bitmap[(int)x1, (int)y1] = color; //Color.FromRgb(0, 0, 255); } } int error2 = error * 2; if (error2 > -deltaY) { error -= deltaY; x1 += signX; } if (error2 < deltaX) { error += deltaX; y1 += signY; curZ += signZ * z3; } } return(list); }
public static IEnumerable <Pixel> DrawPixelForRasterization(List <Pixel> sidesList, Bgr24Bitmap bitmap, ZBuffer zBuf, Color color) { List <Pixel> list = new List <Pixel>(); int minY, maxY; Pixel xStartPixel, xEndPixel; FindMinAndMaxY(sidesList, out minY, out maxY); for (int y = minY + 1; y < maxY; y++) { FindStartAndEndXByY(sidesList, y, out xStartPixel, out xEndPixel); int signZ = xStartPixel.Z < xEndPixel.Z ? 1 : -1; float deltaZ = Math.Abs((xEndPixel.Z - xStartPixel.Z) / (float)((xEndPixel.X) - (xStartPixel.X))); float curZ = xStartPixel.Z; for (int x = xStartPixel.X + 1; x < xEndPixel.X; x++) { curZ += signZ * deltaZ; if (x > 0 && x < zBuf.Width && y > 0 && y < zBuf.Height) { if (curZ <= zBuf[x, y]) { zBuf[x, y] = curZ; list.Add(new Pixel(x, y, (int)curZ)); bitmap[x, y] = color; //Color.FromRgb(33, 105, 72); //new Vector4(255, 0, 0, 255); } } } } return(list); }