Skip to content

Commit

Permalink
MATH: switch from GLM to MuGLM
Browse files Browse the repository at this point in the history
  • Loading branch information
K1ngst0m committed Nov 10, 2023
1 parent 418b7e9 commit 6ab8129
Show file tree
Hide file tree
Showing 283 changed files with 2,582 additions and 53,411 deletions.
1 change: 1 addition & 0 deletions cmake/AphDefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(APH_EXTERNAL_DIR ${APH_ROOT_DIR}/external)
set(APH_OUTPUT_DIR ${APH_ROOT_DIR}/bin)

set(APH_ENGINE_COMMON_DIR ${APH_ENGINE_DIR}/common)
set(APH_ENGINE_MATH_DIR ${APH_ENGINE_DIR}/math)
set(APH_ENGINE_API_DIR ${APH_ENGINE_DIR}/api)
set(APH_ENGINE_RESOURCE_DIR ${APH_ENGINE_DIR}/resource)
set(APH_ENGINE_CLI_DIR ${APH_ENGINE_DIR}/cli)
Expand Down
9 changes: 8 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_subdirectory(common)
add_subdirectory(math)
add_subdirectory(allocator)
add_subdirectory(app)
add_subdirectory(cli)
Expand All @@ -15,12 +16,12 @@ add_library(aph_engine INTERFACE)

