From 38127d9260744241fbb45c4549373d850c3dde39 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 18 Aug 2025 19:15:37 -0500 Subject: [PATCH] Ortho re-added --- cmake/modules/Findcglm.cmake | 14 ++++++ src/dusksdl2/CMakeLists.txt | 2 + src/dusksdl2/display/camera/camera.c | 68 ++++++++++++++++++++++++---- src/dusksdl2/dusksdl2.h | 4 +- 4 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 cmake/modules/Findcglm.cmake diff --git a/cmake/modules/Findcglm.cmake b/cmake/modules/Findcglm.cmake new file mode 100644 index 0000000..b356a13 --- /dev/null +++ b/cmake/modules/Findcglm.cmake @@ -0,0 +1,14 @@ +# Copyright (c) 2025 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +include(FetchContent) +FetchContent_Declare( + cglm + GIT_REPOSITORY https://github.com/recp/cglm.git + GIT_TAG v0.9.6 +) + +FetchContent_MakeAvailable(cglm) +set(cglm_FOUND ON) \ No newline at end of file diff --git a/src/dusksdl2/CMakeLists.txt b/src/dusksdl2/CMakeLists.txt index 407a27a..c60259c 100644 --- a/src/dusksdl2/CMakeLists.txt +++ b/src/dusksdl2/CMakeLists.txt @@ -12,12 +12,14 @@ target_compile_definitions(${DUSK_TARGET_NAME} # Libs find_package(SDL2 REQUIRED) find_package(OpenGL REQUIRED) +find_package(cglm REQUIRED) target_link_libraries(${DUSK_TARGET_NAME} PUBLIC SDL2::SDL2 OpenGL::GL GL + cglm ) diff --git a/src/dusksdl2/display/camera/camera.c b/src/dusksdl2/display/camera/camera.c index c4fb9d4..e52f813 100644 --- a/src/dusksdl2/display/camera/camera.c +++ b/src/dusksdl2/display/camera/camera.c @@ -12,13 +12,19 @@ void cameraUIPush(void) { glPushMatrix(); + + glMatrixMode(GL_PROJECTION); glLoadIdentity(); + glViewport(0, 0, RENDER_WIDTH, RENDER_HEIGHT); glOrtho( 0.0f, (float_t)RENDER_WIDTH, (float_t)RENDER_HEIGHT, 0.0f, -1.0f, 1.0f ); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); } void cameraUIPop(void) { @@ -27,6 +33,8 @@ void cameraUIPop(void) { void cameraScreenPush(void) { glPushMatrix(); + + glMatrixMode(GL_PROJECTION); glLoadIdentity(); #if RENDER_USE_FRAMEBUFFER @@ -46,6 +54,9 @@ void cameraScreenPush(void) { -1.0f, 1.0f ); #endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); } void cameraScreenPop(void) { @@ -56,17 +67,58 @@ void cameraOverworldPush(void) { glPushMatrix(); glLoadIdentity(); glViewport(0, 0, RENDER_WIDTH, RENDER_HEIGHT); - glOrtho( - 0.0f, (float_t)RENDER_WIDTH, - (float_t)RENDER_HEIGHT, 0.0f, - -1.0f, 1.0f + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + const float_t fov = glm_rad(75.0f); + const float_t camOffset = 12.0f; + const float_t aspect = (float_t)RENDER_WIDTH / (float_t)RENDER_HEIGHT; + const float_t pixelPerfectOffset = ( + tanf((glm_rad(180) - fov) / 2.0f) * + ((float_t)RENDER_HEIGHT/ 2.0f) ); - glTranslatef( - -((float_t)OVERWORLD_CAMERA_X) + ((float_t)RENDER_WIDTH / 2.0f), - -((float_t)OVERWORLD_CAMERA_Y) + ((float_t)RENDER_HEIGHT / 2.0f), + vec3 look = { + OVERWORLD_CAMERA_X, + OVERWORLD_CAMERA_Y, 0.0f - ); + }; + vec3 eye = { + look[0], + look[1] + camOffset, + look[2] + pixelPerfectOffset + }; + vec3 up = { 0.0f, 1.0f, 0.0f }; + + mat4 proj; + glm_perspective(fov, aspect, 0.1f, 1000.0f, proj); + + // Flips rendering on the Y axis, so that it is still right-down even in 3D; + proj[1][1] = -proj[1][1]; + + mat4 view; + glm_lookat(eye, look, up, view); + + mat4 pv; + glm_mat4_mul(proj, view, pv); + + glLoadMatrixf((const GLfloat*)pv); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + // glOrtho( + // 0.0f, (float_t)RENDER_WIDTH, + // (float_t)RENDER_HEIGHT, 0.0f, + // -1.0f, 1.0f + // ); + + // glTranslatef( + // -((float_t)OVERWORLD_CAMERA_X) + ((float_t)RENDER_WIDTH / 2.0f), + // -((float_t)OVERWORLD_CAMERA_Y) + ((float_t)RENDER_HEIGHT / 2.0f), + // 0.0f + // ); } void cameraOverworldPop(void) { diff --git a/src/dusksdl2/dusksdl2.h b/src/dusksdl2/dusksdl2.h index 318c111..1000059 100644 --- a/src/dusksdl2/dusksdl2.h +++ b/src/dusksdl2/dusksdl2.h @@ -11,4 +11,6 @@ #define GL_GLEXT_PROTOTYPES #include -#include \ No newline at end of file +#include + +#include \ No newline at end of file