bool detect_collision_point(const int aircraft[], const int obstacle[], const int aircraft_vel[], const int obtacle_vel[], int collision_point[], bool *ray){ bool valid_t; int collision_2d[3]; t = calculate_t(aircraft,obstacle,aircraft_vel,obstacle_vel,&valid_t); if ( valid_t == false ){ if ( ray_tracing(aircraft,obstacle,aircraft[3]+obstacle[3],collision_2d) ){ *ray = true; return true; } else { return false; } } else { collision_point[0] = aircraft[0] + aircraft_vel[0]*t; collision_point[1] = aircraft[1] + aircraft_vel[1]*t; collision_point[2] = aircraft[2] + aircraft_vel[2]*t; *ray = false; return true; } }
/* point_position() calculates an average 3D position implied by the rays sent toward it from cameras through the image projections of the point. Arguments: vec2d targets[] - for each camera, the 2D metric, flat, centred coordinates of the identified point projection. int num_cams - number of cameras ( = number of elements in ``targets``). mm_np *multimed_pars - multimedia parameters struct for ray tracing through several layers. Calibration* cals[] - each camera's calibration object. vec3d res - the result average point. Returns: The ray convergence measure, an average of skew ray distance across all ray pairs. */ double point_position(vec2d targets[], int num_cams, mm_np *multimed_pars, Calibration* cals[], vec3d res) { int cam, pair; /* loop counters */ int num_used_pairs = 0; /* averaging accumulators */ double dtot = 0; vec3d point_tot = {0., 0., 0.}; vec3d* vertices = (vec3d *) calloc(num_cams, sizeof(vec3d)); vec3d* directs = (vec3d *) calloc(num_cams, sizeof(vec3d)); vec3d point; /* Shoot rays from all cameras. */ for (cam = 0; cam < num_cams; cam++) { if (targets[cam][0] != PT_UNUSED) { ray_tracing(targets[cam][0], targets[cam][1], cals[cam], *multimed_pars, vertices[cam], directs[cam]); } } /* Check intersection distance for each pair of rays and find position */ for (cam = 0; cam < num_cams; cam++) { if (targets[cam][0] == PT_UNUSED) continue; for (pair = cam + 1; pair < num_cams; pair++) { if (targets[pair][0] == PT_UNUSED) continue; num_used_pairs++; dtot += skew_midpoint(vertices[cam], directs[cam], vertices[pair], directs[pair], point); vec_add(point_tot, point, point_tot); } } free(vertices); free(directs); vec_scalar_mul(point_tot, 1./num_used_pairs, res); return (dtot / num_used_pairs); }
void Engine::rendering() { timer.Start(); unsigned int w = scene->get_cam()->get_width(); unsigned int h = scene->get_cam()->get_height(); unsigned int i, j; Camera * cam = scene->get_cam(); int aa = cam->get_aa(); #ifndef use_tbb #pragma omp parallel for private(i, j) firstprivate(aa) schedule(static, grainsize) for (j = 0; j < h; j++) for (i = 0; i < w; i++) { vec4 color_total; for (unsigned int s = 0; s < aa; s++) { color_total += ray_tracing(cam->get_ray(i, j, s), depth); } color_total /= static_cast<float>(aa); vbuf[j * w + i] = to_color(color_total); } #else //static tbb::task_scheduler_init init ( threads ); //static tbb::affinity_partitioner ap; tbb::parallel_for ( tbb::blocked_range<size_t> ( static_cast<size_t>(0), w * h, grainsize), [&]( const tbb::blocked_range<size_t> & range ) { size_t i, j; for ( size_t ind = range.begin(); ind < range.end(); ++ind) { i = ind / w; j = ind - i * w; vec4 color_total; for (size_t s = 0; s < aa; ++s) { color_total += ray_tracing(cam->get_ray(i, j, s), depth); } color_total /= static_cast<float>(aa); vbuf[ind] = to_color(color_total); } }//, ap //, tbb::auto_partitioner() ); #endif timer.Stop(); fps = static_cast<float>(timer.OperationPerSecond()); num_frame++; }
vec4 Engine::ray_tracing(const Ray & ray, size_t depth_local) { vec4 point; float t; Primitive * obj = scene->crossing(ray, t); vec4 color(0.0f); if (obj == NULL) { return color; } else { vec4 rdir = ray.dir(); vec4 rpos = ray.pos(); point = rpos + t * rdir; int cl = scene->get_lights(); vec4 normal = obj->get_normal(point); vec4 ref = reflect(normal, rdir); Light light; color = vec4(0.05f, 0.05f, 0.05f, 0.0f); for (int i = 0; i < cl; i++) { light = scene->get_light(i); vec4 l_pos = light.pos(); vec4 l_col = light.color(); vec4 dir_to_l = normalize(l_pos - point); Ray ray_shadow(point + dir_to_l * 0.00025f, dir_to_l); Primitive * obj_shadow = scene->crossing(ray_shadow, t); if (obj_shadow == NULL || calc_distance(point, l_pos) < t) { float d = dot(normal, dir_to_l); if (d < 0.0f) d = 0.0f; color += l_col * d; float tt = dot(ref, normalize(rpos - point)); if (tt > 0.0f) { color += vec4(0.75f) * powf(tt, 64.0f); // spec } } } if ( depth_local > 1) { Ray rray(point + ref * 0.0025f, ref); color += ray_tracing(rray, depth_local - 1) * 0.75f; } return color; } }