111 lines
2.6 KiB
C
111 lines
2.6 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "camera.h"
|
|
#include "display/display.h"
|
|
#include "assert/assert.h"
|
|
#include "display/framebuffer/framebuffer.h"
|
|
|
|
void cameraInit(camera_t *camera) {
|
|
assertNotNull(camera, "Not a camera component");
|
|
|
|
camera->projType = CAMERA_PROJECTION_TYPE_PERSPECTIVE;
|
|
camera->perspective.fov = glm_rad(45.0f);
|
|
camera->nearClip = 0.1f;
|
|
camera->farClip = 100.0f;
|
|
|
|
camera->viewType = CAMERA_VIEW_TYPE_LOOKAT;
|
|
glm_vec3_copy((vec3){ 5.0f, 5.0f, 5.0f }, camera->lookat.position);
|
|
glm_vec3_copy((vec3){ 0.0f, 1.0f, 0.0f }, camera->lookat.up);
|
|
glm_vec3_copy((vec3){ 0.0f, 0.0f, 0.0f }, camera->lookat.target);
|
|
}
|
|
|
|
void cameraPushMatrix(camera_t *camera) {
|
|
assertNotNull(camera, "Not a camera component");
|
|
|
|
mat4 projection;
|
|
mat4 view;
|
|
|
|
switch(camera->projType) {
|
|
case CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC:
|
|
glm_ortho(
|
|
camera->orthographic.left,
|
|
camera->orthographic.right,
|
|
camera->orthographic.bottom,
|
|
camera->orthographic.top,
|
|
camera->nearClip,
|
|
camera->farClip,
|
|
projection
|
|
);
|
|
break;
|
|
|
|
case CAMERA_PROJECTION_TYPE_PERSPECTIVE:
|
|
const float_t aspect = (
|
|
(float_t)frameBufferGetWidth(FRAMEBUFFER_BOUND) /
|
|
(float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND)
|
|
);
|
|
glm_perspective(
|
|
camera->perspective.fov,
|
|
aspect,
|
|
camera->nearClip,
|
|
camera->farClip,
|
|
projection
|
|
);
|
|
}
|
|
|
|
switch(camera->viewType) {
|
|
case CAMERA_VIEW_TYPE_MATRIX:
|
|
glm_mat4_copy(camera->view, view);
|
|
break;
|
|
|
|
case CAMERA_VIEW_TYPE_LOOKAT:
|
|
glm_lookat(
|
|
camera->lookat.position,
|
|
camera->lookat.target,
|
|
camera->lookat.up,
|
|
view
|
|
);
|
|
break;
|
|
|
|
case CAMERA_VIEW_TYPE_2D:
|
|
glm_mat4_identity(view);
|
|
glm_translate(view, (vec3){
|
|
-camera->_2d.position[0],
|
|
-camera->_2d.position[1],
|
|
0.0f
|
|
});
|
|
glm_scale(view, (vec3){
|
|
camera->_2d.zoom,
|
|
camera->_2d.zoom,
|
|
1.0f
|
|
});
|
|
break;
|
|
|
|
default:
|
|
assertUnreachable("Invalid camera view type");
|
|
}
|
|
|
|
#if DISPLAY_SDL2
|
|
// mat4 pv;
|
|
// glm_mat4_mul(projection, camera->transform, pv);
|
|
|
|
glPushMatrix();
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
glLoadMatrixf((const GLfloat*)projection);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadIdentity();
|
|
glLoadMatrixf((const GLfloat*)view);
|
|
#endif
|
|
}
|
|
|
|
void cameraPopMatrix(void) {
|
|
#if DISPLAY_SDL2
|
|
glPopMatrix();
|
|
#endif
|
|
} |