コード例 #1
0
ファイル: Scene.cs プロジェクト: ioly54/TheRayTracerChallenge
        public Canvas Render(Camera camera)
        {
            RT.Canvas canvas = new RT.Canvas(camera.hSize, camera.vSize);
            canvas.FillCanvas(RT.Color.black);

            if (this.lights.Count == 0)
            {
                Console.WriteLine("No lights in scene, this will always produce a black image.");
            }

            //Precalculate the bounds
            PreCalculateBounds();

            for (int y = 0; y < camera.vSize; y++)
            {
                for (int x = 0; x < camera.hSize; x++)
                {
                    Console.WriteLine(x.ToString() + ',' + y.ToString());
                    Ray   temp       = this.RayForPixel(camera, x, y);
                    Color pixelColor = this.ColorAt(temp);
                    canvas.SetPixel(x, y, pixelColor);
                }
            }
            return(canvas);
        }
コード例 #2
0
        public static void RunSimulation()
        {
            int currentIteration = 0;
            int maxIteration     = 60000;

            RT.Canvas canvas = new RT.Canvas(1000, 500);

            RT.Projectile projectile = new RT.Projectile(new RT.Point(0.0f, 0.0f, 0.0f),
                                                         new RT.Vector(25.0f, 50.0f, 0.0f));
            RT.Environment environment = new RT.Environment(new RT.Vector(0.0f, -3.0f, 0.0f),
                                                            new RT.Vector(0.0f, 0.0f, 0.0f));

            while (projectile.position.y >= 0.0f && currentIteration < maxIteration)
            {
                Tick(projectile, environment);
                currentIteration++;
                //Draw to canvas
                int x = (int)projectile.position.x;
                int y = (int)projectile.position.y;
                canvas.SetPixel(x, y, RT.Color.green);
            }

            if (currentIteration == maxIteration)
            {
                Console.WriteLine("Error, max iteration count exceeded.");
            }

            Console.WriteLine("--Simulation Results--");
            Console.WriteLine("Projectile: " + projectile.ToString());
            Console.WriteLine("Environment: " + environment.ToString());
            RT.Save.SaveCanvas(canvas);
        }
コード例 #3
0
        public static void Chapter5Challenge()
        {
            //Create an image of a sphere by only testing for hits or misses.
            RT.Mat4  transMatrix = new RT.Mat4();
            RT.Scene scene       = new RT.Scene();
            //transMatrix = RT.Mat4.ScaleMatrix(1,0.5f,1);
            //transMatrix = RT.Mat4.ScaleMatrix(0.5f,1,1);
            //transMatrix = RT.Mat4.RotateMatrix(0.0f, 0.0f, RT.Constants.pi * 0.25f) * RT.Mat4.ScaleMatrix(1,0.5f,1);
            transMatrix = RT.Mat4.ShearMatrix(1, 0, 0, 0, 0, 0) * RT.Mat4.ScaleMatrix(0.5f, 1, 1);

            int resolution = 200;

            RT.Canvas canvas = new RT.Canvas(resolution, resolution);
            canvas.FillCanvas(RT.Color.black);

            RT.Sphere sphere = new RT.Sphere();
            sphere.SetMatrix(transMatrix);

            RT.Point camera = new RT.Point(0, 0, -5);

            //Use the wall x and y as the width and height and the position of the wall as the z value
            RT.Point wall     = new RT.Point(0.0f, 0.0f, 7f);
            double   wallSize = 7.0f;

            //Camera is the start point, rays are created by taking iterating over the wall in resultion steps
            //vertically and horizontally, calc wall - camera to get direction of camera to wall location.
            //Check if the ray hits the sphere, if it does draw red if it does not draw black.

            for (int y = 0; y < resolution; y++)
            {
                for (int x = 0; x < resolution; x++)
                {
                    //Need to start at half the width over from the walls origin and increment from there

                    double increment = wallSize / resolution;

                    RT.Vector currentWallPixel = wall - new RT.Point((wallSize * 0.5f) - x * increment,
                                                                     (wallSize * 0.5f) - y * increment,
                                                                     wall.z);

                    //This presents a problem when I want to convert a point to a vector...
                    RT.Point  point     = (currentWallPixel - camera);
                    RT.Vector direction = new RT.Vector(point).Normalize();

                    RT.Ray ray = new RT.Ray(camera, direction);

                    RT.Intersection hit = RT.Scene.current.Hit(scene.Intersections(ray));

                    if (hit != null)
                    {
                        canvas.SetPixel(x, y, RT.Color.red);
                    }
                }
            }
            RT.Save.SaveCanvas(canvas, "Chapter5Challenge");
        }
コード例 #4
0
        public static void TransformChallenge()
        {
            //Create canvas of set size and width
            //Create Transform that first offsets a point by 1/2 canvas size then
            //rotates the object by 1/12th of 2*pi through 12 iterations
            //At each location draw to the canvas a circle
            int circleRadius = 5;

            RT.Canvas canvas = new RT.Canvas(100, 100);

            RT.Point currentLocation = new RT.Point();

            //Offset 1/3 distance of canvas size;
            currentLocation = RT.Mat4.TranslateMatrix(canvas.GetWidth() * 0.3f,
                                                      0.0f,
                                                      0.0f)
                              * currentLocation;
            //Rotate loop
            int maxIterations = 12;

            for (int r = 0; r < maxIterations; r++)
            {
                currentLocation = RT.Mat4.RotateZMatrix(2.0f * RT.Constants.pi * (1.0f / maxIterations)) * currentLocation;

                //Offset current location so that it is centered in the image by 1/2 width and height through translation
                RT.Point screenSpaceLocation = RT.Mat4.TranslateMatrix(canvas.GetWidth() * 0.5f,
                                                                       canvas.GetHeight() * 0.5f,
                                                                       0.0f) * currentLocation;

                Console.WriteLine("Point " + r.ToString());
                Console.WriteLine(screenSpaceLocation);

                //Draw circle at current location
                canvas.DrawCircle((int)screenSpaceLocation.x,
                                  (int)screenSpaceLocation.y,
                                  circleRadius,
                                  RT.Color.green);
            }

            //Save out the image when completed.
            RT.Save.SaveCanvas(canvas, "TransformChallenge");
        }
コード例 #5
0
ファイル: Scene.cs プロジェクト: ioly54/TheRayTracerChallenge
        public Canvas DrawAABBs(Camera camera)
        {
            RT.Canvas canvas = new RT.Canvas(camera.hSize, camera.vSize);
            canvas.FillCanvas(RT.Color.black);

            if (this.lights.Count == 0)
            {
                Console.WriteLine("No lights in scene, this will always produce a black image.");
            }

            for (int y = 0; y < camera.vSize; y++)
            {
                for (int x = 0; x < camera.hSize; x++)
                {
                    Console.WriteLine(x.ToString() + ',' + y.ToString());
                    Ray   temp       = this.RayForPixel(camera, x, y);
                    Color pixelColor = this.CheckAABB(temp);
                    //Blend the colors to allow for transparency among the cubes and layering
                    canvas.SetPixel(x, y, canvas.GetPixel(x, y) + pixelColor);
                }
            }
            return(canvas);
        }