Ejemplo n.º 1
0
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);
}
Ejemplo n.º 2
0
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);
}
Ejemplo n.º 3
0
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);
}
Ejemplo n.º 4
0
void Raytracer::aarender( int width, int height, int aa, char* fileName){
	double red = 0;
	double green = 0;
	double blue = 0;
	int locx = 0;
	int locy = 0;
	int pos = 0;

	Colour temp(0,0,0);

	for (int i = 0; i < _scrHeight; i++) {
		for (int j = 0; j < _scrWidth; j++) {
			for (int k = 0; k < aa; k++) {
				for (int m = 0; m < aa; m++) {
					locx = aa * j + m;
					locy = aa * i + k;
					if (locx < width && locy < height) {
						pos = locy * width + locx;
						red = double(re[pos])/255;
						green = double(gr[pos])/255;
						blue = double(bl[pos])/255;
						Colour p(red, green, blue);
						temp = temp + p;
					}
					//std::cout << pos;
					//std::cout << " \n";

				}
			}
			double aamultiple = 1.0 / (aa * aa);
			temp = aamultiple * temp;
			temp.clamp();

			_rbuffer[i*_scrWidth+j] = int(temp[0]*255);
			_gbuffer[i*_scrWidth+j] = int(temp[1]*255);
			_bbuffer[i*_scrWidth+j] = int(temp[2]*255);
		
		}
	}

	flushPixelBuffer(fileName);

}
Ejemplo n.º 5
0
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);
}
Ejemplo n.º 6
0
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);
}
Ejemplo n.º 7
0
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);
}
Ejemplo n.º 8
0
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);
}
Ejemplo n.º 9
0
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;
	}
}