// 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); } } }
// 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/update a light based on the camera position. case 'L': MyLightPositions.push_back(getCameraPosition()); break; case 'l': MyLightPositions[MyLightPositions.size()-1]=getCameraPosition(); break; case 'r': { clock_t t; t = clock(); //Pressing r will launch the raytracing. cout<<"\n\n"<<endl; //Setup an image with the size of the current image. Image result(WindowSize_X,WindowSize_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,WindowSize_Y-1, &origin01, &dest01); produceRay(WindowSize_X-1,0, &origin10, &dest10); produceRay(WindowSize_X-1,WindowSize_Y-1, &origin11, &dest11); std::vector<std::future<Vec3Df> > threads; float progressc(0.f); printf("\e[?25l"); /* hide the cursor */ for (unsigned int y=0; y<WindowSize_Y;++y) { for (unsigned int x=0; x<WindowSize_X;++x) { //produce the rays for each pixel, by interpolating //the four rays of the frustum corners. float xscale=1.0f-float(x)/(WindowSize_X-1); float yscale=1.0f-float(y)/(WindowSize_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; rgb.init(0,0,0); //launch raytracing for the given ray. threads.push_back(std::async(performRayTracing, 0, origin, dest)); } } int count = 0; for (unsigned int y = 0; y < WindowSize_Y; ++y) { for (unsigned int x = 0; x < WindowSize_X; ++x) { ++progressc; Vec3Df rgb = threads[count].get(); //store the result in an image result.setPixel(x, y, RGBValue(rgb[0], rgb[1], rgb[2])); printf("\r[Raytracing][%.0f%%]", (progressc / (WindowSize_X*WindowSize_Y)) * 100); // ++count; } } threads.resize(0); printf("\e[?25h"); /* hide the cursor */ result.writeImage("result.ppm"); t = clock() - t; std::vector<Triangle>& triangles = MyMesh.triangles; int size = triangles.size(); printf ("\n[Render time: %f s | Triangles: %d | Resolution: %dx%d (%dpx)]\n\n\n",((float)t)/CLOCKS_PER_SEC, size, WindowSize_X, WindowSize_Y, WindowSize_X*WindowSize_Y); break; } case 27: // touche ESC exit(0); } //produce the ray for the current mouse position Vec3Df testRayOrigin, testRayDestination; produceRay(x, y, &testRayOrigin, &testRayDestination); yourKeyboardFunc(key,x,y, testRayOrigin, testRayDestination); }
// 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); }