Fixed camera

This commit is contained in:
2025-10-13 12:26:59 -05:00
parent 2c0fd84c72
commit 0c0650a2c3
6 changed files with 82 additions and 66 deletions

View File

@@ -14,11 +14,23 @@
#include "rpg/rpgcamera.h"
#include "util/memory.h"
#define TILE_SIZE 1
#define TILE_SIZE 16
errorret_t sceneMapInit(scenedata_t *data) {
cameraInitPerspective(&data->sceneMap.camera);
data->sceneMap.camera.projType = CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED;
data->sceneMap.camera.viewType = CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT;
glm_vec3_zero(data->sceneMap.camera.lookatPixelPerfect.offset);
data->sceneMap.camera.lookatPixelPerfect.offset[2] = 32.0f;
glm_vec3_copy(
(vec3){ 0.0f, 0.0f, 0.0f },
data->sceneMap.camera.lookatPixelPerfect.target
);
glm_vec3_copy(
(vec3){ 0.0f, 1.0f, 0.0f },
data->sceneMap.camera.lookatPixelPerfect.up
);
data->sceneMap.camera.lookatPixelPerfect.pixelsPerUnit = 1.0f;
errorOk();
}
@@ -27,49 +39,20 @@ void sceneMapUpdate(scenedata_t *data) {
}
void sceneMapRender(scenedata_t *data) {
// Look at target.
glm_vec3_scale(
RPG_CAMERA.position,
TILE_SIZE,
data->sceneMap.camera.lookat.target
data->sceneMap.camera.lookatPixelPerfect.target
);
// Center within tile
glm_vec3_add(
data->sceneMap.camera.lookat.target,
data->sceneMap.camera.lookatPixelPerfect.target,
(vec3){TILE_SIZE / 2.0f, TILE_SIZE / 2.0f, TILE_SIZE / 2.0f },
data->sceneMap.camera.lookat.target
data->sceneMap.camera.lookatPixelPerfect.target
);
// Apply pixel perfect offset and camera offset
const float_t camOffset = 0.01f;
const float_t pixelPerfectOffset = tanf(
data->sceneMap.camera.perspective.fov / 2.0f
) * ((float_t)SCREEN.height / 2.0f);
glm_vec3_copy((vec3){
data->sceneMap.camera.lookat.target[0],
data->sceneMap.camera.lookat.target[1] + camOffset,
data->sceneMap.camera.lookat.target[2] + pixelPerfectOffset
}, data->sceneMap.camera.lookat.position);
// TESTING ONLY
camera_t backup = data->sceneMap.camera;
cameraInitOrthographic(&data->sceneMap.camera);
data->sceneMap.camera.projType = CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC;
glm_vec3_copy((vec3){
RPG_CAMERA.position[0] * TILE_SIZE,
RPG_CAMERA.position[1] * TILE_SIZE,
10.0f
}, data->sceneMap.camera._2d.position);
data->sceneMap.camera.orthographic.left = 0.0f;
data->sceneMap.camera.orthographic.right = SCREEN.width;
data->sceneMap.camera.orthographic.top = 0.0f;
data->sceneMap.camera.orthographic.bottom = SCREEN.height;
data->sceneMap.camera.nearClip = -100.0f;
data->sceneMap.camera.farClip = 100.0f;
// Push camera
cameraPushMatrix(&data->sceneMap.camera);
@@ -82,9 +65,6 @@ void sceneMapRender(scenedata_t *data) {
// Finished, pop back camera.
cameraPopMatrix();
//END TESTING
data->sceneMap.camera = backup;
}
void sceneMapRenderEntity(entity_t *entity) {
@@ -93,8 +73,7 @@ void sceneMapRenderEntity(entity_t *entity) {
if(entity->type == ENTITY_TYPE_NULL) return;
vec3 posMin, posMax;
// glm_vec3_scale(entity->position, TILE_SIZE, posMin);
posMin[0] = 1, posMin[1] = 1, posMin[2] = 0;
glm_vec3_scale(entity->position, TILE_SIZE, posMin);
glm_vec3_add(posMin, (vec3){TILE_SIZE, TILE_SIZE, TILE_SIZE }, posMax);
vec2 uv0 = { 0.0f, 0.0f };