Canvas * detect_edges_canvas(Canvas * base, int num_threads) { Canvas * grayscaled_canv = grayscale_canvas(base, num_threads); const int w = base->w; const int h = base->h; Canvas * grad_canv = new_canvas(w, h); omp_set_num_threads((num_threads < 2) ? 1 : num_threads); int x; int y; #pragma omp parallel private(x, y) #pragma omp for collapse(2) schedule(dynamic, IMG_CHUNK) for(x = 1; x < w - 1; ++x) { for(y = 1; y < h - 1; ++y) { int i; int j; int gx = 0; for(i = -1; i < 2; ++i) { for(j = -1; j < 2; ++j) { gx += mattrix_x[i + 1][j + 1] * get_pixel(x + i, y + j, grayscaled_canv).r; } } int gy = 0; for(i = -1; i < 2; ++i) { for(j = -1; j < 2; ++j) { gy += mattrix_y[i + 1][j + 1] * get_pixel(x + i, y + j, grayscaled_canv).r; } } Byte grad = (Byte) sqrt(gx * gx + gy * gy); set_pixel(x, y, rgb(grad, grad, grad), grad_canv); } } release_canvas(grayscaled_canv); return grad_canv; }
int main(void) { Camera * camera = create_camera(); Canvas * canvas = new_canvas(CANVAS_W, CANVAS_H); int i; for(i = 0; i < MAX_SERPINSKY_PYRAMID_LEVEL; i++) { Scene * scene = new_scene(MAX_OBJECTS_NUMBER, MAX_LIGHT_SOURCES_NUMBER, BACKGROUND_COLOR); create_serpinsky_pyramid(scene, i); // Randomness causes unreproducible results // But in general - results are similar to Serpinsky pyramid //generate_random_spheres(scene, i * 400); prepare_scene(scene); printf("Number of polygons: %i. ", scene->last_object_index + 1); render_scene(scene, camera, canvas, THREADS_NUM); release_scene(scene); } release_canvas(canvas); release_camera(camera); return 0; }
int main(void) { // Allocating scene Scene * scene = new_scene(MAX_OBJECTS_NUMBER, MAX_LIGHT_SOURCES_NUMBER, BACKGROUND_COLOR); // Allocating new sphere Float radius = 100; Point3d center = point3d(0, 0, 0); Color sphere_color = rgb(250, 30, 30); Material sphere_material = material(1, 5, 5, 10, 0, 10); Object3d * sphere = new_sphere(center, radius, sphere_color, sphere_material); // Adding sphere to the scene add_object(scene, sphere); // Allocating new triangle Object3d * triangle = new_triangle(point3d(-700, -700, -130), // vertex 1 point3d( 700, -700, -130), // vertex 2 point3d( 0, 400, -130), // vertex 3 rgb(100, 255, 30), // color material(1, 6, 0, 2, 0, 0) // surface params ); // Adding triangle to the scene add_object(scene, triangle); // Loading 3D model of cow from *.obj file // defining transformations and parameters of 3D model // TODO: must be refactored... SceneFaceHandlerParams load_params = new_scene_face_handler_params(scene, // scale: 40, // move dx, dy, dz: -150, -100, 30, // rotate around axises x, y, z: 0, 0, 0, // color rgb(200, 200, 50), // surface params material(2, 3, 0, 0, 0, 0) ); load_obj("./demo/models/cow.obj", // default handler which adding polygons of 3D model to scene: scene_face_handler, &load_params); // This function is requried (bulding k-d tree of entire scene) prepare_scene(scene); printf("\nNumber of polygons: %i\n", scene->last_object_index + 1); // Allocating new light source Color light_source_color = rgb(255, 255, 255); Point3d light_source_location = point3d(-300, 300, 300); LightSource3d * light_source = new_light_source(light_source_location, light_source_color); // Adding light source to the scene add_light_source(scene, light_source); // Adding fog Float density = 0.002; set_exponential_fog(scene, density); // Allocating camera // TODO: It's a pity, but quaternions are not implemented yet :( Point3d camera_location = point3d(0, 500, 0); Float focus = 320; Float x_angle = -1.57; Float y_angle = 0; Float z_angle = 3.14; Camera * camera = new_camera(camera_location, x_angle, y_angle, z_angle, focus); // Rotate camera if needed // rotate_camera(camera, d_x_angle, d_y_angle, d_z_angle); // Move camera if needed // move_camera(camera, vector3df(d_x, d_y, d_z)); // Alocate new canvas, to render scene on it Canvas * canvas = new_canvas(CANVAS_W, CANVAS_H); render_scene(scene, camera, canvas, THREADS_NUM); // Saving rendered image in PNG format write_png("example.png", canvas); release_canvas(canvas); release_scene(scene); release_camera(camera); return 0; }
void render_scene(const Scene * const scene, const Camera * const camera, Canvas * canvas, const int num_threads) { const int w = canvas->w; const int h = canvas->h; const Float dx = w / 2.0; const Float dy = h / 2.0; const Float focus = camera->proj_plane_dist; // TODO: consider possibility to define these OpenMP parameters // in declarative style (using directives of preprocessor) omp_set_num_threads((num_threads < 2) ? 1 : num_threads); #ifdef RAY_INTERSECTIONS_STAT // intersections_per_ray is not atomic variable // avoid multithreaded rendering to prevent from race-conditions // in case of incrementing this variable omp_set_num_threads(1); intersections_per_ray = 0; #endif // RAY_INTERSECTIONS_STAT int i; int j; #pragma omp parallel private(i, j) #pragma omp for collapse(2) schedule(dynamic, CHUNK) for(i = 0; i < w; i++) { for(j = 0; j < h; j++) { const Float x = i - dx; const Float y = j - dy; const Vector3d ray = vector3df(x, y, focus); const Color col = trace(scene, camera, ray); set_pixel(i, j, col, canvas); } } // TODO: argument of the function? global variable? const int antialiasing = ANTIALIASING; if(antialiasing) { Canvas * edges = detect_edges_canvas(canvas, num_threads); #pragma omp parallel private(i, j) #pragma omp for collapse(2) schedule(dynamic, CHUNK) for(i = 1; i < w - 1; i++) { for(j = 1; j < h - 1; j++) { // edges canvas is grayscaled // it means that color components (r, g, b) are equal Byte gray = get_pixel(i, j, edges).r; // TODO: improve if(gray > 10) { const Float x = i - dx; const Float y = j - dy; Color c = get_pixel(i, j, canvas); const Float weight = 1.0 / 4; c = mul_color(c, weight); c = add_colors(c, mul_color(trace(scene, camera, vector3df(x + 0.5, y, focus)), weight)); c = add_colors(c, mul_color(trace(scene, camera, vector3df(x, y + 0.5, focus)), weight)); c = add_colors(c, mul_color(trace(scene, camera, vector3df(x + 0.5, y + 0.5, focus)), weight)); set_pixel(i, j, c, canvas); } } } release_canvas(edges); } #ifdef RAY_INTERSECTIONS_STAT intersections_per_ray /= (w * h); printf("Average intersections number per pixel: %li\n", intersections_per_ray); #endif // RAY_INTERSECTIONS_STAT }