Daniele Bartolini преди 9 години
родител
ревизия
18fc5ae986
променени са 5 файла, в които са добавени 83 реда и са изтрити 83 реда
  1. 57 57
      src/core/math/intersection.cpp
  2. 2 2
      src/core/math/intersection.h
  3. 11 11
      src/core/math/math_types.h
  4. 12 12
      src/core/math/plane.h
  5. 1 1
      src/lua/lua_api.cpp

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

@@ -11,7 +11,7 @@
 
 namespace crown
 {
-f32 ray_plane_intersection(const Vector3& from, const Vector3& dir, const Plane& p)
+f32 ray_plane_intersection(const Vector3& from, const Vector3& dir, const Plane3& p)
 {
 	const f32 num = dot(from, p.n);
 	const f32 den = dot(dir, p.n);
@@ -24,7 +24,7 @@ f32 ray_plane_intersection(const Vector3& from, const Vector3& dir, const Plane&
 
 f32 ray_disc_intersection(const Vector3& from, const Vector3& dir, const Vector3& center, f32 radius, const Vector3& normal)
 {
-	const Plane p = plane::from_point_and_normal(center, normal);
+	const Plane3 p = plane3::from_point_and_normal(center, normal);
 	const f32 t = ray_plane_intersection(from, dir, p);
 
 	if (t == -1.0f)
@@ -217,7 +217,7 @@ f32 ray_mesh_intersection(const Vector3& from, const Vector3& dir, const Matrix4
 	return hit ? tmin : -1.0f;
 }
 
-bool plane_3_intersection(const Plane& a, const Plane& b, const Plane& c, Vector3& ip)
+bool plane_3_intersection(const Plane3& a, const Plane3& b, const Plane3& c, Vector3& ip)
 {
 	const Vector3 na = a.n;
 	const Vector3 nb = b.n;
@@ -240,20 +240,20 @@ bool plane_3_intersection(const Plane& a, const Plane& b, const Plane& c, Vector
 
 bool frustum_sphere_intersection(const Frustum& f, const Sphere& s)
 {
-	if (plane::distance_to_point(f.left, s.c) < -s.r ||
-		plane::distance_to_point(f.right, s.c) < -s.r)
+	if (plane3::distance_to_point(f.left, s.c) < -s.r ||
+		plane3::distance_to_point(f.right, s.c) < -s.r)
 	{
 		return false;
 	}
 
-	if (plane::distance_to_point(f.bottom, s.c) < -s.r ||
-		plane::distance_to_point(f.top, s.c) < -s.r)
+	if (plane3::distance_to_point(f.bottom, s.c) < -s.r ||
+		plane3::distance_to_point(f.top, s.c) < -s.r)
 	{
 		return false;
 	}
 
-	if (plane::distance_to_point(f.near, s.c) < -s.r ||
-		plane::distance_to_point(f.far, s.c) < -s.r)
+	if (plane3::distance_to_point(f.near, s.c) < -s.r ||
+		plane3::distance_to_point(f.far, s.c) < -s.r)
 	{
 		return false;
 	}
@@ -273,69 +273,69 @@ bool frustum_box_intersection(const Frustum& f, const AABB& b)
 	const Vector3 v7 = aabb::vertex(b, 7);
 
 	u8 out = 0;
-	out += (plane::distance_to_point(f.left, v0) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.left, v1) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.left, v2) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.left, v3) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.left, v4) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.left, v5) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.left, v6) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.left, v7) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.left, v0) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.left, v1) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.left, v2) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.left, v3) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.left, v4) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.left, v5) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.left, v6) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.left, v7) < 0.0f) ? 1 : 0;
 	if (out == 8) return false;
 
 	out = 0;
-	out += (plane::distance_to_point(f.right, v0) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.right, v1) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.right, v2) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.right, v3) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.right, v4) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.right, v5) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.right, v6) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.right, v7) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.right, v0) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.right, v1) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.right, v2) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.right, v3) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.right, v4) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.right, v5) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.right, v6) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.right, v7) < 0.0f) ? 1 : 0;
 	if (out == 8) return false;
 
 	out = 0;
