////////////////////////////////////////////////////////////////////// // Create an image and write it out to a file ////////////////////////////////////////////////////////////////////// int main() { int width = 400; int height = 400; VEC3F* image = new VEC3F[width * height]; // pixel values VEC3F* ray[width * height]; // array of VEC3F pointers int maxFrames = 1; // set to 30 to make 30 sequenced images for(int v = 0; v < maxFrames; v++) { int i = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, i++) { ray[i] = generateRay(x, y, -0.5f, 0.5f, -0.5f, 0.5f, width, height, v); // generate Ray image[i] = rayColor(ray[i]); // get RGB pixel color of ray intersecting on surface } } //save sequence of images for movie // std::stringstream stream; // stream << "image" << v << ".ppm"; // std::string arraystring = stream.str(); // const char* filename = arraystring.c_str(); //writePPM(image, width, height, filename); } // v for loop writePPM(image, width, height, "image.ppm"); // comment this when saving movie delete[] image; return 0; }
void drawScene (GLint nColorFilter) { // Declare data structures on stack to avoid malloc & free point worldPix; // Current pixel in world coordinates point direction; // direction.w will be set to 0 since it is a vector ray r; // ray with start point worldPix and end vector direction color c; // z and w are constant for all pixels worldPix.w = 1.0; worldPix.z = pnear; // viewing plane is (x, y, z=1.0) r.start = &worldPix; r.end = &direction; GLfloat imageWidth = 2 * pnear * tan(fovx / 2); // Trace a ray for every pixel for ( int i = 0; i < width; i++ ) { for ( int j = 0; j < height; j++ ) { // find position of pixel in world coordinates // y position = (pixel height/middle) scaled to world coords worldPix.y = (j - (height / 2))* imageWidth / width; // x position = (pixel width/middle) scaled to world coords worldPix.x = (i - (width / 2))* imageWidth / width; // find direction // note: direction vector is NOT NORMALIZED calculateDirection(&viewpoint, &worldPix, &direction); // direction calculated here... direction.w <-- 0 // Find color for pixel rayColor(r, &c, nColorFilter); // write the pixel! drawPixel(i, j, c.r, c.g, c.b); } } }