95 lines
2.8 KiB
C
95 lines
2.8 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "entity/entitymanager.h"
|
|
#include "entity/entity.h"
|
|
#include "entity/component/display/entityposition.h"
|
|
#include "display/framebuffer/framebuffer.h"
|
|
#include "display/screen/screen.h"
|
|
|
|
void entityCameraInit(const entityid_t ent, const componentid_t comp) {
|
|
entitycamera_t *cam = (entitycamera_t *)componentGetData(
|
|
ent, comp, COMPONENT_TYPE_CAMERA
|
|
);
|
|
cam->nearClip = 0.1f;
|
|
cam->farClip = 100.0f;
|
|
cam->projType = ENTITY_CAMERA_PROJECTION_TYPE_PERSPECTIVE;
|
|
cam->perspective.fov = glm_rad(45.0f);
|
|
}
|
|
|
|
void entityCameraGetProjection(
|
|
const entityid_t ent,
|
|
const componentid_t comp,
|
|
mat4 out
|
|
) {
|
|
entitycamera_t *cam = (entitycamera_t *)componentGetData(
|
|
ent, comp, COMPONENT_TYPE_CAMERA
|
|
);
|
|
|
|
if(
|
|
cam->projType == ENTITY_CAMERA_PROJECTION_TYPE_PERSPECTIVE ||
|
|
cam->projType == ENTITY_CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED
|
|
) {
|
|
glm_mat4_identity(out);
|
|
glm_perspective(
|
|
cam->perspective.fov,
|
|
SCREEN.aspect,
|
|
cam->nearClip,
|
|
cam->farClip,
|
|
out
|
|
);
|
|
|
|
if(cam->projType == ENTITY_CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED) {
|
|
out[1][1] *= -1.0f;
|
|
}
|
|
} else if(cam->projType == ENTITY_CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC) {
|
|
glm_mat4_identity(out);
|
|
glm_ortho(
|
|
cam->orthographic.left,
|
|
cam->orthographic.right,
|
|
cam->orthographic.top,
|
|
cam->orthographic.bottom,
|
|
cam->nearClip,
|
|
cam->farClip,
|
|
out
|
|
);
|
|
}
|
|
}
|
|
|
|
entityid_t entityCameraGetCurrent(void) {
|
|
entityid_t camEnts[ENTITY_COUNT_MAX];
|
|
componentid_t camComps[ENTITY_COUNT_MAX];
|
|
entityid_t count = componentGetEntitiesWithComponent(
|
|
COMPONENT_TYPE_CAMERA, camEnts, camComps
|
|
);
|
|
if(count == 0) return ENTITY_COUNT_MAX;
|
|
return camEnts[0];
|
|
}
|
|
|
|
void entityCameraGetForward(const entityid_t entityId, vec2 out) {
|
|
componentid_t posComp = entityGetComponent(entityId, COMPONENT_TYPE_POSITION);
|
|
entityposition_t *pos = entityPositionGet(entityId, posComp);
|
|
// View matrix column layout: M[col][row], forward = {-M[0][2], -M[1][2], -M[2][2]}
|
|
float_t fx = -pos->transform[0][2];
|
|
float_t fz = -pos->transform[2][2];
|
|
float_t len = sqrtf(fx * fx + fz * fz);
|
|
if(len > 1e-6f) { fx /= len; fz /= len; }
|
|
out[0] = fx;
|
|
out[1] = fz;
|
|
}
|
|
|
|
void entityCameraGetRight(const entityid_t entityId, vec2 out) {
|
|
componentid_t posComp = entityGetComponent(entityId, COMPONENT_TYPE_POSITION);
|
|
entityposition_t *pos = entityPositionGet(entityId, posComp);
|
|
// View matrix column layout: right = {M[0][0], M[1][0], M[2][0]}
|
|
float_t rx = pos->transform[0][0];
|
|
float_t rz = pos->transform[2][0];
|
|
float_t len = sqrtf(rx * rx + rz * rz);
|
|
if(len > 1e-6f) { rx /= len; rz /= len; }
|
|
out[0] = rx;
|
|
out[1] = rz;
|
|
} |