Kemena3D
Loading...
Searching...
No Matches
koffscreenrenderer.h
Go to the documentation of this file.
1
5
6#ifndef KOFFSCREENRENDERER_H
7#define KOFFSCREENRENDERER_H
8
9#include "kexport.h"
10#include "kdatatype.h"
11#include "kcamera.h"
12
13#include <string>
14
15namespace kemena
16{
17 class kMesh;
18 class kScene;
19 class kWorld;
20 class kObject;
21 class kShader;
22 class kDriver;
23
45 {
46 public:
53 kOffscreenRenderer(int width = 256, int height = 256);
54
59
68 void resize(int newWidth, int newHeight);
69
78 void setBackgroundColor(kVec4 color) { bgColor = color; }
79
84 kVec4 getBackgroundColor() const { return bgColor; }
85
96 void render(kWorld *world, kScene *scene, kCamera *camera);
97
110 void renderMesh(kMesh *mesh, kCamera *camera = nullptr);
111
126 void renderMeshWithMaterial(kMesh *mesh, kCamera *camera = nullptr);
127
134 uint32_t getTexture() const { return colorTex; }
135
140 int getWidth() const { return width; }
141
146 int getHeight() const { return height; }
147
160 bool saveToFile(const kString &filePath) const;
161
162 private:
163 kDriver *driver = nullptr;
164 uint32_t fbo = 0;
165 uint32_t colorTex = 0;
166 uint32_t depthRbo = 0;
167
168 int width;
169 int height;
170 int ssaaScale = 2; // render at Nx resolution, box-filter downsample on save
171 kVec4 bgColor = kVec4(0.15f, 0.15f, 0.15f, 1.0f);
172
173 kShader *builtinShader = nullptr;
174
175 // Cascaded-shadow-map resources for the offscreen pass. Lazily allocated
176 // on the first render() call so headless thumbnail use cases that never
177 // render a scene don't pay the VRAM cost.
178 static constexpr int kMaxShadowCascades = 4;
179 kShader *shadowShader = nullptr;
180 uint32_t shadowFbo = 0;
181 uint32_t shadowTexArray = 0;
182 int shadowResolution = 1024; // smaller than main renderer to save VRAM
183 int shadowCascadeCount = 3;
184 float shadowSplitLambda = 0.85f;
185 kMat4 lightSpaceMatrices[kMaxShadowCascades];
186 float cascadeSplits[kMaxShadowCascades] = {};
187
188 void createFBO();
189 void destroyFBO();
190 void ensureBuiltinShader();
191
192 void ensureShadowResources();
193 void applyShadowResolution(int resolution);
194 void renderShadowPass(kWorld *world, kScene *scene, kCamera *camera);
195 void renderShadowNode(kObject *node, const kMat4 &lightSpace, kShader *shader);
196
197 void renderNodeFull(kObject *node, kScene *scene, kCamera *camera);
198 void drawMeshWithMaterial(kMesh *mesh, kScene *scene, kCamera *camera,
199 int sunCount, int pointCount, int spotCount);
200 void drawMeshBuiltin(kMesh *mesh, kCamera *camera);
201 void drawMeshHierarchy(kMesh *mesh, kCamera *camera);
202
203 void setupLightsFromScene(kShader *shader, kScene *scene,
204 int &outSun, int &outPoint, int &outSpot);
205 void setupSingleSunLight(kShader *shader,
206 kVec3 direction, kVec3 diffuse, float power);
207 void bindMaterialTextures(kMesh *mesh, kShader *shader);
208 void unbindMaterialTextures(kMesh *mesh);
209 };
210}
211
212#endif // KOFFSCREENRENDERER_H
Scene-graph camera node.
Definition kcamera.h:33
Pure-virtual graphics driver interface.
Definition kdriver.h:86
Scene-graph node that holds renderable geometry.
Definition kmesh.h:35
Base scene-graph node.
Definition kobject.h:43
int getWidth() const
Get the output texture width in pixels.
Definition koffscreenrenderer.h:140
int getHeight() const
Get the output texture height in pixels.
Definition koffscreenrenderer.h:146
kOffscreenRenderer(int width=256, int height=256)
Construct the renderer and allocate its offscreen FBO.
~kOffscreenRenderer()
Destroy the renderer and release all GPU resources (FBO, textures, shaders).
bool saveToFile(const kString &filePath) const
Save the current render result to an image file.
void renderMesh(kMesh *mesh, kCamera *camera=nullptr)
Render a single mesh to the offscreen buffer.
void renderMeshWithMaterial(kMesh *mesh, kCamera *camera=nullptr)
Render a mesh using its assigned material shader and uniforms.
uint32_t getTexture() const
GPU texture ID of the current render result.
Definition koffscreenrenderer.h:134
void setBackgroundColor(kVec4 color)
Set the background clear color and alpha (default: opaque dark grey).
Definition koffscreenrenderer.h:78
kVec4 getBackgroundColor() const
Get the current background clear color and alpha.
Definition koffscreenrenderer.h:84
void resize(int newWidth, int newHeight)
Resize the offscreen buffer.
void render(kWorld *world, kScene *scene, kCamera *camera)
Render the full scene to the offscreen buffer.
Holds all objects, lights, and rendering settings for one scene.
Definition kscene.h:36
Wraps a compiled GLSL shader program.
Definition kshader.h:55
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.
Symbol visibility / linkage macro for the Kemena3D library.
#define KEMENA3D_API
Definition kexport.h:35
Top-level Kemena3D engine namespace.
Definition kanimation.h:23
std::string kString
Standard string alias.
Definition kdatatype.h:42
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