public void Draw(ZBuffer img) { var backGround = Settings.Default.CameraDefaultBackground; var clipped = _object.Select(p => p.Init).ToList(); for (var y = ZBuffer.WindowMinHeight; y < ZBuffer.WindowMaxHeight; y++) { var nodeX = new List <int>(); var points = 0; int index; var j = clipped.Count - 1; for (index = 0; index < clipped.Count; index++) { if (clipped[index].Y < y && clipped[j].Y >= y || clipped[j].Y < y && clipped[index].Y >= y) { nodeX.Insert(points++, (int)(clipped[index].X + (y - clipped[index].Y) / (clipped[j].Y - clipped[index].Y) * (clipped[j].X - clipped[index].X))); } j = index; } nodeX = nodeX.OrderBy(p => p).ToList(); for (index = 0; index < points; index += 2) { if (nodeX[index] >= ZBuffer.WindowMaxWidth) { break; } if (nodeX[index + 1] <= ZBuffer.WindowMinWidth) { continue; } if (nodeX[index] < ZBuffer.WindowMinWidth) { nodeX[index] = ZBuffer.WindowMinWidth; } if (nodeX[index + 1] >= ZBuffer.WindowMaxWidth) { nodeX[index + 1] = ZBuffer.WindowMaxWidth - 1; } for (var x = nodeX[index]; x < nodeX[index + 1]; x++) { img.SetPixel(x, y, 0, backGround); } } } //foreach (var polEdge in _object.Select(p => new Line(p.Init, p.Final))) //{ // polEdge.Draw(img); //} }
public static void DrawLine(this ZBuffer img, Point initPoint, Point endPoint, Color color) { var init = new Point(initPoint.X, initPoint.Y, initPoint.Z); var end = new Point(endPoint.X, endPoint.Y, endPoint.Z); if (!Clip(ref init, ref end, ZBuffer.MinSize, ZBuffer.MaxSize)) { return; } var x = (int)init.X; var y = (int)init.Y; var z = init.Z; var xf = (int)end.X; var yf = (int)end.Y; var dx = Math.Abs(xf - x); var sx = x < xf ? 1 : -1; var dy = Math.Abs(yf - y); var sy = y < yf ? 1 : -1; var err = (dx > dy ? dx : -dy) / 2; var txz = (end.Z - init.Z) / (dx == 0 ? 1 : dx); var tyz = (end.Z - init.Z) / (dy == 0 ? 1 : dy); while (true) { img.SetPixel(x, y, z, color); if (x == xf && y == yf) { break; } var e2 = err; if (e2 > -dx) { err -= dy; x += sx; z += txz; } if (e2 >= dy) { continue; } err += dx; y += sy; z += tyz; } }
public void Draw(ZBuffer img) { var clipped = _object.Select(p => p.Init).ToList(); for (var y = ZBuffer.WindowMinHeight; y < ZBuffer.WindowMaxHeight; y++) { var nodeX = new List <int>(); var points = 0; int index; var j = clipped.Count - 1; for (index = 0; index < clipped.Count; index++) { if (clipped[index].Y < y && clipped[j].Y >= y || clipped[j].Y < y && clipped[index].Y >= y) { nodeX.Insert(points++, (int)(clipped[index].X + (y - clipped[index].Y) / (clipped[j].Y - clipped[index].Y) * (clipped[j].X - clipped[index].X))); } j = index; } nodeX = nodeX.OrderBy(p => p).ToList(); for (index = 0; index < points; index += 2) { if (nodeX[index] >= ZBuffer.WindowMaxWidth) { break; } if (nodeX[index + 1] <= ZBuffer.WindowMinWidth) { continue; } if (nodeX[index] < ZBuffer.WindowMinWidth) { nodeX[index] = ZBuffer.WindowMinWidth; } if (nodeX[index + 1] >= ZBuffer.WindowMaxWidth) { nodeX[index + 1] = ZBuffer.WindowMaxWidth - 1; } for (var x = nodeX[index]; x < nodeX[index + 1]; x++) { img.SetPixel(x, y, 0, Color.Blue); } } } }
private static void Draw(ZBuffer img, Color color, int xmin, int xmax, int ymin, int ymax, List <Point> clipped) { for (var y = ymin; y < ymax; y++) { var inter = new List <Point>(); int index; var j = clipped.Count - 1; for (index = 0; index < clipped.Count; index++) { if (clipped[index].Y < y && clipped[j].Y >= y || clipped[j].Y < y && clipped[index].Y >= y) { var pt = DoesHit(clipped[j], clipped[index], new Point(0, y), new Point(ZBuffer.Width - 1, y)); if (pt != null) { inter.Add(pt); } } j = index; } inter = inter.OrderBy(p => p.X).ToList(); for (index = 0; index < inter.Count; index += 2) { var z = inter[index].Z; var tx = (inter[index + 1].Z - inter[index].Z) / (inter[index + 1].X - inter[index].X); var end = (int)inter[index + 1].X; for (var x = (int)inter[index].X; x < end; x++) { img.SetPixel(x, y, z, color); z += tx; } } } }