feat: render module now tracks shaders and updates common uniforms

This commit is contained in:
Sara 2024-09-19 11:34:52 +02:00
parent 10bd6ae727
commit fa4517f9b5
2 changed files with 41 additions and 6 deletions

View file

@ -1,13 +1,18 @@
#include "render.h"
#include "engine_global.h"
#include "scene.h"
#include "resources.h"
#include "utils/debug.h"
#include "utils/list.h"
static List g_render_objects = {}; //!< List of all registered rendering objects
static List g_shaders = {};
static Matrix g_light_direction = {};
void InitializeRenderingSubsystem() {
g_render_objects = list_from_type(Renderable); // Allocate list of renderable typeclass wrappers
g_light_direction = QuaternionToMatrix(QuaternionFromEuler(-65.f, 45.f, 0.f));
g_shaders = list_from_type(ShaderResource);
}
void CleanupRenderingSubsystem() {
@ -25,7 +30,20 @@ void RemoveRenderable(Renderable renderable) {
list_erase(&g_render_objects, idx);
}
void SetLightDirection(Transform transform) {
g_light_direction = TransformGetMatrix(transform);
}
static
void Internal_UpdateShader(Shader *shader) {
int const light_dir_loc = GetShaderLocation(*shader, "lightDirection");
if(light_dir_loc != -1)
SetShaderValue(*shader, light_dir_loc, &MATRIX_FORWARD(g_light_direction), SHADER_UNIFORM_VEC3);
}
void RenderNextFrame() {
list_foreach(ShaderResource *,shader, &g_shaders)
Internal_UpdateShader(shader->resource);
BeginDrawing();
ClearBackground(DARKGRAY);
// get current camera
@ -36,9 +54,23 @@ void RenderNextFrame() {
BeginMode3D(CameraNodeGetCamera(camera));
list_foreach(Renderable *,object, &g_render_objects)
object->tc->draw(object->data);
DrawGrid(100, 1.f);
DrawGrid(100, 1.f); // TODO: Remove this (or make it a scene node entity)
EndMode3D();
}
DrawFPS(20, 20);
EndDrawing();
}
void Internal_OnShaderLoaded(ShaderResource resource) {
list_add(&g_shaders, &resource);
int light_direction_loc = GetShaderLocation(*resource.resource, "lightDirection");
int ambient_level_loc = GetShaderLocation(*resource.resource, "ambient");
SetShaderValue(*resource.resource, light_direction_loc, &MATRIX_FORWARD(g_light_direction), SHADER_UNIFORM_VEC3);
SetShaderValue(*resource.resource, ambient_level_loc, &WHITE, SHADER_UNIFORM_VEC4);
}
void Internal_OnShaderUnloaded(ShaderResource resource) {
size_t const found = list_find(&g_shaders, &resource);
if(found != g_shaders.len)
list_erase(&g_shaders, found);
}

View file

@ -4,6 +4,7 @@
#include "camera_node.h"
#include "renderable.h"
#include "raylib.h"
#include "resources.h"
//! Initialize the rendering subsystem.
extern void InitializeRenderingSubsystem();
@ -15,13 +16,15 @@ extern void CleanupRenderingSubsystem();
extern void AddRenderable(Renderable renderable);
//! Remove a renderable object from draw list
extern void RemoveRenderable(Renderable renderable);
//! Set a camera as the main rendering camera.
extern void SetMainCamera(CameraNode *camera);
//! Get the current main rendering camera.
extern void GetMainCamera(CameraNode *camera);
//! Set the direction of light for simple shading.
extern void SetLightDirection(Transform transform);
//! Draw a frame to the screen based on the current state of the game.
extern void RenderNextFrame();
//! Called when a shader is loaded, will store the shader to update it's uniforms
extern void Internal_OnShaderLoaded(ShaderResource shader);
//! Called when a shader is unloaded
extern void Internal_OnShaderUnloaded(ShaderResource shader);
#endif // !RENDER_H