예제 #1
0
void RayTracer::traceTris(const int start, const int stop, const Vec3f& dir, const Vec3f& orig, int& imin, float& t, float& umin, float& vmin, RaycastResult& result, std::vector<Vec3f> *lines) const
{
	for ( int i = start; i <= stop; ++i )
	{

		float rayt, u, v;
		if ( intersect_triangle1(	&orig.x,
									&dir.x,
									&(*m_triangles)[i].vertices[0]->position.x,
									&(*m_triangles)[i].vertices[1]->position.x,
									&(*m_triangles)[i].vertices[2]->position.x,
									rayt, u, v ) )
		{
			if ( rayt >= 0.0f && rayt < t )
			{
				imin = i;
				t = rayt;
				umin = u;
				vmin = v;
				result = RaycastResult(&(*m_triangles)[imin], t, umin, vmin, orig + t*dir, orig, dir, -1);
				result.hit = true;
			}
		}
	}
}
Hit RayTracer::rayIntersectTriangles(const Vec3f& orig, const Vec3f& dir, int startPrim, int endPrim) const
{
	float umin = 0.0f, vmin = 0.0f, tmin = 1.0f;
	int imin = -1;

	// naive loop over all triangles
	for ( int i = startPrim; i <= endPrim; ++i )
	{
		float t = std::numeric_limits<float>::max(), u, v;
		if ( intersect_triangle1( &orig.x,
								  &dir.x,
								  &(*m_triangles)[i].m_vertices[0]->x,
								  &(*m_triangles)[i].m_vertices[1]->x,
								  &(*m_triangles)[i].m_vertices[2]->x,
								  t, u, v ) )
		{
			if ( t > 0.0f && t < tmin )
			{
				imin = i;
				tmin = t;
				umin = u;
				vmin = v;
			}
		}
	}

	if ( imin != -1 )
		return Hit(&(*m_triangles)[imin], orig + tmin*dir, tmin, umin, vmin);
	else
		return Hit(NULL);
}