// ----------------------------------------------------------------------------- render scene // explained on page 191 void Spherical::render_scene(const World& wr) { RGBColor L; ViewPlane vp(wr.vp); int hres = vp.hres; int vres = vp.vres; float s = vp.s; Ray ray; int depth = 0; Point2D sp; // sample point in [0, 1] X [0, 1] Point2D pp; // sample point on the pixel ray.o = eye; for (int r = 0; r < vres; r++) // up for (int c = 0; c < hres; c++) { // across L = black; for (int j = 0; j < vp.num_samples; j++) { sp = vp.sampler_ptr->sample_unit_square(); pp.x = s * (c - 0.5 * hres + sp.x); pp.y = s * (r - 0.5 * vres + sp.y); ray.d = ray_direction(pp, hres, vres, s); L += wr.tracer_ptr->trace_ray(ray, depth); } L /= vp.num_samples; L *= exposure_time; wr.display_pixel(r, c, L); } }
void Pinhole::render_scene(World & w) { this->compute_uvw(); RGBColor pixel_color; ViewPlane & vp = w.vp; Ray ray; Point2D sp, pp; vp.s /= zoom; ray.o = this->eye; for(int r = 0; r < vp.vRes; r++) { for(int c = 0; c < vp.hRes; c++) { pixel_color = black; for(int j = 0; j < vp.get_num_samples(); j++) { sp = vp.get_sampler_ptr()->sample_unit_square(); pp.x = vp.s * (c - 0.5 * (vp.hRes - 1.0) + sp.x); // Get the X position in the world pp.y = vp.s * (r - 0.5 * (vp.vRes - 1.0) + sp.y); //std::cout << "Tracing at " << sp.x << "," << sp.y << std::endl; ray.d = ray_direction(pp); pixel_color += w.tracer_ptr->trace_ray(ray,0); } pixel_color /= vp.get_num_samples(); w.display_pixel(r,c,pixel_color); } } }
void Fisheye::render_scene(const World& w) { RGBColor L; ViewPlane vp(w.vp); int hres = vp.hres; int vres = vp.vres; float s = vp.s; Ray ray; int depth = 0; Point2D sp; // sample point in [0, 1] x [0, 1] Point2D pp; // sample point on the pixel float r_squared = 1.0; // sum of squares of normalized device coordinates ray.o = eye; for (int r = 0; r < vres; r++) { // up for (int c = 0; c < hres; c++) { // accross L = black; for (int j = 0; j < vp.num_samples; j++) { sp = vp.sampler_ptr->sample_unit_square(); pp.x = s * (c - 0.5 * hres + sp.x); pp.y = s * (r - 0.5 * vres + sp.y); ray.d = ray_direction(pp, hres, vres, s, r_squared); if ((r_squared <= 1.0 && !rectangular) || rectangular) L += w.tracer_ptr->trace_ray(ray, depth); } L /= vp.num_samples; L *= exposure_time; w.display_pixel(r, c, L); } } }
void ThinLens::render_scene(World& w) { RGBColor L; Ray ray; ViewPlane vp(w.vp); FreeImage_Initialise(); FIBITMAP* bitmap = FreeImage_Allocate(vp.hres, vp.vres, 24); RGBQUAD color; int depth = 0; Point2D sp; Point2D pp; Point2D dp; Point2D lp; vp.s /= zoom; for(int r = 0; r < vp.vres; r++) for(int c = 0 ; c < vp.hres; c++) { L = black; for(int n = 0; n < vp.num_samples; n++) { sp = vp.sampler_ptr -> sample_unit_square(); pp.x = vp.s * (c - vp.hres / 2.0 + sp.x); pp.y = vp.s * (r - vp.vres / 2.0 + sp.y); dp = sampler_ptr -> sample_unit_disk(); lp = dp * lens_radius; ray.o = eye + lp.x * u + lp.y * v; ray.d = ray_direction(pp, lp); L += w.tracer_ptr -> trace_ray(ray); } L /= vp.num_samples; L *= exposure_time; w.display_pixel(r, c, L); color.rgbRed = (int)(L.r*255); color.rgbGreen = (int)(L.g*255); color.rgbBlue = (int)(L.b*255); FreeImage_SetPixelColor(bitmap, c, r, &color); } if (FreeImage_Save(FIF_PNG, bitmap, "test.png", 0)) std::cout << "Image Successfully Saved!" << std::endl; FreeImage_DeInitialise(); }
void Fisheye::render_scene(World& w) { RGBColor L; ViewPlane vp(w.vp); FreeImage_Initialise(); FIBITMAP* bitmap = FreeImage_Allocate(vp.hres, vp.vres, 24); RGBQUAD color; int hres = vp.hres; int vres = vp.vres; float s = vp.s; Ray ray; int depth = 0; Point2D sp; Point2D pp; float r_squared; ray.o = eye; for(int r = 0; r < vres; r++) for(int c = 0; c < hres; c++) { L = black; for(int j = 0; j < vp.num_samples; j++) { sp = vp.sampler_ptr -> sample_unit_square(); pp.x = s * (c - hres / 2.0 + sp.x); pp.y = s * (r - vres / 2.0 + sp.y); ray.d = ray_direction(pp, hres, vres, s, r_squared); if(r_squared <= 1.0) L += w.tracer_ptr -> trace_ray(ray); } L /= vp.num_samples; L *= exposure_time; w.display_pixel(r, c, L); color.rgbRed = (int)(L.r*255); color.rgbGreen = (int)(L.g*255); color.rgbBlue = (int)(L.b*255); FreeImage_SetPixelColor(bitmap, c, r, &color); } if (FreeImage_Save(FIF_PNG, bitmap, "test.png", 0)) std::cout << "Image Successfully Saved!" << std::endl; FreeImage_DeInitialise(); }
void CombatCamera::ViewportRay(double screen_x, double screen_y, double out_ray_origin[3], double out_ray_direction[3]) const { Matrix projection(4, 4); std::copy(m_camera.getProjectionMatrix()[0], m_camera.getProjectionMatrix()[0] + 16, projection.data().begin()); Matrix view(4, 4); std::copy(m_camera.getViewMatrix(true)[0], m_camera.getViewMatrix(true)[0] + 16, view.data().begin()); Matrix inverse_vp = Inverse4(prod(projection, view)); double nx = (2.0 * screen_x) - 1.0; double ny = 1.0 - (2.0 * screen_y); Matrix near_point(3, 1); near_point(0, 0) = nx; near_point(1, 0) = ny; near_point(2, 0) = -1.0; // Use mid_point rather than far point to avoid issues with infinite projection Matrix mid_point(3, 1); mid_point(0, 0) = nx; mid_point(1, 0) = ny; mid_point(2, 0) = 0.0; // Get ray origin and ray target on near plane in world space Matrix ray_origin = Matrix4xVector3(inverse_vp, near_point); Matrix ray_target = Matrix4xVector3(inverse_vp, mid_point); Matrix ray_direction = ray_target - ray_origin; ray_direction /= ray_direction(0, 0) * ray_direction(0, 0) + ray_direction(1, 0) * ray_direction(1, 0) + ray_direction(2, 0) * ray_direction(2, 0); std::copy(ray_origin.data().begin(), ray_origin.data().end(), out_ray_origin); std::copy(ray_direction.data().begin(), ray_direction.data().end(), out_ray_direction); }