-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRenderCommandSystem.cpp
More file actions
333 lines (290 loc) · 17 KB
/
RenderCommandSystem.cpp
File metadata and controls
333 lines (290 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//// RenderCommandSystem.cpp //////////////////////////////////////////////////
//
// zzzzz zzz zzzzzzzzzzzzz zzzz zzzz zzzzzz zzzzz
// zzzzzzz zzz zzzz zzzz zzzz zzzz
// zzz zzz zzz zzzzzzzzzzzzz zzzz zzzz zzz
// zzz zzz zzz z zzzz zzzz zzzz zzzz
// zzz zzz zzzzzzzzzzzzz zzzz zzz zzzzzzz zzzzz
//
// Author: Mehdy MORVAN
// Date: 09/03/2025
// Description: Source file for the render system
//
///////////////////////////////////////////////////////////////////////////////
#include "RenderCommandSystem.hpp"
#include "Renderer3D.hpp"
#include "renderer/DrawCommand.hpp"
#include "components/Editor.hpp"
#include "components/Light.hpp"
#include "components/Render3D.hpp"
#include "components/RenderContext.hpp"
#include "components/SceneComponents.hpp"
#include "components/Camera.hpp"
#include "components/StaticMesh.hpp"
#include "components/Transform.hpp"
#include "core/event/Input.hpp"
#include "math/Frustum.hpp"
#include "math/Projection.hpp"
#include "math/Vector.hpp"
#include "renderPasses/Masks.hpp"
#include "Application.hpp"
#include "renderer/ShaderLibrary.hpp"
#include <glm/gtc/type_ptr.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>
namespace nexo::system {
/**
* @brief Sets up the lighting uniforms in the given shader.
*
* This static helper function binds the provided shader and sets uniforms for ambient, directional,
* point, and spot lights based on the current lightContext data. After updating the uniforms, the shader is unbound.
*
* @param shader Shared pointer to the shader used for rendering.
* @param lightContext The light context containing lighting information for the scene.
*
* @note The light context must contain valid values for:
* - ambientLight
* - directionalLights (and directionalLightCount)
* - pointLights (and pointLightCount)
* - spotLights (and spotLightCount)
*/
void RenderCommandSystem::setupLights(renderer::DrawCommand &cmd, const components::LightContext& lightContext)
{
cmd.uniforms["uAmbientLight"] = lightContext.ambientLight;
cmd.uniforms["uNumPointLights"] = static_cast<int>(lightContext.pointLightCount);
cmd.uniforms["uNumSpotLights"] = static_cast<int>(lightContext.spotLightCount);
const auto &directionalLight = lightContext.dirLight;
cmd.uniforms["uDirLight.direction"] = directionalLight.direction;
cmd.uniforms["uDirLight.color"] = glm::vec4(directionalLight.color, 1.0f);
const auto &pointLightComponentArray = coord->getComponentArray<components::PointLightComponent>();
const auto &transformComponentArray = coord->getComponentArray<components::TransformComponent>();
for (unsigned int i = 0; i < lightContext.pointLightCount; ++i)
{
const auto &pointLight = pointLightComponentArray->get(lightContext.pointLights[i]);
const auto &transform = transformComponentArray->get(lightContext.pointLights[i]);
cmd.uniforms[std::format("uPointLights[{}].position", i)] = transform.pos;
cmd.uniforms[std::format("uPointLights[{}].color", i)] = glm::vec4(pointLight.color, 1.0f);
cmd.uniforms[std::format("uPointLights[{}].constant", i)] = pointLight.constant;
cmd.uniforms[std::format("uPointLights[{}].linear", i)] = pointLight.linear;
cmd.uniforms[std::format("uPointLights[{}].quadratic", i)] = pointLight.quadratic;
}
const auto &spotLightComponentArray = coord->getComponentArray<components::SpotLightComponent>();
for (unsigned int i = 0; i < lightContext.spotLightCount; ++i)
{
const auto &spotLight = spotLightComponentArray->get(lightContext.spotLights[i]);
const auto &transform = transformComponentArray->get(lightContext.spotLights[i]);
cmd.uniforms[std::format("uSpotLights[{}].position", i)] = transform.pos;
cmd.uniforms[std::format("uSpotLights[{}].color", i)] = glm::vec4(spotLight.color, 1.0f);
cmd.uniforms[std::format("uSpotLights[{}].constant", i)] = spotLight.constant;
cmd.uniforms[std::format("uSpotLights[{}].linear", i)] = spotLight.linear;
cmd.uniforms[std::format("uSpotLights[{}].quadratic", i)] = spotLight.quadratic;
cmd.uniforms[std::format("uSpotLights[{}].direction", i)] = spotLight.direction;
cmd.uniforms[std::format("uSpotLights[{}].cutOff", i)] = spotLight.cutOff;
cmd.uniforms[std::format("uSpotLights[{}].outerCutoff", i)] = spotLight.outerCutoff;
}
}
static renderer::DrawCommand createOutlineDrawCommand(const components::CameraContext &camera)
{
renderer::DrawCommand cmd;
cmd.type = renderer::CommandType::FULL_SCREEN;
cmd.filterMask = 0;
cmd.filterMask |= renderer::F_OUTLINE_PASS;
cmd.shader = renderer::ShaderLibrary::getInstance().get("Outline pulse flat");
cmd.uniforms["uViewProjection"] = camera.viewProjectionMatrix;
cmd.uniforms["uCamPos"] = camera.cameraPosition;
cmd.uniforms["uMaskTexture"] = 0;
cmd.uniforms["uDepthTexture"] = 1;
cmd.uniforms["uDepthMaskTexture"] = 2;
cmd.uniforms["uTime"] = static_cast<float>(glfwGetTime());
const glm::vec2 screenSize = {camera.renderTarget->getSize().x, camera.renderTarget->getSize().y};
cmd.uniforms["uScreenSize"] = screenSize;
cmd.uniforms["uOutlineWidth"] = 10.0f;
return cmd;
}
static renderer::DrawCommand createGridDrawCommand(const components::CameraContext &camera, const components::RenderContext &renderContext)
{
renderer::DrawCommand cmd;
cmd.type = renderer::CommandType::FULL_SCREEN;
cmd.filterMask = 0;
cmd.filterMask |= renderer::F_GRID_PASS;
cmd.shader = renderer::ShaderLibrary::getInstance().get("Grid shader");
cmd.uniforms["uViewProjection"] = camera.viewProjectionMatrix;
cmd.uniforms["uCamPos"] = camera.cameraPosition;
const components::RenderContext::GridParams &gridParams = renderContext.gridParams;
cmd.uniforms["uGridSize"] = gridParams.gridSize;
cmd.uniforms["uGridCellSize"] = gridParams.cellSize;
cmd.uniforms["uGridMinPixelsBetweenCells"] = gridParams.minPixelsBetweenCells;
constexpr glm::vec4 gridColorThin = {0.5f, 0.55f, 0.7f, 0.6f};
constexpr glm::vec4 gridColorThick = {0.7f, 0.75f, 0.9f, 0.8f};
cmd.uniforms["uGridColorThin"] = gridColorThin;
cmd.uniforms["uGridColorThick"] = gridColorThick;
const glm::vec2 globalMousePos = event::getMousePosition();
glm::vec3 mouseWorldPos = camera.cameraPosition; // Default position (camera position)
const glm::vec2 renderTargetSize = camera.renderTarget->getSize();
if (renderContext.isChildWindow) {
// viewportBounds[0] is min (top-left), viewportBounds[1] is max (bottom-right)
const glm::vec2& viewportMin = renderContext.viewportBounds[0];
const glm::vec2& viewportMax = renderContext.viewportBounds[1];
const glm::vec2 viewportSize(viewportMax.x - viewportMin.x, viewportMax.y - viewportMin.y);
// Check if mouse is within the viewport bounds
if (math::isPosInBounds(globalMousePos, viewportMin, viewportMax)) {
// Calculate relative mouse position within the viewport
glm::vec2 relativeMousePos(
globalMousePos.x - viewportMin.x,
globalMousePos.y - viewportMin.y
);
// Convert to normalized coordinates [0,1]
glm::vec2 normalizedPos(
relativeMousePos.x / viewportSize.x,
relativeMousePos.y / viewportSize.y
);
// Convert to framebuffer coordinates
glm::vec2 framebufferPos(
normalizedPos.x * renderTargetSize.x,
normalizedPos.y * renderTargetSize.y
);
// Project ray
const glm::vec3 rayDir = math::projectRayToWorld(
framebufferPos.x, framebufferPos.y,
camera.viewProjectionMatrix, camera.cameraPosition,
static_cast<unsigned int>(renderTargetSize.x), static_cast<unsigned int>(renderTargetSize.y)
);
// Calculate intersection with y=0 plane (grid plane)
if (rayDir.y != 0.0f) {
float t = -camera.cameraPosition.y / rayDir.y;
if (t > 0.0f) {
mouseWorldPos = camera.cameraPosition + rayDir * t;
}
}
}
} else {
const glm::vec3 rayDir = math::projectRayToWorld(
globalMousePos.x, globalMousePos.y,
camera.viewProjectionMatrix, camera.cameraPosition,
static_cast<unsigned int>(renderTargetSize.x), static_cast<unsigned int>(renderTargetSize.y)
);
if (rayDir.y != 0.0f) {
float t = -camera.cameraPosition.y / rayDir.y;
if (t > 0.0f) {
mouseWorldPos = camera.cameraPosition + rayDir * t;
}
}
}
cmd.uniforms["uMouseWorldPos"] = mouseWorldPos;
cmd.uniforms["uTime"] = static_cast<float>(glfwGetTime());
return cmd;
}
static renderer::DrawCommand createSelectedDrawCommand(
const components::StaticMeshComponent &mesh,
const std::shared_ptr<assets::Material> &materialAsset,
const components::TransformComponent &transform)
{
renderer::DrawCommand cmd;
cmd.vao = mesh.vao;
const bool isOpaque = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->isOpaque : true;
if (isOpaque)
cmd.shader = renderer::ShaderLibrary::getInstance().get("Flat color");
else {
cmd.shader = renderer::ShaderLibrary::getInstance().get("Albedo unshaded transparent");
cmd.uniforms["uMaterial.albedoColor"] = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->albedoColor : glm::vec4(0.0f);
const auto albedoTextureAsset = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->albedoTexture.lock() : nullptr;
const auto albedoTexture = albedoTextureAsset && albedoTextureAsset->isLoaded() ? albedoTextureAsset->getData()->texture : nullptr;
cmd.uniforms["uMaterial.albedoTexIndex"] = renderer::NxRenderer3D::get().getTextureIndex(albedoTexture);
}
cmd.uniforms["uMatModel"] = transform.worldMatrix;
cmd.filterMask = 0;
cmd.filterMask = renderer::F_OUTLINE_MASK;
return cmd;
}
static renderer::DrawCommand createDrawCommand(
const ecs::Entity entity,
const std::shared_ptr<renderer::NxShader> &shader,
const components::StaticMeshComponent &mesh,
const std::shared_ptr<assets::Material> &materialAsset,
const components::TransformComponent &transform)
{
renderer::DrawCommand cmd;
cmd.vao = mesh.vao;
cmd.shader = shader;
cmd.uniforms["uMatModel"] = transform.worldMatrix;
cmd.uniforms["uEntityId"] = static_cast<int>(entity);
cmd.uniforms["uMaterial.albedoColor"] = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->albedoColor : glm::vec4(0.0f);
const auto albedoTextureAsset = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->albedoTexture.lock() : nullptr;
const auto albedoTexture = albedoTextureAsset && albedoTextureAsset->isLoaded() ? albedoTextureAsset->getData()->texture : nullptr;
cmd.uniforms["uMaterial.albedoTexIndex"] = renderer::NxRenderer3D::get().getTextureIndex(albedoTexture);
cmd.uniforms["uMaterial.specularColor"] = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->specularColor : glm::vec4(0.0f);
const auto specularTextureAsset = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->metallicMap.lock() : nullptr;
const auto specularTexture = specularTextureAsset && specularTextureAsset->isLoaded() ? specularTextureAsset->getData()->texture : nullptr;
cmd.uniforms["uMaterial.specularTexIndex"] = renderer::NxRenderer3D::get().getTextureIndex(specularTexture);
cmd.uniforms["uMaterial.emissiveColor"] = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->emissiveColor : glm::vec3(0.0f);
const auto emissiveTextureAsset = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->emissiveMap.lock() : nullptr;
const auto emissiveTexture = emissiveTextureAsset && emissiveTextureAsset->isLoaded() ? emissiveTextureAsset->getData()->texture : nullptr;
cmd.uniforms["uMaterial.emissiveTexIndex"] = renderer::NxRenderer3D::get().getTextureIndex(emissiveTexture);
cmd.uniforms["uMaterial.roughness"] = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->roughness : 1.0f;
const auto roughnessTextureAsset = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->roughnessMap.lock() : nullptr;
const auto roughnessTexture = roughnessTextureAsset && roughnessTextureAsset->isLoaded() ? roughnessTextureAsset->getData()->texture : nullptr;
cmd.uniforms["uMaterial.roughnessTexIndex"] = renderer::NxRenderer3D::get().getTextureIndex(roughnessTexture);
cmd.filterMask = 0;
cmd.filterMask |= renderer::F_FORWARD_PASS;
return cmd;
}
void RenderCommandSystem::update()
{
auto &renderContext = getSingleton<components::RenderContext>();
if (renderContext.sceneRendered == -1)
return;
const auto sceneRendered = static_cast<unsigned int>(renderContext.sceneRendered);
const SceneType sceneType = renderContext.sceneType;
const auto scenePartition = m_group->getPartitionView<components::SceneTag, unsigned int>(
[](const components::SceneTag& tag) { return tag.id; }
);
const auto *partition = scenePartition.getPartition(sceneRendered);
auto &app = Application::getInstance();
const std::string &sceneName = app.getSceneManager().getScene(sceneRendered).getName();
if (!partition) {
LOG_ONCE(NEXO_WARN, "Nothing to render in scene {}, skipping", sceneName);
return;
}
Logger::resetOnce(NEXO_LOG_ONCE_KEY("Nothing to render in scene {}, skipping", sceneName));
const auto transformSpan = get<components::TransformComponent>();
const auto meshSpan = get<components::StaticMeshComponent>();
const auto materialSpan = get<components::MaterialComponent>();
const std::span<const ecs::Entity> entitySpan = m_group->entities();
for (auto &camera : renderContext.cameras) {
const math::Frustum frustum(camera.viewProjectionMatrix);
std::vector<renderer::DrawCommand> drawCommands;
for (size_t i = partition->startIndex; i < partition->startIndex + partition->count; ++i) {
const ecs::Entity entity = entitySpan[i];
if (coord->entityHasComponent<components::CameraComponent>(entity) && sceneType != SceneType::EDITOR)
continue;
const auto &transform = transformSpan[i];
const auto &mesh = meshSpan[i];
// Frustum culling: skip entities whose AABB is entirely outside the camera frustum
if (mesh.hasBounds)
{
glm::vec3 worldMin, worldMax;
math::transformAABB(mesh.localMin, mesh.localMax, transform.worldMatrix, worldMin, worldMax);
if (!frustum.intersectsAABB(worldMin, worldMax))
continue;
}
const auto &materialAsset = materialSpan[i].material.lock();
std::string shaderStr = materialAsset && materialAsset->isLoaded() ? materialAsset->getData()->shader : "";
auto shader = renderer::ShaderLibrary::getInstance().get(shaderStr);
if (!shader)
continue;
auto cmd = createDrawCommand(entity, shader, mesh, materialAsset, transform);
cmd.uniforms["uViewProjection"] = camera.viewProjectionMatrix;
cmd.uniforms["uCamPos"] = camera.cameraPosition;
setupLights(cmd, renderContext.sceneLights);
drawCommands.push_back(std::move(cmd));
if (coord->entityHasComponent<components::SelectedTag>(entity))
drawCommands.push_back(createSelectedDrawCommand(mesh, materialAsset, transform));
}
camera.pipeline.addDrawCommands(drawCommands);
if (sceneType == SceneType::EDITOR && renderContext.gridParams.enabled)
camera.pipeline.addDrawCommand(createGridDrawCommand(camera, renderContext));
if (sceneType == SceneType::EDITOR)
camera.pipeline.addDrawCommand(createOutlineDrawCommand(camera));
}
}
}