rgb_value_t Sphere::shade_reflective(Ray* ray, Light* light, std::list<Sphere*>* sphereList, coord_t* point, int ref_cnt){ rgb_value_t darkness, ret_val; darkness.r = 0; darkness.g = 0; darkness.b = 0; coord_t norm_vect; norm_vect = subtract_coord(point, ¢er); norm_vect = normalize_vect(&norm_vect); coord_t reflect_dir; reflect_dir = subtract_coord_nopt(ray->getDirection(), mult_vect(&norm_vect, 2.0*(scalar_mult_vect_nopt(norm_vect, ray->getDirection())))); if (ref_cnt > 0) { Ray* reflectedRay = new Ray(point, &reflect_dir); Sphere* pointer_dummy=this; ret_val = reflectedRay->raytrace(sphereList, light, &pointer_dummy, ref_cnt-1); delete reflectedRay; return ret_val; } else { return darkness; } }
rgb_value_t Sphere::shade_glass(Ray* ray, Light* light, std::list<Sphere*>* sphereList, coord_t* point, int ref_cnt){ rgb_value_t darkness, ret_val; darkness.r = 0; darkness.g = 0; darkness.b = 0; if (ref_cnt == 0) return darkness; coord_t norm_ray_dir_vect = ray->getDirection(); norm_ray_dir_vect = normalize_vect(&norm_ray_dir_vect); coord_t norm_vect; norm_vect = subtract_coord(point, ¢er); norm_vect = normalize_vect(&norm_vect); bool ray_going_inside = (scalar_mult_vect(&norm_vect, &norm_ray_dir_vect)<=0.0) ? true : false; coord_t refract_dir; float r, w, k; if (ray_going_inside) { r = 1/brechzahl; } else { r = brechzahl; } w = -scalar_mult_vect(&norm_vect, &norm_ray_dir_vect)*r; k = 1.0 + (w-r)*(w+r); if (k<0.0) { return this->shade_reflective(ray, light, sphereList, point, ref_cnt-1); } norm_ray_dir_vect = mult_vect(&norm_ray_dir_vect, r); norm_vect = mult_vect(&norm_vect, (w-sqrt(k))); refract_dir = add_coord(&norm_ray_dir_vect, &norm_vect); Ray* refractedRay = new Ray(point, &refract_dir); Sphere* pointer_dummy=this; ret_val = refractedRay->raytrace(sphereList, light, &pointer_dummy, ref_cnt-1); delete refractedRay; return ret_val; }
static void test_normalize() { vec3 v; v.x = 1; v.y = 0; v.z = 0; normalize_vect(&v); assert(v.x == 1); assert(v.y == 0); assert(v.z == 0); v.x = 3; v.y = 1; v.z = 2; normalize_vect(&v); assert(equal_f3(v.x, 0.802)); assert(equal_f3(v.y, 0.267)); assert(equal_f3(v.z, 0.535)); }
int pt_lighted(t_obj *obj, t_point pt, t_light *light) { t_ray ray; float dist_to_light; ray.orig.x = pt.x; ray.orig.y = pt.y; ray.orig.z = pt.z; ray.dir.x = light->orig.x - ray.oriug.x; ray.dir.y = light->orig.y - ray.oriug.y; ray.dir.z = light->orig.z - ray.oriug.z; dist_to_light = vect_norm(&ray.dir); normalize_vect(&ray, 0, obj, 0); if (ray.inter_t <= dist_ti_light) return (0); return (1); }
rgb_value_t Sphere::shade_non_reflective(Ray* ray, Light* light, std::list<Sphere*>* sphereList, coord_t* point){ rgb_value_t darkness; darkness.r = 0; darkness.g = 0; darkness.b = 0; coord_t dist_ray_vect; float dist_ray_abs; coord_t dist_light_vect; float dist_light_abs; dist_ray_vect = subtract_coord_nopt(this->getCenter(), ray->getStartPoint()); dist_ray_abs = vect_abs(&dist_ray_vect); dist_light_vect = subtract_coord_nopt(this->getCenter(), light->getPosition()); dist_light_abs = vect_abs(&dist_light_vect); bool light_inside = (dist_light_abs<=this->getRadius()) ? true : false; bool ray_inside = (dist_ray_abs<=this->getRadius()) ? true : false; if (light_inside!=ray_inside) { return darkness; } if (!light_inside) { //std::cout << "outside" << std::endl; coord_t norm_vect; norm_vect = subtract_coord(point, ¢er); coord_t light_point_vect; light_point_vect = subtract_coord_nopt(*point, light->getPosition()); float light_point_abs = vect_abs(&light_point_vect); float parallelism = scalar_mult_vect(&norm_vect, &light_point_vect); if (parallelism>0.0) return darkness; coord_t light_pos = light->getPosition(); Ray* lightRay = new Ray(&light_pos, &light_point_vect); float buffer; for (std::list<Sphere*>::iterator p = (*sphereList).begin(); p!=(*sphereList).end(); p++) { if ((*p)!=this) { if ((*p)->intersectsWithRay(lightRay, &buffer)) { if (buffer<light_point_abs) { delete lightRay; return darkness; } } } } delete lightRay; norm_vect = normalize_vect(&norm_vect); light_point_vect = div_vect(&light_point_vect, -light_point_abs); parallelism = scalar_mult_vect(&norm_vect, &light_point_vect); rgb_value_t rgb_ret = this->getColour(); rgb_ret.r *= parallelism; rgb_ret.g *= parallelism; rgb_ret.b *= parallelism; return rgb_ret; } else { //std::cout << "inside" << std::endl; coord_t norm_vect; norm_vect = subtract_coord(point, ¢er); coord_t light_point_vect; light_point_vect = subtract_coord_nopt(*point, light->getPosition()); float light_point_abs = vect_abs(&light_point_vect); coord_t light_pos = light->getPosition(); Ray* lightRay = new Ray(&light_pos, &light_point_vect); float buffer; for (std::list<Sphere*>::iterator p = (*sphereList).begin(); p!=(*sphereList).end(); p++) { if ((*p)!=this) { if ((*p)->intersectsWithRay(lightRay, &buffer)) { if (buffer<light_point_abs) { delete lightRay; return darkness; } } } } delete lightRay; norm_vect = normalize_vect(&norm_vect); light_point_vect = div_vect(&light_point_vect, light_point_abs); float parallelism = scalar_mult_vect(&norm_vect, &light_point_vect); rgb_value_t rgb_ret = this->getColour(); rgb_ret.r *= parallelism; rgb_ret.g *= parallelism; rgb_ret.b *= parallelism; return rgb_ret; } }
void Camera::makeSnapShot(std::list<Sphere*>* sphereList, Light* light, int ref_max, bool show) { coord_t ray_dir_temp, ray_dir, ray_dir_projection_help, focal_point; focal_point = point_on_straight(&optical_center, &direction, -focal_length); rgb_value_t pixel_temp; Sphere* dummy_pointer = NULL; Ray privateRay(&focal_point, &focal_point); omp_set_num_threads(2); #pragma omp parallel for firstprivate(privateRay, ray_dir_temp, ray_dir, pixel_temp, ray_dir_projection_help) schedule(static,1) for (int i=0; i<sensor_height_px; i++) { for (int j=0; j<sensor_width_px; j++) { //initial Koordinatenursprung ray_dir.y = j*px_width -sensor_width_physical/2; ray_dir.z = i*px_height -sensor_height_physical/2; ray_dir.x = 0.0; //rotation um y achse ray_dir_projection_help = direction; ray_dir_projection_help.y= 0.0; ray_dir_projection_help = normalize_vect(&ray_dir_projection_help); if (ray_dir_projection_help.x < 0.0) ray_dir_projection_help.x=-ray_dir_projection_help.x; ray_dir_temp.x = -ray_dir.z*ray_dir_projection_help.z; ray_dir_temp.y = ray_dir.y; ray_dir_temp.z = ray_dir.z*ray_dir_projection_help.x; //rotation um z achse ray_dir_projection_help = direction; ray_dir_projection_help.z= 0.0; ray_dir_projection_help = normalize_vect(&ray_dir_projection_help); ray_dir.x = ray_dir_temp.x*ray_dir_projection_help.x - ray_dir_temp.y*ray_dir_projection_help.y; ray_dir.y = ray_dir_temp.x*ray_dir_projection_help.y + ray_dir_temp.y*ray_dir_projection_help.x; ray_dir.z = ray_dir_temp.z; //ray_dir = ray_dir_temp; //translation to optical center ray_dir = add_coord(&ray_dir, &optical_center); ray_dir = subtract_coord(&ray_dir, &focal_point); privateRay.setStartPoint(&optical_center); privateRay.setDirection(&ray_dir); pixel_temp = privateRay.raytrace(sphereList, light, &dummy_pointer, ref_max); image_matrix->at<Vec3b>(i,j)[2] = pixel_temp.r; image_matrix->at<Vec3b>(i,j)[1] = pixel_temp.g; image_matrix->at<Vec3b>(i,j)[0] = pixel_temp.b; } } if (show) { namedWindow( "Raytrace", CV_WINDOW_AUTOSIZE ); imshow("Raytrace", *image_matrix); std::cout << "BLA" << std::endl; cvWaitKey(0); } else { //imwrite("raytrace.jpg", image_matrix); } }
void Camera::setDir(coord_t dir) { direction = normalize_vect(&dir); }
void Ray::setDirection (coord_t* coord) { direction = normalize_vect(coord); }