Kemena3D
Loading...
Searching...
No Matches
kemena::kRenderer Class Reference

Orchestrates the full render pipeline for a scene. More...

#include <krenderer.h>

Public Member Functions

 kRenderer ()
bool init (kWindow *window=nullptr, kRendererType type=kRendererType::RENDERER_GL)
 Initialises the renderer and creates the graphics driver.
void destroy ()
 Destroys all GPU resources and the driver.
void setEngineInfo (const kString name, uint32_t version)
 Stores an optional application name/version for driver diagnostics.
kWindowgetWindow ()
 Returns the window the renderer was initialised with.
kDrivergetDriver ()
 Returns the active graphics driver.
void clear ()
 Clears the active framebuffer to the clear colour.
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.
kVec4 getClearColor ()
 Returns the background clear colour.
void setClearColor (kVec4 newColor)
 Sets the background clear colour.
void setEnableScreenBuffer (bool newEnable, bool useDefaultShader=true)
 Enables or disables the off-screen screen buffer (post-process FBO).
bool getEnableScreenBuffer ()
 Returns whether the screen buffer post-process FBO is active.
void setScreenShader (kShader *newShader)
 Sets a custom screen-space shader for post-processing.
kShadergetScreenShader ()
 Returns the current screen-space shader.
void setEnableShadow (bool newEnable, bool useDefaultShader=true)
 Enables or disables shadow mapping.
bool getEnableShadow ()
 Returns whether shadow mapping is active.
void setShadowShader (kShader *newShader)
 Sets the shadow-map depth shader.
kShadergetShadowShader ()
 Returns the current shadow-map shader.
void setShadowCascadeCount (int count)
 Sets the number of cascades (1..4). Cheap; the underlying texture array always allocates the maximum, so no realloc.
int getShadowCascadeCount () const
 Returns the active cascade count.
void setShadowResolution (int resolution)
 Sets the per-cascade shadow-map resolution (e.g. 1024/2048/4096). Rebuilds the shadow texture if shadows are enabled.
int getShadowResolution () const
 Returns the per-cascade shadow-map resolution.
void setShadowSplitLambda (float lambda)
 Sets the cascade split blend: 0 = uniform, 1 = logarithmic.
float getShadowSplitLambda () const
 Returns the cascade split blend factor.
void setShadowDebug (bool enable)
 Toggles a debug view that tints each cascade a distinct colour.
bool getShadowDebug () const
 Returns whether the cascade debug view is active.
void setShadowBias (float bias)
 Sets the constant shadow bias passed to lit shaders.
float getShadowBias () const
 Returns the constant shadow bias.
void setShadowNormalBias (float bias)
 Sets the slope-scaled (normal) shadow bias passed to lit shaders.
float getShadowNormalBias () const
 Returns the slope-scaled shadow bias.
void setShadowSoftness (float softness)
 Sets the PCF tap spacing (in shadow-map texels) passed to lit shaders.
float getShadowSoftness () const
 Returns the PCF tap spacing.
void setEnableAutoExposure (bool newEnable)
 Enables or disables automatic exposure adjustment.
bool getEnableAutoExposure ()
 Returns whether automatic exposure adjustment is active.
void resizeFbo (int newWidth, int newHeight)
 Resizes the screen-buffer FBOs to match a new viewport size.
uint32_t getFboTexture ()
 Returns the resolved (single-sample) FBO colour texture handle.
int getFboWidth ()
 Returns the current FBO width in pixels.
int getFboHeight ()
 Returns the current FBO height in pixels.
float srgbToLinear (float c)
 Converts an sRGB component value to linear space.
kVec3 idToRgb (unsigned int i)
 Converts a packed integer ID to an RGB colour for object picking.
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 setEnableObjectPicking (bool enable, bool useDefaultShader=true)
 Enables or disables the color-ID picking FBO and compiles the built-in picking shader.
bool getEnableObjectPicking ()
 Returns whether color-ID object picking is active.
void setOctreeCullingEnabled (bool enable)
 Enable or disable octree-based frustum culling (default: enabled).
void setOctreeDirty ()
 Marks the static-mesh octree as needing a rebuild.
bool getOctreeCullingEnabled () const
 Returns whether octree frustum culling is active.