target_include_directories(aph_engine INTERFACE
${APH_EXTERNAL_DIR}/volk
${APH_EXTERNAL_DIR}/glm
${APH_ENGINE_DIR}
${APH_EXTERNAL_DIR}/unordered_dense/include
)
target_link_libraries(aph_engine INTERFACE
common
aph_math
aph_allocator
threads
aph_filesystem
Expand All @@ -34,6 +35,12 @@ target_link_libraries(aph_engine INTERFACE
rendergraph
)

add_compile_definitions(
GLM_FORCE_RADIANS
GLM_FORCE_DEPTH_ZERO_TO_ONE
GLM_ENABLE_EXPERIMENTAL
)

target_link_libraries(aph_engine INTERFACE
mimalloc-static
${xcb_libraries}
Expand Down
2 changes: 2 additions & 0 deletions engine/aph_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
#include "resource/resourceLoader.h"
#include "resource/geometry.h"

#include "math/math.h"

#endif // APH_H_
1 change: 0 additions & 1 deletion engine/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ target_include_directories(api PRIVATE
${APH_EXTERNAL_DIR}/unordered_dense/include
${APH_EXTERNAL_DIR}/spirv-cross
${APH_EXTERNAL_DIR}/vulkan
${APH_EXTERNAL_DIR}/glm
${APH_EXTERNAL_DIR}/vma/include
)
target_link_libraries(api PRIVATE
Expand Down
1 change: 0 additions & 1 deletion engine/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ add_library(cli STATIC ${API_CLI_SRC})
aph_compiler_options(cli)
target_include_directories(cli PRIVATE
${APH_ENGINE_DIR}
${APH_EXTERNAL_DIR}/glm
${APH_EXTERNAL_DIR}/unordered_dense/include
)
1 change: 0 additions & 1 deletion engine/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ target_include_directories(common PRIVATE
${APH_ENGINE_DIR}
${APH_EXTERNAL_DIR}/backward-cpp/include
${APH_EXTERNAL_DIR}/unordered_dense/include
${APH_EXTERNAL_DIR}/glm
${APH_EXTERNAL_DIR}/stb/include
)
8 changes: 0 additions & 8 deletions engine/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@

#include "pch.h"

#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include "common/logger.h"
#include "common/uuid.h"
#include "common/hash.h"
Expand Down
3 changes: 3 additions & 0 deletions engine/math/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file(GLOB APH_MATH_SRC ${APH_ENGINE_MATH_DIR}/*.cpp)
add_library(aph_math STATIC ${APH_MATH_SRC})
aph_compiler_options(aph_math)
285 changes: 285 additions & 0 deletions engine/math/math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
/* Copyright (c) 2017-2023 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "./math.h"
#include "mathImpl.h"

namespace aph
{
mat3 mat3_cast(const quat& q_)
{
auto& q = q_.as_vec4();

mat3 res(1.0f);
float qxx = q.x * q.x;
float qyy = q.y * q.y;
float qzz = q.z * q.z;
float qxz = q.x * q.z;
float qxy = q.x * q.y;
float qyz = q.y * q.z;
float qwx = q.w * q.x;
float qwy = q.w * q.y;
float qwz = q.w * q.z;

res[0][0] = 1.0f - 2.0f * (qyy + qzz);
res[0][1] = 2.0f * (qxy + qwz);
res[0][2] = 2.0f * (qxz - qwy);

res[1][0] = 2.0f * (qxy - qwz);
res[1][1] = 1.0f - 2.0f * (qxx + qzz);
res[1][2] = 2.0f * (qyz + qwx);

res[2][0] = 2.0f * (qxz + qwy);
res[2][1] = 2.0f * (qyz - qwx);
res[2][2] = 1.0f - 2.0f * (qxx + qyy);

return res;
}

mat4 mat4_cast(const quat& q)
{
return mat4(mat3_cast(q));
}

mat4 translate(const vec3& v)
{
return {vec4(1.0f, 0.0f, 0.0f, 0.0f), vec4(0.0f, 1.0f, 0.0f, 0.0f), vec4(0.0f, 0.0f, 1.0f, 0.0f), vec4(v, 1.0f)};
}

mat4 scale(const vec3& v)
{
return {vec4(v.x, 0.0f, 0.0f, 0.0f), vec4(0.0f, v.y, 0.0f, 0.0f), vec4(0.0f, 0.0f, v.z, 0.0f),
vec4(0.0f, 0.0f, 0.0f, 1.0f)};
}

mat2 inverse(const mat2& m)
{
float OneOverDeterminant = 1.0f / (m[0][0] * m[1][1] - m[1][0] * m[0][1]);

mat2 Inverse(vec2(m[1][1] * OneOverDeterminant, -m[0][1] * OneOverDeterminant),
vec2(-m[1][0] * OneOverDeterminant, m[0][0] * OneOverDeterminant));

return Inverse;
}

mat3 inverse(const mat3& m)
{
float OneOverDeterminant =
1.0f / (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) - m[1][0] * (m[0][1] * m[2][2] - m[2][1] * m[0][2]) +
m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]));

mat3 Inverse;
Inverse[0][0] = +(m[1][1] * m[2][2] - m[2][1] * m[1][2]) * OneOverDeterminant;
Inverse[1][0] = -(m[1][0] * m[2][2] - m[2][0] * m[1][2]) * OneOverDeterminant;
Inverse[2][0] = +(m[1][0] * m[2][1] - m[2][0] * m[1][1]) * OneOverDeterminant;
Inverse[0][1] = -(m[0][1] * m[2][2] - m[2][1] * m[0][2]) * OneOverDeterminant;
Inverse[1][1] = +(m[0][0] * m[2][2] - m[2][0] * m[0][2]) * OneOverDeterminant;
Inverse[2][1] = -(m[0][0] * m[2][1] - m[2][0] * m[0][1]) * OneOverDeterminant;
Inverse[0][2] = +(m[0][1] * m[1][2] - m[1][1] * m[0][2]) * OneOverDeterminant;
Inverse[1][2] = -(m[0][0] * m[1][2] - m[1][0] * m[0][2]) * OneOverDeterminant;
Inverse[2][2] = +(m[0][0] * m[1][1] - m[1][0] * m[0][1]) * OneOverDeterminant;

return Inverse;
}

mat4 inverse(const mat4& m)
{
float Coef00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
float Coef02 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
float Coef03 = m[1][2] * m[2][3] - m[2][2] * m[1][3];

float Coef04 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
float Coef06 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
float Coef07 = m[1][1] * m[2][3] - m[2][1] * m[1][3];

float Coef08 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
float Coef10 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
float Coef11 = m[1][1] * m[2][2] - m[2][1] * m[1][2];

float Coef12 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
float Coef14 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
float Coef15 = m[1][0] * m[2][3] - m[2][0] * m[1][3];

float Coef16 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
float Coef18 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
float Coef19 = m[1][0] * m[2][2] - m[2][0] * m[1][2];

float Coef20 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
float Coef22 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
float Coef23 = m[1][0] * m[2][1] - m[2][0] * m[1][1];

vec4 Fac0(Coef00, Coef00, Coef02, Coef03);
vec4 Fac1(Coef04, Coef04, Coef06, Coef07);
vec4 Fac2(Coef08, Coef08, Coef10, Coef11);
vec4 Fac3(Coef12, Coef12, Coef14, Coef15);
vec4 Fac4(Coef16, Coef16, Coef18, Coef19);
vec4 Fac5(Coef20, Coef20, Coef22, Coef23);

vec4 Vec0(m[1][0], m[0][0], m[0][0], m[0][0]);
vec4 Vec1(m[1][1], m[0][1], m[0][1], m[0][1]);
vec4 Vec2(m[1][2], m[0][2], m[0][2], m[0][2]);
vec4 Vec3(m[1][3], m[0][3], m[0][3], m[0][3]);

vec4 Inv0(Vec1 * Fac0 - Vec2 * Fac1 + Vec3 * Fac2);
vec4 Inv1(Vec0 * Fac0 - Vec2 * Fac3 + Vec3 * Fac4);
vec4 Inv2(Vec0 * Fac1 - Vec1 * Fac3 + Vec3 * Fac5);
vec4 Inv3(Vec0 * Fac2 - Vec1 * Fac4 + Vec2 * Fac5);

vec4 SignA(+1, -1, +1, -1);
vec4 SignB(-1, +1, -1, +1);
mat4 Inverse(Inv0 * SignA, Inv1 * SignB, Inv2 * SignA, Inv3 * SignB);

vec4 Row0(Inverse[0][0], Inverse[1][0], Inverse[2][0], Inverse[3][0]);

vec4 Dot0(m[0] * Row0);
float Dot1 = (Dot0.x + Dot0.y) + (Dot0.z + Dot0.w);

float OneOverDeterminant = 1.0f / Dot1;

return Inverse * OneOverDeterminant;
}

void decompose(const mat4& m, vec3& scale, quat& rotation, vec3& trans)
{
vec4 rot;

// Make a lot of assumptions.
// We don't need skew, nor perspective.

// Isolate translation.
trans = m[3].xyz();

vec3 cols[3];
cols[0] = m[0].xyz();
cols[1] = m[1].xyz();
cols[2] = m[2].xyz();

scale.x = length(cols[0]);
scale.y = length(cols[1]);
scale.z = length(cols[2]);

// Isolate scale.
cols[0] /= scale.x;
cols[1] /= scale.y;
cols[2] /= scale.z;

vec3 pdum3 = cross(cols[1], cols[2]);
if(dot(cols[0], pdum3) < 0.0f)
{
scale = -scale;
cols[0] = -cols[0];
cols[1] = -cols[1];
cols[2] = -cols[2];
}

int i, j, k = 0;
float root, trace = cols[0].x + cols[1].y + cols[2].z;
if(trace > 0.0f)
{
root = sqrt(trace + 1.0f);
rot.w = 0.5f * root;
root = 0.5f / root;
rot.x = root * (cols[1].z - cols[2].y);
rot.y = root * (cols[2].x - cols[0].z);
rot.z = root * (cols[0].y - cols[1].x);
}
else
{
static const int Next[3] = {1, 2, 0};

i = 0;
if(cols[1].y > cols[0].x)
i = 1;
if(cols[2].z > cols[i][i])
i = 2;

j = Next[i];
k = Next[j];

root = sqrt(cols[i][i] - cols[j][j] - cols[k][k] + 1.0f);

rot[i] = 0.5f * root;
root = 0.5f / root;
rot[j] = root * (cols[i][j] + cols[j][i]);
rot[k] = root * (cols[i][k] + cols[k][i]);
rot.w = root * (cols[j][k] - cols[k][j]);
}

rotation = quat(rot);
}

mat4 ortho(float left, float right, float bottom, float top, float near, float far)
{
mat4 result(1.0f);
result[0][0] = 2.0f / (right - left);
result[1][1] = 2.0f / (top - bottom);
result[2][2] = -1.0f / (far - near);
result[3][0] = -(right + left) / (right - left);
result[3][1] = -(top + bottom) / (top - bottom);
result[3][2] = -near / (far - near);

result[0].y *= -1.0f;
result[1].y *= -1.0f;
result[2].y *= -1.0f;
result[3].y *= -1.0f;

return result;
}

mat4 frustum(float left, float right, float bottom, float top, float near, float far)
{
mat4 result(0.0f);
result[0][0] = (2.0f * near) / (right - left);
result[1][1] = (2.0f * near) / (top - bottom);
result[2][0] = (right + left) / (right - left);
result[2][1] = (top + bottom) / (top - bottom);
result[2][2] = far / (near - far);
result[2][3] = -1.0f;
result[3][2] = -(far * near) / (far - near);

result[0].y *= -1.0f;
result[1].y *= -1.0f;
result[2].y *= -1.0f;
result[3].y *= -1.0f;

return result;
}

mat4 perspective(float fovy, float aspect, float near, float far)
{
float tanHalfFovy = tan(fovy / 2.0f);

mat4 result(0.0f);
result[0][0] = 1.0f / (aspect * tanHalfFovy);
result[1][1] = 1.0f / (tanHalfFovy);
result[2][2] = far / (near - far);
result[2][3] = -1.0f;
result[3][2] = -(far * near) / (far - near);

result[0].y *= -1.0f;
result[1].y *= -1.0f;
result[2].y *= -1.0f;
result[3].y *= -1.0f;

return result;
}
} // namespace aph
Loading

0 comments on commit 6ab8129

Please sign in to comment.