Example #1
0
        private void MenuItem_Load_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fDialog = new System.Windows.Forms.OpenFileDialog();

            if (fDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            string  filename = fDialog.FileName;
            DataSet data     = new DataSet();

            RayTracer.Parser parser = new RayTracer.Parser();

            RayTracer.Database.DataTableHelper.ReadXmlIntoDataSet(data, filename);
            List <RayTracer.Scene> scenes = parser.LoadScene(data);

            addFloor = false;
            DragCanvas canvas = (DragCanvas)this.FindName("canvas");

            canvas.Children.Clear();
            objects.Clear();
            ContextMenu contextMenu = (ContextMenu)this.FindName("selectContextMenu");

            contextMenu.Items.Clear();


            foreach (Scene scene in scenes)
            {
                this.camera = scene.camera;
                this.light  = scene.light;

                foreach (AObject aObject in scene.allObjects)
                {
                    if (aObject.GetType() == typeof(RayTracer.Floor))
                    {
                        addFloor = true;
                    }
                    else if (aObject.GetType() == typeof(RayTracer.Block))
                    {
                        CreateBlock((RayTracer.Block)aObject);
                    }
                    else if (aObject.GetType() == typeof(RayTracer.Sphere))
                    {
                        CreateEllipse((RayTracer.Sphere)aObject);
                    }
                }

                break;
            }
        }
        // Camera = new Camera(new Vector3(0, 0, -5), new Vector3(0, 0, 1));
        public Image TraceRay(Viewport viewport, Camera camera, Scene scene)
        {
            var image = new Image(viewport);
            Color[,] colours;

            for (int y = 0; y < viewport.Height; y++)
            {
                for (int x = 0; x < viewport.Width; x++)
                {
                    Ray ray = camera.GetRay(x, y, viewport);

                }
            }

            throw new NotImplementedException();
        }
Example #3
0
        public void Draw(Camera camera)
        {
            Guard.IsNotNull(nameof(camera), camera);

            Colour[,] imageBuffer = new Colour[camera.ViewPort.Width, camera.ViewPort.Height];

            for (int x = 0; x < camera.ViewPort.Width; x++)
            {
                for (int y = 0; y < camera.ViewPort.Height; y++)
                {
                    Ray ray = camera.GetRay(x, y);
                    imageBuffer[x, y] = TraceRay(ray, 0);
                }
            }

            //todo return something
        }
Example #4
0
        protected override void LoadContent()
        {
            Vector3 camPos = new Vector3(0, 1, 1);
            Vector3 camTarget = new Vector3(0, 1, 0);
            cam = new Camera();
            cam.Proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f), width / height, 0.1f, 1000f);
            cam.View = Matrix.CreateLookAt(camPos, camTarget, Vector3.Up);
            cam.World = Matrix.CreateTranslation(camPos);

            InitializeEyeRays(cam);
            InitializeSpheres(numSpheres);
            InitializeTextures();
            InitializeLights(numLights);
            Thread t = new Thread(new ThreadStart(RayTrace));
            t.Start();

            //RayTrace();

            spriteBatch = new SpriteBatch(GraphicsDevice);
        }
Example #5
0
        public MainWindow()
        {
            InitializeComponent();
            light = new RayTracer.Light(1, new RayTracer.Point(-15, 10, 20));
            RayTracer.Point point = new RayTracer.Point(5.3368f, 8.0531f, 9.8769f);

            RayTracer.Vector direction = new RayTracer.Vector(-0.38363f, -0.42482f, -0.82f);

            RayTracer.Vector directionUp = new RayTracer.Vector(-0.16485f, 0.90515f, -0.391826f);

            this.camera = new RayTracer.Camera(-1, point, direction, directionUp, 60f);


            Button front = (Button)this.FindName("viewButtonFront");

            front.Foreground = new SolidColorBrush(Colors.Red);

            TextBox positionZ, depth;

            positionZ           = (TextBox)this.FindName("positionZ");
            positionZ.IsEnabled = false;
            depth           = (TextBox)this.FindName("rectangleDepth");
            depth.IsEnabled = false;
        }
