void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName ) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); //std::cout<<viewToWorld<<std::endl; // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { Colour col; if (antiAliasing) { col = anti_aliasing(viewToWorld, width, height, factor, i, j); } else { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; imagePlane[2] = -1; Vector3D _dir (imagePlane[0],imagePlane[1],imagePlane[2]); Vector3D dir = viewToWorld * _dir; origin = viewToWorld * origin;; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. Ray3D ray; ray.origin = origin; ray.dir = dir; Colour col = shadeRay(ray); //std:: cout << "i:" <<i << "j:" << j <<"Hit: "<<ray.intersection.none<<std::endl; } _rbuffer[i*width+j] = int(col[0]*255); _gbuffer[i*width+j] = int(col[1]*255); _bbuffer[i*width+j] = int(col[2]*255); } } flushPixelBuffer(fileName); }
Camera::Camera(unsigned int width, unsigned int height, double fov) : factor_((double(height)/2)/tan(fov*M_PI/360.0)), apertureRadius_(0), sensor_(width, height), gamma_(1.0), subSampler_(1), apertureSampler_(1), eye_(0.0, 0.0, 0.0), view_(0.0, 0.0, -1.0), up_(0.0, 1.0, 0.0), viewToWorld_(initInvViewMatrix(eye_, view_, up_)) { initDefault(); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { //perform antialiasing with a factor of 4 so ray is computed 4 times at a pixel //and multiplied by factor 1/4 and summed together for antialiased image for(float fragmenti = i; fragmenti < i + 1.0f; fragmenti += 0.5f){ for(float fragmentj = j; fragmentj < j + 1.0f; fragmentj += 0.5f){ // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + fragmentj)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + fragmenti)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. //multiply each ray by 1/4 float coef = 0.25f; Ray3D ray; // want ray to be in world frame ray.origin = viewToWorld*origin; ray.dir = viewToWorld*(imagePlane-origin); ray.dir.normalize(); Colour col = shadeRay(ray); //now sum the total contributions from 4 rays per pixel _rbuffer[i*width+j] += int(col[0]*255*coef); _gbuffer[i*width+j] += int(col[1]*255*coef); _bbuffer[i*width+j] += int(col[2]*255*coef); } } } } flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, std::string fileName ) { computeTransforms(_root); Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0., 0., 0.); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. // ray -> world space Point3D rayOriginWorld = viewToWorld * imagePlane; Vector3D rayDir = imagePlane - origin; Vector3D rayDirWorld = viewToWorld * rayDir; Ray3D ray; ray.origin = rayOriginWorld; ray.dir = rayDirWorld; ray.dir.normalize(); // pixel colour Colour col = shadeRay(ray); _rbuffer[i*width+j] = int(col[0]*255); _gbuffer[i*width+j] = int(col[1]*255); _bbuffer[i*width+j] = int(col[2]*255); } } flushPixelBuffer(fileName); }
Camera::Camera(unsigned int width, unsigned int height, Point3D const& eye, Vector3D const& view, Vector3D const& up, double fov) : factor_((double(height)/2)/tan(fov*M_PI/360.0)), apertureRadius_(0), sensor_(width, height), gamma_(1.0), subSampler_(1), apertureSampler_(1), eye_(eye), view_(view), up_(up), viewToWorld_(initInvViewMatrix(eye, view, up)) { initDefault(); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName, char mode ) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. /////////////////////// OUR CODE /////////////////////////////// Vector3D d = Vector3D(imagePlane[0], imagePlane[1], imagePlane[2]); d = viewToWorld*d; origin = viewToWorld*origin; Ray3D ray = Ray3D( origin , d ); if (mode == 'd'){ (*(ray.intersection.mat)).specular = Colour(0,0,0); } Colour col; col = shadeRay(ray, mode); /////////////////////// OUR CODE /////////////////////////////// _rbuffer[i*width+j] = int(col[0]*255); _gbuffer[i*width+j] = int(col[1]*255); _bbuffer[i*width+j] = int(col[2]*255); } } flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName ) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. Point3D ray_origin = viewToWorld * imagePlane; Vector3D ray_dir = ray_origin - eye; Ray3D ray(ray_origin, ray_dir); Colour col = shadeRay(ray); _rbuffer[i*width+j] = int(col[0]*255); _gbuffer[i*width+j] = int(col[1]*255); _bbuffer[i*width+j] = int(col[2]*255); } } flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName ) { printf("\n --------------------- \n RENDERING: %s\n", fileName); printf("\t view[%f, %f, %f]\n", view[0], view[1], view[2]); printf("\t eye[%f, %f, %f]\n", eye[0], eye[1], eye[2]); printf("\t up[%f, %f, %f]\n", up[0], up[1], up[2]); Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); clock_t start = clock(); // color each pixel switch ( getAAMode() ) { case Raytracer::AA_SUPER_SAMPLING: render_SS(width, factor, height, viewToWorld, eye); break; case Raytracer::NONE: render_no_AA(width, factor, height, viewToWorld, eye); break; } clock_t end = clock(); double ms = ((end - start) * 1000)/CLOCKS_PER_SEC; printf("RENDER TIME :: %f ms\n", ms); flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, int AA_level, char* fileName, char renderStyle ) { Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(_scrHeight)/2)/tan(fov*M_PI/360.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); /** * Extension: Anti-aliasing level via supersampling method * Algorithm: * Generate an image of dimensions equal to a multiple of the original * dimensions (as specified by the AA level) * Then sample individual pixels and average them to compose a pixel * on the actual screen */ _aaLevel = AA_level; initSuperPixelBuffer(); /// A little print for the user fprintf(stderr, "Rendering %dx%d scene, AA-level %d\n", _scrWidth, _scrHeight, _aaLevel); int superi, superj; double offi, offj; // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { // Prepare to supersample for anti aliasing for (int m = 0; m < _aaLevel; m++) { for (int n = 0; n < _aaLevel; n++) { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; offj = double(1)/(0.5 * _aaLevel) + double(n)/_aaLevel; offi = double(1)/(0.5 * _aaLevel) + double(m)/_aaLevel; imagePlane[0] = (-double((_scrWidth))/2 + offj + j)/factor; imagePlane[1] = (-double((_scrHeight))/2 + offi + i)/factor; imagePlane[2] = -1; // Create the transformed ray and shoot it out (shadeRay) Vector3D pixelVector = Vector3D(imagePlane[0], imagePlane[1], imagePlane[2]); Vector3D transformedPixelVector = viewToWorld * pixelVector; Point3D transformedOrigin = viewToWorld * origin; Ray3D ray = Ray3D(transformedOrigin, transformedPixelVector); //Check for scene render style Colour col = shadeRay(ray, renderStyle); superi = i*_aaLevel + m; superj = j*_aaLevel + n; _superrbuffer[superi*(_aaLevel*_scrWidth)+superj] = int(col[0]*255); _supergbuffer[superi*(_aaLevel*_scrWidth)+superj] = int(col[1]*255); _superbbuffer[superi*(_aaLevel*_scrWidth)+superj] = int(col[2]*255); } }//End supersampling loop } }//finsihed pixel loop // Now average out the pixels from the super buffer (supersampling) factor = double(1)/(_aaLevel * _aaLevel); unsigned long rtemp; unsigned long gtemp; unsigned long btemp; for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { rtemp = 0; gtemp = 0; btemp = 0; for (int m = 0; m < _aaLevel; m++) { superi = i*_aaLevel + m; for (int n = 0; n < _aaLevel; n++) { superj = j*_aaLevel + n; rtemp += _superrbuffer[superi*(_aaLevel*_scrWidth)+superj]; gtemp += _supergbuffer[superi*(_aaLevel*_scrWidth)+superj]; btemp += _superbbuffer[superi*(_aaLevel*_scrWidth)+superj]; } } _rbuffer[i*_scrWidth+j] = factor * rtemp; _gbuffer[i*_scrWidth+j] = factor * gtemp; _bbuffer[i*_scrWidth+j] = factor * btemp; } } flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, const char* fileName ) { /*****************anti-aliasing************************/ Matrix4x4 viewToWorld; _scrWidth = width; _scrHeight = height; double factor = (double(height)/2)/tan(fov*M_PI/360.0); Colour totalCol(0.0,0.0,0.0); initPixelBuffer(); viewToWorld = initInvViewMatrix(eye, view, up); scfCache(_root); // Construct a ray for each pixel. for (int i = 0; i < _scrHeight; i++) { for (int j = 0; j < _scrWidth; j++) { //anti-aliasing, each pixel can have 4 rays, the pixel color determined by the avgerage for(double a = i; a < i+1 ; a += 0.5){ for(double b = j; b < j+1;b += 0.5){ // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + b)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + a)/factor; imagePlane[2] = -1; // TODO: Convert ray to world space and call // shadeRay(ray) to generate pixel colour. Point3D originW = viewToWorld * imagePlane; Vector3D directionW = viewToWorld * (imagePlane -origin); directionW.normalize(); Ray3D ray(originW, directionW); Colour col = shadeRay(ray,0,0); //each ray contributed 0.25 color to the final rending color for the pixel _rbuffer[i*width+j] += int(col[0]*255*0.25); _gbuffer[i*width+j] += int(col[1]*255*0.25); _bbuffer[i*width+j] += int(col[2]*255*0.25); } } } } /********************anti-aliasing**************************/ // Matrix4x4 viewToWorld; // _scrWidth = width; // _scrHeight = height; // double factor = (double(height)/2)/tan(fov*M_PI/360.0); // // initPixelBuffer(); // viewToWorld = initInvViewMatrix(eye, view, up); // scfCache(_root); // // Construct a ray for each pixel. // for (int i = 0; i < _scrHeight; i++) { // for (int j = 0; j < _scrWidth; j++) { // // Sets up ray origin and direction in view space, // // image plane is at z = -1. // Point3D origin(0, 0, 0); // Point3D imagePlane; // imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; // imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; // imagePlane[2] = -1; // // // TODO: Convert ray to world space and call // // shadeRay(ray) to generate pixel colour. // // //Vector3D dir = imagePlane-origin; // Ray3D ray(origin,imagePlane-origin); // /*my dode here*/ // ray.origin = viewToWorld*ray.origin; // ray.dir = viewToWorld*ray.dir; // ray.dir.normalize(); // Colour col = shadeRay(ray,0,0);//the original is shadeRay(ray) // // _rbuffer[i*width+j] = int(col[0]*255); // _gbuffer[i*width+j] = int(col[1]*255); // _bbuffer[i*width+j] = int(col[2]*255); // } // } flushPixelBuffer(fileName); }
void Raytracer::render( int width, int height, Point3D eye, Vector3D view, Vector3D up, double fov, char* fileName, int aa, int fl, bool dof ) { Matrix4x4 viewToWorld; width = width * aa; height = height * aa; double factor = (double(height)/2)/tan(fov*M_PI/360.0); viewToWorld = initInvViewMatrix(eye, view, up); initAABuffer(aa, width, height); _scrWidth = width / aa; _scrHeight = height / aa; initPixelBuffer(); // Construct a ray for each pixel. for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { // Sets up ray origin and direction in view space, // image plane is at z = -1. Point3D origin(0, 0, 0); Point3D imagePlane; imagePlane[0] = (-double(width)/2 + 0.5 + j)/factor; imagePlane[1] = (-double(height)/2 + 0.5 + i)/factor; imagePlane[2] = -1; Ray3D ray; ray.origin = viewToWorld * origin; ray.dir = viewToWorld * imagePlane - ray.origin; ray.dir.normalize(); Colour col(0,0,0); //implement depth of field if true if (dof == true) { //generate a point in the scene as part of the focal plane Vector3D pointAimed = (ray.origin + fl * ray.dir) - Point3D(0,0,0); float r = 1; Colour pc(0,0,0); for (int px=0; px < 15; px++) { float du = ((float) rand())/(float(RAND_MAX)); float dv = ((float) rand())/(float(RAND_MAX)); //x and y axis of camera/eye Vector4D u = viewToWorld.getColumn(1); Vector4D v = viewToWorld.getColumn(0); //convert them to 3d vectors; Vector3D u2(u[0],u[1],u[2]); Vector3D v2(v[0],v[1],v[2]); //convert ray.origin to vector for vector operation; Vector3D tempv(ray.origin[0], ray.origin[1], ray.origin[2]); //generate random point around the eye (vector is used for operation) Vector3D eyev = tempv - (r/2)*u2 - (r/2)*v2 + r*(du)*u2 + r*(dv)*v2; //generate a ray from random point to pointAimed Ray3D dovr; dovr.dir = pointAimed - eyev; dovr.origin = Point3D(eyev[0], eyev[1], eyev[2]); dovr.dir.normalize(); //bounce ray into the scene and get its color Colour dovCol = shadeRay(dovr, 2); pc = pc + dovCol; } //average the pixel colors to get the blurriness effect col = (1/15.0) * pc; } else { col = shadeRay(ray, 2); } col.clamp(); if (aa == 1) { _rbuffer[i*width+j] = int(col[0]*255); _gbuffer[i*width+j] = int(col[1]*255); _bbuffer[i*width+j] = int(col[2]*255); } else { re[i*width+j] = int(col[0]*255); gr[i*width+j] = int(col[1]*255); bl[i*width+j] = int(col[2]*255); } } } if (aa == 1) { flushPixelBuffer(fileName); } else { aarender(width, height, aa, fileName); delete re; delete gr; delete bl; } }
void Camera::setUp (Vector3D const& up) { up_ = up; viewToWorld_ = initInvViewMatrix(eye_, view_, up_); }
void Camera::setView(Vector3D const& view) { view_ = view; viewToWorld_ = initInvViewMatrix(eye_, view_, up_); }
void Camera::setEye (Point3D const& eye) { eye_ = eye; viewToWorld_ = initInvViewMatrix(eye_, view_, up_); }