private void DrawInterpolatedColorLine(PointF p1, PointF p2, PointF[] points, MyVector[] colors, float polygonArea)
        {
            int scanLine = (int)(p1.Y);

            for (int j = (int)p1.X; j <= p2.X; j++)
            {
                MyVector v = new MyVector();
                for (int i = 0; i < points.Length; i++)
                {
                    v += colors[i] * (PolygonArea(points[(i + 1) % points.Length], points[(i + 2) % points.Length], new PointF(j, scanLine)) / polygonArea);
                }
                myDirectBitmap.SetPixel(j, scanLine, v.ToColor());
            }
        }
        private void GenerateMyBubbleMesh()
        {
            MyBubbleMesh = new MyVector[BUBBLE_RADIUS * 2, BUBBLE_RADIUS * 2];
            float dx, dy, z;

            for (int x = 0; x < BUBBLE_RADIUS * 2; x++)
            {
                for (int y = 0; y < BUBBLE_RADIUS * 2; y++)
                {
                    dx = BUBBLE_RADIUS - x;
                    dy = BUBBLE_RADIUS - y;
                    z  = (float)Math.Sqrt(BUBBLE_RADIUS * BUBBLE_RADIUS - dx * dx - dy * dy);
                    MyBubbleMesh[x, y] = MyVector.NormalizeVector(dx, dy, z);
                }
            }
        }
Beispiel #3
0
        private void LightColorPictureBox_Click(object sender, EventArgs e)
        {
            ColorDialog MyDialog = new ColorDialog();

            MyDialog.AllowFullOpen = false;
            MyDialog.ShowHelp      = true;
            MyDialog.Color         = FillColorPictureBox.BackColor;
            if (MyDialog.ShowDialog() == DialogResult.OK)
            {
                currentLightColor = MyDialog.Color;
                LightColorPictureBox.BackColor = MyDialog.Color;
                IL = new MyVector(currentLightColor);
                if (LightColorCustomRadioButton.Checked)
                {
                    RedrawBitmap();
                }
            }
        }
 public static float ScalarProduct(MyVector v1, MyVector v2)
 {
     return(v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
 }
Beispiel #5
0
        private void FillPolygonWithVerticlesSorting(params PointF[] points)
        {
            MyVector[] colors      = null;
            float      polygonArea = 0;

            if (FillModeInterpolateRadioButton.Checked)
            {
                colors = new MyVector[points.Length];
                for (int i = 0; i < colors.Length; i++)
                {
                    colors[i] = new MyVector(GetRealColorOfPoint((int)points[i].X, (int)points[i].Y));
                }
                polygonArea = PolygonArea(points);
            }

            List <AETNode> AET = new List <AETNode>();

            int[] indexes = GetIndexesOfSortedPoints(points);
            int   ymin    = (int)points[indexes[0]].Y;
            int   iter    = 0;

            for (int scanLine = ymin; scanLine < DrawingArea.Height; scanLine++)
            {
                //Add new nodes
                while (Math.Abs(scanLine - points[indexes[iter]].Y) < 1)
                {
                    int idxcurr = indexes[iter];
                    int idxnext = (indexes[iter] + 1) % points.Length;
                    int idxprev = (indexes[iter] - 1 + points.Length) % points.Length;

                    if (points[idxprev].Y >= scanLine)
                    {
                        AET.Add(new AETNode(points[idxcurr], points[idxprev]));
                    }
                    if (points[idxnext].Y >= scanLine)
                    {
                        AET.Add(new AETNode(points[idxcurr], points[idxnext]));
                    }
                    AET.Sort((n1, n2) => n1.x.CompareTo(n2.x));
                    if (++iter == points.Length)
                    {
                        return;
                    }
                }
                //Sort nodes
                AET.Sort((n1, n2) => n1.x.CompareTo(n2.x));

                //RemoveNodes
                for (int i = AET.Count - 1; i >= 0; i--)
                {
                    if (AET[i].ChcekNode(scanLine) == false)
                    {
                        AET.RemoveAt(i);
                    }
                }

                //Draw scanline
                DrawAET(scanLine, AET, points, colors, polygonArea);


                //Update nodes
                for (int i = AET.Count - 1; i >= 0; i--)
                {
                    AET[i].UpdateNode();
                }
            }
        }
        private Color GetRealColorOfPoint(int x, int y)
        {
            MyVector I;

            //kolor obiektu
            //
            MyVector IO;

            if (FillColorRadioButton.Checked)
            {
                IO = new MyVector(currentColor);
            }
            else
            {
                IO = new MyVector(GetPixelFromTexture(x, y, currentTexture));
            }
            //
            //kolor światła
            //
            MyVector tmpIL;

            if (LightColorCustomRadioButton.Checked)
            {
                tmpIL = IL;
            }
            else
            {
                tmpIL = new MyVector(1, 1, 1);
            }
            //
            //wektor do światła
            //
            MyVector L;

            if (LightLocationDefaultRadioButton.Checked) // Default
            {
                L = new MyVector(0, 0, 1);
            }
            else if (LightLocationCenterRadioButtton.Checked) //Center
            {
                L = MyVector.NormalizeVector(x - WIDTH / 2, y - HEIGHT / 2, 300);
            }
            else // Animation
            {
                L = MyVector.NormalizeVector(x - LightLocation.x, y - LightLocation.y, LightLocation.z);
            }
            //
            //wektor normalny
            //
            MyVector N;

            if (!BubbleCheckbox.Checked)
            {
                if (NVectorConstantRadioButton.Checked)
                {
                    N = new MyVector(0, 0, 1);
                }
                else
                {
                    N = GetNVectorFromMap(x, y, currentNormalMap);
                }
            }
            else
            {
                if (Distance(currentPoint, new PointF(x, y)) < BUBBLE_RADIUS) //generujemy wektor normalny dla bąbelka
                {
                    N = MyBubbleMesh[(int)(x - currentPoint.X + BUBBLE_RADIUS), (int)(y - currentPoint.Y + BUBBLE_RADIUS)];
                }
                else
                {
                    if (NVectorConstantRadioButton.Checked)
                    {
                        N = new MyVector(0, 0, 1);
                    }
                    else
                    {
                        N = GetNVectorFromMap(x, y, currentNormalMap);
                    }
                }
            }
            //

            MyVector R = 2 * MyVector.ScalarProduct(N, L) * N - L;

            I = kd * tmpIL * IO * MyVector.ScalarProduct(N, L) + ks * tmpIL * IO * (float)Math.Pow(MyVector.ScalarProduct(V, R), m);

            I.PrepareToDraw();

            return(Color.FromArgb((byte)(I.x * 255), (byte)(I.y * 255), (byte)(I.z * 255)));
        }