Просмотр исходного кода

Represent OBB in terms of transform and half-extents

Daniele Bartolini 10 лет назад
Родитель
Сommit
5eeb705d38
3 измененных файлов с 19 добавлено и 18 удалено
  1. 14 14
      src/core/math/intersection.cpp
  2. 4 3
      src/core/math/intersection.h
  3. 1 1
      src/core/math/math_types.h

+ 14 - 14
src/core/math/intersection.cpp

@@ -43,23 +43,23 @@ float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sph
 }
 
 // http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-custom-ray-obb-function/
-float ray_oobb_intersection(const Vector3& from, const Vector3& dir, const OBB& obb)
+float ray_obb_intersection(const Vector3& from, const Vector3& dir, const Matrix4x4& tm, const Vector3& half_extents)
 {
 	float tmin = 0.0f;
 	float tmax = 100000.0f;
 
-	Vector3 obb_pos = vector3(obb.tm.t.x, obb.tm.t.y, obb.tm.t.z);
+	Vector3 obb_pos = vector3(tm.t.x, tm.t.y, tm.t.z);
 	Vector3 delta = obb_pos - from;
 
 	{
-		const Vector3 xaxis = vector3(obb.tm.x.x, obb.tm.x.y, obb.tm.x.z);
+		const Vector3 xaxis = vector3(tm.x.x, tm.x.y, tm.x.z);
 		float e = dot(xaxis, delta);
 		float f = dot(dir, xaxis);
 
 		if (fabs(f) > 0.001f)
 		{
-			float t1 = (e+obb.aabb.min.x)/f;
-			float t2 = (e+obb.aabb.max.x)/f;
+			float t1 = (e-half_extents.x)/f;
+			float t2 = (e+half_extents.x)/f;
 
 			if (t1>t2){
 				float w=t1;t1=t2;t2=w;
@@ -76,20 +76,20 @@ float ray_oobb_intersection(const Vector3& from, const Vector3& dir, const OBB&
 		}
 		else
 		{
-			if(-e+obb.aabb.min.x > 0.0f || -e+obb.aabb.max.x < 0.0f)
+			if(-e-half_extents.x > 0.0f || -e+half_extents.x < 0.0f)
 				return -1.0f;
 		}
 	}
 
 	{
-		const Vector3 yaxis = vector3(obb.tm.y.x, obb.tm.y.y, obb.tm.y.z);
+		const Vector3 yaxis = vector3(tm.y.x, tm.y.y, tm.y.z);
 		float e = dot(yaxis, delta);
 		float f = dot(dir, yaxis);
 
 		if (fabs(f) > 0.001f){
 
-			float t1 = (e+obb.aabb.min.y)/f;
-			float t2 = (e+obb.aabb.max.y)/f;
+			float t1 = (e-half_extents.y)/f;
+			float t2 = (e+half_extents.y)/f;
 
 			if (t1>t2){float w=t1;t1=t2;t2=w;}
 
@@ -103,20 +103,20 @@ float ray_oobb_intersection(const Vector3& from, const Vector3& dir, const OBB&
 		}
 		else
 		{
-			if(-e+obb.aabb.min.y > 0.0f || -e+obb.aabb.max.y < 0.0f)
+			if(-e-half_extents.y > 0.0f || -e+half_extents.y < 0.0f)
 				return -1.0f;
 		}
 	}
 
 	{
-		const Vector3 zaxis = vector3(obb.tm.z.x, obb.tm.z.y, obb.tm.z.z);
+		const Vector3 zaxis = vector3(tm.z.x, tm.z.y, tm.z.z);
 		float e = dot(zaxis, delta);
 		float f = dot(dir, zaxis);
 
 		if (fabs(f) > 0.001f){
 
-			float t1 = (e+obb.aabb.min.z)/f;
-			float t2 = (e+obb.aabb.max.z)/f;
+			float t1 = (e-half_extents.z)/f;
+			float t2 = (e+half_extents.z)/f;
 
 			if (t1>t2){float w=t1;t1=t2;t2=w;}
 
@@ -131,7 +131,7 @@ float ray_oobb_intersection(const Vector3& from, const Vector3& dir, const OBB&
 		}
 		else
 		{
-			if(-e+obb.aabb.min.z > 0.0f || -e+obb.aabb.max.z < 0.0f)
+			if(-e-half_extents.z > 0.0f || -e+half_extents.z < 0.0f)
 				return -1.0f;
 		}
 	}

+ 4 - 3
src/core/math/intersection.h

@@ -18,9 +18,10 @@ float ray_plane_intersection(const Vector3& from, const Vector3& dir, const Plan
 /// or -1.0 if no intersection.
 float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sphere& s);
 
-/// Returns the distance along ray (from, dir) to intersection point with @a obb
-/// or -1.0 if no intersection.
-float ray_oobb_intersection(const Vector3& from, const Vector3& dir, const OBB& obb);
+/// Returns the distance along ray (from, dir) to intersection point with the oriented
+/// bounding-box represented by @a tm and @a half_extents.
+/// If no intersection is found, it returns -1.0.
+float ray_obb_intersection(const Vector3& from, const Vector3& dir, const Matrix4x4& tm, const Vector3& half_extents);
 
 bool plane_3_intersection(const Plane& p1, const Plane& p2, const Plane& p3, Vector3& ip);
 bool frustum_sphere_intersection(const Frustum& f, const Sphere& s);

+ 1 - 1
src/core/math/math_types.h

@@ -59,7 +59,7 @@ struct AABB
 struct OBB
 {
 	Matrix4x4 tm;
-	AABB aabb;
+	Vector3 half_extents;
 };
 
 /// 3D Plane.