Fixed some camera bugs.

This commit is contained in:
2025-09-01 11:02:30 -05:00
parent 127392a1ae
commit 368729f0f3
17 changed files with 423 additions and 70 deletions

View File

@@ -8,13 +8,14 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE
display.c
camera.c
renderer.c
)
# Subdirectories
add_subdirectory(framebuffer)
add_subdirectory(mesh)
add_subdirectory(texture)
add_subdirectory(scene)
add_subdirectory(spritebatch)
if(DUSK_TARGET_SYSTEM STREQUAL "linux")
target_compile_definitions(${DUSK_TARGET_NAME}

View File

@@ -16,22 +16,20 @@ camera_t *CAMERA_MAIN = NULL;
void cameraInit(camera_t *camera) {
assertNotNull(camera, "Not a camera component");
camera->type = CAMERA_TYPE_PERSPECTIVE
;
glm_mat4_identity(camera->transform);
camera->perspective.fov = 45.0f;
camera->type = CAMERA_TYPE_PERSPECTIVE;
camera->perspective.fov = glm_rad(45.0f);
camera->nearClip = 0.1f;
camera->farClip = 100.0f;
glm_look(
(vec3){ 3.0f, 3.0f, 3.0f },
glm_lookat(
(vec3){ 5.0f, 5.0f, 5.0f },
(vec3){ 0.0f, 0.0f, 0.0f },
(vec3){ 0.0f, 1.0f, 0.0f },
camera->transform
);
}
void cameraPush(camera_t *camera) {
void cameraPushMatrix(camera_t *camera) {
assertNotNull(camera, "Not a camera component");
mat4 projection;
@@ -50,12 +48,13 @@ void cameraPush(camera_t *camera) {
break;
case CAMERA_TYPE_PERSPECTIVE:
const float_t aspect = (
(float_t)frameBufferGetWidth(FRAMEBUFFER_BOUND) /
(float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND)
);
glm_perspective(
camera->perspective.fov,
(
(float_t)frameBufferGetWidth(FRAMEBUFFER_BOUND) /
(float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND)
),
aspect,
camera->nearClip,
camera->farClip,
projection
@@ -63,20 +62,21 @@ void cameraPush(camera_t *camera) {
}
#if DISPLAY_SDL2
mat4 pv;
glm_mat4_mul(projection, camera->transform, pv);
// mat4 pv;
// glm_mat4_mul(projection, camera->transform, pv);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadMatrixf((const GLfloat*)pv);
glLoadMatrixf((const GLfloat*)projection);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixf((const GLfloat*)camera->transform);
#endif
}
void cameraPop(void) {
void cameraPopMatrix(void) {
#if DISPLAY_SDL2
glPopMatrix();
#endif

View File

@@ -7,8 +7,9 @@
#include "display/display.h"
#include "console/console.h"
#include "display/renderer.h"
#include "display/framebuffer/framebuffer.h"
#include "display/scene/scenemanager.h"
#include "display/spritebatch/spritebatch.h"
#include "display/mesh/quad.h"
@@ -59,6 +60,8 @@ errorret_t displayInit(void) {
quadInit();
frameBufferInitBackbuffer();
spriteBatchInit();
sceneManagerInit();
errorOk();
}
@@ -82,18 +85,30 @@ errorret_t displayUpdate(void) {
SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
#endif
// rendererRender(CAMERA_MAIN);
frameBufferBind(&FRAMEBUFFER_BACKBUFFER);
spriteBatchClear();
sceneManagerUpdate();
sceneManagerRender();
#if DISPLAY_SDL2
SDL_GL_SwapWindow(DISPLAY.window);
#endif
GLenum err = glGetError();
if (err != GL_NO_ERROR) {
printf("GL Error: %d\n", err);
}
// For now, we just return an OK error.
errorOk();
}
errorret_t displayDispose(void) {
sceneManagerDispose();
spriteBatchDispose();
#if DISPLAY_SDL2
if(DISPLAY.glContext) {
SDL_GL_DeleteContext(DISPLAY.glContext);

View File

@@ -1,32 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "renderer.h"
#include "display/framebuffer/framebuffer.h"
// void rendererRender(const ecsid_t camera) {
// if(camera == -1) return;
// // Get the meshes.
// uint32_t meshCount;
// ecsid_t meshes[ECS_ENTITY_COUNT_MAX];
// ecsid_t id;
// meshCount = meshRendererGetAll(meshes);
// frameBufferBind(NULL);
// frameBufferClear(
// FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
// COLOR_CORNFLOWER_BLUE
// );
// cameraPush(camera);
// for(uint32_t i = 0; i < meshCount; i++) {
// id = meshes[i];
// nodeMatrixPush(id);
// meshRendererDraw(id);
// }
// cameraPop();
// }

View File

@@ -1,16 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/camera.h"
/**
* Render the given scene from the perspective of the given camera.
*
* @param camera The ID of the camera entity to render from.
*/
// void rendererRender(const ecsid_t camera);

View File

@@ -0,0 +1,13 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
scenemanager.c
)
# Subdirs
add_subdirectory(overworld)

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
sceneoverworld.c
)

View File

@@ -0,0 +1,58 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "sceneoverworld.h"
#include "rpg/entity/entity.h"
#include "display/spritebatch/spritebatch.h"
#include "display/framebuffer/framebuffer.h"
#include "display/scene/scenemanager.h"
#include "display/mesh/quad.h"
camera_t SCENE_OVERWORLD_CAMERA;
void sceneOverworldInit(void) {
cameraInit(&SCENE_OVERWORLD_CAMERA);
SCENE_OVERWORLD_CAMERA.type = CAMERA_TYPE_ORTHOGRAPHIC;
SCENE_OVERWORLD_CAMERA.orthographic.left = 0.0f;
SCENE_OVERWORLD_CAMERA.orthographic.right = frameBufferGetWidth(FRAMEBUFFER_BOUND);
SCENE_OVERWORLD_CAMERA.orthographic.top = 0.0f;
SCENE_OVERWORLD_CAMERA.orthographic.bottom = frameBufferGetHeight(FRAMEBUFFER_BOUND);
SCENE_OVERWORLD_CAMERA.nearClip = -1.0f;
SCENE_OVERWORLD_CAMERA.farClip = 100.0f;
glm_mat4_identity(SCENE_OVERWORLD_CAMERA.transform);
scene_t *scene = &SCENE_MANAGER_SCENES[SCENE_TYPE_OVERWORLD];
scene->flags |= SCENE_FLAG_ACTIVE | SCENE_FLAG_VISIBLE;
}
void sceneOverworldUpdate(void) {
}
void sceneOverworldRender(void) {
frameBufferClear(
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
COLOR_CORNFLOWER_BLUE
);
cameraPushMatrix(&SCENE_OVERWORLD_CAMERA);
meshDraw(&QUAD_MESH_SIMPLE, -1, -1);
spriteBatchPush(
NULL,
0.0f, 0.0f, 32.0f, 32.0f,
0xFF, 0x00, 0x00, 0xFF,
0.0f, 0.0f, 1.0f, 1.0f
);
spriteBatchFlush();
cameraPopMatrix();
}
void sceneOverworldDispose(void) {
// Dispose of the overworld scene.
}

View File

@@ -0,0 +1,31 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/camera.h"
extern camera_t SCENE_OVERWORLD_CAMERA;
/**
* Initialize the overworld scene.
*/
void sceneOverworldInit(void);
/**
* Update the overworld scene.
*/
void sceneOverworldUpdate(void);
/**
* Render the overworld scene.
*/
void sceneOverworldRender(void);
/**
* Dispose of the overworld scene.
*/
void sceneOverworldDispose(void);

32
src/display/scene/scene.h Normal file
View File

@@ -0,0 +1,32 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
#define SCENE_FLAG_VISIBLE (1 << 0)
#define SCENE_FLAG_ACTIVE (1 << 1)
typedef struct {
void (*init)(void);
void (*update)(void);
void (*render)(void);
void (*dispose)(void);
void (*active)(void);
void (*sleep)(void);
uint8_t flags;
} scene_t;
typedef enum {
SCENE_TYPE_LOGO,
SCENE_TYPE_OVERWORLD,
SCENE_TYPE_COUNT
} scenetype_t;
#define SCENE_TYPE_INITIAL SCENE_TYPE_OVERWORLD

View File

@@ -0,0 +1,52 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scenemanager.h"
#include "display/scene/overworld/sceneoverworld.h"
scenemanager_t SCENE_MANAGER;
scene_t SCENE_MANAGER_SCENES[SCENE_TYPE_COUNT] = {
[SCENE_TYPE_LOGO] = { 0 },
[SCENE_TYPE_OVERWORLD] = {
.init = sceneOverworldInit,
.update = sceneOverworldUpdate,
.render = sceneOverworldRender,
.dispose = sceneOverworldDispose
}
};
void sceneManagerInit(void) {
scene_t *initial = &SCENE_MANAGER_SCENES[SCENE_TYPE_INITIAL];
if(initial->init != NULL) initial->init();
}
void sceneManagerUpdate(void) {
// For each scene.
for(uint8_t i = 0; i < SCENE_TYPE_COUNT; i++) {
scene_t *scene = &SCENE_MANAGER_SCENES[i];
if((scene->flags & SCENE_FLAG_ACTIVE) == 0) continue;
if(scene->update != NULL) scene->update();
}
}
void sceneManagerRender(void) {
for(uint8_t i = 0; i < SCENE_TYPE_COUNT; i++) {
scene_t *scene = &SCENE_MANAGER_SCENES[i];
if((scene->flags & SCENE_FLAG_VISIBLE) == 0) continue;
if(scene->render != NULL) scene->render();
}
}
void sceneManagerDispose(void) {
for(uint8_t i = 0; i < SCENE_TYPE_COUNT; i++) {
scene_t *scene = &SCENE_MANAGER_SCENES[i];
if(scene->dispose != NULL) scene->dispose();
}
}

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "scene.h"
typedef struct {
int nothing;
} scenemanager_t;
extern scenemanager_t SCENE_MANAGER;
extern scene_t SCENE_MANAGER_SCENES[SCENE_TYPE_COUNT];
void sceneManagerInit(void);
void sceneManagerUpdate(void);
void sceneManagerRender(void);
void sceneManagerDispose(void);

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
spritebatch.c
)

View File

@@ -0,0 +1,75 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "spritebatch.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "console/console.h"
spritebatch_t SPRITEBATCH;
void spriteBatchInit() {
SPRITEBATCH.spriteCount = 0;
SPRITEBATCH.currentTexture = NULL;
meshInit(
&SPRITEBATCH.mesh,
MESH_PRIMITIVE_TRIANGLES,
SPRITEBATCH_VERTEX_COUNT,
&SPRITEBATCH.vertices[0]
);
}
void spriteBatchPush(
texture_t *texture,
const float_t minX,
const float_t minY,
const float_t maxX,
const float_t maxY,
const uint8_t r,
const uint8_t g,
const uint8_t b,
const uint8_t a,
const float_t u0,
const float_t v0,
const float_t u1,
const float_t v1
) {
// Need to flush?
if(
SPRITEBATCH.currentTexture != texture ||
SPRITEBATCH.spriteCount >= SPRITEBATCH_SPRITES_MAX
) {
spriteBatchFlush();
SPRITEBATCH.currentTexture = texture;
}
quadBuffer(
&SPRITEBATCH.vertices[SPRITEBATCH.spriteCount * QUAD_VERTEX_COUNT],
minX, minY, maxX, maxY,
r, g, b, a,
u0, v0, u1, v1
);
SPRITEBATCH.spriteCount++;
}
void spriteBatchClear() {
SPRITEBATCH.spriteCount = 0;
SPRITEBATCH.currentTexture = NULL;
}
void spriteBatchFlush() {
if(SPRITEBATCH.spriteCount == 0) return;
textureBind(SPRITEBATCH.currentTexture);
meshDraw(&SPRITEBATCH.mesh, 0, QUAD_VERTEX_COUNT * SPRITEBATCH.spriteCount);
spriteBatchClear();
}
void spriteBatchDispose() {
meshDispose(&SPRITEBATCH.mesh);
}

View File

@@ -0,0 +1,83 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/mesh/quad.h"
#include "display/texture/texture.h"
#define SPRITEBATCH_SPRITES_MAX 1
#define SPRITEBATCH_VERTEX_COUNT (SPRITEBATCH_SPRITES_MAX * QUAD_VERTEX_COUNT)
typedef struct {
mesh_t mesh;
int32_t spriteCount;
texture_t *currentTexture;
meshvertex_t vertices[SPRITEBATCH_VERTEX_COUNT];
} spritebatch_t;
extern spritebatch_t SPRITEBATCH;
/**
* Initializes a sprite batch.
*
* @param spriteBatch The sprite batch to initialize.
*/
void spriteBatchInit();
/**
* Pushes a sprite to the batch. This basically "queues" it to render (well
* technically it is buffering the vertices to the mesh at the moment, but
* that is likely to change when we switch to VAOs or VBOs or even Shader UBOs).
*
* Currently changing texture pointer will cause the buffer to flush but this is
* also likely to change in the future.
*
* @param texture The texture to use for the sprite.
* @param minX The minimum x coordinate of the sprite.
* @param minY The minimum y coordinate of the sprite.
* @param maxX The maximum x coordinate of the sprite.
* @param maxY The maximum y coordinate of the sprite.
* @param r The red color component of the sprite.
* @param g The green color component of the sprite.
* @param b The blue color component of the sprite.
* @param a The alpha component of the sprite.
* @param u0 The texture coordinate for the top-left corner of the sprite.
* @param v0 The texture coordinate for the top-left corner of the sprite.
* @param u1 The texture coordinate for the bottom-right corner of the sprite.
* @param v1 The texture coordinate for the bottom-right corner of the sprite.
*/
void spriteBatchPush(
texture_t *texture,
const float_t minX,
const float_t minY,
const float_t maxX,
const float_t maxY,
const uint8_t r,
const uint8_t g,
const uint8_t b,
const uint8_t a,
const float_t u0,
const float_t v0,
const float_t u1,
const float_t v1
);
/**
* Clears the sprite batch. This will mean calling flush renders nothing.
*/
void spriteBatchClear();
/**
* Flushes the sprite batch, rendering all queued sprites.
*/
void spriteBatchFlush();
/**
* Disposes of the sprite batch, freeing any allocated resources.
*/
void spriteBatchDispose();

View File

@@ -7,7 +7,7 @@
#pragma once
#include "direction.h"
#include "player.h"
#include "rpg/entity/player.h"
#include "npc.h"
#define ENTITY_COUNT_MAX 32

View File

@@ -7,7 +7,7 @@
#pragma once
#include "dusk.h"
#include "item/inventory.h"
#include "rpg/item/inventory.h"
#define PLAYER_SPEED_WALK 1
#define PLAYER_SPEED_RUN 2