示例#1
0
void RaytracerApplication::update( real_t delta_time )
{
    if ( raytracing ) {
        // do part of the raytrace
        if ( !raytrace_finished ) {
            assert( buffer );
            raytrace_finished = raytracer.raytrace( buffer, &delta_time );
        }
    } else {
        // copy camera over from camera control (if not raytracing)
        camera_control.update( delta_time );
        scene.camera = camera_control.camera;
    }
}
示例#2
0
void display(char* fileName)
{
	Ray ray;

	long startTime = getMsTime();
	long setupTime = 0;
	long fileWritingTime = 0;

    Intersection intersection;

    if (!state->drawn)
    {
		vector float negZero = (vector float) vec_splat_u32(-1);
		negZero = (vector float) vec_sl( (vector unsigned int) negZero, (vector unsigned int) negZero);

		VertexGroup raystart = VertexGroup(*state->camera);

		float xStepFactor __attribute__ ((aligned (16))) = 4.0 * params->screenXStep;
		float yStepFactor __attribute__ ((aligned (16))) = params->screenYStep;

		VertexGroup start = VertexGroup(*state->camera);
		VertexGroup xStep = VertexGroup(*state->xBasis * xStepFactor);
		VertexGroup yStep = VertexGroup(*state->yBasis * yStepFactor);

		VertexGroup screenCorner = VertexGroup((*state->screenCorner - *state->camera));

		vector float offset = (vector float){0, .25, .5, .75};

		VertexGroup pixel = xStep.madd(offset, screenCorner);

		BMP myImage;

		myImage.SetSize(params->xPixels, params->yPixels);
		myImage.SetBitDepth(24);

		VertexGroup rowPixel = VertexGroup(Vertex());
		VertexGroup raydir = VertexGroup(Vertex());

		vector bool int allHit = vec_cmplt(negativeZero(), vectorOne());

		VectorSInt rInt;
		VectorSInt gInt;
		VectorSInt bInt;

		int xPixel_4 = params->xPixels / 4;

		setupTime = getMsTime() - startTime;

		for (int i = 0; i < params->yPixels; i++)
		{
			rowPixel = pixel;
		
			for (int j = 0; j < xPixel_4; j++)
			{
				raydir = rowPixel;

				raydir.normalize();

				int index = i * xPixel_4 + j;

				ray.start = &raystart;
				ray.direction = &raydir;
				intersection.hit = allHit;

                raytracer.raytrace(&ray, &pixels[index], scene, params, 
					intersection);
			
				// cout << "Raycasting (" << index << ") - (" << 4*j << "," << i << ")" << endl;
				
				rInt.vec = vec_cts(pixels[index].r, 8);
				gInt.vec = vec_cts(pixels[index].g, 8);
				bInt.vec = vec_cts(pixels[index].b, 8);

				for (int k = 0; k < 4; k++)
				{
       	        	myImage(4*j+k,i)->Red   = round(rInt.points[k]);
	           	    myImage(4*j+k,i)->Green = round(gInt.points[k]);
    	           	myImage(4*j+k,i)->Blue  = round(bInt.points[k]);
				}

				rowPixel += xStep;
			}

			pixel -= yStep;
		}

		long fileStart = getMsTime();

		myImage.WriteToFile(fileName);

		fileWritingTime = getMsTime() - fileStart;

	    state->drawn = true;
	}

	long rayCasting = getMsTime() - startTime;

	long tracing = rayCasting - setupTime - fileWritingTime;

	cout << "Ray casting elapsed time: " << rayCasting << " ms (setup: "
		 << setupTime << " ms, tracing: " << tracing << " ms, file writing: " 
		<< fileWritingTime << " ms)" << endl;
}