Ejemplo n.º 1
0
        public static void DrawWiredPolygon(this ZBuffer img, Color color, List <Point> points)
        {
            if (points.Count < 3)
            {
                return;
            }

            var a      = points[2] - points[1];
            var b      = points[0] - points[1];
            var normal = b.VectorialProduct(a);

            normal.Normalize();

            if (normal.Z > 0)
            {
                return;
            }

            var p = points.Last();

            foreach (var point in points)
            {
                img.DrawLine(p, point, color);
                p = point;
            }
        }
Ejemplo n.º 2
0
        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);
            //}
        }
Ejemplo n.º 3
0
        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;
            }
        }
Ejemplo n.º 4
0
 public Camera(Point p, Point vrp, Vector viewUp)
 {
     _canvas          = new ZBuffer();
     _p               = p;
     _vrp             = vrp;
     _worldMaxWidth   = ZBuffer.Width * 0.8;
     _worldMaxWidth  -= _worldMaxWidth / 2;
     _worldMaxHeight  = ZBuffer.Height * 0.8;
     _worldMaxHeight -= _worldMaxHeight / 2;
     _worldMinWidth   = -_worldMaxWidth;
     _worldMinHeight  = -_worldMaxHeight;
     _viewUp          = viewUp;
     _drawAction      = new OccultWire(this);
 }
Ejemplo n.º 5
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public static void DrawPolygon(this ZBuffer img, Color color, params Point[] points)
        {
            if (points.Length < 3)
            {
                return;
            }

            var a      = points[2] - points[1];
            var b      = points[0] - points[1];
            var normal = b.VectorialProduct(a);

            normal.Normalize();

            if (normal.Z <= 0)
            {
                return;
            }

            var poly = Clip(points, ZBuffer.MinSize, ZBuffer.MaxSize);

            var last = poly.Last();

            var ymax = 0;
            var ymin = ZBuffer.Height;
            var xmax = 0;
            var xmin = ZBuffer.Width;

            foreach (var point in poly)
            {
                if (point.X < xmin)
                {
                    xmin = (int)point.X;
                }
                if (point.X > xmax)
                {
                    xmax = (int)point.X;
                }
                if (point.Y < ymin)
                {
                    ymin = (int)point.Y;
                }
                if (point.Y > ymax)
                {
                    ymax = (int)point.Y;
                }
            }

            Draw(img, color, xmin, xmax, ymin, ymax, poly);
        }
Ejemplo n.º 7
0
        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;
                    }
                }
            }
        }