Made a big mess of the codebase

This commit is contained in:
2025-08-28 07:14:13 -05:00
parent 30232d1275
commit af1329710d
44 changed files with 1363 additions and 388 deletions

View File

@@ -8,67 +8,63 @@
#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
})
);
camera_t CAMERA_DATA[CAMERA_COUNT_MAX] = { 0 };
camera_t *CAMERA_MAIN = NULL;
ecsid_t CAMERA_MAIN = -1;
void cameraInit(camera_t *camera) {
assertNotNull(camera, "Not a camera component");
void cameraEntityAdded(const ecsid_t id) {
if(CAMERA_MAIN == -1) CAMERA_MAIN = id;
camera->type = CAMERA_TYPE_PERSPECTIVE
;
glm_mat4_identity(camera->transform);
camera->perspective.fov = 45.0f;
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;
camera->nearClip = 0.1f;
camera->farClip = 100.0f;
glm_look(
(vec3){ 3.0f, 3.0f, 3.0f },
(vec3){ 0.0f, 0.0f, 0.0f },
(vec3){ 0.0f, 1.0f, 0.0f },
camera->transform
);
}
void cameraPush(const ecsid_t id) {
assertTrue(cameraHas(id), "Not a camera component");
void cameraPush(camera_t *camera) {
assertNotNull(camera, "Not a camera component");
camera_t *cam = cameraGet(id);
mat4 projection;
mat4 projection, view;
nodeMatrixGet(id, view);
switch(cam->type) {
switch(camera->type) {
case CAMERA_TYPE_ORTHOGRAPHIC:
glm_ortho(
cam->orthographic.left,
cam->orthographic.right,
cam->orthographic.bottom,
cam->orthographic.top,
cam->nearClip,
cam->farClip,
camera->orthographic.left,
camera->orthographic.right,
camera->orthographic.bottom,
camera->orthographic.top,
camera->nearClip,
camera->farClip,
projection
);
break;
case CAMERA_TYPE_PERSPECTIVE:
glm_perspective(
cam->perspective.fov,
camera->perspective.fov,
(
(float_t)frameBufferGetWidth(FRAMEBUFFER_BOUND) /
(float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND)
),
cam->nearClip,
cam->farClip,
camera->nearClip,
camera->farClip,
projection
);
}
#if DISPLAY_SDL2
mat4 pv;
glm_mat4_mul(projection, view, pv);
glm_mat4_mul(projection, camera->transform, pv);
glPushMatrix();
glMatrixMode(GL_PROJECTION);

View File

@@ -6,9 +6,11 @@
*/
#pragma once
#include "ecs/ecscomponent.h"
#include "dusk.h"
#include "display/color.h"
#define CAMERA_COUNT_MAX 4
typedef enum {
CAMERA_TYPE_PERSPECTIVE,
CAMERA_TYPE_ORTHOGRAPHIC
@@ -17,6 +19,8 @@ typedef enum {
typedef struct {
cameraprojectiontype_t type;
mat4 transform;
union {
struct {
float_t fov;
@@ -34,31 +38,22 @@ typedef struct {
float_t farClip;
} camera_t;
extern camera_t CAMERA_DATA[ECS_ENTITY_COUNT_MAX];
extern ecscomponent_t CAMERA_COMPONENT;
extern ecsid_t CAMERA_MAIN;
#define cameraAdd(id) ((camera_t*)ecsComponentDataAdd(&CAMERA_COMPONENT, id))
#define cameraGet(id) ((camera_t*)ecsComponentDataGet(&CAMERA_COMPONENT, id))
#define cameraHas(id) ecsComponentDataHas(&CAMERA_COMPONENT, id)
#define cameraRemove(id) ecsComponentDataRemove(&CAMERA_COMPONENT, id)
extern camera_t CAMERA_DATA[CAMERA_COUNT_MAX];
extern camera_t *CAMERA_MAIN;
/**
* Callback function called when a new entity is added to the camera component.
* Initializes the camera data for the entity.
*
* @param id The ID of the newly added entity.
* Initializes a camera to default values.
*/
void cameraEntityAdded(const ecsid_t id);
void cameraInit(camera_t *camera);
/**
* Pushes the camera's view matrix onto the matrix stack.
*
* @param id The ID of the camera entity to use.
*/
void cameraPush(const ecsid_t id);
void cameraPushMatrix(camera_t* camera);
/**
* Pops the camera's view matrix off the matrix stack.
*/
void cameraPop(void);
void cameraPopMatrix(void);

View File

@@ -8,7 +8,6 @@
#include "display/display.h"
#include "console/console.h"
#include "display/renderer.h"
#include "ecs/ecssystem.h"
#include "display/framebuffer/framebuffer.h"
#include "display/mesh/quad.h"
@@ -84,7 +83,7 @@ errorret_t displayUpdate(void) {
glViewport(0, 0, windowWidth, windowHeight);
#endif
rendererRender(CAMERA_MAIN);
// rendererRender(CAMERA_MAIN);
#if DISPLAY_SDL2
SDL_GL_SwapWindow(DISPLAY.window);

View File

@@ -8,5 +8,4 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE
mesh.c
quad.c
meshrenderer.c
)

View File

@@ -1,28 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "meshrenderer.h"
#include "time/time.h"
meshrenderer_t MESH_RENDERER_DATA[ECS_ENTITY_COUNT_MAX] = { 0 };
ecscomponent_t MESH_RENDERER_COMPONENT = ecsComponentInit(
MESH_RENDERER_DATA,
((ecscomponentcallbacks_t){
.init = NULL,
.entityAdd = NULL,
.entityRemove = NULL
})
);
void meshRendererDraw(const ecsid_t id) {
if(!meshRendererHas(id)) return;
meshrenderer_t *renderer = &MESH_RENDERER_DATA[id];
if(!renderer->mesh) return;
textureBind(renderer->texture);
meshDraw(renderer->mesh, 0, -1);
}

View File

@@ -1,37 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "ecs/ecscomponent.h"
#include "display/mesh/mesh.h"
#include "display/texture/texture.h"
typedef struct {
mesh_t *mesh;
texture_t *texture;
} meshrenderer_t;
extern meshrenderer_t MESH_RENDERER_DATA[ECS_ENTITY_COUNT_MAX];
extern ecscomponent_t MESH_RENDERER_COMPONENT;
#define meshRendererAdd(id) \
((meshrenderer_t*)ecsComponentDataAdd(&MESH_RENDERER_COMPONENT, id))
#define meshRendererGet(id) \
((meshrenderer_t*)ecsComponentDataGet(&MESH_RENDERER_COMPONENT, id))
#define meshRendererHas(id) \
(ecsComponentDataHas(&MESH_RENDERER_COMPONENT, id))
#define meshRendererRemove(id) \
ecsComponentDataRemove(&MESH_RENDERER_COMPONENT, id)
#define meshRendererGetAll(out) \
ecsComponentGetAll(&MESH_RENDERER_COMPONENT, out)
/**
* Draw the mesh for the given entity.
*
* @param id The ID of the entity with the mesh renderer component.
*/
void meshRendererDraw(const ecsid_t id);

View File

@@ -6,29 +6,27 @@
*/
#include "renderer.h"
#include "display/mesh/meshrenderer.h"
#include "scene/node.h"
#include "display/framebuffer/framebuffer.h"
void rendererRender(const ecsid_t camera) {
if(camera == -1) return;
// 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);
// // 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();
}
// 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

@@ -13,4 +13,4 @@
*
* @param camera The ID of the camera entity to render from.
*/
void rendererRender(const ecsid_t camera);
// void rendererRender(const ecsid_t camera);