/** Display callback; render the scene. */ void handle_display() { // The ray itself. ray3_t ray ; ray.base = eye ; color_t color ; #ifndef NDEBUG clock_t start_time, end_time ; start_time = clock() ; #endif for (int x=0; x<win_width; ++x) { for (int y=0; y<win_height; ++y) { win2world(x, y, &ray.dir) ; debug_c((x==400 && y==300), "view ray = {(%f, %f, %f), (%f, %f, %f)}.\n", eye.x, eye.y, eye.z, ray.dir.x, ray.dir.y, ray.dir.z) ; color = ray_trace(ray, 1.0 + EPSILON, FLT_MAX, 5) ; *(fb+fb_offset(y, x, 0)) = color.red ; *(fb+fb_offset(y, x, 1)) = color.green ; *(fb+fb_offset(y, x, 2)) = color.blue ; } } #ifndef NDEBUG end_time = clock() ; debug("handle_display(): frame calculation time = %f sec.", ((double)(end_time-start_time))/CLOCKS_PER_SEC) ; #endif glWindowPos2s(0, 0) ; glDrawPixels(win_width, win_height, GL_RGB, GL_FLOAT, fb) ; glFlush() ; glutSwapBuffers() ; }
/** Compute the viewing ray direction in the world frame basis. * * @param x the x-position on the window (in pixels), starting at the left. * @param y the y-position on the window (in pixels), starting at the top. * @param dir a vector3_t object that will be filled with the coordinates * (in the world frame basis) for the direction of the viewing ray * through <code>(x, y)</code>. */ void win2world(int x, int y, vector3_t* dir) { // Compute coordinates in eye frame of corners of view plane. float left = -view_plane_width/2.0f; float bottom = -view_plane_height/2.0f; // Compute vector from eye to window position in eye coordinates. float u = left + (x+.5f)/win_width*view_plane_width; float v = bottom + (y+.5f)/win_height*view_plane_height; float w = -view_plane_dist; debug_c((x == 400 && y == 300), "win2world(): u, v, w, = %f, %f, %f", u, v, w); // Transform vector to world coordinates. dir->x = u*eye_frame_u.x + v*eye_frame_v.x + w*eye_frame_w.x; dir->y = u*eye_frame_u.y + v*eye_frame_v.y + w*eye_frame_w.y; dir->z = u*eye_frame_u.z + v*eye_frame_v.z + w*eye_frame_w.z; debug_c((x==400 && y==300), "win2world(): i, j, k = %f, %f, %f\n", dir->x, dir->y, dir->z); }
/* start quit or write debug, always run at the end of all key functions*/ int quit_key(int input_character) { switch(input_character) { case K_EXIT: loc_player->state = PS_NO_PLAYER; return 1; break; default: /* in this case we shouldn't redraw the screen */ debug_c(1, "unsupported key", input_character); } return 0; }
/** Display callback; render the scene. */ void handle_display() { // The ray itself. ray3_t ray; ray.base = eye; color_t color; #ifndef NDEBUG clock_t start_time, end_time; start_time = clock(); #endif for (int x=0; x<win_width; ++x) { for (int y=0; y<win_height; ++y) { win2world(x, y, &ray.dir); debug_c((x==400 && y==300), "view ray = {(%f, %f, %f), (%f, %f, %f)}.\n", eye.x, eye.y, eye.z, ray.dir.x, ray.dir.y, ray.dir.z); //Start ray eye assuming we're not inside a transparent surface. color = ray_trace(ray, 1.0 + EPSILON, FLT_MAX, 5, false); *(fb+fb_offset(y, x, 0)) = color.red; *(fb+fb_offset(y, x, 1)) = color.green; *(fb+fb_offset(y, x, 2)) = color.blue; } } #ifndef NDEBUG end_time = clock(); debug("handle_display(): frame calculation time = %f sec.", ((double)(end_time-start_time))/CLOCKS_PER_SEC); #endif // The following line throws a implicit declaration compiler warning: but // it was in hw2bp1.c solution file so I will ignore it. glWindowPos2s(0, 0); glDrawPixels(win_width, win_height, GL_RGB, GL_FLOAT, fb); glFlush(); glutSwapBuffers(); }