GameEngine/source/render.c
2026-01-31 18:55:31 +01:00

154 lines
4.1 KiB
C

#include <engine.h>
#include <rlgl.h>
#include <raymath.h>
//#define RAYGUI_IMPLEMENTATION
//#include <raygui.h>
RenderCtx ctx;
/*
Audio
Video
Gameplay
Control
Accessibility
Filter
*/
void RenderOption() {
}
void RenderInventory() {
//DrawTextureRec();
}
void DebugInterface() {
DrawFPS(10, 10);
}
void UserInterface() {
}
void MainMenu() {
int current_state;
switch (current_state) {
case(0): {
break;
}
default:
break;
}
}
void LoadCameraMode(int mode) {
switch (mode) {
case(0): {
break;
}
default:
break;
}
}
void LoadPipeline() {
ctx.width = 800;
ctx.height = 480;
ctx.hud_on = true;
ctx.debug_on = true;
InitWindow(ctx.width, ctx.height, "GAME!!");
assert(IsWindowReady());
ctx.window = GetWindowHandle();
ctx.shader.deferred = LoadShader("source/shader/deferred.vs", "source/shader/deferred.fs");
ctx.shader.gbuffer = LoadShader("source/shader/gbuffer.vs", "source/shader/gbuffer.fs");
ctx.shader.filter = LoadShader("", "source/shader/Screen_filter.fs");
ctx.gbuffer.framebufferId = rlLoadFramebuffer();
assert(ctx.gbuffer.framebufferId);
rlEnableFramebuffer(ctx.gbuffer.framebufferId);
ctx.gbuffer.depth_tex_id = rlLoadTextureDepth(ctx.width, ctx.height, false);
// If Artefact appear on Normal, may need to change to RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32
ctx.gbuffer.normal_tex_id = rlLoadTexture(NULL, ctx.width, ctx.height, RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16, 1);
// The color in RGB, and the specular strength in the alpha channel
ctx.gbuffer.albedoSpec_tex_id = rlLoadTexture(NULL, ctx.width, ctx.height, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
rlActiveDrawBuffers(3);
rlFramebufferAttach(ctx.gbuffer.framebufferId, ctx.gbuffer.depth_tex_id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
rlFramebufferAttach(ctx.gbuffer.framebufferId, ctx.gbuffer.normal_tex_id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
rlFramebufferAttach(ctx.gbuffer.framebufferId, ctx.gbuffer.albedoSpec_tex_id, RL_ATTACHMENT_COLOR_CHANNEL1, RL_ATTACHMENT_TEXTURE2D, 0);
assert(rlFramebufferComplete(ctx.gbuffer.framebufferId));
rlEnableShader(ctx.shader.deferred.id);
int texUnitDepth = 0;
int texUnitNormal = 1;
int texUnitAlbedoSpec = 2;
SetShaderValue(ctx.shader.deferred, rlGetLocationUniform(ctx.shader.deferred.id, "gDepth"), &texUnitDepth, RL_SHADER_UNIFORM_SAMPLER2D);
SetShaderValue(ctx.shader.deferred, rlGetLocationUniform(ctx.shader.deferred.id, "gNormal"), &texUnitNormal, RL_SHADER_UNIFORM_SAMPLER2D);
SetShaderValue(ctx.shader.deferred, rlGetLocationUniform(ctx.shader.deferred.id, "gAlbedoSpec"), &texUnitAlbedoSpec, RL_SHADER_UNIFORM_SAMPLER2D);
rlDisableShader();
//now need to set this to things that use this Material, and add light used in the scene.
rlEnableDepthTest();
rlEnableBackfaceCulling();
TraceLog(LOG_INFO, "Render Pipeline Built");
SetTargetFPS(120);//may change
}
void UnloadPipeline() {
rlUnloadFramebuffer(ctx.gbuffer.framebufferId);
rlUnloadTexture(ctx.gbuffer.depth_tex_id);
rlUnloadTexture(ctx.gbuffer.normal_tex_id);
rlUnloadTexture(ctx.gbuffer.albedoSpec_tex_id);
UnloadShader(ctx.shader.deferred);
UnloadShader(ctx.shader.gbuffer);
UnloadShader(ctx.shader.filter);
CloseWindow();
}
void Rendering() {
BeginDrawing();
ClearBackground(BLACK);
rlEnableDepthMask();
rlEnableFramebuffer(ctx.gbuffer.framebufferId);
rlEnableShader(ctx.shader.gbuffer.id);
BeginMode3D(ctx.camera);
//MainGeamoetry;
EndMode3D();
rlDisableShader();
rlDisableFramebuffer();
rlDisableDepthMask();//may not be usefull
//rlDisableDepthTest();
//Light Pass and forward pass;
//I should build a shader that take every light in the scene and make a 3D heightmap of lighting then compute it once for all static light allowing to cheat computing everylight
//light is computed into a scalar field
//Light Pass
//Screen Filter Pass
BeginShaderMode(ctx.shader.filter);//may be good to have this disabled for accessibility
//DrawTextureRec();
EndShaderMode();
//render ui
EndDrawing();
}
void LoadAssets() {
//Font
//Textures
//Models
//Sound
//Data
TraceLog(LOG_INFO, "Assets Loaded");
}
void UnloadAssets() {
}