Skip to main content

OpenGLMeshUploadManager.ixx File

OpenGL manager that uploads mesh data and creates VAO/VBO/EBO objects. More...

Included Headers

#include <glad/gl.h> #include <GLFW/glfw3.h> #include <cassert> #include <iostream> #include <memory> #include <ostream> #include <ranges> #include <unordered_map> #include <vector> #include <helios.engine.runtime.world.tags> #include <helios.engine.util.log> #include <helios.opengl.OpenGLEnumMapper> #include <helios.engine.rendering.common.types.Vertex> #include <helios.engine.util.io> #include <helios.engine.runtime.messaging.command.concepts> #include <helios.engine.rendering.mesh.commands> #include <helios.engine.rendering.mesh.components> #include <helios.engine.rendering.mesh.types> #include <helios.engine.rendering.mesh.MeshEntity> #include <helios.engine.runtime.world> #include <helios.engine.rendering.mesh.concepts> #include <helios.opengl.components.OpenGLMeshComponent> #include <helios.engine.runtime.world.EngineWorld> #include <helios.engine.runtime.messaging.command.NullCommandBuffer> #include <helios.engine.runtime.messaging.command.CommandHandlerRegistry> #include <helios.engine.runtime.concepts>

Namespaces Index

namespacehelios
namespaceopengl
namespaceio
namespacetags
namespacecommands
namespacemesh
namespaceconcepts
namespaceconcepts
namespacecommand

Classes Index

classOpenGLMeshUploadManager<THandle, TCommandBuffer>

Manager that consumes mesh-upload commands and performs OpenGL buffer setup. More...

Macro Definitions Index

#defineHELIOS_LOG_SCOPE   "helios::opengl::OpenGLMeshUploadManager"

Description

OpenGL manager that uploads mesh data and creates VAO/VBO/EBO objects.

Macro Definitions

HELIOS_LOG_SCOPE

#define HELIOS_LOG_SCOPE   "helios::opengl::OpenGLMeshUploadManager"

Definition at line 58 of file OpenGLMeshUploadManager.ixx.

