Kemena3D
Loading...
Searching...
No Matches
krenderer.h
Go to the documentation of this file.
1
8
9#ifndef KRENDERER_H
10#define KRENDERER_H
11
12#include "kexport.h"
13#include "kdriver.h"
14
15#include "kwindow.h"
16#include "kdatatype.h"
17
18#include <string>
19#include <vector>
20#include <set>
21#include <assert.h>
22#include <algorithm>
23#include <cmath>
24
25#include <glm/glm.hpp>
26#include <glm/gtc/matrix_transform.hpp>
27
28#include "kmesh.h"
29#include "kcamera.h"
30#include "kshader.h"
31#include "klight.h"
32#include "kworld.h"
33#include "kscene.h"
34#include "kobject.h"
35#include "koctree.h"
36
37#include <unordered_set>
38#include <memory>
39
40#include <glm/gtx/string_cast.hpp>
41
42namespace kemena
43{
67 {
68 public:
70
77 bool init(kWindow *window = nullptr, kRendererType type = kRendererType::RENDERER_GL);
78
80 void destroy();
81
87 void setEngineInfo(const kString name, uint32_t version);
88
94
100
107 void clear();
108
124 void render(kWorld *world, kScene *scene, int x, int y, int width, int height,
125 float deltaTime = 0.0f, bool autoClearSwapWindow = true);
126
132
140 void setClearColor(kVec4 newColor);
141
152 void setEnableScreenBuffer(bool newEnable, bool useDefaultShader = true);
153
156
161 void setScreenShader(kShader *newShader);
162
168
175 void setEnableShadow(bool newEnable, bool useDefaultShader = true);
176
179
184 void setShadowShader(kShader *newShader);
185
191
196 void setShadowCascadeCount(int count);
198 int getShadowCascadeCount() const { return shadowCascadeCount; }
199
204 void setShadowResolution(int resolution);
206 int getShadowResolution() const { return shadowResolution; }
207
209 void setShadowSplitLambda(float lambda) { shadowSplitLambda = lambda; }
211 float getShadowSplitLambda() const { return shadowSplitLambda; }
212
214 void setShadowDebug(bool enable) { shadowDebug = enable; }
216 bool getShadowDebug() const { return shadowDebug; }
217
219 void setShadowBias(float bias) { shadowBias = bias; }
221 float getShadowBias() const { return shadowBias; }
222
224 void setShadowNormalBias(float bias) { shadowNormalBias = bias; }
226 float getShadowNormalBias() const { return shadowNormalBias; }
227
229 void setShadowSoftness(float softness) { shadowSoftness = softness; }
231 float getShadowSoftness() const { return shadowSoftness; }
232
241 void setEnableAutoExposure(bool newEnable);
242
245
253 void resizeFbo(int newWidth, int newHeight);
254
259 uint32_t getFboTexture();
260
263
266
272 float srgbToLinear(float c);
273
279 kVec3 idToRgb(unsigned int i);
280
288 unsigned int rgbToId(unsigned int r, unsigned int g, unsigned int b);
289
290 // --- Color ID object picking -----------------------------------------
291
307 void setEnableObjectPicking(bool enable, bool useDefaultShader = true);
308
311
318 void setOctreeCullingEnabled(bool enable) { octreeCullingEnabled = enable; }
319
327 void setOctreeDirty() { octreeDirty = true; }
328
330 bool getOctreeCullingEnabled() const { return octreeCullingEnabled; }
331
342 void setCullingCamera(kCamera *camera) { cullingCamera = camera; }
343
345 kCamera *getCullingCamera() const { return cullingCamera; }
346
348 const kOctree &getOctree() const { return *sceneOctree; }
349
368 int mouseX, int mouseY,
369 int viewWidth, int viewHeight);
370
382 void renderPickingPass(kWorld *world, kScene *scene, int viewWidth, int viewHeight);
383
389
392
407 void renderOutline(kWorld *world, kScene *scene,
408 const std::vector<kString> &selectedUuids,
409 kVec4 color = kVec4(1.0f, 0.6f, 0.0f, 1.0f),
410 float thickness = 3.0f);
411
425 void renderDebugShapes(kWorld *world, kScene *scene,
426 const std::vector<kString> &selectedUuids);
427
429 void setOctreeDebugEnabled(bool enable) { octreeDebugEnabled = enable; }
430
432 bool getOctreeDebugEnabled() const { return octreeDebugEnabled; }
433
441 void renderOctreeDebug(kWorld *world, kScene *scene);
442
451 void renderDebugLines(kWorld *world, const std::vector<kVec3> &segments, kVec3 color);
452
453 protected:
454 private:
455 kString engineName;
456 uint32_t engineVersion = 0;
457
458 // Octree frustum culling
459 std::unique_ptr<kOctree> sceneOctree = std::make_unique<kOctree>();
460 bool octreeCullingEnabled = true;
461 bool octreeDirty = true;
462 bool octreeDebugEnabled = false;
463 kCamera *cullingCamera = nullptr;
464 std::unordered_set<kMesh*> visibleMeshSet;
465 kFrustum currentFrustum;
466 bool currentFrustumValid = false;
467 kWindow *appWindow = nullptr;
468
469 kRendererType renderType;
470 kDriver *driver = nullptr;
471
472 kVec4 clearColor = kVec4(0.0f, 0.0f, 0.0f, 1.0f);
473
475 int frameId = 0;
476
485 void renderSceneGraph(kWorld *world, kScene *scene, kObject *rootNode,
486 bool transparent = false, float deltaTime = 0.0f);
487
496 void renderSceneGraphShadow(kWorld *world, kScene *scene, kObject *rootNode,
497 const kMat4 &lightSpaceMatrix,
498 float deltaTime = 0.0f);
499
508 void renderSceneGraphPicking(kWorld *world, kScene *scene, kObject *rootNode);
509
510 // Screen FBO
511 bool enableScreenBuffer = false;
512 kShader *screenShader = nullptr;
513 uint32_t quadVao = 0, quadVbo = 0, quadEbo = 0;
514 uint32_t fbo = 0, fboTexColor = 0, rboDepth = 0;
515 uint32_t fboMsaa = 0, fboTexColorMsaa = 0, rboDepthMsaa = 0;
516
517 int fboWidth = 0, fboHeight = 0;
518
519 // Cascaded shadow maps — one depth-texture-array layer per cascade.
520 static constexpr int kMaxShadowCascades = 4;
521 bool enableShadow = false;
522 kShader *shadowShader = nullptr;
523 int shadowResolution = 2048;
524 int shadowCascadeCount = 3;
525 float shadowSplitLambda = 0.85f;
526 bool shadowDebug = false;
527 float shadowBias = 0.0008f;
528 float shadowNormalBias = 0.003f;
529 float shadowSoftness = 1.5f;
530 uint32_t shadowFbo = 0;
531 uint32_t shadowTexArray = 0;
532 kMat4 lightSpaceMatrices[kMaxShadowCascades];
533 float cascadeSplits[kMaxShadowCascades] = {};
534
535 // Picking FBO
536 bool enablePicking = false;
537 kShader *pickingShader = nullptr;
538 kShader *pickingIconShader = nullptr;
539 uint32_t pickingIconVAO = 0, pickingIconVBO = 0;
540 uint32_t pickFbo = 0, pickFboTex = 0, pickRboDepth = 0;
541 int pickFboWidth = 0, pickFboHeight = 0;
542
543 // Outline shader (compiled on first renderOutline call)
544 kShader *outlineShader = nullptr;
545
546 // Passthrough shader for Object IDs display mode
547 kShader *debugPickShader = nullptr;
548
549 // Debug / render-mode shaders (compiled on first use)
550 kShader *debugAlbedoShader = nullptr;
551 kShader *debugNormalsShader = nullptr;
552 kShader *debugWireShader = nullptr;
553 kShader *debugDepthShader = nullptr;
554
555 // Debug shape line rendering
556 kShader *debugLineShader = nullptr;
557 uint32_t debugLineVao = 0;
558 uint32_t debugLineVbo = 0;
559
561
570 void renderSceneGraphDebug(kWorld *world, kScene *scene, kObject *rootNode,
571 kShader *shader, bool wireframe);
572
573 // Auto exposure
574 bool enableAutoExposure = false;
575 float averageLuminance = 0.0f;
576 float averageLuminanceColor[4] = {};
577 float exposureKey = 0.18f;
578 float exposureAdaptationRate = 2.0f;
579 float exposure = 1.0f;
580 };
581}
582
583#endif // KRENDERER_H
Scene-graph camera node.
Definition kcamera.h:33
Pure-virtual graphics driver interface.
Definition kdriver.h:86
View frustum defined by six planes extracted from a view-projection matrix.
Definition koctree.h:41
Base scene-graph node.
Definition kobject.h:43
Loose octree that spatially indexes scene meshes for frustum culling.
Definition koctree.h:84
void setEnableShadow(bool newEnable, bool useDefaultShader=true)
Enables or disables shadow mapping.
kShader * getShadowShader()
Returns the current shadow-map shader.
kDriver * getDriver()
Returns the active graphics driver.
float getShadowBias() const
Returns the constant shadow bias.
Definition krenderer.h:221
kCamera * getCullingCamera() const
Returns the override culling camera, or nullptr if using main camera.
Definition krenderer.h:345
void setEnableObjectPicking(bool enable, bool useDefaultShader=true)
Enables or disables the color-ID picking FBO and compiles the built-in picking shader.
int getShadowCascadeCount() const
Returns the active cascade count.
Definition krenderer.h:198
void destroy()
Destroys all GPU resources and the driver.
void renderDebugLines(kWorld *world, const std::vector< kVec3 > &segments, kVec3 color)
Draws world-space line segments in a single colour into the view FBO. Used for editor debug overlays ...
int getShadowResolution() const
Returns the per-cascade shadow-map resolution.
Definition krenderer.h:206
void setOctreeCullingEnabled(bool enable)
Enable or disable octree-based frustum culling (default: enabled).
Definition krenderer.h:318
void setRenderMode(kRenderMode mode)
Sets the debug visualization mode for subsequent render() calls.
float srgbToLinear(float c)
Converts an sRGB component value to linear space.
bool getEnableAutoExposure()
Returns whether automatic exposure adjustment is active.
kShader * getScreenShader()
Returns the current screen-space shader.
void setShadowResolution(int resolution)
Sets the per-cascade shadow-map resolution (e.g. 1024/2048/4096). Rebuilds the shadow texture if shad...
void setShadowNormalBias(float bias)
Sets the slope-scaled (normal) shadow bias passed to lit shaders.
Definition krenderer.h:224
void setShadowSoftness(float softness)
Sets the PCF tap spacing (in shadow-map texels) passed to lit shaders.
Definition krenderer.h:229
void setShadowDebug(bool enable)
Toggles a debug view that tints each cascade a distinct colour.
Definition krenderer.h:214
unsigned int rgbToId(unsigned int r, unsigned int g, unsigned int b)
Converts an RGB colour read back from the GPU to an object ID.
void setCullingCamera(kCamera *camera)
Override the camera used to compute the culling frustum.
Definition krenderer.h:342
bool getEnableShadow()
Returns whether shadow mapping is active.
bool getEnableScreenBuffer()
Returns whether the screen buffer post-process FBO is active.
void setShadowSplitLambda(float lambda)
Sets the cascade split blend: 0 = uniform, 1 = logarithmic.
Definition krenderer.h:209
bool getOctreeDebugEnabled() const
Returns whether octree debug visualization is active.
Definition krenderer.h:432
void setEnableScreenBuffer(bool newEnable, bool useDefaultShader=true)
Enables or disables the off-screen screen buffer (post-process FBO).
void resizeFbo(int newWidth, int newHeight)
Resizes the screen-buffer FBOs to match a new viewport size.
void setOctreeDebugEnabled(bool enable)
Enable or disable octree AABB wireframe debug visualization.
Definition krenderer.h:429
float getShadowSplitLambda() const
Returns the cascade split blend factor.
Definition krenderer.h:211
void setShadowBias(float bias)
Sets the constant shadow bias passed to lit shaders.
Definition krenderer.h:219
void setShadowCascadeCount(int count)
Sets the number of cascades (1..4). Cheap; the underlying texture array always allocates the maximum,...
void clear()
Clears the active framebuffer to the clear colour.
bool getEnableObjectPicking()
Returns whether color-ID object picking is active.
bool init(kWindow *window=nullptr, kRendererType type=kRendererType::RENDERER_GL)
Initialises the renderer and creates the graphics driver.
void renderOutline(kWorld *world, kScene *scene, const std::vector< kString > &selectedUuids, kVec4 color=kVec4(1.0f, 0.6f, 0.0f, 1.0f), float thickness=3.0f)
Renders a color-ID-based outline around selected objects.
void renderOctreeDebug(kWorld *world, kScene *scene)
Draws octree node AABBs and mesh world AABBs as wireframe boxes.
const kOctree & getOctree() const
Returns a reference to the scene octree (read-only).
Definition krenderer.h:348
bool getOctreeCullingEnabled() const
Returns whether octree frustum culling is active.
Definition krenderer.h:330
int getFboHeight()
Returns the current FBO height in pixels.
void render(kWorld *world, kScene *scene, int x, int y, int width, int height, float deltaTime=0.0f, bool autoClearSwapWindow=true)
Renders the scene from the world's main camera.
void setEngineInfo(const kString name, uint32_t version)
Stores an optional application name/version for driver diagnostics.
kVec3 idToRgb(unsigned int i)
Converts a packed integer ID to an RGB colour for object picking.
void renderPickingPass(kWorld *world, kScene *scene, int viewWidth, int viewHeight)
Renders the color-ID picking pass into the picking FBO.
float getShadowNormalBias() const
Returns the slope-scaled shadow bias.
Definition krenderer.h:226
bool getShadowDebug() const
Returns whether the cascade debug view is active.
Definition krenderer.h:216
kRenderMode getRenderMode()
Returns the current debug visualization mode.
kVec4 getClearColor()
Returns the background clear colour.
uint32_t getFboTexture()
Returns the resolved (single-sample) FBO colour texture handle.
void setScreenShader(kShader *newShader)
Sets a custom screen-space shader for post-processing.
void setShadowShader(kShader *newShader)
Sets the shadow-map depth shader.
kObject * pickObject(kWorld *world, kScene *scene, int mouseX, int mouseY, int viewWidth, int viewHeight)
Returns the scene object under the given viewport-relative pixel coordinate using a color-ID render p...
int getFboWidth()
Returns the current FBO width in pixels.
void renderDebugShapes(kWorld *world, kScene *scene, const std::vector< kString > &selectedUuids)
Draws editor debug shapes for selected lights and cameras.
kWindow * getWindow()
Returns the window the renderer was initialised with.
float getShadowSoftness() const
Returns the PCF tap spacing.
Definition krenderer.h:231
void setClearColor(kVec4 newColor)
Sets the background clear colour.
void setEnableAutoExposure(bool newEnable)
Enables or disables automatic exposure adjustment.
void setOctreeDirty()
Marks the static-mesh octree as needing a rebuild.
Definition krenderer.h:327
Holds all objects, lights, and rendering settings for one scene.
Definition kscene.h:36
Wraps a compiled GLSL shader program.
Definition kshader.h:55
Creates and manages an SDL3 application window.
Definition kwindow.h:32
Root container for the entire simulation environment.
Definition kworld.h:54
Perspective camera node (free-look or look-at).
Core type aliases, enumerations, structs, and utility functions used throughout the engine.
Abstract graphics driver interface used by the engine.
Symbol visibility / linkage macro for the Kemena3D library.
#define KEMENA3D_API
Definition kexport.h:35
Light source scene-graph node (point, directional, spotlight).
Polygonal mesh node with skeletal animation support.
Base class for all scene-graph nodes in the Kemena3D engine.
Frustum class and loose octree for scene-level mesh culling.
Container for a self-contained scene (objects, lights, skybox).
GLSL shader program wrapper.
Application window management backed by SDL3.
Top-level container holding scenes and cameras.
Top-level Kemena3D engine namespace.
Definition kanimation.h:23
std::string kString
Standard string alias.
Definition kdatatype.h:42
kRenderMode
Selects the debug visualization mode for the scene viewport.
Definition kdatatype.h:62
@ RENDER_MODE_FULL
Normal fully-lit render.
Definition kdatatype.h:63
kRendererType
Identifies the graphics backend used by kRenderer.
Definition kdatatype.h:54
@ RENDERER_GL
OpenGL renderer.
Definition kdatatype.h:55
glm::vec4 kVec4
4-component float vector.
Definition kdatatype.h:34
glm::vec3 kVec3
3-component float vector.
Definition kdatatype.h:33
glm::mat4 kMat4
4x4 float matrix.
Definition kdatatype.h:39