archivemuh
This commit is contained in:
125
archive/dusksdl2/display/camera/camera.c
Normal file
125
archive/dusksdl2/display/camera/camera.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "camera.h"
|
||||
#include "display/render.h"
|
||||
|
||||
#include "world/overworld.h"
|
||||
|
||||
void cameraUIPush(void) {
|
||||
glPushMatrix();
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
glViewport(0, 0, RENDER_WIDTH, RENDER_HEIGHT);
|
||||
|
||||
mat4 ortho;
|
||||
glm_ortho(
|
||||
0.0f, (float_t)RENDER_WIDTH,
|
||||
(float_t)RENDER_HEIGHT, 0.0f,
|
||||
-1.0f, 1.0f,
|
||||
ortho
|
||||
);
|
||||
glLoadMatrixf((const GLfloat*)ortho);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
}
|
||||
|
||||
void cameraUIPop(void) {
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
void cameraScreenPush(void) {
|
||||
glPushMatrix();
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
mat4 ortho;
|
||||
#if RENDER_USE_FRAMEBUFFER
|
||||
int32_t windowWidth, windowHeight;
|
||||
SDL_GetWindowSize(RENDER_WINDOW, &windowWidth, &windowHeight);
|
||||
|
||||
glViewport(0, 0, windowWidth, windowHeight);
|
||||
glm_ortho(
|
||||
0.0f, (float_t) windowWidth,
|
||||
(float_t)windowHeight, 0.0f,
|
||||
-1.0f, 1.0f,
|
||||
ortho
|
||||
);
|
||||
#else
|
||||
glm_ortho(
|
||||
0.0f, (float_t)RENDER_WIDTH,
|
||||
(float_t)RENDER_HEIGHT, 0.0f,
|
||||
-1.0f, 1.0f,
|
||||
ortho
|
||||
);
|
||||
#endif
|
||||
glLoadMatrixf((const GLfloat*)ortho);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
}
|
||||
|
||||
void cameraScreenPop(void) {
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
void cameraOverworldPush(void) {
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
|
||||
#if RENDER_USE_FRAMEBUFFER
|
||||
glViewport(0, 0, RENDER_WIDTH, RENDER_HEIGHT);
|
||||
#endif
|
||||
|
||||
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)
|
||||
);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void cameraOverworldPop(void) {
|
||||
glPopMatrix();
|
||||
}
|
Reference in New Issue
Block a user