-	out += (plane::distance_to_point(f.bottom, v0) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.bottom, v1) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.bottom, v2) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.bottom, v3) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.bottom, v4) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.bottom, v5) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.bottom, v6) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.bottom, v7) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.bottom, v0) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.bottom, v1) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.bottom, v2) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.bottom, v3) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.bottom, v4) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.bottom, v5) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.bottom, v6) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.bottom, v7) < 0.0f) ? 1 : 0;
 	if (out == 8) return false;
 
 	out = 0;
-	out += (plane::distance_to_point(f.top, v0) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.top, v1) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.top, v2) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.top, v3) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.top, v4) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.top, v5) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.top, v6) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.top, v7) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.top, v0) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.top, v1) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.top, v2) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.top, v3) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.top, v4) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.top, v5) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.top, v6) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.top, v7) < 0.0f) ? 1 : 0;
 	if (out == 8) return false;
 
 	out = 0;
-	out += (plane::distance_to_point(f.near, v0) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.near, v1) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.near, v2) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.near, v3) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.near, v4) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.near, v5) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.near, v6) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.near, v7) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.near, v0) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.near, v1) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.near, v2) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.near, v3) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.near, v4) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.near, v5) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.near, v6) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.near, v7) < 0.0f) ? 1 : 0;
 	if (out == 8) return false;
 
 	out = 0;
-	out += (plane::distance_to_point(f.far, v0) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.far, v1) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.far, v2) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.far, v3) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.far, v4) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.far, v5) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.far, v6) < 0.0f) ? 1 : 0;
-	out += (plane::distance_to_point(f.far, v7) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.far, v0) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.far, v1) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.far, v2) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.far, v3) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.far, v4) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.far, v5) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.far, v6) < 0.0f) ? 1 : 0;
+	out += (plane3::distance_to_point(f.far, v7) < 0.0f) ? 1 : 0;
 	if (out == 8) return false;
 
 	// If we are here, it is because either the box intersects or it is contained in the frustum

+ 2 - 2
src/core/math/intersection.h

@@ -14,7 +14,7 @@ namespace crown
 
 /// Returns the distance along ray (from, dir) to intersection point with plane @a p
 /// or -1.0 if no intersection.
-f32 ray_plane_intersection(const Vector3& from, const Vector3& dir, const Plane& p);
+f32 ray_plane_intersection(const Vector3& from, const Vector3& dir, const Plane3& p);
 
 /// Returns the distance along ray (from, dir) to intersection point with disc defined by
 /// @a center, @a radius and @a normal or -1.0 if no intersection.
