96 lines
2.1 KiB
C
96 lines
2.1 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 "scene/node.h"
|
|
#include "display/framebuffer/framebuffer.h"
|
|
|
|
camera_t CAMERA_DATA[ECS_ENTITY_COUNT_MAX] = { 0 };
|
|
ecscomponent_t CAMERA_COMPONENT = ecsComponentInit(
|
|
CAMERA_DATA,
|
|
((ecscomponentcallbacks_t){
|
|
.init = NULL,
|
|
.entityAdd = cameraEntityAdded
|
|
})
|
|
);
|
|
|
|
ecsid_t CAMERA_MAIN = -1;
|
|
|
|
void cameraEntityAdded(const ecsid_t id) {
|
|
if(CAMERA_MAIN == -1) CAMERA_MAIN = id;
|
|
|
|
camera_t *cam = cameraGet(id);
|
|
cam->type = CAMERA_TYPE_PERSPECTIVE;
|
|
cam->perspective.fov = glm_rad(90.0f);
|
|
cam->nearClip = 0.1f;
|
|
cam->farClip = 1000.0f;
|
|
cam->clearColor = COLOR_CORNFLOWER_BLUE;
|
|
}
|
|
|
|
void cameraPush(const ecsid_t id) {
|
|
assertTrue(cameraHas(id), "Not a camera component");
|
|
|
|
camera_t *cam = cameraGet(id);
|
|
|
|
mat4 projection, view;
|
|
nodeMatrixGet(id, view);
|
|
|
|
switch(cam->type) {
|
|
case CAMERA_TYPE_ORTHOGRAPHIC:
|
|
glm_ortho(
|
|
cam->orthographic.left,
|
|
cam->orthographic.right,
|
|
cam->orthographic.bottom,
|
|
cam->orthographic.top,
|
|
cam->nearClip,
|
|
cam->farClip,
|
|
projection
|
|
);
|
|
break;
|
|
|
|
case CAMERA_TYPE_PERSPECTIVE:
|
|
glm_perspective(
|
|
cam->perspective.fov,
|
|
(
|
|
(float_t)frameBufferGetWidth(FRAMEBUFFER_BOUND) /
|
|
(float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND)
|
|
),
|
|
cam->nearClip,
|
|
cam->farClip,
|
|
projection
|
|
);
|
|
}
|
|
|
|
#if DUSK_DISPLAY_SDL2
|
|
mat4 pv;
|
|
glm_mat4_mul(projection, view, pv);
|
|
|
|
glPushMatrix();
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
glLoadMatrixf((const GLfloat*)pv);
|
|
|
|
glClearColor(
|
|
cam->clearColor.r / 255.0f,
|
|
cam->clearColor.g / 255.0f,
|
|
cam->clearColor.b / 255.0f,
|
|
cam->clearColor.a / 255.0f
|
|
);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadIdentity();
|
|
#endif
|
|
}
|
|
|
|
void cameraPop(void) {
|
|
#if DUSK_DISPLAY_SDL2
|
|
glPopMatrix();
|
|
#endif
|
|
} |