ECS rendering
This commit is contained in:
@@ -8,6 +8,7 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
display.c
|
||||
camera.c
|
||||
renderer.c
|
||||
)
|
||||
|
||||
# Subdirectories
|
||||
|
@@ -8,6 +8,7 @@
|
||||
#include "camera.h"
|
||||
#include "display/display.h"
|
||||
#include "assert/assert.h"
|
||||
#include "scene/node.h"
|
||||
|
||||
camera_t CAMERA_DATA[ECS_ENTITY_COUNT_MAX] = { 0 };
|
||||
ecscomponent_t CAMERA_COMPONENT = ecsComponentInit(
|
||||
@@ -24,8 +25,8 @@ void cameraEntityAdded(const ecsid_t id) {
|
||||
if(CAMERA_MAIN == -1) CAMERA_MAIN = id;
|
||||
|
||||
camera_t *cam = cameraGet(id);
|
||||
cam->type = CAMERA_TYPE_ORTHOGRAPHIC;
|
||||
cam->perspective.fov = glm_rad(75.0f);
|
||||
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;
|
||||
@@ -36,7 +37,9 @@ void cameraPush(const ecsid_t id) {
|
||||
|
||||
camera_t *cam = cameraGet(id);
|
||||
|
||||
mat4 projection;
|
||||
mat4 projection, view;
|
||||
nodeMatrixGet(id, view);
|
||||
|
||||
switch(cam->type) {
|
||||
case CAMERA_TYPE_ORTHOGRAPHIC:
|
||||
glm_ortho(
|
||||
@@ -61,10 +64,13 @@ void cameraPush(const ecsid_t id) {
|
||||
}
|
||||
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
mat4 pv;
|
||||
glm_mat4_mul(projection, view, pv);
|
||||
|
||||
glPushMatrix();
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glLoadMatrixf((const GLfloat*)projection);
|
||||
glLoadMatrixf((const GLfloat*)pv);
|
||||
|
||||
glClearColor(
|
||||
cam->clearColor.r / 255.0f,
|
||||
@@ -73,6 +79,9 @@ void cameraPush(const ecsid_t id) {
|
||||
cam->clearColor.a / 255.0f
|
||||
);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@@ -7,18 +7,11 @@
|
||||
|
||||
#include "display/display.h"
|
||||
#include "console/console.h"
|
||||
#include "display/camera.h"
|
||||
#include "display/mesh/mesh.h"
|
||||
#include "display/renderer.h"
|
||||
#include "ecs/ecssystem.h"
|
||||
|
||||
display_t DISPLAY;
|
||||
|
||||
mesh_t mesh;
|
||||
meshvertex_t triangle[3] = {
|
||||
{{255, 0, 0, 255}, {0.0f, 0.0f}, {0.0f, 1.0f}}, // Vertex 1
|
||||
{{0, 255, 0, 255}, {1.0f, 0.0f}, {1.0f, 1.0f}}, // Vertex 2
|
||||
{{0, 0, 255, 255}, {0.5f, 1.0f}, {0.5f, 0.0f}} // Vertex 3
|
||||
};
|
||||
|
||||
errorret_t displayInit(void) {
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) != 0) {
|
||||
@@ -61,8 +54,6 @@ errorret_t displayInit(void) {
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
#endif
|
||||
|
||||
meshInit(&mesh, MESH_PRIMITIVE_TRIANGLES, 3, triangle);
|
||||
|
||||
// For now, we just return an OK error.
|
||||
errorOk();
|
||||
@@ -88,11 +79,7 @@ errorret_t displayUpdate(void) {
|
||||
glViewport(0, 0, windowWidth, windowHeight);
|
||||
#endif
|
||||
|
||||
if(CAMERA_MAIN != -1) {
|
||||
cameraPush(CAMERA_MAIN);
|
||||
meshDraw(&mesh, 0, -1);
|
||||
cameraPop();
|
||||
}
|
||||
rendererRender(CAMERA_MAIN);
|
||||
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
SDL_GL_SwapWindow(DISPLAY.window);
|
||||
@@ -103,8 +90,6 @@ errorret_t displayUpdate(void) {
|
||||
}
|
||||
|
||||
errorret_t displayDispose(void) {
|
||||
meshDispose(&mesh);
|
||||
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
if(DISPLAY.glContext) {
|
||||
SDL_GL_DeleteContext(DISPLAY.glContext);
|
||||
|
@@ -8,4 +8,5 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
mesh.c
|
||||
quad.c
|
||||
meshrenderer.c
|
||||
)
|
26
src/display/mesh/meshrenderer.c
Normal file
26
src/display/mesh/meshrenderer.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "meshrenderer.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;
|
||||
meshDraw(renderer->mesh, 0, -1);
|
||||
}
|
35
src/display/mesh/meshrenderer.h
Normal file
35
src/display/mesh/meshrenderer.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 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"
|
||||
|
||||
typedef struct {
|
||||
mesh_t *mesh;
|
||||
} 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);
|
24
src/display/renderer.c
Normal file
24
src/display/renderer.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "renderer.h"
|
||||
#include "display/mesh/meshrenderer.h"
|
||||
|
||||
void rendererRender(const ecsid_t camera) {
|
||||
if(camera == -1) return;
|
||||
|
||||
// Get the meshes.
|
||||
uint32_t meshCount;
|
||||
ecsid_t meshes[ECS_ENTITY_COUNT_MAX];
|
||||
meshCount = meshRendererGetAll(meshes);
|
||||
|
||||
cameraPush(camera);
|
||||
for(uint32_t i = 0; i < meshCount; i++) {
|
||||
meshRendererDraw(meshes[i]);
|
||||
}
|
||||
cameraPop();
|
||||
}
|
16
src/display/renderer.h
Normal file
16
src/display/renderer.h
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* 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);
|
Reference in New Issue
Block a user