void setCullingCamera (kCamera *camera)
 Override the camera used to compute the culling frustum.
kCameragetCullingCamera () const
 Returns the override culling camera, or nullptr if using main camera.
const kOctreegetOctree () const
 Returns a reference to the scene octree (read-only).
kObjectpickObject (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 pass.
void renderPickingPass (kWorld *world, kScene *scene, int viewWidth, int viewHeight)
 Renders the color-ID picking pass into the picking FBO.
void setRenderMode (kRenderMode mode)
 Sets the debug visualization mode for subsequent render() calls.
kRenderMode getRenderMode ()
 Returns the current debug visualization mode.
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 renderDebugShapes (kWorld *world, kScene *scene, const std::vector< kString > &selectedUuids)
 Draws editor debug shapes for selected lights and cameras.
void setOctreeDebugEnabled (bool enable)
 Enable or disable octree AABB wireframe debug visualization.
bool getOctreeDebugEnabled () const
 Returns whether octree debug visualization is active.
void renderOctreeDebug (kWorld *world, kScene *scene)
 Draws octree node AABBs and mesh world AABBs as wireframe boxes.
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 (e.g. nav-mesh wireframe). Each consecutive pair of points is one line.

Detailed Description

Orchestrates the full render pipeline for a scene.

Usage outline:

kWindow window;
window.init(1280, 720, "My App");
kRenderer renderer;
renderer.init(&window, kRendererType::RENDERER_GL);
renderer.setEnableScreenBuffer(true); // optional post-process FBO
renderer.setEnableShadow(true); // optional shadow map
// main loop
while (window.getRunning()) {
window.getTimer()->tick();
renderer.render(&world, &scene, 0, 0, 1280, 720,
window.getTimer()->getDeltaTime());
}
renderer.destroy();
void setEnableShadow(bool newEnable, bool useDefaultShader=true)
Enables or disables shadow mapping.
void destroy()
Destroys all GPU resources and the driver.
void setEnableScreenBuffer(bool newEnable, bool useDefaultShader=true)
Enables or disables the off-screen screen buffer (post-process FBO).
bool init(kWindow *window=nullptr, kRendererType type=kRendererType::RENDERER_GL)
Initialises the renderer and creates the graphics driver.
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.
float getDeltaTime()
Returns the elapsed time (in seconds) between the last two tick() calls.
void tick()
Records the current time and computes the delta since the last tick.
Creates and manages an SDL3 application window.
Definition kwindow.h:32
bool getRunning()
Returns whether the application main loop should keep running.
kTimer * getTimer()
Returns the frame timer associated with this window.
bool init(int width, int height, kString title, bool maximized=false, kWindowType type=kWindowType::WINDOW_DEFAULT, void *nativeHandle=nullptr)
Creates and shows the SDL window.
@ RENDERER_GL
OpenGL renderer.
Definition kdatatype.h:55

Constructor & Destructor Documentation

◆ kRenderer()

kemena::kRenderer::kRenderer ( )

Member Function Documentation

◆ clear()

void kemena::kRenderer::clear ( )

Clears the active framebuffer to the clear colour.

Binds the screen FBO (if enabled) before clearing so that subsequent render() calls draw into the same buffer.

◆ destroy()

void kemena::kRenderer::destroy ( )

Destroys all GPU resources and the driver.

◆ getClearColor()

kVec4 kemena::kRenderer::getClearColor ( )

Returns the background clear colour.

Returns
RGBA colour in linear space.

◆ getCullingCamera()

kCamera * kemena::kRenderer::getCullingCamera ( ) const
inline

Returns the override culling camera, or nullptr if using main camera.

◆ getDriver()

kDriver * kemena::kRenderer::getDriver ( )

Returns the active graphics driver.

Returns
Pointer to the kDriver owned by this renderer.

◆ getEnableAutoExposure()

bool kemena::kRenderer::getEnableAutoExposure ( )

Returns whether automatic exposure adjustment is active.

◆ getEnableObjectPicking()

bool kemena::kRenderer::getEnableObjectPicking ( )

Returns whether color-ID object picking is active.

◆ getEnableScreenBuffer()

bool kemena::kRenderer::getEnableScreenBuffer ( )

Returns whether the screen buffer post-process FBO is active.

◆ getEnableShadow()

bool kemena::kRenderer::getEnableShadow ( )

Returns whether shadow mapping is active.

◆ getFboHeight()

int kemena::kRenderer::getFboHeight ( )

Returns the current FBO height in pixels.

◆ getFboTexture()

uint32_t kemena::kRenderer::getFboTexture ( )

Returns the resolved (single-sample) FBO colour texture handle.

Returns
Texture handle suitable for use as an ImGui image or sampler.

◆ getFboWidth()

int kemena::kRenderer::getFboWidth ( )

Returns the current FBO width in pixels.

◆ getOctree()

const kOctree & kemena::kRenderer::getOctree ( ) const
inline

Returns a reference to the scene octree (read-only).

◆ getOctreeCullingEnabled()

bool kemena::kRenderer::getOctreeCullingEnabled ( ) const
inline

Returns whether octree frustum culling is active.

◆ getOctreeDebugEnabled()

bool kemena::kRenderer::getOctreeDebugEnabled ( ) const
inline

Returns whether octree debug visualization is active.

◆ getRenderMode()

kRenderMode kemena::kRenderer::getRenderMode ( )

Returns the current debug visualization mode.

◆ getScreenShader()

kShader * kemena::kRenderer::getScreenShader ( )

Returns the current screen-space shader.

Returns
Pointer to the screen shader, or nullptr if none is set.

◆ getShadowBias()

float kemena::kRenderer::getShadowBias ( ) const
inline

Returns the constant shadow bias.

◆ getShadowCascadeCount()

int kemena::kRenderer::getShadowCascadeCount ( ) const
inline

Returns the active cascade count.

◆ getShadowDebug()

bool kemena::kRenderer::getShadowDebug ( ) const
inline

Returns whether the cascade debug view is active.

◆ getShadowNormalBias()

float kemena::kRenderer::getShadowNormalBias ( ) const
inline

Returns the slope-scaled shadow bias.

◆ getShadowResolution()

int kemena::kRenderer::getShadowResolution ( ) const
inline

Returns the per-cascade shadow-map resolution.

◆ getShadowShader()

kShader * kemena::kRenderer::getShadowShader ( )

Returns the current shadow-map shader.

Returns
Pointer to the shadow shader, or nullptr if none is set.

◆ getShadowSoftness()

float kemena::kRenderer::getShadowSoftness ( ) const
inline

Returns the PCF tap spacing.

◆ getShadowSplitLambda()

float kemena::kRenderer::getShadowSplitLambda ( ) const
inline

Returns the cascade split blend factor.

◆ getWindow()

kWindow * kemena::kRenderer::getWindow ( )

Returns the window the renderer was initialised with.

Returns
Pointer to the kWindow, or nullptr if none was provided.

◆ idToRgb()

kVec3 kemena::kRenderer::idToRgb ( unsigned int i)

Converts a packed integer ID to an RGB colour for object picking.

Parameters
iObject ID.
Returns
RGB colour with each channel in [0, 255].

◆ init()

bool kemena::kRenderer::init ( kWindow * window = nullptr,
kRendererType type = kRendererType::RENDERER_GL )

Initialises the renderer and creates the graphics driver.

Parameters
windowTarget window (may be nullptr for off-screen rendering).
typeGraphics backend to use.
Returns
true on success.

◆ pickObject()

kObject * kemena::kRenderer::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 pass.

Renders all mesh nodes into a dedicated picking FBO with each mesh colored by its unique ID, reads the pixel at (mouseX, mouseY), decodes the ID, and returns the corresponding kObject.

Parameters
worldWorld containing the active camera.
sceneScene to pick from.
mouseXViewport-relative X coordinate (0 = left edge).
mouseYViewport-relative Y coordinate (0 = top edge).
viewWidthViewport width in pixels.
viewHeightViewport height in pixels.
Returns
Pointer to the picked kObject, or nullptr if nothing was hit. setEnableObjectPicking(true) must have been called first.

◆ render()

void kemena::kRenderer::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.

The render order is:

  1. Shadow map pass (if a sun light and shadow shader are present).
  2. Opaque scene geometry.
  3. Screen-space post-processing blit (if screen buffer is enabled).
Parameters
worldWorld containing the main camera.
sceneScene to render (lights, meshes, etc.).
x,yViewport origin in pixels.
width,heightViewport dimensions in pixels.
deltaTimeFrame delta time in seconds (used for animation).
autoClearSwapWindowIf true, automatically clears and swaps the window.

◆ renderDebugLines()

void kemena::kRenderer::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 (e.g. nav-mesh wireframe). Each consecutive pair of points is one line.

Parameters
worldSupplies the main camera (view/projection).
segmentsFlat list of point pairs.
colorLine colour (RGB).

◆ renderDebugShapes()

void kemena::kRenderer::renderDebugShapes ( kWorld * world,
kScene * scene,
const std::vector< kString > & selectedUuids )

Draws editor debug shapes for selected lights and cameras.

Renders wire shapes (light range spheres/cones, camera frustums, sun direction arrows) for any selected light or camera node.

Must be called after render() and before the final ImGui pass. Requires setEnableScreenBuffer(true).

Parameters
worldWorld containing the active editor camera.
sceneScene whose lights and cameras are inspected.
selectedUuidsUUIDs of currently selected objects.

◆ renderOctreeDebug()

void kemena::kRenderer::renderOctreeDebug ( kWorld * world,
kScene * scene )

Draws octree node AABBs and mesh world AABBs as wireframe boxes.

Octree leaf nodes: green, internal nodes: grey. Static mesh AABBs: yellow, dynamic mesh AABBs: cyan. Call after renderDebugShapes each frame.

◆ renderOutline()

void kemena::kRenderer::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.

Requires renderPickingPass() to have been called this frame. Uses the picking texture to detect selection boundaries in screen space, then composites an outline with alpha blending — gives uniform pixel-width outlines on any mesh shape.

Parameters
worldWorld containing the active camera.
sceneScene owning the selected objects.
selectedUuidsUUIDs of selected objects to outline.
colorOutline RGBA colour (default: orange).
thicknessOutline radius in pixels (default: 3).

◆ renderPickingPass()

void kemena::kRenderer::renderPickingPass ( kWorld * world,
kScene * scene,
int viewWidth,
int viewHeight )

Renders the color-ID picking pass into the picking FBO.

Should be called once per frame so the picking texture is always fresh. pickObject() will read from this texture instead of re-rendering.

Parameters
worldWorld containing the active camera.
sceneScene to render.
viewWidthViewport width in physical pixels.
viewHeightViewport height in physical pixels.

◆ resizeFbo()

void kemena::kRenderer::resizeFbo ( int newWidth,
int newHeight )

Resizes the screen-buffer FBOs to match a new viewport size.

Called automatically by render() when the viewport dimensions change.

Parameters
newWidthNew FBO width in pixels.
newHeightNew FBO height in pixels.

◆ rgbToId()

unsigned int kemena::kRenderer::rgbToId ( unsigned int r,
unsigned int g,
unsigned int b )

Converts an RGB colour read back from the GPU to an object ID.

Parameters
rRed channel value [0, 255].
gGreen channel value [0, 255].
bBlue channel value [0, 255].
Returns
Packed object ID.

◆ setClearColor()

void kemena::kRenderer::setClearColor ( kVec4 newColor)

Sets the background clear colour.

The input is expected to be in sRGB space and is converted to linear before being stored.

Parameters
newColorsRGB RGBA colour.

◆ setCullingCamera()

void kemena::kRenderer::setCullingCamera ( kCamera * camera)
inline

Override the camera used to compute the culling frustum.

By default (nullptr) the world's main camera is used. Set a different camera here to freeze or redirect the frustum — useful for debugging culling without changing the render viewpoint.

Parameters
cameraCamera to use for frustum extraction, or nullptr to revert to the main camera.

◆ setEnableAutoExposure()

void kemena::kRenderer::setEnableAutoExposure ( bool newEnable)

Enables or disables automatic exposure adjustment.

When enabled, the average luminance of the resolved FBO colour texture is sampled each frame (via mipmaps) and used to drive an exposure value that is passed to the screen shader.

Parameters
newEnabletrue to enable.

◆ setEnableObjectPicking()

void kemena::kRenderer::setEnableObjectPicking ( bool enable,
bool useDefaultShader = true )

Enables or disables the color-ID picking FBO and compiles the built-in picking shader.

When enabled, pickObject() can be called to determine which scene object is under a given screen coordinate by rendering all meshes with unique solid ID colors and reading back the clicked pixel.

Intended for editor use — works for any object regardless of whether it has a physics body attached.

Parameters
enabletrue to enable.
useDefaultShaderIf true, a built-in picking shader is compiled automatically.

◆ setEnableScreenBuffer()

void kemena::kRenderer::setEnableScreenBuffer ( bool newEnable,
bool useDefaultShader = true )

Enables or disables the off-screen screen buffer (post-process FBO).

When enabled the scene is first rendered into an MSAA FBO, resolved to a single-sample texture, and then drawn to the screen via a full-screen quad shader.

Parameters
newEnabletrue to enable.
useDefaultShaderIf true, a built-in tone-mapping/gamma shader is compiled and set automatically.

◆ setEnableShadow()

void kemena::kRenderer::setEnableShadow ( bool newEnable,
bool useDefaultShader = true )

Enables or disables shadow mapping.

Parameters
newEnabletrue to enable.
useDefaultShaderIf true, a built-in depth-only shadow shader is compiled and set automatically.

◆ setEngineInfo()

void kemena::kRenderer::setEngineInfo ( const kString name,
uint32_t version )

Stores an optional application name/version for driver diagnostics.

Parameters
nameApplication name.
versionApplication version number.

◆ setOctreeCullingEnabled()

void kemena::kRenderer::setOctreeCullingEnabled ( bool enable)
inline

Enable or disable octree-based frustum culling (default: enabled).

When enabled, an octree is rebuilt each frame from all scene meshes and only meshes whose world AABB intersects the camera frustum are rendered.

◆ setOctreeDebugEnabled()

void kemena::kRenderer::setOctreeDebugEnabled ( bool enable)
inline

Enable or disable octree AABB wireframe debug visualization.

◆ setOctreeDirty()

void kemena::kRenderer::setOctreeDirty ( )
inline

Marks the static-mesh octree as needing a rebuild.

Call this whenever a static mesh is added, removed, or moved. The octree rebuilds once on the next rendered frame, then stays valid until marked dirty again.

◆ setRenderMode()

void kemena::kRenderer::setRenderMode ( kRenderMode mode)

Sets the debug visualization mode for subsequent render() calls.

Parameters
modeOne of the kRenderMode values.

◆ setScreenShader()

void kemena::kRenderer::setScreenShader ( kShader * newShader)

Sets a custom screen-space shader for post-processing.

Parameters
newShaderShader to use; the renderer takes ownership.

◆ setShadowBias()

void kemena::kRenderer::setShadowBias ( float bias)
inline

Sets the constant shadow bias passed to lit shaders.

◆ setShadowCascadeCount()

void kemena::kRenderer::setShadowCascadeCount ( int count)

Sets the number of cascades (1..4). Cheap; the underlying texture array always allocates the maximum, so no realloc.

◆ setShadowDebug()

void kemena::kRenderer::setShadowDebug ( bool enable)
inline

Toggles a debug view that tints each cascade a distinct colour.

◆ setShadowNormalBias()

void kemena::kRenderer::setShadowNormalBias ( float bias)
inline

Sets the slope-scaled (normal) shadow bias passed to lit shaders.

◆ setShadowResolution()

void kemena::kRenderer::setShadowResolution ( int resolution)

Sets the per-cascade shadow-map resolution (e.g. 1024/2048/4096). Rebuilds the shadow texture if shadows are enabled.

◆ setShadowShader()

void kemena::kRenderer::setShadowShader ( kShader * newShader)

Sets the shadow-map depth shader.

Parameters
newShaderShader used for the shadow pass.

◆ setShadowSoftness()

void kemena::kRenderer::setShadowSoftness ( float softness)
inline

Sets the PCF tap spacing (in shadow-map texels) passed to lit shaders.

◆ setShadowSplitLambda()

void kemena::kRenderer::setShadowSplitLambda ( float lambda)
inline

Sets the cascade split blend: 0 = uniform, 1 = logarithmic.

◆ srgbToLinear()

float kemena::kRenderer::srgbToLinear ( float c)

Converts an sRGB component value to linear space.

Parameters
csRGB channel value in [0, 1].
Returns
Linearised value.

The documentation for this class was generated from the following file: