Computer Graphics -Ray Tracing- 1 st quarter, 2015 Nara Institute of Science and Technology May 21, 2015
Today s topics Questions and Answers How to build a ray tracer (Homework 3.1 explanation)
Questions? https://goo.gl/9tfgsg
Homework 3.1??? 1. Take lecture 9 and lecture 10 @ edx 2. Download the course notes from edx website: L9: Ray Tracing 1 PDF Slides and L10: Ray Tracing 2 PDF Slides 3. Download a skeleton code from our lecture website 4. Find 3 blanks in main.cpp, then fill it (search todo comments) 5. Submit your implementation to the submission form.
Instruction For the homework 3.1 (beginner course), Fill 3 blank functions in main.cpp. 1. void RayThruPixel() 2. double Intersect() 3. Vector3d FindColor() NEVER mind the instruction of original homework3 on edx. This is our original assignments. You may implement these functions reference to edx lecture notes. homework 3.1 main.cpp 1. void RayThruPixel() 2. double Intersect() 3. Vector3d FindColor() edx homework3 edx
Kazutaka Sawa Portfolio Let s start building a ray tracer!
1. Take lecture 9 and lecture 10 @ edx, 2. Download the course notes from edx website Lecture9 Slide Lecture10 Slide
Ray tracing algorithm overview Light Source 1. Generate a ray from camera origin to a pixel. 2. If a ray hits an object, find color. Else, returns background color. Object Virtual screen FoV_Y (Field of View) Cam
3. Download a skeleton code from our lecture website 1. Download from here-> 2. Unzip it!
In case: Visual Studio 2010;
Goal When you finish your implementation, a cute snowman is rendered.
Almost everything are written in main.cpp. main.cpp 4 significant structs and classes: struct Ray: Light Ray struct Sphere: Sphere s size and location, also its material property. class Camera: Virtual camera propertis, such as losition, center etc Scene: Contains objects and lights. Currently, only single point light can be stored. 1 struct Ray Vector3d P0; /* Origin */ Vector3d P1; /* Direction */ }; struct Sphere Vector3d C; /* Center */ double Radius;/* Radius */ Vector3d Kd; Vector3d Ke; }; class Camera public: Vector3d Pos; /* Virtual Viewpoint */ Vector3d Center;/* Camera Center */ Vector3d Up; /* Up Vector */ int Width; /* Sensor Resolution */ int Height;/* Sensor Resolution */ double FoV_Y; /* Vertical Field of View */ double Aspect; double FoV_X; }; class Scene public: Sphere* spheres; int NumObjects; Vector3d PointLightPos; double Li; }; main.cpp
Ray struct Ray: Light Ray P0: origin P1: direction struct Ray Vector3d P0; /* Origin */ Vector3d P1; /* Direction */ Ray(const Vector3d origin, const Vector3d dirction) P0 = origin; P1 = dirction; } Ray(const Ray &ray) P0 = ray.p0; P1 = ray.p1; } }; Q: P1 is vector. main.cpp
Sphere struct Sphere: Sphere s size and location, also its material property. Radius C: center struct Sphere Vector3d C; /* Center */ double Radius; /* Radius */ Vector3d Kd; /* Diffuse */ Vector3d Ke; /* Emmision */ Sphere(double r, Vector3d center, Vector3d kd, Vector3d ke) : C(center), Radius(r), Kd(kd), Ke(ke)} }; main.cpp
Camera struct Sphere: Sphere s size and location, also its material property. Up Width Pos Center Height FoV_Y: Field of View Angle (y) class Camera public: Vector3d Pos; /* Virtual Viewpoint */ Vector3d Center;/* Camera Center */ Vector3d Up; /* Up Vector */ int Width; /* Sensor Resolution */ int Height;/* Sensor Resolution */ double FoV_Y; /* Vertical Field of View */ double double public: Aspect; FoV_X; Camera(const Vector3d pos, const Vector3d center, const Vector3d up, const int width, const int height, const double fov_y) :Pos(pos), Center(center), Up(up), Width(width), Height(height), FoV_Y(fov_y) this->aspect = (double)width/(double)height; this->fov_x = 2.0 * atan(this->aspect*tan(this->fov_y/2.0)); } }; main.cpp
Did you finish download a skeleton code?
int main(void) int main(void) /* Camera Settings */ /*Camera Position */ Vector3d pos = Vector3d(50.0, 52.0, 295.6); /*Camera Direction */ Vector3d dir = Vector3d::Normalize(Vector3d(0.0,0.0,-1.0)); int width = 128; /* Image width */ int height = 128; /* Image height */ double fov = 60.0 / 180.0 * M_PI; /* field of view */ Camera cam(pos, pos + dir, Vector3d(0, 1, 0), width, height, fov); /* Scene Settings */ Scene scene; /* Current scene contains 3 spheres. */ scene.spheres = spheres; scene.numobjects = 3; /* and contains one point light source. */ scene.pointlightpos = Vector3d(100, 100, 100); scene.li = 1.0; /* Do the ray tracing */ Image* img = RayTrace(cam, scene, cam.width, cam.height); } /* Save image */ img->savebmp("image.bmp"); delete img; return 0;
1. Generate a ray from camera origin to a pixel. 2. If a ray hits an object, find color. Else, returns background color. /* See also slides_raytrace2.pdf pp1. */ Image* RayTrace(const Camera& cam, const Scene& scene, const int width, const int height) Image* image = new Image(width, height); int count = 0; for (int i = 0 ; i < height ; i++) for (int j = 0; j < width ; j++) Ray ray = RayThruPixel(cam, j, i); 1. Gen a ray from camera fprintf(stderr, " rrender: %5.2f%%", (100.0*(count++))/(width*height-1)); } IntersectionInfo hit = Intersect(ray, scene); image->data[width*i+j] = FindColor(scene, hit); } } return image; 2. Find color Cam
Homework 3.1 Steps and Hints Step 1: We would like to find a snowman s silhouette. Implement RayThruPixel and Intersect, first. Step 2: Then fill a blank space in Find Color /* See also slides_raytrace2.pdf pp1. */ Image* RayTrace(const Camera& cam, const Scene& scene, const int width, const int height) Image* image = new Image(width, height); int count = 0; for (int i = 0 ; i < height ; i++) for (int j = 0; j < width ; j++) Ray ray = RayThruPixel(cam, j, i); fprintf(stderr, " rrender: %5.2f%%", (100.0*(count++))/(width*height-1)); } IntersectionInfo hit = Intersect(ray, scene); image->data[width*i+j] = FindColor(scene, hit); } } return image;
Step1 (1) RayThruPixel RayThruPixel generates a ray from camera position to a specific pixel on a virtual screen. /* See also slides_raytrace2.pdf pp2. */ /* Note: there're several typo in the slide. */ Ray RayThruPixel(const Camera& camera, const int i, const int j) /* TODO: Implement Camera Ray Generation*/ Vector3d origin = camera.pos; Vector3d direction = Vector3d(0, 0, -1); pixel } return Ray(camera.Pos, direction); Ray Virtual Screen Cam
origin direction
Lesson1. Implement RayThruPixel Function
Step1 (2) Intersection Intersect returns a distance between the ray origin and the hit point on the sphere. If no intersection, returns -1. double Intersect(const Ray& ray, const Sphere& sphere) /*TODO: Implement ray-sphere intersection. */ /* return positive distance from ray origin to intersection */ /* return -1, if no sphere intersects */ /* solve the eq. : at^2 + bt + c = 0 */ /* Complex roots: no intersection */ /* 2 roots found */ distance /* 2 real positive roots: pick smaller root */ /* One positive, one negative root: ray origin inside sphere (pick + root) */ Ray origin } /* Both negative, no intersection */ return -1;
Object/Object Intersections http://www.realtimerendering.com/intersecti ons.html
Do you remember Quadric Formula? 2 ax 2 + bx + c = 0 x = b ± b2 4ac 2a
Lesson2. Implement Intersect Function
Errata There s a typo in my skeleton code (sorry!), Ray ray = RayThruPixel(cam, j, i); Ray ray = RayThruPixel(cam, i, j);
Step 2: Fill a blank space in Find Color To implement Vector3d FindColor(), you may refer the slide Please ignore both ambient term and specular term. My ray tracer does not support both effects. There s a single point light source, only. You don t have to sum up all lights.
Shading Model I = K e + VL i K d max(l i n, 0) I: Radiance (~ pixel color) Ke: Emission V: Visibility term. Li: Light Intensity Kd: Diffuse reflectance li: Light vector n: normal vector 1. First, you may try without visibility term. (V is just set to 1.0) 2. Next, generate the second ray (called shadow ray) from hitpoint to light source, then trace a ray as : IntersectionInfo hit = Intersect(ray, scene); if hit.hitobject == null, there s no intersection. V = 1.0. Else, V = 0.0. 3. Hint: The origin of the shadow ray must push up slightly, along the normal direction to avoid intersecting by itself.
Step 2: Fill a blank space in Find Color /* See also slides_raytrace2.pdf pp2. */ /* Note: there're several typo in the slide. */ Ray RayThruPixel(const Camera& camera, const int i, const int j) /* TODO: Implement Camera Ray Generation*/ Vector3d origin = camera.pos; Vector3d direction = Vector3d(0, 0, -1); } return Ray(camera.Pos, direction);
Lesson3. Implement FindColor Function
Lesson 4. Create another snowman with another color. Lesson 5. Create another light source Lesson 6. Create a large mirror ball