Example #6
0
        private void InitializeEyeRays(Camera camera)
        {
            eyeRays = new Ray[width * height];

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Vector3 source1 = new Vector3(i, j, 0f);
                    Vector3 source2 = new Vector3(i, j, 1f);
                    Vector3 unproj1 = GraphicsDevice.Viewport.Unproject(source1, cam.Proj, cam.View, cam.World);
                    Vector3 unproj2 = GraphicsDevice.Viewport.Unproject(source2, cam.Proj, cam.View, cam.World);

                    Ray r = new Ray();
                    r.Pos = unproj1;
                    r.Dir = Vector3.Normalize(unproj2-unproj1);

                    eyeRays[(j * width) + i] = r;
                }
            }
        }
Example #7
0
File: Scene.cs Project: tgy/CSharp
 public Scene(Camera camera)
 {
     Objects3D = new List<Object3D>();
     Lights = new List<Light>();
     Camera = camera;
 }
Example #8
0
        public static Bitmap run_raytracer(LinkedList<Object> objects, LinkedList<LightSource> lights, Camera c, Size size)
        {
            Console.WriteLine("Daniel Bolink's Ray Tracer");

            //image size
            int width = size.Width;
            int height = size.Height;

            // Create Camera
            //Camera c = new Camera(new Position(0, 0, -10), new Position(0,0,0));

            //create colors
            Color BGColor = new Color(0, 0, 0);
            //Color BLACK = new Color(0, 0, 0);
            //Color WHITE = new Color(255, 255, 255);
            //Color GREEN = new Color(0, 255, 0);
            //Color YELLOW = new Color(255, 255, 0);
            //Color ORANGE = new Color(255, 0, 255);
            //Color RED = new Color(200, 0, 0);
            //Color BLUE = new Color(0, 0, 255);
            //create Objects in frame

            //red plane at below 10 units
            //Object p1 = new Plane(new Vector(0, 1, 0), -1, RED);
            //blue sphere 10 units ahead radis 0.5
            //Object s1 = new Sphere(1, new Position(0, 0, 0), BLACK);
            //Object s2 = new Sphere(1, new Position(2, 0, 0), ORANGE);

            //add Objects to list
            //objects.AddLast(s1);
            //objects.AddLast(p1);
            //objects.AddLast(s2);

            //add Light Sources
            //LightSource l1 = new LightSource(new Position(20, 5, 10), WHITE);
            //LightSource l2 = new LightSource(new Position(-20, 5, 10), BLUE);

            //add lightsources to linked list
            //lights.AddLast(l1);
            //lights.AddLast(l2);

            // Create Pixels
            Color[,] pixels = new Color[width, height];

            //ray tracing algorithm
            for(int x = 0; x < width; x++)
            {
                for (int y = 0; y<height; y++)
                {
                    double xamnt = (x + 0.5) / width;
                    double yamnt = ((height - y) + 0.5) / height;

                    Vector ray_direction = (c.direction + (c.right * (xamnt-0.5) + (c.down * (yamnt - 0.5)))).normalize();

                    Ray ray = new Ray(c.position, ray_direction);

                    LinkedList<ObjectPair> intersection_objects = new LinkedList<ObjectPair>();
                    foreach(Object o in objects)
                    {
                        double val = o.find_intersection(ray);
                        if (val > 0)
                        {
                            intersection_objects.AddLast(new ObjectPair(o, val));
                        }
                        //Console.WriteLine(val);
                    }
                    //Console.WriteLine(intersection_objects.Count);
                    if(intersection_objects.Count == 0)
                    {
                        pixels[x, y] = BGColor;
                    }
                    else
                    {
                        ObjectPair winner = new ObjectPair(null, -1);
                        foreach (ObjectPair o in intersection_objects)
                        {
                            if (winner.val == -1)
                            {
                                winner = o;
                            }
                            else if(winner.val > o.val && o.val >= 0)
                            {
                                winner = o;
                            }
                        }
                        if(winner.val > 0)
                        {
                            Color final_color = winner.obj.color;
                            Position intersection_point = ray.origin + (ray.direction * (winner.val * 0.999));
                            foreach (LightSource l in lights)
                            {
                                Boolean shadowed = false;
                                Vector light_direction = new Vector(l.origin, intersection_point).normalize();
                                //Vector light_direction = new Vector(intersection_point, l.origin).normalize();
                                Ray shadow_ray = new Ray(intersection_point, light_direction);
                                foreach(Object o in objects)
                                {
                                    double obj_intersection = o.find_intersection(shadow_ray);
                                    if(obj_intersection > 0)
                                    {
                                        shadowed = true;
                                        break;
                                    }
                                }

                                if (!shadowed)
                                {

                                    Vector Winner_obj_normal = winner.obj.getNormalAt(intersection_point);
                                    double diffuse_coeffient = (shadow_ray.direction.normalize().dot(Winner_obj_normal));
                                    Vector reflection_direction = shadow_ray.direction - (2 * (shadow_ray.direction.dot(Winner_obj_normal)/Winner_obj_normal.magnitude()*Winner_obj_normal));
                                    Ray reflection = new Ray(intersection_point, reflection_direction);

                                    double specular_coefficient = reflection.direction.dot(ray.direction);
                                    final_color += l.color * 0.15;
                                    if(diffuse_coeffient > 0)
                                    {
                                        final_color += l.color * diffuse_coeffient * 0.15;
                                    }
                                    if(specular_coefficient > 0)
                                    {
                                        final_color += l.color * Math.Pow(specular_coefficient, 1000);
                                    }

                                }
                            }
                            pixels[x, y] = final_color.clip();
                        }
                        else
                        {
                            pixels[x, y] = BGColor;
                        }
                    }
                }
            }

            //create image
            Bitmap bmp = CreateImg(pixels);
            //Process photoViewer = Process.Start("RayTracer.png");
            //prompt for exit
            //Console.WriteLine("Press any key to exit.");
            //Console.ReadKey();
            return bmp;
        }
