Bläddra i källkod

world: use bx::mtx{Ortho,Proj}

Daniele Bartolini 6 år sedan
förälder
incheckning
eac886bf5e
4 ändrade filer med 16 tillägg och 64 borttagningar
  1. 0 51
      src/core/math/matrix4x4.cpp
  2. 0 6
      src/core/math/matrix4x4.h
  3. 5 2
      src/device/device.cpp
  4. 11 5
      src/world/world.cpp

+ 0 - 51
src/core/math/matrix4x4.cpp

@@ -7,57 +7,6 @@
 
 namespace crown
 {
-void perspective(Matrix4x4& m, f32 fovy, f32 aspect, f32 nnear, f32 ffar)
-{
-	const f32 height = 1.0f / ftan(fovy * 0.5f);
-	const f32 width = height * 1.0f / aspect;
-	const f32 aa = ffar / (ffar - nnear);
-	const f32 bb = -nnear * aa;
-
-	m.x.x = width;
-	m.x.y = 0.0f;
-	m.x.z = 0.0f;
-	m.x.w = 0.0f;
-
-	m.y.x = 0.0f;
-	m.y.y = height;
-	m.y.z = 0.0f;
-	m.y.w = 0.0f;
-
-	m.z.x = 0.0f;
-	m.z.y = 0.0f;
-	m.z.z = aa;
-	m.z.w = 1.0f;
-
-	m.t.x = 0.0f;
-	m.t.y = 0.0f;
-	m.t.z = bb;
-	m.t.w = 0.0f;
-}
-
-void orthographic(Matrix4x4& m, f32 left, f32 right, f32 bottom, f32 top, f32 nnear, f32 ffar)
-{
-	m.x.x = 2.0f / (right - left);
-	m.x.y = 0.0f;
-	m.x.z = 0.0f;
-	m.x.w = 0.0f;
-
-	m.y.x = 0.0f;
-	m.y.y = 2.0f / (top - bottom);
-	m.y.z = 0.0f;
-	m.y.w = 0.0f;
-
-	m.z.x = 0.0f;
-	m.z.y = 0.0f;
-	m.z.z = 1.0f / (ffar - nnear);
-	m.z.w = 0.0f;
-
-	m.t.x = (left + right) / (left - right);
-	m.t.y = (top + bottom) / (bottom - top);
-	m.t.z = nnear / (nnear - ffar);
-	m.t.w = 1.0f;
-}
-
 void look(Matrix4x4& m, const Vector3& pos, const Vector3& target, const Vector3& up)
 {
 	Vector3 zaxis = pos - target;

+ 0 - 6
src/core/math/matrix4x4.h

@@ -291,12 +291,6 @@ inline Matrix4x4 operator*(Matrix4x4 a, const Matrix4x4& b)
 	return a;
 }
 
-/// Sets the matrix @a m to perspective.
-void perspective(Matrix4x4& m, f32 fovy, f32 aspect, f32 nnear, f32 ffar);
-
-/// Sets the matrix @a m to orthographic.
-void orthographic(Matrix4x4& m, f32 left, f32 right, f32 bottom, f32 top, f32 nnear, f32 ffar);
-
 /// Transposes the matrix @a m and returns the result.
 inline Matrix4x4& transpose(Matrix4x4& m)
 {

+ 5 - 2
src/device/device.cpp

@@ -58,6 +58,7 @@
 #include "world/world.h"
 #include <bgfx/bgfx.h>
 #include <bx/allocator.h>
+#include <bx/math.h>
 
 #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
 
@@ -563,8 +564,10 @@ void Device::render(World& world, UnitId camera_unit)
 	const Matrix4x4 view = world.camera_view_matrix(camera_unit);
 	const Matrix4x4 proj = world.camera_projection_matrix(camera_unit);
 
-	Matrix4x4 ortho_proj;
-	orthographic(ortho_proj, 0, _width, 0, _height, 0.01f, 1.0f);
+	const bgfx::Caps* caps = bgfx::getCaps();
+	f32 bx_ortho[16];
+	bx::mtxOrtho(bx_ortho, 0, _width, 0, _height, 0.01f, 1.0f, 0.0f, caps->homogeneousDepth);
+	Matrix4x4 ortho_proj = matrix4x4(bx_ortho);
 
 	bgfx::setViewClear(VIEW_SPRITE_0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x353839ff, 1.0f, 0);
 

+ 11 - 5
src/world/world.cpp

@@ -23,6 +23,8 @@
 #include "world/sound_world.h"
 #include "world/unit_manager.h"
 #include "world/world.h"
+#include <bgfx/bgfx.h>
+#include <bx/math.h>
 
 namespace crown
 {
@@ -324,27 +326,31 @@ Matrix4x4 World::camera_projection_matrix(UnitId unit)
 {
 	CameraInstance i = camera_instances(unit);
 	Camera& cam = _camera[i.i];
-	Matrix4x4 projection;
 
+	const bgfx::Caps* caps = bgfx::getCaps();
+	f32 bx_proj[16];
 	switch (cam.projection_type)
 	{
 	case ProjectionType::ORTHOGRAPHIC:
-		orthographic(projection
+		bx::mtxOrtho(bx_proj
 			, -cam.half_size * cam.aspect
 			, cam.half_size * cam.aspect
 			, -cam.half_size
 			, cam.half_size
 			, cam.near_range
 			, cam.far_range
+			, 0.0f
+			, caps->homogeneousDepth
 			);
 		break;
 
 	case ProjectionType::PERSPECTIVE:
-		perspective(projection
-			, cam.fov
+		bx::mtxProj(bx_proj
+			, fdeg(cam.fov)
 			, cam.aspect
 			, cam.near_range
 			, cam.far_range
+			, caps->homogeneousDepth
 			);
 		break;
 
@@ -353,7 +359,7 @@ Matrix4x4 World::camera_projection_matrix(UnitId unit)
 		break;
 	}
 
-	return projection;
+	return matrix4x4(bx_proj);
 }
 
 Matrix4x4 World::camera_view_matrix(UnitId unit)