Exemplo n.º 1
0
 public Square(float x, float y, float s, floatColour colour)
 {
     this.x      = x;
     this.y      = y;
     this.s      = s;
     this.colour = colour;
 }
Exemplo n.º 2
0
 public Circle(float x, float y, float r, bool light, floatColour colour)
 {
     this.x      = x;
     this.y      = y;
     this.r      = r;
     this.light  = light;
     this.colour = colour;
 }
Exemplo n.º 3
0
        // initialize
        public void Init()
        {
            float[] aax = { 0, 0.5f, 0, 0.5f };
            float[] aay = { 0, 0, 0.5f, 0.5f };

            light   = new List <Circle>();
            circles = new List <Circle>();
            squares = new List <Square>();

            circles.Add(new Circle(0.3f, 0.1f, 0.1f, false, new floatColour(0, 0, 0)));
            circles.Add(new Circle(-0.3f, 0.5f, 0.1f, false, new floatColour(0, 0, 0)));
            squares.Add(new Square(-0.3f, -0.3f, 0.2f, new floatColour(0, 0, 0)));
            light.Add(new Circle(0f, 0.5f, 0.25f, true, new floatColour(1, 0, 1)));
            light.Add(new Circle(-0.2f, 0.2f, 0.25f, true, new floatColour(0, 1, 0)));
            light.Add(new Circle(0.2f, -0.4f, 0.25f, true, new floatColour(0, 0, 1)));


            ray = new Ray();
            for (int x = 0; x < screen.width; x++)
            {
                for (int y = 0; y < screen.height; y++)
                {
                    floatColour[] pixelColour = { new floatColour(0, 0, 0), new floatColour(0, 0, 0), new floatColour(0, 0, 0), new floatColour(0, 0, 0) };
                    foreach (Circle c in light)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            ray.O = pixelPosition(x + aax[k], y + aay[k]);
                            ray.D = (new Vector2(ray.O.X - c.x, ray.O.Y - c.y)).Normalized();
                            ray.t = (float)Math.Sqrt(Math.Pow(ray.O.X - c.x, 2) + Math.Pow(ray.O.Y - c.y, 2));

                            bool occluded = false;
                            foreach (Circle p in circles)
                            {
                                if ((Math.Pow(ray.O.X - p.x, 2) + Math.Pow(ray.O.Y - p.y, 2)) > (p.r * p.r))
                                {
                                    if (ray.intersection(p))
                                    {
                                        occluded = true;
                                        float tmp = (float)Math.Sqrt((Math.Pow(c.x - p.x, 2) + Math.Pow(c.y - p.y, 2)));
                                        if (ray.t < tmp && (ray.t > 0 && tmp > 0))
                                        {
                                            occluded = false;
                                        }
                                        tmp = (float)Math.Sqrt((Math.Pow(ray.O.X - p.x, 2) + Math.Pow(ray.O.Y - p.y, 2)));
                                        if (ray.t < tmp)
                                        {
                                            occluded = false;
                                        }
                                    }
                                }
                                else
                                {
                                    occluded       = true;
                                    pixelColour[k] = new floatColour(0, 0, 0);
                                }
                            }
                            foreach (Square p in squares)
                            {
                                if (ray.intersection2(p))
                                {
                                    occluded = true;
                                    float tmp = (float)Math.Sqrt((Math.Pow(ray.O.X - (p.x + p.s), 2) + Math.Pow(ray.O.Y - (p.y + p.s), 2)));
                                    if (ray.t < tmp)
                                    {
                                        occluded = false;
                                    }
                                }
                            }
                            if (!occluded)
                            {
                                pixelColour[k] += c.colour * lightAttenuation(Vector2.Distance(ray.O, new Vector2(c.x, c.y)), c.r);
                            }
                        }
                    }
                    screen.Plot(x, y, ((pixelColour[0] + pixelColour[1] + pixelColour[2] + pixelColour[3]) / 4.0f).ToRGB32());
                }
            }
        }