Ejemplo n.º 1
0
// React to keyboard input
void keyboard(unsigned char key, int x, int y)
{
    printf("key %d pressed at %d,%d\n",key,x,y);
    fflush(stdout);
	switch (key)
	{
		// Add a light based on the camera position.
		case 'L':
			MyLightPositions.push_back(getCameraPosition());
			break;

		// Update a light based on the camera position.
		case 'l':
			MyLightPositions[MyLightPositions.size() - 1] = getCameraPosition();
			break;

		// Pressing r will launch the raytracing.
		case 'r':
		{
			cout << "Raytracing" << endl;

			// Setup an image with the size of the current image.
			Image result(ImageSize_X, ImageSize_Y);

			// Produce the rays for each pixel, by first computing
			// the rays for the corners of the frustum.
			Vec3Df origin00, dest00;
			Vec3Df origin01, dest01;
			Vec3Df origin10, dest10;
			Vec3Df origin11, dest11;
			Vec3Df origin, dest;

			produceRay(0, 0, &origin00, &dest00);
			produceRay(0, ImageSize_Y - 1, &origin01, &dest01);
			produceRay(ImageSize_X - 1, 0, &origin10, &dest10);
			produceRay(ImageSize_X - 1, ImageSize_Y - 1, &origin11, &dest11);

			for (unsigned int y = 0; y < ImageSize_Y; ++y) {
				for (unsigned int x = 0; x < ImageSize_X; ++x)
				{
					// Produce the rays for each pixel, by interpolating 
					// the four rays of the frustum corners.
					float xscale = 1.0f - float(x) / (ImageSize_X - 1);
					float yscale = 1.0f - float(y) / (ImageSize_Y - 1);

					origin = yscale*(xscale*origin00 + (1 - xscale)*origin10) +
						(1 - yscale)*(xscale*origin01 + (1 - xscale)*origin11);
					dest = yscale*(xscale*dest00 + (1 - xscale)*dest10) +
						(1 - yscale)*(xscale*dest01 + (1 - xscale)*dest11);

					// Launch raytracing for the given ray.
					Vec3Df rgb = performRayTracing(origin, dest);
					// Store the result in an image 
					result.setPixel(x, y, RGBValue(rgb[0], rgb[1], rgb[2]));
				}

				loadbar(y, ImageSize_Y, 50);
			}

			loadbar(ImageSize_Y, ImageSize_Y, 50);
			cout << endl;
			cout << endl;

			result.writeImage("result.ppm");

			cout << endl;
			break;
		}

		// Pressing s will launch the raytracing with sampling.
		case 's':
		{
			cout << "Raytracing with sampling" << endl;

			// Setup an image with the size of the current image.
			Image result(ImageSize_X, ImageSize_Y);

			// Produce the rays for each pixel, by first computing
			// the rays for the corners of the frustum.
			Vec3Df origin00, dest00;
			Vec3Df origin01, dest01;
			Vec3Df origin10, dest10;
			Vec3Df origin11, dest11;
			Vec3Df origin, dest;

			produceRay(0, 0, &origin00, &dest00);
			produceRay(0, ImageSize_Y - 1, &origin01, &dest01);
			produceRay(ImageSize_X - 1, 0, &origin10, &dest10);
			produceRay(ImageSize_X - 1, ImageSize_Y - 1, &origin11, &dest11);
			
			Vec3Df rgb;

			unsigned int fromY = 0;
			unsigned int toY = 100;

			for (unsigned int y = fromY; y < toY; ++y) {
				for (unsigned int x = 0; x < ImageSize_X; ++x)
				{
					// Produce the rays for each pixel, by interpolating 
					// the four rays of the frustum corners.
					float xscale; // = 1.0f - float(x) / (ImageSize_X - 1);
					float yscale; // = 1.0f - float(y) / (ImageSize_Y - 1);

					for (unsigned int sx = 0; sx < ns; sx++) {
						for (unsigned int sy = 0; sy < ns; sy++) {
							xscale = 1.0f - (x + (sx + 0.5f) / ns) / (ImageSize_X - 1);
							yscale = 1.0f - (y + (sy + 0.5f) / ns) / (ImageSize_Y - 1);

							origin = yscale*(xscale*origin00 + (1 - xscale)*origin10) + (1 - yscale)*(xscale*origin01 + (1 - xscale)*origin11);
							dest = yscale*(xscale*dest00 + (1 - xscale)*dest10) + (1 - yscale)*(xscale*dest01 + (1 - xscale)*dest11);

							// Launch raytracing for the given ray.
							rgb += performRayTracing(origin, dest);
						}
					}
					
					rgb /= float(ns*ns);

					// Store the result in an image 
					result.setPixel(x, y, RGBValue(rgb[0], rgb[1], rgb[2]));
				}

				loadbar(y-fromY, toY-fromY, 50);
			}

			loadbar(toY - fromY, toY - fromY, 50);
			cout << endl;
			cout << endl;

			result.writeImage("result.ppm");

			cout << endl;
			break;
		}

		// ESC pressed
		case 27:
		{
			exit(0);
		}

		// Space pressed, trace our debug line.
		case 32:
		{
			cout << "Raytracing single ray" << endl;

			// Produce the ray for the current mouse position
			Vec3Df testRayOrigin, testRayDestination;
			produceRay(x, y, &testRayOrigin, &testRayDestination);

			yourKeyboardFunc(key, x, y, testRayOrigin, testRayDestination);
		}
	}
}
Ejemplo n.º 2
0
// prise en compte du clavier
void keyboard(unsigned char key, int x, int y) {
  printf("key %d pressed at %d,%d\n",key,x,y);
  fflush(stdout);
  switch (key) {
  	case 'L':
  		MyLightPositions.push_back(getCameraPosition());
  		break;
  	case 'l':
  		MyLightPositions[MyLightPositions.size()-1]=getCameraPosition();
  		break;
  	case 'r': {
  		//C'est nouveau!!!
  		//commencez ici et lancez vos propres fonctions par rayon.
      cout << config.toString() << endl;
  		cout << "Starting raytracing ..." << endl;

      int pixelsTotal, pixelsRendered;
      float fraction, previousFraction;
      bool timing = true;
      clock_t t1, t2;
      if (timing) {
        t1 = clock();
      }
      bool progressBar = true;
      int barLength = 100;
      if (progressBar) {
        pixelsTotal = config.renderSize_X * config.renderSize_Y;
        cout << " 0%";
        for (unsigned int i = 0; i < barLength; ++i) cout << " ";
        cout << "100%" << endl;
        cout << "  |";
        for (unsigned int i = 0; i < barLength; ++i) cout << " ";
        cout << "|" << endl;
      }
  		Image result(config.renderSize_X,config.renderSize_Y);
  		Vec3Df origin00, dest00;
  		Vec3Df origin01, dest01;
  		Vec3Df origin10, dest10;
  		Vec3Df origin11, dest11;
  		Vec3Df origin, dest;


  		produceRay(0,0, &origin00, &dest00);
  		produceRay(0,config.viewportSize_Y-1, &origin01, &dest01);
  		produceRay(config.viewportSize_X-1,0, &origin10, &dest10);
  		produceRay(config.viewportSize_X-1,config.viewportSize_Y-1, &origin11, &dest11);

      if (progressBar) {
        previousFraction = 0;
        cout << "   ";
      }
  		for (unsigned int y=0; y<config.renderSize_X;++y)
  			for (unsigned int x=0; x<config.renderSize_Y;++x)
  			{
  				//svp, decidez vous memes quels parametres vous allez passer à la fonction
  				//e.g., maillage, triangles, sphères etc.
  				float xscale=1.0f-float(x)/(config.renderSize_X-1);
  				float yscale=1.0f-float(y)/(config.renderSize_Y-1);

  				origin=yscale*(xscale*origin00+(1-xscale)*origin10)+
  					(1-yscale)*(xscale*origin01+(1-xscale)*origin11);
  				dest=yscale*(xscale*dest00+(1-xscale)*dest10)+
  					(1-yscale)*(xscale*dest01+(1-xscale)*dest11);


  				Vec3Df rgb = performRayTracing(origin, dest);
  				result.setPixel(x,y, RGBValue(rgb[0], rgb[1], rgb[2]));

        // Update progress bar in console output
        if (progressBar) {
          pixelsRendered = (y * config.renderSize_X) + x;
          if (pixelsRendered > 0)
            fraction = static_cast<float>(pixelsRendered) / static_cast<float>(pixelsTotal);
          else
            fraction = 0;
          float fDelta = fraction - previousFraction;
          if (fDelta > 1.0f/barLength) {
            cout << "#";
            cout.flush();
            // Fraction plus residual
            previousFraction = fraction + (1.0f/barLength - fDelta);
          }
  			}
      }
      if (progressBar)
        cout << "#" << endl;
      if (timing) {
        t2 = clock();
        float diffSeconds = ((static_cast<float>(t2) - static_cast<float>(t1)) / 1000000.0F);
        cout << endl;
        std::cout << "   Render time: " << diffSeconds << " seconds" << std::endl;
        cout << endl;
      }
  		result.writeImage("result.ppm");
  		break;
  	}
  	case 27:     // touche ESC
      exit(0);
  }

	yourKeyboardFunc(key,x,y);
}