Example #1
0
t_pos		cone_normal(t_obj *o, t_pos r, t_pos p)
{
	t_pos			n;
	t_pos			a;
	t_pos			c;
	t_data_cone		*data;

	(void)r;
	(void)p;
	n = get_pos(0, 0, 0);
	data = (t_data_cone*)o->data;
	if (data)
	{
		p = transform(o->o, o->sp, p, o->rot);
		c = get_pos(0, data->top, 0);
		a = get_pos(0, 1, 0);
		if (p.y < data->top)
			a = get_pos(0, -1, 0);
		n = p;
		pos_mult_to_number(&a, pos_norme(pos_vector(c, p))
							/ cos(RAD(data->ang / 2)));
		pos_add_to_pos(&c, a);
		pos_mult_to_number(&c, -1);
		pos_add_to_pos(&n, c);
		n = pos_normalize(n);
	}
	return (n);
}
Example #2
0
static double		is_collision(t_ray *ray, t_data_sphere *data,
								t_obj *obj, t_rt *rt)
{
	double			ret;
	t_pos			u;
	t_pos			v;
	t_pos			e;
	double			tmp;

	ret = -1.;
	if (rt && ray && data)
	{
		u = ray->rd;
		v = pos_vector(obj->sp.o, ray->ro);
		e.x = pos_dot_product(u, u);
		e.y = 2. * pos_dot_product(u, v);
		e.z = pos_dot_product(v, v) - pow(data->radius, 2);
		if ((tmp = delta(e)) < 0.)
			ret = -1.;
		else if (tmp == 0.)
			ret = (-1. * e.y) / (2. * e.x);
		else
			ret = get_dist(e, tmp);
	}
	return (ret);
}
Example #3
0
/**
 * Returns the color based on lighting (points in world coordinates)
 *
 * Normals are scaled here, so they do not need to be unit vectors.
 */
color lighting(point *pos, point *normal, object_copy *obj,
		vector<light> *lights, camera *CAM) {
	color diffuse_sum = { 0.0, 0.0, 0.0 };
	color specular_sum = { 0.0, 0.0, 0.0 };

	Vector3d tmp(normal->x, normal->y, normal->z);
	Vector3d normal_vector = tmp / tmp.norm();

	Vector3d pos_vector(pos->x, pos->y, pos->z);

	Vector3d cam_pos(CAM->position[0], CAM->position[1], CAM->position[2]);

	Vector3d cam_direction = (cam_pos - pos_vector) / (cam_pos - pos_vector).norm();

	for (unsigned int i = 0; i < lights->size(); i++) {
		Vector3d light_pos((*lights)[i].position.x,
				(*lights)[i].position.y,
				(*lights)[i].position.z);

		Vector3d light_dir = (light_pos - pos_vector) /
				(light_pos - pos_vector).norm();


		float distance = (light_pos - pos_vector).norm();
		float attenuation = 1.0 / (1 + (*lights)[i].k * distance * distance);

		Vector3d midpoint = (cam_direction + light_dir) /
				(cam_direction + light_dir).norm();

		float tmp2 = light_dir.dot(normal_vector);
		float tmp3 = normal_vector.dot(light_dir);

		float diffuse_mult = attenuation *
				max(0.0, light_dir.dot(normal_vector));

		float tmp4 = normal_vector.dot(midpoint);
		float specular_mult = attenuation *
				pow(max(0.0, normal_vector.dot(midpoint)), obj->shininess);

		diffuse_sum += (*lights)[i].col * diffuse_mult;
		specular_sum += (*lights)[i].col * specular_mult;
	}
	color final_color = obj->ambient + (diffuse_sum * obj->diffuse) + (specular_sum * obj->specular);

	if (final_color.r > 1.0) {
		final_color.r = 1.0;
	}
	if (final_color.g > 1.0) {
		final_color.g = 1.0;
	}
	if (final_color.b > 1.0) {
		final_color.b = 1.0;
	}
	return final_color;
}
Example #4
0
inline boost::array<typename Plane<T_>::length_type, 3>
to_internal(Plane<T_> const& obj, typename Plane<T_>::position_type const& pos)
{
    typedef typename Plane<T_>::position_type position_type;
    position_type pos_vector(subtract(pos, obj.position()));

    return array_gen<typename Plane<T_>::length_type>(
        dot_product(pos_vector, obj.unit_x()),
        dot_product(pos_vector, obj.unit_y()),
        dot_product(pos_vector, obj.unit_z()));
}
Example #5
0
File: Box.hpp Project: kozo2/ecell4
inline boost::array<Box::length_type, 3>
to_internal(Box const& obj, Box::position_type const& pos)
{
    // Return pos relative to position of box. 
    typedef Box::position_type position_type;
    position_type pos_vector(subtract(pos, obj.position()));

    return array_gen<Box::length_type>(
        dot_product(pos_vector, obj.unit_x()),
        dot_product(pos_vector, obj.unit_y()),
        dot_product(pos_vector, obj.unit_z()));
}