@@ -37,7 +37,7 @@ f32 ray_triangle_intersection(const Vector3& from, const Vector3& dir, const Vec
 f32 ray_mesh_intersection(const Vector3& from, const Vector3& dir, const Matrix4x4& tm, const void* vertices, u32 stride, const u16* indices, u32 num);
 
 /// Returns whether the planes @a a, @a b and @a c intersects and if so fills @a ip with the intersection point.
-bool plane_3_intersection(const Plane& a, const Plane& b, const Plane& c, Vector3& ip);
+bool plane_3_intersection(const Plane3& a, const Plane3& b, const Plane3& c, Vector3& ip);
 
 /// Returns whether the frustum @a f and the sphere @a s intersects.
 bool frustum_sphere_intersection(const Frustum& f, const Sphere& s);

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

@@ -70,7 +70,7 @@ struct OBB
 /// where: d = -vector3::dot(n, p)
 ///
 /// @ingroup Math
-struct Plane
+struct Plane3
 {
 	Vector3 n;
 	f32 d;
@@ -79,12 +79,12 @@ struct Plane
 /// @ingroup Math
 struct Frustum
 {
-	Plane left;
-	Plane right;
-	Plane bottom;
-	Plane top;
-	Plane near;
-	Plane far;
+	Plane3 left;
+	Plane3 right;
+	Plane3 bottom;
+	Plane3 top;
+	Plane3 near;
+	Plane3 far;
 };
 
 /// @ingroup Math
@@ -134,10 +134,10 @@ const Matrix3x3 MATRIX3X3_IDENTITY = { VECTOR3_XAXIS, VECTOR3_YAXIS, VECTOR3_ZAX
 
 const Matrix4x4 MATRIX4X4_IDENTITY = { VECTOR4_XAXIS, VECTOR4_YAXIS, VECTOR4_ZAXIS, VECTOR4_WAXIS };
 
-const Plane PLANE_ZERO  = { VECTOR3_ZERO,  0.0f };
-const Plane PLANE_XAXIS = { VECTOR3_XAXIS, 0.0f };
-const Plane PLANE_YAXIS = { VECTOR3_YAXIS, 0.0f };
-const Plane PLANE_ZAXIS = { VECTOR3_ZAXIS, 0.0f };
+const Plane3 PLANE3_ZERO  = { VECTOR3_ZERO,  0.0f };
+const Plane3 PLANE3_XAXIS = { VECTOR3_XAXIS, 0.0f };
+const Plane3 PLANE3_YAXIS = { VECTOR3_YAXIS, 0.0f };
+const Plane3 PLANE3_ZAXIS = { VECTOR3_ZAXIS, 0.0f };
 /// @}
 
 } // namespace crown

+ 12 - 12
src/core/math/plane.h

@@ -11,33 +11,33 @@
 
 namespace crown
 {
-/// Functions to manipulate Plane.
+/// Functions to manipulate Plane3.
 ///
 /// @ingroup Math
-namespace plane
+namespace plane3
 {
 	/// Returns the plane defined by @a point and @a normal.
-	Plane from_point_and_normal(const Vector3& point, const Vector3& normal);
+	Plane3 from_point_and_normal(const Vector3& point, const Vector3& normal);
 
 	/// Normalizes the plane @a p and returns its result.
-	Plane& normalize(Plane& p);
+	Plane3& normalize(Plane3& p);
 
 	/// Returns the signed distance between plane @a p and point @a point.
-	f32 distance_to_point(const Plane& p, const Vector3& point);
+	f32 distance_to_point(const Plane3& p, const Vector3& point);
 
-} // namespace plane
+} // namespace plane3
 
-namespace plane
+namespace plane3
 {
-	inline Plane from_point_and_normal(const Vector3& point, const Vector3& normal)
+	inline Plane3 from_point_and_normal(const Vector3& point, const Vector3& normal)
 	{
-		Plane p;
+		Plane3 p;
 		p.n = normal;
 		p.d = -dot(normal, point);
 		return p;
 	}
 
-	inline Plane& normalize(Plane& p)
+	inline Plane3& normalize(Plane3& p)
 	{
 		const f32 len = length(p.n);
 
@@ -52,10 +52,10 @@ namespace plane
 		return p;
 	}
 
-	inline f32 distance_to_point(const Plane& p, const Vector3& point)
+	inline f32 distance_to_point(const Plane3& p, const Vector3& point)
 	{
 		return dot(p.n, point) + p.d;
 	}
-} // namespace plane
+} // namespace plane3
 
 } // namespace crown

+ 1 - 1
src/lua/lua_api.cpp

@@ -113,7 +113,7 @@ static RaycastMode::Enum name_to_raycast_mode(const char* name)
 static int math_ray_plane_intersection(lua_State* L)
 {
 	LuaStack stack(L);
-	const Plane p = plane::from_point_and_normal(stack.get_vector3(3)
+	const Plane3 p = plane3::from_point_and_normal(stack.get_vector3(3)
 		, stack.get_vector3(4)
 		);
 	const f32 t = ray_plane_intersection(stack.get_vector3(1)