Пример #1
0
        public static Pixel[] start()
        {
            Pixel[] pixels    = new Pixel[Constants.WIN_X * Constants.WIN_Y];
            Point3  viewPlane = new Point3(-Constants.WIN_X / 2, -Constants.WIN_X / 2, 0);

            createScene();

            for (int y = 0; y < Constants.WIN_Y; y++)
            {
                for (int x = 0; x < Constants.WIN_X; x++)
                {
                    Pixel p = new Pixel();
                    p.rect.X = x;
                    p.rect.Y = Constants.WIN_Y - 1 - y;

                    Point3 viewPixel = new Point3();
                    viewPixel.x = viewPlane.x + x;
                    viewPixel.y = viewPlane.y + y;
                    viewPixel.z = viewPlane.z;

                    Ray r = new Ray();
                    r.direction = Point3.vectorize(layout.Cam.Eye, viewPixel);
                    r.start     = layout.Cam.Eye;

                    p.color = fireRay(r, Constants.MIN_DEPTH, null);

                    pixels[y * Constants.WIN_X + x] = p;
                }
            }

            return(pixels);
        }
Пример #2
0
        public static Color3 diffuse(Hit h, Shape s, Light bulb)
        {
            Color3  diffuse    = Black;
            Vector3 normal     = s.calcNormal(h);
            Vector3 lightDir   = Point3.vectorize(bulb.Location, h.intersect);
            double  diffuseDot = Vector3.DotProduct(lightDir, normal);

            if (diffuseDot >= 0)
            {
                diffuse = (bulb.Color * s.Material.Diffuse) * diffuseDot;
            }

            return(diffuse);
        }
Пример #3
0
        public static Color3 specular(Ray r, Hit h, Shape s, Light bulb)
        {
            Color3  specular = Black;
            Vector3 normal   = s.calcNormal(h);

            Vector3 lightDir    = Point3.vectorize(bulb.Location, h.intersect);
            Vector3 refDir      = lightDir - (2 * (Vector3.DotProduct(lightDir, normal) / Math.Pow(normal.Abs(), 2)) * normal);
            double  specularDot = Vector3.DotProduct(r.direction, refDir);

            if (specularDot >= 0)
            {
                specular = (bulb.Color * s.Material.Specular) * Math.Pow(specularDot, s.Material.Ke);
            }

            return(specular);
        }
Пример #4
0
        // spawn a shadow ray from the intersection point to the light source.
        private static bool spawnShadow(Light bulb, Hit finalHit, Shape hitShape, Shape fromShape)
        {
            Ray shadowRay = new Ray();

            shadowRay.start     = finalHit.intersect;
            shadowRay.direction = Point3.vectorize(shadowRay.start, bulb.Location);
            double shadowDist = Point3.distance(shadowRay.start, bulb.Location);

            foreach (Shape s in layout.Shapes)
            {
                // If this is the object we're checking from, ignore. Duh.
                if (s.Equals(hitShape) || s.Equals(fromShape) || s.Material.Kt > 0)
                {
                    continue;
                }

                Hit shadowHit = s.intersect(shadowRay);

                if (shadowHit.intersect.z == Constants.FAR_AWAY)
                {
                    continue;
                }

                // We need to check if hitShape object is in FRONT OF the current one. if it is, we need to ignore this current shape.
                Vector3 frontTest = Point3.vectorize(shadowHit.intersect, finalHit.intersect);

                if (frontTest.X > 0 && frontTest.Y > 0 && frontTest.Z > 0)
                {
                    continue;
                }

                // something has to come between the light and the current shape.
                double shapeDist = Point3.distance(shadowRay.start, shadowHit.intersect);
                if (shapeDist < shadowDist)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #5
0
 public void setup()
 {
     _Normal = Point3.vectorize(Lookat, Eye);
     _Up     = Vector3.CrossProduct(Orient, Normal);
     _View   = Vector3.CrossProduct(Normal, Up);
 }