Example #9
0
        private void RayTracer_Form_Load(object sender, System.EventArgs e)
        {
            objects = new LinkedList<Object>();
            lights = new LinkedList<LightSource>();

            objects.AddLast(new Plane(new Vector(0, 1, 0), -1, new Color(255, 0, 0)));
            objects.AddLast(new Sphere(1, new Position(0, 0, 0), new Color(0, 255, 0)));

            cam_pos = new Position(0, 1, -5);
            lookat = new Position(0, 0, 0);

            Color WHITE = new Color(255, 255, 255);
            LightSource l1 = new LightSource(new Position(0, 1, -10), WHITE);
            lights.AddLast(l1);
            this.camera = new Camera(cam_pos, lookat);
            pictureBox1.Image = Program.run_raytracer(objects, lights, camera, pictureBox1.Size);
            pictureBox1.Refresh();

            updateObjects();
            updateLights();
        }
Example #10
0
 private Vector GetPoint(double x, double y, Camera camera)
 {
     return Vector.Norm(Vector.Plus(camera.Forward, Vector.Plus(Vector.Times(RecenterX(x), camera.Right),
                                                                Vector.Times(RecenterY(y), camera.Up))));
 }
Example #11
0
        private Vector GetPoint(float x, float y, Camera camera)
        {
            float rx = RecenterX(x);
            float ry = RecenterY(y);
            float vx = camera.Forward.X + (rx * camera.Right.X + ry * camera.Up.X);
            float vy = camera.Forward.Y + (rx * camera.Right.Y + ry * camera.Up.Y);
            float vz = camera.Forward.Z + (rx * camera.Right.Z + ry * camera.Up.Z);

            float sqrLength, invLength;

            sqrLength = vx * vx + vy * vy + vz * vz;
            invLength = SceneObject.InvSqrt(sqrLength);

            return new Vector(vx * invLength, vy * invLength, vz * invLength);
        }