SleakEngine 1.0.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 DrawIndexed(uint32_t indexCount) override;
45 virtual void DrawInstance(uint32_t instanceCount,
46 uint32_t vertexPerInstance) override;
47 virtual void DrawIndexedInstance(uint32_t instanceCount,
48 uint32_t indexPerInstance) override;
49
50 virtual void SetRenderFace(RenderFace face) override;
51 virtual void SetRenderMode(RenderMode mode) override;
52 virtual void SetViewport(float x, float y, float width, float height,
53 float minDepth = 0.0f,
54 float maxDepth = 1.0f) override;
55 virtual void ClearRenderTarget(float r, float g, float b,
56 float a) override;
57 virtual void ClearDepthStencil(bool clearDepth, bool clearStencil,
58 float depth, uint8_t stencil) override;
59
60 virtual void SetDepthWrite(bool enabled) override;
61 virtual void SetDepthCompare(DepthCompare compare) override;
62 virtual void SetCullEnabled(bool enabled) override;
63 virtual void BindTexture(RefPtr<Sleak::Texture> texture, uint32_t slot = 0) override;
64
65 virtual void BindVertexBuffer(RefPtr<BufferBase> buffer,
66 uint32_t slot = 0) override;
67 virtual void BindIndexBuffer(RefPtr<BufferBase> buffer,
68 uint32_t slot = 0) override;
69 virtual void BindConstantBuffer(RefPtr<BufferBase> buffer,
70 uint32_t slot = 0) override;
71 virtual void BindBoneBuffer(RefPtr<BufferBase> buffer) override;
72
73 virtual void BeginDebugLinePass() override;
74 virtual void EndDebugLinePass() override;
75
76 virtual BufferBase* CreateBuffer(BufferType Type, uint32_t size,
77 void* data) override;
78 virtual Shader* CreateShader(const std::string& shaderSource) override;
79 virtual Texture* CreateTexture(const std::string& TexturePath) override;
80 virtual Texture* CreateTextureFromData(uint32_t width, uint32_t height,
81 void* data) override;
82
83 /// Loads a cubemap from six face image paths.
84 Texture* CreateCubemapTexture(const std::array<std::string, 6>& facePaths);
85 /// Loads a cubemap by converting a single equirectangular panorama.
86 Texture* CreateCubemapTextureFromPanorama(const std::string& panoramaPath);
87
88private:
89 Window* m_Window;
90 SDL_GLContext m_GLContext = nullptr;
91 bool m_Initialized = false;
92
93 GLuint m_VAO = 0;
94 bool m_debugLineMode = false;
95
96 // MSAA FBO resources
97 GLuint m_msaaFBO = 0;
98 GLuint m_msaaColorRBO = 0;
99 GLuint m_msaaDepthRBO = 0;
100 /// Allocates the multisampled color/depth renderbuffers sized to the current swapchain.
101 void CreateMSAAFramebuffer();
102 void CleanupMSAAFramebuffer();
103
104 virtual void ConfigureRenderMode() override;
105 virtual void ConfigureRenderFace() override;
106
107 /// Configures the shared VAO's vertex attribute layout for the standard mesh format.
108 void SetupVertexLayout();
109
110 // Shadow mapping
111 GLuint m_shadowFBO = 0;
112 GLuint m_shadowDepthTex = 0;
113 GLuint m_shadowUBO = 0;
114 bool m_shadowMapCreated = false;
115 bool m_shadowUBOCreated = false;
116 float m_lightVP[16] = {};
117 float m_pendingLightVP[16] = {};
118 bool m_hasPendingLightVP = false;
119 bool m_inShadowPass = false;
120 GLuint m_shadowTransformUBO = 0;
121 void SetLightVP(const float* mat) override;
122 void UpdateShadowLightUBO(const void* data, uint32_t size) override;
123 /// Allocates the shadow-pass depth FBO and texture at the configured resolution.
124 bool CreateShadowMapResources();
125 /// Allocates the UBO backing ShadowLightUBO.
126 bool CreateShadowUBO();
127 /// Replays cached shadow-caster draws into the depth-only shadow FBO.
128 void RenderShadowPass();
129
130 // ---- Deferred rendering (GBuffer) ----
131 // Deferred mode overrides
132 virtual bool IsDeferredEnabled() const override;
133 virtual bool IsInGeometryPass() const override { return m_inGeometryPass; }
134 virtual void BindGBufferShader() override;
135 virtual void BindPBRMaterial(Sleak::Material* material) override;
136 virtual void ExecuteDeferredLightingPass() override;
137 virtual void BeginForwardTransparentPass() override;
138 virtual void EndForwardTransparentPass() override;
139 virtual void UpdateDeferredCB(const void* data, uint32_t size) override;
140
141 // GBuffer FBO + textures
142 GLuint m_gbufferFBO = 0;
143 GLuint m_gbufferAlbedoAO = 0; // RT0: RGBA8 — Albedo + AO
144 GLuint m_gbufferNormalRough = 0; // RT1: RGBA16F — Normal + Roughness
145 GLuint m_gbufferMetalEmit = 0; // RT2: RGBA8 — Metallic + EmissiveScale
146 GLuint m_gbufferDepth = 0; // Depth texture (world pos reconstructed)
147 bool m_gbufferCreated = false;
148 int m_gbufferWidth = 0;
149 int m_gbufferHeight = 0;
150
151 // GBuffer + lighting pass shaders
152 class OpenGLShader* m_gbufferShader = nullptr; // geometry pass shader
153 class OpenGLShader* m_lightingShader = nullptr; // lighting pass shader
154 GLuint m_lightingVAO = 0; // empty VAO for fullscreen triangle
155
156 // Deferred CB UBO (InvViewProj + screen size; bound at slot 6)
157 GLuint m_deferredCBUBO = 0;
158 bool m_deferredCBCreated = false;
159
160 bool m_inGeometryPass = false;
161 bool m_inForwardTransparentPass = false;
162
163 /// Allocates the GBuffer FBO and its albedo/normal/metal-emit/depth attachments.
164 bool CreateGBufferResources();
165 void CleanupGBufferResources();
166 /// Tears down and reallocates the GBuffer at a new swapchain size.
167 void RecreateGBufferOnResize(int width, int height);
168
169 // ---- SSAO ----
170 GLuint m_ssaoFBO = 0;
171 GLuint m_ssaoTexture = 0; // R8: raw AO
172 GLuint m_ssaoBlurFBO = 0;
173 GLuint m_ssaoBlurTex = 0; // R8: blurred AO (consumed by lighting)
174 GLuint m_ssaoNoiseTex = 0; // 4x4 RGBA16F
175 GLuint m_ssaoSettingsUBO = 0; // binding 0
176 GLuint m_ssaoKernelUBO = 0; // binding 1
177 GLuint m_ssaoCameraUBO = 0; // binding 2 (Projection/View/InvProjection)
178 class OpenGLShader* m_ssaoShader = nullptr;
179 class OpenGLShader* m_ssaoBlurShader = nullptr;
180 bool m_ssaoCreated = false;
181
182 /// Allocates the SSAO/blur FBOs, noise texture, and kernel/settings UBOs.
183 bool CreateSSAOResources();
184 void CleanupSSAOResources();
185 /// Tears down and reallocates SSAO targets at a new swapchain size.
186 void RecreateSSAOOnResize(int width, int height);
187 /// Runs the SSAO sample pass followed by the blur pass into m_ssaoBlurTex.
188 void ExecuteSSAOPass();
189
190 // ---- IBL (Image-Based Lighting) ----
191 class OpenGLIBL* m_ibl = nullptr;
192 GLuint m_iblSettingsUBO = 0; // binding 10 in lighting_pass_gl.frag
193 GLuint m_iblBoundCubemap = 0; // last skybox cubemap we baked from
194 /// Lazily (re)bakes IBL data when the bound skybox cubemap changes.
195 void EnsureIBL();
196 void CleanupIBL();
197};
198
199} // namespace RenderEngine
200} // namespace Sleak
201
202#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