58#define HELIOS_LOG_SCOPE "helios::opengl::OpenGLMeshUploadManager"

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <glad/gl.h>
8#include <GLFW/glfw3.h>
9#include <cassert>
10#include <iostream>
11#include <memory>
12#include <ostream>
13#include <ranges>
14#include <unordered_map>
15#include <vector>
16
17
18export module helios.opengl.OpenGLMeshUploadManager;
19
20import helios.engine.util.log;
21import helios.engine.util.io;
22
23import helios.engine.rendering.common.types.Vertex;
24import helios.engine.rendering.mesh.commands;
25import helios.engine.rendering.mesh.components;
26import helios.engine.rendering.mesh.MeshEntity;
27import helios.engine.rendering.mesh.types;
28import helios.engine.rendering.mesh.concepts;
29
30import helios.engine.runtime.world.EngineWorld;
31import helios.engine.runtime.messaging.command.concepts;
32import helios.engine.runtime.messaging.command.NullCommandBuffer;
33import helios.engine.runtime.messaging.command.CommandHandlerRegistry;
34import helios.engine.runtime.concepts;
35import helios.engine.runtime.world.tags;
36import helios.engine.runtime.world;
37
38
39import helios.opengl.components.OpenGLMeshComponent;
40import helios.opengl.OpenGLEnumMapper;
41
42using namespace helios::engine::runtime::world;
43using namespace helios::engine::util::log;
44using namespace helios::engine::util::io;
45using namespace helios::engine::runtime::world::tags;
46using namespace helios::engine::rendering::mesh::commands;
47using namespace helios::engine::rendering::mesh::components;
48using namespace helios::engine::rendering::mesh;
49using namespace helios::engine::rendering::mesh::types;
50using namespace helios::engine::rendering::mesh::concepts;
51using namespace helios::engine::rendering::mesh::commands;
52using namespace helios::engine::rendering::common::types;
53using namespace helios::opengl;
54using namespace helios::opengl::components;
55using namespace helios::engine::runtime::messaging::command::concepts;
56using namespace helios::engine::runtime::messaging::command;
57
58#define HELIOS_LOG_SCOPE "helios::opengl::OpenGLMeshUploadManager"
59export namespace helios::opengl {
60
67 template<typename THandle, typename TCommandBuffer = NullCommandBuffer>
68 requires IsMeshHandle<THandle> && IsCommandBufferLike<TCommandBuffer>
70
71 RenderResourceWorld& renderResourceWorld_;
72
73 std::vector<MeshHandle> meshHandles_;
74
75
76 inline static const Logger& logger_ = LogManager::loggerForScope(HELIOS_LOG_SCOPE);
77
78
85 bool upload(MeshEntity mesh) noexcept {
86
87 using Handle = typename MeshEntity::Handle_type;
88
89 logger_.info("Uploading mesh data for MeshEntity {0}...", mesh.handle().entityId);
90
91 if (!mesh.template get<MeshUploadRequestComponent<Handle>>()) {
92 logger_.error("MeshUpload not requested by this entity");
93 assert(false && "MeshUpload not requested by this entity");
94 return false;
95 }
96
97
98 if (mesh.template get<OpenGLMeshComponent<Handle>>()) {
99 logger_.error("Mesh already has a MeshComponent");
100 assert(false && "Mesh already has a MeshComponent");
101 return false;
102 }
103
104 auto* meshDataComponent = mesh.template get<MeshDataComponent<Handle>>();
105 auto& meshData = meshDataComponent->meshData;
106
107 auto& openglMesh = mesh.template add<OpenGLMeshComponent<Handle>>();
108
109 openglMesh.indexCount = meshData.indices.size();
110 openglMesh.primitiveType = OpenGLEnumMapper::toOpenGL(meshData.primitiveType);
111
112 glGenVertexArrays(1, &openglMesh.vao);
113 glGenBuffers(1, &openglMesh.vbo);
114 glGenBuffers(1, &openglMesh.ebo);
115
116 glBindVertexArray(openglMesh.vao);
117 glBindBuffer(GL_ARRAY_BUFFER, openglMesh.vbo);
118
119 glBufferData(
120 GL_ARRAY_BUFFER,
121 meshData.vertices.size() * sizeof(Vertex),
122 &(meshData.vertices)[0],
123 GL_STATIC_DRAW
124 );
125
126 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, openglMesh.ebo);
127 glBufferData(
128 GL_ELEMENT_ARRAY_BUFFER,
129 meshData.indices.size() * sizeof(unsigned int),
130 &(meshData.indices)[0],
131 GL_STATIC_DRAW
132 );
133
134 // vertex position
135 glEnableVertexAttribArray(0);
136 glVertexAttribPointer(
137 0, 3, GL_FLOAT,
138 GL_FALSE, sizeof(Vertex), nullptr
139 );
140
141 // vertex normals
142 glEnableVertexAttribArray(1);
143 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
144 reinterpret_cast<void*>(offsetof(Vertex, normal))
145 );
146
147 // vertex texture coords
148 glEnableVertexAttribArray(2);
149 glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
150 reinterpret_cast<void*>(offsetof(Vertex, texCoords))
151 );
152
153 glBindVertexArray(0);
154 return true;
155 }
156
157 public:
158
159
160 explicit OpenGLMeshUploadManager(RenderResourceWorld& renderResourceWorld)
161 :
162 renderResourceWorld_(renderResourceWorld)
163 { }
164
168 using EngineRoleTag = ManagerRole;
169
170
176 void flush(UpdateContext& updateContext) noexcept {
177
178 if (meshHandles_.empty()) {
179 return;
180 }
181
182 for (const auto& sourceHandle : meshHandles_) {
183 auto meshEntity = renderResourceWorld_.findEntity<THandle>(sourceHandle);
184
185 if (!meshEntity) {
186 logger_.error("Could not find mesh entity");
187 assert(false && "Could not find mesh entity");
188 continue;
189 }
190
191 if (!upload(*meshEntity)) {
192 logger_.error("Could not compile mesh");
193 } else {
194 meshEntity->template remove<MeshUploadRequestComponent<THandle>>();
195 }
196 }
197
198 meshHandles_.clear();
199 }
200
207 bool submit(const MeshBatchUploadCommand<THandle>& command) noexcept {
208 for (const auto& meshHandle : command.meshHandles) {
209 meshHandles_.push_back(meshHandle);
210 }
211 return true;
212 }
213
219 void init(helios::engine::runtime::messaging::command::CommandHandlerRegistry& commandHandlerRegistry) noexcept {
220 commandHandlerRegistry.registerHandler<MeshBatchUploadCommand<THandle>>(*this);
221 }
222 };
223
224
225}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.