Example #1
0
void Cylindre::computeUV(Hit& hit) const
{

	float u,v;
	qglviewer::Vec inter = hit.intersection();
	u = atan2(inter.y,inter.x);
	// mettre sur [0 1] 
	if ( u < 0 ){
		u = u + 2*M_PI;
	}
	u = u/(2*M_PI);
	
	v = inter.z/height();
	hit.setCoord(u,v);
}
Example #2
0
bool Plane::intersect(const Ray& ray, Hit& hit) const
{
#ifdef DEBUG_INTERSEC
	std::cout << "TENTATIVE d'intersection du rayon :\n" << ray << "avec le plan\n" << *this << std::endl;
#endif
	// on place le ray dans le répère associé au plan
	Ray ray_associe_plan(frame().coordinatesOf(ray.start()),
			     frame().transformOf(ray.direction()));
	
#ifdef DEBUG_INTERSEC
	std::cout << "rayon dans l'espace du plan : \n" << ray_associe_plan << std::endl;
#endif
	// on vérifie que le ray pointe vers le plan
	if ( ( ray_associe_plan.start().z <= 0 && ray_associe_plan.direction().z <= 0 )
	     || 
	     ( ray_associe_plan.start().z >= 0 && ray_associe_plan.direction().z >= 0 )
		)
	{
#ifdef DEBUG_INTERSEC
		std::cout << "PAS D'INTERSECTION" << std::endl;
#endif
		return false;
	}
	// on détermine le t de l'intersection dans la formule
	// zstart + t*zdir = 0
	float t = -ray_associe_plan.start().z/ray_associe_plan.direction().z;

	if ( hit.time() < t ){
#ifdef DEBUG_INTERSEC
		std::cout << "PAS D'INTERSECTION : " << t << " > " << hit.time() << std::endl;
#endif
		return false;
	}


#ifdef DEBUG_INTERSEC
	std::cout << "Intersection au temps : " << t << std::endl;
#endif
	// Calcul des coordonnées de l'intersection dans le plan et
	// rejet si on dépasse
	float u,v;
	u = ray_associe_plan.start().x+t*ray_associe_plan.direction().x;
	if ( width_ > 0 && (u > width_/2.0 || u < -width_/2.0) ){
#ifdef DEBUG_INTERSEC
		std::cout << "PAS D'INTERSECTION : u = " << u << ", width = " << width_ << std::endl;
#endif
		return false;
	}
	v = ray_associe_plan.start().y+t*ray_associe_plan.direction().y;
	if ( height_ > 0 && (v > height_/2.0 || v < -height_/2.0) ){
#ifdef DEBUG_INTERSEC
		std::cout << "PAS D'INTERSECTION : v = " << v << ", height = " << height_ << std::endl;
#endif
		return false;
	}
	qglviewer::Vec inter_plan = qglviewer::Vec(u,v,0);

#ifdef DEBUG_INTERSEC
	std::cout << "INTERSECTION d'un rayon avec le plan \n"<< *this << "\tau temps : " << t << std::endl;
#endif
 	hit.setTime(t);
	hit.setIntersection(frame().inverseCoordinatesOf(inter_plan));
	if ( ray_associe_plan.start().z <= 0 ){
		hit.setNormal(frame().inverseTransformOf(qglviewer::Vec(0,0,-1)));
	}else{
		hit.setNormal(frame().inverseTransformOf(qglviewer::Vec(0,0,1)));		
	}
	hit.setMaterial(material());
	
	// On envoie les coordonnées de la texture (entre 0 et 1 si la
	// partie est finie)
	if ( width_ > 0 ){
		u = u/width_ + 0.5;
	}
	if ( height_ > 0 ){
		v = v/height_ + 0.5;
	}
#ifdef DEBUG_INTERSEC
	std::cout << "Coordonnées de textures ("<< u << "," << v << ")" << std::endl;
#endif
	hit.setCoord(u,v);
	
	return true;
#ifdef DEBUG
	cerr << "Erreur lors du calcul de l'intersection d'un objet avec un rayon" << endl;
#endif
	return false;
}