Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
mesh_renderer.h
1#ifndef OE_RENDER_MESH_RENDERER_H
2#define OE_RENDER_MESH_RENDERER_H
3
4#include "../scene/node.h" // IWYU pragma: keep (Required for NodePtr)
5
6#include "../scene/material.h"
7#include "../scene/camera.h"
8
9#include "mesh.h"
10#include "shader_base.h"
11
12namespace oe::render
13{
15 {
16 std::vector<bool> enabled_primitives;
17 scene::Material* material;
18 };
19
23 struct MeshRenderer
24 {
25 constexpr const static oe::render::GeometryPass geometry_pass = oe::render::GeometryPass::SOLID;
26
27 template <typename BaseMeshType>
28 MeshRenderer(oe::scene::NodePtr& node, const BaseMeshType& scene_mesh):
29 node(node),
30 render_mesh(scene_mesh)
31 {
32 node->raw_aabb = scene_mesh.getBoundingBox();
33
34 // By default, we consider the mesh to have only one material applied on all primitives
35 addMaterial(true);
36 }
37
49 void addMaterial(bool assign_to_primitives = false)
50 {
51 const auto primitives_count = render_mesh.primitives.size();
52 assert(primitives_count > 0);
53
54 materials.reserve(materials.size() + 1);
55 materials.push_back({});
56
57 primitives_by_material.push_back(std::vector<bool>(primitives_count, assign_to_primitives));
58 }
59
65 bool fillRenderCommands(const scene::Camera& camera, const bool is_blending_pass, std::vector<render::DrawCommand>& result)
66 {
67 const size_t material_count = materials.size();
68
69 if (material_count == 0 || !camera.isInFrustum(node->getBoundingBox()))
70 {
71 return false;
72 }
73
74 assert(primitives_by_material.size() == material_count);
75
76 result.reserve(material_count);
77
78 for (auto id_material = 0u; id_material < material_count; ++id_material)
79 {
80 auto* material = &materials.at(id_material);
81
82 if (material->is_blend != is_blending_pass)
83 {
84 continue;
85 }
86
87 assert(primitives_by_material.size() > id_material);
88 assert(render_mesh.primitives.size() == primitives_by_material.at(id_material).size());
89
90 result.push_back({
91 .enabled_primitives = primitives_by_material.at(id_material),
92 .material = material
93 });
94 }
95
96 return true;
97 }
98
104 void fillShader(ShaderBase* shader, const oe::scene::Camera& camera)
105 {
106 auto model_matrix = node->getAbsoluteModelMatrix();
107 auto normal_matrix = glm::transpose(glm::mat3(glm::inverse(camera.getView() * model_matrix)));
108
109 shader->setMat4("uModel", model_matrix);
110 shader->setMat3("uNormalMatrix", normal_matrix);
111 }
112
117
122
128 std::vector<scene::Material> materials;
129
137 std::vector<std::vector<bool>> primitives_by_material;
138 };
139}
140
141#endif
Definition mesh.h:80
Shader class.
Definition shader_base.h:32
void setMat3(const std::string &name, const glm::mat3 &value)
Send a 3x3 matrix uniform to the shader.
void setMat4(const std::string &name, const glm::mat4 &value)
Send a 4x4 matrix uniform to the shader.
The "eye of the scene".
Definition camera.h:31
bool isInFrustum(const glm::vec3 &point) const
Check if a point is visible from the camera point of view.
const glm::mat4 & getView() const
Get the view matrix of the camera.
Render agnostic material.
Definition material.h:90
Render related abstractions (Shader, Framebuffer, Cubemaps, Textures).
Definition opengl.h:12
GeometryPass
Vertex rendering passes.
Definition passes.h:12
@ SOLID
Definition passes.h:14
Definition mesh_renderer.h:15
void fillShader(ShaderBase *shader, const oe::scene::Camera &camera)
Send shader uniforms related to this mesh.
Definition mesh_renderer.h:104
oe::scene::NodePtr node
Related scene node.
Definition mesh_renderer.h:116
std::vector< scene::Material > materials
Distinct list of materials applied on this mesh.
Definition mesh_renderer.h:128
void addMaterial(bool assign_to_primitives=false)
Add a new material.
Definition mesh_renderer.h:49
Mesh render_mesh
Related render mesh (stored in GPU) of this renderer.
Definition mesh_renderer.h:121
bool fillRenderCommands(const scene::Camera &camera, const bool is_blending_pass, std::vector< render::DrawCommand > &result)
Generate a list of {primitives, Material*} to render this mesh.
Definition mesh_renderer.h:65
std::vector< std::vector< bool > > primitives_by_material
List to assign materials to primitives.
Definition mesh_renderer.h:137
Wrapper to a node reference to use pointers to node even if the actual node moves in memory.
Definition node_ptr.h:19