SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
OpenGLRenderer.hpp
Go to the documentation of this file.
1#ifndef _OPENGLRENDERER_H
2#define _OPENGLRENDERER_H
3
4#include <Core/OSDef.hpp>
8#include <SDL3/SDL.h>
9#include <glad/glad.h>
10#include <array>
11#include <imgui.h>
12#include <backends/imgui_impl_opengl3.h>
13
14namespace Sleak {
15
16namespace RenderEngine {
17
18/// OpenGL 3.3+ backend: deferred GBuffer pipeline, SSAO, shadow mapping, and baked IBL.
19class ENGINE_API OpenGLRenderer : public Renderer, public RenderContext {
20public:
21 OpenGLRenderer(Window* window);
22 virtual ~OpenGLRenderer();
23
24 bool Initialize() override;
25 void BeginRender() override;
26 void EndRender() override;
27 void Cleanup() override;
28
29 virtual uint32_t GetFeatureCaps() const override {
31 }
32
33 virtual void Resize(uint32_t width, uint32_t height) override;
34
35 virtual bool CreateImGUI() override;
36
37 void ApplyMSAAChange() override;
38 void ApplyVSyncChange() override;
39
40 virtual RenderContext* GetContext() override { return this; }
41
42 // RenderContext interface
43 virtual void Draw(uint32_t vertexCount) override;
44 virtual void Draw(uint32_t vertexCount, uint32_t firstVertex) override;
45 virtual void DrawIndexed(uint32_t indexCount) override;
46 virtual void DrawIndexed(uint32_t indexCount, uint32_t firstIndex,
47 int32_t baseVertex) override;
48 virtual void DrawInstance(uint32_t instanceCount,
49 uint32_t vertexPerInstance) override;
50 virtual void DrawIndexedInstance(uint32_t instanceCount,
51 uint32_t indexPerInstance) override;
52 virtual void DrawIndexedInstance(uint32_t instanceCount,
53 uint32_t indexPerInstance,
54 uint32_t firstIndex,
55 int32_t baseVertex) override;
56
57 virtual void SetRenderFace(RenderFace face) override;
58 virtual void SetRenderMode(RenderMode mode) override;
59 virtual void SetViewport(float x, float y, float width, float height,
60 float minDepth = 0.0f,
61 float maxDepth = 1.0f) override;
62 virtual void ClearRenderTarget(float r, float g, float b,
63 float a) override;
64 virtual void ClearDepthStencil(bool clearDepth, bool clearStencil,
65 float depth, uint8_t stencil) override;
66
67 virtual void SetDepthWrite(bool enabled) override;
68 virtual void SetDepthCompare(DepthCompare compare) override;
69 virtual void SetCullEnabled(bool enabled) override;
70 virtual void BindTexture(RefPtr<Sleak::Texture> texture, uint32_t slot = 0) override;
71
72 virtual void BindVertexBuffer(RefPtr<BufferBase> buffer,
73 uint32_t slot = 0) override;
74 virtual void BindIndexBuffer(RefPtr<BufferBase> buffer,
75 uint32_t slot = 0) override;
76 virtual void BindConstantBuffer(RefPtr<BufferBase> buffer,
77 uint32_t slot = 0) override;
78 virtual void BindBoneBuffer(RefPtr<BufferBase> buffer) override;
79
80 virtual void BeginDebugLinePass() override;
81 virtual void EndDebugLinePass() override;
82
83 virtual BufferBase* CreateBuffer(BufferType Type, uint32_t size,
84 void* data) override;
85 virtual Shader* CreateShader(const std::string& shaderSource) override;
86 virtual Texture* CreateTexture(const std::string& TexturePath) override;
87 virtual Texture* CreateTextureFromData(uint32_t width, uint32_t height,
88 void* data) override;
89
90 /// Loads a cubemap from six face image paths.
91 Texture* CreateCubemapTexture(const std::array<std::string, 6>& facePaths);
92 /// Loads a cubemap by converting a single equirectangular panorama.
93 Texture* CreateCubemapTextureFromPanorama(const std::string& panoramaPath);
94
95private:
96 Window* m_Window;
97 SDL_GLContext m_GLContext = nullptr;
98 bool m_Initialized = false;
99
100 GLuint m_VAO = 0;
101 bool m_debugLineMode = false;
102
103 // MSAA FBO resources
104 GLuint m_msaaFBO = 0;
105 GLuint m_msaaColorRBO = 0;
106 GLuint m_msaaDepthRBO = 0;
107 /// Allocates the multisampled color/depth renderbuffers sized to the current swapchain.
108 void CreateMSAAFramebuffer();
109 void CleanupMSAAFramebuffer();
110
111 virtual void ConfigureRenderMode() override;
112 virtual void ConfigureRenderFace() override;
113
114 /// Configures the shared VAO's vertex attribute layout for the standard mesh format.
115 void SetupVertexLayout();
116
117 // Shadow mapping
118 GLuint m_shadowFBO = 0;
119 GLuint m_shadowDepthTex = 0;
120 GLuint m_shadowUBO = 0;
121 bool m_shadowMapCreated = false;
122 bool m_shadowUBOCreated = false;
123 float m_lightVP[16] = {};
124 float m_pendingLightVP[16] = {};
125 bool m_hasPendingLightVP = false;
126 bool m_inShadowPass = false;
127 GLuint m_shadowTransformUBO = 0;
128 void SetLightVP(const float* mat) override;
129 void UpdateShadowLightUBO(const void* data, uint32_t size) override;
130 /// Allocates the shadow-pass depth FBO and texture at the configured resolution.
131 bool CreateShadowMapResources();
132 /// Allocates the UBO backing ShadowLightUBO.
133 bool CreateShadowUBO();
134 /// Replays cached shadow-caster draws into the depth-only shadow FBO.
135 void RenderShadowPass();
136
137 // ---- Deferred rendering (GBuffer) ----
138 // Deferred mode overrides
139 virtual bool IsDeferredEnabled() const override;
140 virtual bool IsInGeometryPass() const override { return m_inGeometryPass; }
141 virtual void BindGBufferShader() override;
142 virtual void BindPBRMaterial(Sleak::Material* material) override;
143 virtual void ExecuteDeferredLightingPass() override;
144 virtual void BeginForwardTransparentPass() override;
145 virtual void EndForwardTransparentPass() override;
146 virtual void UpdateDeferredCB(const void* data, uint32_t size) override;
147
148 // GBuffer FBO + textures
149 GLuint m_gbufferFBO = 0;
150 GLuint m_gbufferAlbedoAO = 0; // RT0: RGBA8 — Albedo + AO
151 GLuint m_gbufferNormalRough = 0; // RT1: RGBA16F — Normal + Roughness
152 GLuint m_gbufferMetalEmit = 0; // RT2: RGBA8 — Metallic + EmissiveScale
153 GLuint m_gbufferDepth = 0; // Depth texture (world pos reconstructed)
154 bool m_gbufferCreated = false;
155 int m_gbufferWidth = 0;
156 int m_gbufferHeight = 0;
157
158 // GBuffer + lighting pass shaders
159 class OpenGLShader* m_gbufferShader = nullptr; // geometry pass shader
160 class OpenGLShader* m_lightingShader = nullptr; // lighting pass shader
161 GLuint m_lightingVAO = 0; // empty VAO for fullscreen triangle
162
163 // Deferred CB UBO (InvViewProj + screen size; bound at slot 6)
164 GLuint m_deferredCBUBO = 0;
165 bool m_deferredCBCreated = false;
166
167 bool m_inGeometryPass = false;
168 bool m_inForwardTransparentPass = false;
169
170 /// Allocates the GBuffer FBO and its albedo/normal/metal-emit/depth attachments.
171 bool CreateGBufferResources();
172 void CleanupGBufferResources();
173 /// Tears down and reallocates the GBuffer at a new swapchain size.
174 void RecreateGBufferOnResize(int width, int height);
175
176 // ---- SSAO ----
177 GLuint m_ssaoFBO = 0;
178 GLuint m_ssaoTexture = 0; // R8: raw AO
179 GLuint m_ssaoBlurFBO = 0;
180 GLuint m_ssaoBlurTex = 0; // R8: blurred AO (consumed by lighting)
181 GLuint m_ssaoNoiseTex = 0; // 4x4 RGBA16F
182 GLuint m_ssaoSettingsUBO = 0; // binding 0
183 GLuint m_ssaoKernelUBO = 0; // binding 1
184 GLuint m_ssaoCameraUBO = 0; // binding 2 (Projection/View/InvProjection)
185 class OpenGLShader* m_ssaoShader = nullptr;
186 class OpenGLShader* m_ssaoBlurShader = nullptr;
187 bool m_ssaoCreated = false;
188
189 /// Allocates the SSAO/blur FBOs, noise texture, and kernel/settings UBOs.
190 bool CreateSSAOResources();
191 void CleanupSSAOResources();
192 /// Tears down and reallocates SSAO targets at a new swapchain size.
193 void RecreateSSAOOnResize(int width, int height);
194 /// Runs the SSAO sample pass followed by the blur pass into m_ssaoBlurTex.
195 void ExecuteSSAOPass();
196
197 // ---- IBL (Image-Based Lighting) ----
198 class OpenGLIBL* m_ibl = nullptr;
199 GLuint m_iblSettingsUBO = 0; // binding 10 in lighting_pass_gl.frag
200 GLuint m_iblBoundCubemap = 0; // last skybox cubemap we baked from
201 /// Lazily (re)bakes IBL data when the bound skybox cubemap changes.
202 void EnsureIBL();
203 void CleanupIBL();
204};
205
206} // namespace RenderEngine
207} // namespace Sleak
208
209#endif // _OPENGLRENDERER_H
int width
int height
Backend-agnostic GPU buffer: vertex, index, constant, or resource view target.
virtual RenderContext * GetContext() override
Returns the backend's command-recording interface.
virtual uint32_t GetFeatureCaps() const override
Feature capability mask; backends override with the audited truth.
OpenGLRenderer(Window *window)
Registers this backend's buffer/shader/texture factories with ResourceManager.
void BeginRender() override
Acquires the next frame and prepares it for command recording.
void EndRender() override
Submits recorded commands and presents the frame.
bool Initialize() override
Creates the device, swapchain, and backend resources.
Abstract interface for high-level graphics command execution and resource management.
Abstract render backend: swapchain, frame lifecycle, and post-effect toggles.
Definition Renderer.hpp:37
Backend-agnostic compiled shader program.
Definition Shader.hpp:11
Backend-facing rendering layer shared by the four graphics backends.
DepthCompare
Depth test comparison function.
RenderMode
Rasterizer fill style for a draw.
BufferType
GPU buffer usage kind, drives backend binding flags and layout.
RenderFace
Which triangle winding gets culled.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10