Răsfoiți Sursa

core: add OBB utils

Daniele Bartolini 2 ani în urmă
părinte
comite
c8ea4740d6
3 a modificat fișierele cu 104 adăugiri și 20 ștergeri
  1. 28 0
      src/core/math/obb.h
  2. 55 0
      src/core/math/obb.inl
  3. 21 20
      src/world/debug_line.cpp

+ 28 - 0
src/core/math/obb.h

@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2012-2023 Daniele Bartolini et al.
+ * SPDX-License-Identifier: MIT
+ */
+
+#pragma once
+
+#include "core/math/types.h"
+
+namespace crown
+{
+/// Functions to manipulate Oriented Bounding Box (OBB).
+///
+/// @ingroup Math
+namespace obb
+{
+	/// Resets the OBB @a obb.
+	void reset(OBB &obb);
+
+	/// Returns the center of the box @a obb.
+	Vector3 center(const OBB &obb);
+
+	/// Returns the eight vertices of the box @a obb.
+	void to_vertices(Vector3 vertices[8], const OBB &obb);
+
+} // namespace obb
+
+} // namespace crown

+ 55 - 0
src/core/math/obb.inl

@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2012-2023 Daniele Bartolini et al.
+ * SPDX-License-Identifier: MIT
+ */
+
+#pragma once
+
+#include "core/math/obb.h"
+#include "core/math/matrix4x4.inl"
+
+namespace crown
+{
+namespace obb
+{
+	inline void reset(OBB &obb)
+	{
+		obb.tm = MATRIX4X4_IDENTITY;
+		obb.half_extents = VECTOR3_ZERO;
+	}
+
+	inline Vector3 center(const OBB &obb)
+	{
+		return translation(obb.tm);
+	}
+
+	inline void to_vertices(Vector3 vertices[8], const OBB &obb)
+	{
+		const Vector3 center = translation(obb.tm);
+		const Vector3 axis_x = x(obb.tm) * obb.half_extents.x;
+		const Vector3 axis_y = y(obb.tm) * obb.half_extents.y;
+		const Vector3 axis_z = z(obb.tm) * obb.half_extents.z;
+
+		// 7 ---- 6
+		// |      |
+		// |      |  <--- Top face
+		// 4 ---- 5
+		//
+		// 3 ---- 2
+		// |      |
+		// |      |  <--- Bottom face
+		// 0 ---- 1
+		vertices[0] = center - axis_x - axis_y - axis_z;
+		vertices[1] = center + axis_x - axis_y - axis_z;
+		vertices[2] = center + axis_x - axis_y + axis_z;
+		vertices[3] = center - axis_x - axis_y + axis_z;
+
+		vertices[4] = center - axis_x + axis_y - axis_z;
+		vertices[5] = center + axis_x + axis_y - axis_z;
+		vertices[6] = center + axis_x + axis_y + axis_z;
+		vertices[7] = center - axis_x + axis_y + axis_z;
+	}
+
+} // namespace obb
+
+} // namespace crown

+ 21 - 20
src/world/debug_line.cpp

@@ -9,6 +9,7 @@
 #include "core/math/intersection.h"
 #include "core/math/math.h"
 #include "core/math/matrix4x4.inl"
+#include "core/math/obb.inl"
 #include "core/math/vector3.inl"
 #include "core/strings/string_id.inl"
 #include "device/pipeline.h"
@@ -158,26 +159,26 @@ void DebugLine::add_frustum(const Matrix4x4 &mvp, const Color4 &color)
 
 void DebugLine::add_obb(const Matrix4x4 &tm, const Vector3 &half_extents, const Color4 &color)
 {
-	const Vector3 o = vector3(tm.t.x, tm.t.y, tm.t.z);
-	const Vector3 x = vector3(tm.x.x, tm.x.y, tm.x.z) * half_extents.x;
-	const Vector3 y = vector3(tm.y.x, tm.y.y, tm.y.z) * half_extents.y;
-	const Vector3 z = vector3(tm.z.x, tm.z.y, tm.z.z) * half_extents.z;
-
-	// Back face
-	add_line(o - x - y - z, o + x - y - z, color);
-	add_line(o + x - y - z, o + x + y - z, color);
-	add_line(o + x + y - z, o - x + y - z, color);
-	add_line(o - x + y - z, o - x - y - z, color);
-
-	add_line(o - x - y + z, o + x - y + z, color);
-	add_line(o + x - y + z, o + x + y + z, color);
-	add_line(o + x + y + z, o - x + y + z, color);
-	add_line(o - x + y + z, o - x - y + z, color);
-
-	add_line(o - x - y - z, o - x - y + z, color);
-	add_line(o + x - y - z, o + x - y + z, color);
-	add_line(o + x + y - z, o + x + y + z, color);
-	add_line(o - x + y - z, o - x + y + z, color);
+	Vector3 vertices[8];
+	obb::to_vertices(vertices, { tm, half_extents });
+
+	// Bottom face.
+	add_line(vertices[0], vertices[1], color);
+	add_line(vertices[1], vertices[2], color);
+	add_line(vertices[2], vertices[3], color);
+	add_line(vertices[3], vertices[0], color);
+
+	// Top face.
+	add_line(vertices[4], vertices[5], color);
+	add_line(vertices[5], vertices[6], color);
+	add_line(vertices[6], vertices[7], color);
+	add_line(vertices[7], vertices[4], color);
+
+	// Connect faces.
+	add_line(vertices[0], vertices[4], color);
+	add_line(vertices[1], vertices[5], color);
+	add_line(vertices[2], vertices[6], color);
+	add_line(vertices[3], vertices[7], color);
 }
 
 void DebugLine::add_mesh(const Matrix4x4 &tm, const void *vertices, u32 stride, const u16 *indices, u32 num, const Color4 &color)