int main() { readFile(); setRay(); ColorImage image; image.init(Rw, Rh); Pixel pixelColor; for (int i = 0; i < Rw; ++i) { for (int j = 0; j < Rh; ++j) { /// generate the ray. vec3 ray = dir + i*hori - j*vert; ray = ray.normalize(); bestDistance = 5; /// test if it intersected with all triangles. for (unsigned int t = 0; t < Triangle.size(); t+=3) { if( isIntersectedTri(ray, Triangle[t], Triangle[t+1], Triangle[t+2]) ) { pixelColor = {color, color, color}; image.writePixel(i, j, pixelColor); } } /// test if it intersected with all spheres. // for (unsigned int s = 0; s < Sphere.size(); ++s) { // if( isIntersectedSph(ray, Sphere[s]) ) { // image.writePixel(i, j, pixelColor); // } // } } } char outputname[15] = "hw1_output.ppm"; image.outputPPM(outputname); return 0; }
void output(int sampling, string outputFileName[], Viewport &viewport) { ColorImage image; Pixel p = { 0, 0, 0 }; image.init(viewport.width, viewport.width); for (int i = 0; i < viewport.height; i++) { for (int j = 0; j < viewport.width; j++) { viewport.pixel[i][j] *= 255; for (int k = 0; k < 3; k++) { if (viewport.pixel[i][j][k] > 255) { viewport.pixel[i][j][k] = 255; } } p.R = viewport.pixel[i][j][0]; p.G = viewport.pixel[i][j][1]; p.B = viewport.pixel[i][j][2]; image.writePixel(j, i, p); } } image.outputPPM(const_cast<char*>((outputFileName[sampling] + ".ppm").c_str())); }