コード例 #1
0
        internal Bitmap Draw(Bitmap bmp, List <Light> lights, Point worldCenter, Camera camera)
        {
            TransformModel(Translation + new Vector(worldCenter.X, worldCenter.Y, 0), Scale, Rotation, camera.ProjectionValue);
            CompleteModelDraw(lights);
            var ret = new Bitmap(RenderedColors.Bitmap);

            RenderedColors.Dispose();
            return(ret);
        }
コード例 #2
0
        private void CompleteTriangleDraw(Triangle t, List <Light> lights)
        {
            Vector v = t.Norm;
            double A = v.X, B = v.Y, C = v.Z;
            var    D    = -(A * t.V1.X + B * t.V1.Y + C * t.V1.Z);
            int    maxX = (int)Math.Max(t.V1.X, Math.Max(t.V2.X, t.V3.X)); //
            int    minX = (int)Math.Min(t.V1.X, Math.Min(t.V2.X, t.V3.X)); //прямоугольник, который ограничивает треугольник
            var    maxY = (int)Math.Max(t.V1.Y, Math.Max(t.V2.Y, t.V3.Y)); //
            var    minY = (int)Math.Min(t.V1.Y, Math.Min(t.V2.Y, t.V3.Y)); //

            //движемся по пикселям внутри треугольника и проверяем, принадлежит ли конкретный пиксель треугольнику
            for (var x = minX; x <= maxX; x++)
            {
                for (var y = minY; y <= maxY; y++)
                {
                    var z       = -(A * x + B * y + D) / C;
                    var xResult = x;
                    var yResult = -y;
                    if (IsPointInsideTriangle(x, y, t, out var a,
                                              out var b, out var g) && yResult > 0 && xResult > 0)
                    {
                        if (xResult <= ZBuffer.GetUpperBound(0) && yResult <= ZBuffer.GetUpperBound(1) && z > ZBuffer[xResult, yResult])
                        {
                            var texel = TextureMap != null?FindTexel(t.C1, t.C2, t.C3, a, b, g) : Color.CornflowerBlue;

                            List <Color> texels = new List <Color>();
                            foreach (var light in lights)
                            {
                                texels.Add(light.GetPixelColor(t.V1.VNormal, t.V2.VNormal, t.V3.VNormal, texel,
                                                               a, b, g));
                            }
                            texel = Light.GetItogTexel(texels);

                            if (RenderedColors.Bits.Length > xResult * yResult)
                            {
                                RenderedColors.SetPixel(xResult, yResult, texel);
                                ZBuffer[xResult, yResult] = (float)z;
                            }
                        }
                        else
                        {
                        }
                    }
                }
            }
        }