Пример #1
0
VECTOR4D computeNewPosition(VECTOR4D& V0, VECTOR4D& V1, VECTOR4D& V2, VECTOR4D& Point)
{
	VECTOR4D Plane;
	VECTOR4D NewPos = { 0,0,0,0 };
	Plane = Cross3((V1 - V0), (V2 - V0));
	Plane = Normalize(Plane);
	Plane.w = -Dot(V0, Plane);

	/* Compute normal */
	VECTOR4D Raydir;

	Raydir = Normalize(Cross3(V1 - V0, V2 - V0));

	float n, d;
	PlaneIntersect(Point, Raydir, Plane, n, d);
	if (fabs(d) < 0.0001)
	{
		MessageBox(NULL, L"No interseccion 1 ", L"Algo esta mal con el check", MB_ICONERROR);
		return NewPos;
	}
		
	float u = n / d;
	if (u < 0.0f)
	{
		MessageBox(NULL, L"No interseccion 2 ", L"Algo esta mal con el check", MB_ICONERROR);
		return NewPos;
	}
		

	VECTOR4D Offset = { Raydir.x*u, Raydir.y*u, Raydir.z*u, 0 };

	return  Point + Offset;

}
Пример #2
0
bool RayCastOnTriangle(VECTOR4D &V0, VECTOR4D &V1, VECTOR4D &V2, VECTOR4D &RayOrigin, VECTOR4D &RayDir, VECTOR4D &Intersection) {
  VECTOR4D Plane, Offset;
  float n, d, u, w0, w1, w2;

  Plane = Cross3((V1 - V0), (V2 - V0));
  Plane = Normalize(Plane);
  Plane.w = -Dot(V0, Plane);

  PlaneIntersect(RayOrigin, RayDir, Plane, n, d);
  if (fabs(d) < EPSILON)
    return false;

  u = n / d;
  if (u < 0.0f)
    return false;
  Offset = { RayDir.x * u, RayDir.y * u, RayDir.z * u, 0.0f };
  Intersection = RayOrigin + Offset;

  return PtInTriangleBarycentric(V0, V1, V2, Intersection, w0, w1, w2);
}
Пример #3
0
CIntersectionInfo CTriangle::GetIntersection	(CRay ray)
{
	CPoint3D ptNoIntersection (0.0f, 0.0f, 0.0f, 0.0f);	// no intersection by default
	CPoint3D ptOfIntersection;
	CIntersectionInfo hitInfo;

	/***************************************************************
	STRATEGY:

	There are two major steps when trying to find the point of 
	intersection of a ray with a triangle, first to see if the ray
	intersects the plane of the triangle and then to see if the point
	on the plane that the ray intersects with lies within the triangle.

	So:
	1. Find the point of intersection on the triangle plane.
	2. Check if this point lies insdie the plane or not.
	3. Set the information into the hitInfo object.
	4. Return the hitInfo object.
	***************************************************************/

	// 1.
	ptOfIntersection = PlaneIntersect(ray);

	// 2.
	if ( !inTriangle(ptOfIntersection) || ptOfIntersection.AtInfinity()) {

		return hitInfo;
	}

	// 3.
	hitInfo.SetPointOfIntersection (ptOfIntersection);
	hitInfo.SetNormal (GetNormalAt(ptOfIntersection));
	hitInfo.SetColor(objColor);
	hitInfo.SetTexCoords (GetTexCoords(ptOfIntersection));

	// 4.
	return hitInfo;
}
Пример #4
0
bool RayCastOnTriangle(
	VECTOR4D& V0, VECTOR4D& V1, VECTOR4D& V2,
	VECTOR4D& RayOrigin, VECTOR4D RayDir,
	VECTOR4D& Intersection, float* w0, float* w1, float* w2)
{
	VECTOR4D Plane;
	Plane = Cross3((V1 - V0), (V2 - V0));
	Plane = Normalize(Plane);
	Plane.w = -Dot(V0, Plane);
	//Plane = [A,B,C,D] los coeficientes del plano.
	float n, d;
	PlaneIntersect(RayOrigin, RayDir, Plane, n, d);
	if (fabs(d) < 0.0001)
		return false;
	float u = n / d;
	if (u < 0.0f)
		return false;
	VECTOR4D Offset = { RayDir.x*u, RayDir.y*u, RayDir.z*u, 0 };
	Intersection = RayOrigin + Offset;
	//float w0, w1, w2;
	return PtInTriangleBarycentric(
		V0, V1, V2, Intersection, *w0, *w1, *w2);
}