From d2ebb53322ac691a0d445bc54bfd2fc61f5546e0 Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Sun, 16 Jul 2017 20:47:49 +1000 Subject: [PATCH] Fix mat4 lookat, add translate[3f|v3] --- dqn.h | 35 ++++++++++++++++++++++------------- dqn_unit_test.cpp | 13 +------------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/dqn.h b/dqn.h index cab3920..9640b06 100644 --- a/dqn.h +++ b/dqn.h @@ -885,7 +885,8 @@ DQN_FILE_SCOPE DqnMat4 DqnMat4_Orthographic(f32 left, f32 right, f32 bottom, f32 DQN_FILE_SCOPE DqnMat4 DqnMat4_Perspective (f32 fovYDegrees, f32 aspectRatio, f32 zNear, f32 zFar); DQN_FILE_SCOPE DqnMat4 DqnMat4_LookAt (DqnV3 eye, DqnV3 center, DqnV3 up); -DQN_FILE_SCOPE DqnMat4 DqnMat4_Translate (f32 x, f32 y, f32 z); +DQN_FILE_SCOPE DqnMat4 DqnMat4_Translate3f (f32 x, f32 y, f32 z); +DQN_FILE_SCOPE DqnMat4 DqnMat4_TranslateV3 (DqnV3 vec); DQN_FILE_SCOPE DqnMat4 DqnMat4_Rotate (f32 radians, f32 x, f32 y, f32 z); DQN_FILE_SCOPE DqnMat4 DqnMat4_Scale (f32 x, f32 y, f32 z); DQN_FILE_SCOPE DqnMat4 DqnMat4_ScaleV3 (DqnV3 scale); @@ -1933,7 +1934,7 @@ DQN_FILE_SCOPE bool DqnAssertInternal(const bool result, const char *const file, const char *const formatStrWithMsg = "DqnAssert() failed: %s|%d| (%s): %s\n"; const char *const formatStr = (msg) ? formatStrWithMsg : formatStrNoMsg; - char userMsg[512] = {}; + char userMsg[2048] = {}; if (msg) { va_list argList; @@ -3117,24 +3118,24 @@ DQN_FILE_SCOPE DqnMat4 DqnMat4_LookAt(DqnV3 eye, DqnV3 center, DqnV3 up) result.e[0][0] = s.x; result.e[0][1] = u.x; - result.e[0][2] = f.x; + result.e[0][2] = -f.x; result.e[1][0] = s.y; result.e[1][1] = u.y; - result.e[1][2] = f.y; + result.e[1][2] = -f.y; result.e[2][0] = s.z; result.e[2][1] = u.z; - result.e[2][2] = f.z; + result.e[2][2] = -f.z; - result.e[3][0] = DqnV3_Dot(s, eye); - result.e[3][1] = DqnV3_Dot(u, eye); - result.e[3][2] = -DqnV3_Dot(f, eye); + result.e[3][0] = -DqnV3_Dot(s, eye); + result.e[3][1] = -DqnV3_Dot(u, eye); + result.e[3][2] = DqnV3_Dot(f, eye); result.e[3][3] = 1.0f; return result; } -DQN_FILE_SCOPE DqnMat4 DqnMat4_Translate(f32 x, f32 y, f32 z) +DQN_FILE_SCOPE DqnMat4 DqnMat4_Translate3f(f32 x, f32 y, f32 z) { DqnMat4 result = DqnMat4_Identity(); result.e[3][0] = x; @@ -3143,6 +3144,15 @@ DQN_FILE_SCOPE DqnMat4 DqnMat4_Translate(f32 x, f32 y, f32 z) return result; } +DQN_FILE_SCOPE DqnMat4 DqnMat4_TranslateV3(DqnV3 vec) +{ + DqnMat4 result = DqnMat4_Identity(); + result.e[3][0] = vec.x; + result.e[3][1] = vec.y; + result.e[3][2] = vec.z; + return result; +} + DQN_FILE_SCOPE DqnMat4 DqnMat4_Rotate(f32 radians, f32 x, f32 y, f32 z) { DqnMat4 result = DqnMat4_Identity(); @@ -6169,7 +6179,6 @@ DQN_FILE_SCOPE char **DqnDirInternal_PlatformRead(const char *const dir, // TODO(doyle): Logging DQN_ASSERT(DQN_INVALID_CODE_PATH); *numFiles = 0; - closedir(dirHandle); return NULL; } @@ -6184,7 +6193,6 @@ DQN_FILE_SCOPE char **DqnDirInternal_PlatformRead(const char *const dir, // TODO(doyle): Logging *numFiles = 0; - closedir(dirHandle); return NULL; } } @@ -7117,9 +7125,10 @@ DQN_FILE_SCOPE void DqnWin32_DisplayLastError(const char *const errorPrefix) } } +const i32 DQN_WIN32_INTERNAL_ERROR_MSG_SIZE = 2048; DQN_FILE_SCOPE void DqnWin32_DisplayErrorCode(const DWORD error, const char *const errorPrefix) { - char errorMsg[1024] = {0}; + char errorMsg[DQN_WIN32_INTERNAL_ERROR_MSG_SIZE] = {0}; FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, 0, errorMsg, DQN_ARRAY_COUNT(errorMsg), NULL); @@ -7130,7 +7139,7 @@ DQN_FILE_SCOPE void DqnWin32_DisplayErrorCode(const DWORD error, const char *con DQN_FILE_SCOPE void DqnWin32_OutputDebugString(const char *const formatStr, ...) { - char str[1024] = {0}; + char str[DQN_WIN32_INTERNAL_ERROR_MSG_SIZE] = {0}; va_list argList; va_start(argList, formatStr); diff --git a/dqn_unit_test.cpp b/dqn_unit_test.cpp index c811492..66c066f 100644 --- a/dqn_unit_test.cpp +++ b/dqn_unit_test.cpp @@ -75,7 +75,7 @@ void HandmadeMathTest() { hmm_vec3 hmmVec = HMM_Vec3i(1, 2, 3); DqnV3 dqnVec = DqnV3_3i(1, 2, 3); - DqnMat4 dqnTranslate = DqnMat4_Translate(dqnVec.x, dqnVec.y, dqnVec.z); + DqnMat4 dqnTranslate = DqnMat4_Translate3f(dqnVec.x, dqnVec.y, dqnVec.z); hmm_mat4 hmmTranslate = HMM_Translate(hmmVec); HandmadeMathVerifyMat4(dqnTranslate, hmmTranslate); @@ -116,17 +116,6 @@ void HandmadeMathTest() printf("HandmadeMathTest(): Translate/Scale/Rotate Mat4_Mul: Completed successfully\n"); } - - // Test LookAt/Camera/View matrix returns same results - if (1) - { - DqnMat4 dqnViewMatrix = DqnMat4_LookAt(DqnV3_3f(4, 3, 3), DqnV3_1f(0), DqnV3_3f(0, 1, 0)); - hmm_mat4 hmmViewMatrix = - HMM_LookAt(HMM_Vec3(4, 3, 3), HMM_Vec3(0, 0, 0), HMM_Vec3(0, 1, 0)); - - HandmadeMathVerifyMat4(dqnViewMatrix, hmmViewMatrix); - printf("HandmadeMathTest(): LookAt: Completed successfully\n"); - } } void StringsTest()