@@ -170,85 +170,81 @@ void ColorPass::draw(RenderContext &ctx, const glm::ivec2 &resolution,
170170 // Set viewport
171171 ctx.setViewport (0 , 0 , resolution.x , resolution.y );
172172
173- // Iterate over entities with Mesh component
174- auto meshView = scene.view <ecs::Mesh, ecs::GlobalTransform>();
175- for (auto entity : meshView) {
176- auto &mesh = meshView.get <ecs::Mesh>(entity).mesh ;
177- auto &transform = meshView.get <ecs::GlobalTransform>(entity);
178-
179- if (!mesh)
173+ // Iterate over entities with Primitive and Material components
174+ auto primitiveView = scene.view <ecs::Primitive, ecs::Material, ecs::GlobalTransform>();
175+ for (auto [entity, primitiveComp, materialComp, transform] : primitiveView.each ()) {
176+ if (!primitiveComp.primitive )
180177 continue ;
181178
182- // Update uniform buffers
179+ const auto &primitive = *primitiveComp.primitive ;
180+
181+ // Update transform uniform buffer
183182 TransformUBO transformData;
184183 transformData.model = transform.matrix ;
185184 m_transform_ubo.set_sub_data (0 , sizeof (TransformUBO), &transformData);
186185
187- // Render each primitive in the mesh
188- for (const auto &primitive : mesh->primitives ) {
189- // Bind vertex buffers
190- if (primitive.positions ) {
191- ctx.bindVertexBuffer (0 , *primitive.positions , 0 , sizeof (glm::vec3));
192- }
193- if (primitive.normals ) {
194- ctx.bindVertexBuffer (1 , *primitive.normals , 0 , sizeof (glm::vec3));
186+ // Bind vertex buffers
187+ if (primitive.positions ) {
188+ ctx.bindVertexBuffer (0 , *primitive.positions , 0 , sizeof (glm::vec3));
189+ }
190+ if (primitive.normals ) {
191+ ctx.bindVertexBuffer (1 , *primitive.normals , 0 , sizeof (glm::vec3));
192+ }
193+ if (primitive.texcoords ) {
194+ ctx.bindVertexBuffer (2 , *primitive.texcoords , 0 , sizeof (glm::vec2));
195+ }
196+ if (primitive.colors ) {
197+ ctx.bindVertexBuffer (3 , *primitive.colors , 0 , sizeof (glm::vec3));
198+ }
199+
200+ // Bind index buffer if present
201+ if (primitive.indices ) {
202+ ctx.bindIndexBuffer (*primitive.indices , primitive.indexType );
203+ }
204+
205+ // Update material UBO and bind textures from Material component
206+ if (materialComp.material ) {
207+ const auto &mat = materialComp.material ;
208+ const auto &pbr = mat->pbrMetallicRoughness ;
209+
210+ // Prepare material data
211+ MaterialUBO materialData;
212+ materialData.baseColorFactor = pbr.baseColorFactor ;
213+ materialData.emissiveFactor = mat->emissiveFactor ;
214+ materialData.metallicFactor = pbr.metallicFactor ;
215+ materialData.roughnessFactor = pbr.roughnessFactor ;
216+ m_material_ubo.set_sub_data (0 , sizeof (MaterialUBO), &materialData);
217+
218+ // Bind textures with sampler
219+ if (pbr.baseColorTexture && pbr.baseColorTexture ->image ) {
220+ ctx.bindTexture (0 , *pbr.baseColorTexture ->image , *m_sampler);
195221 }
196- if (primitive.texcoords ) {
197- ctx.bindVertexBuffer (2 , *primitive.texcoords , 0 , sizeof (glm::vec2));
222+ if (pbr.metallicRoughnessTexture &&
223+ pbr.metallicRoughnessTexture ->image ) {
224+ ctx.bindTexture (1 , *pbr.metallicRoughnessTexture ->image ,
225+ *m_sampler);
198226 }
199- if (primitive. colors ) {
200- ctx.bindVertexBuffer ( 3 , *primitive. colors , 0 , sizeof (glm::vec3) );
227+ if (mat-> normalTexture && mat-> normalTexture -> image ) {
228+ ctx.bindTexture ( 2 , *mat-> normalTexture -> image , *m_sampler );
201229 }
202-
203- // Bind index buffer if present
204- if (primitive.indices ) {
205- ctx.bindIndexBuffer (*primitive.indices , primitive.indexType );
230+ if (mat->emissiveTexture && mat->emissiveTexture ->image ) {
231+ ctx.bindTexture (3 , *mat->emissiveTexture ->image , *m_sampler);
206232 }
207-
208- // Update material UBO and bind textures
209- if (primitive.material ) {
210- const auto &mat = primitive.material ;
211- const auto &pbr = mat->pbrMetallicRoughness ;
212-
213- // Prepare material data
214- MaterialUBO materialData;
215- materialData.baseColorFactor = pbr.baseColorFactor ;
216- materialData.emissiveFactor = mat->emissiveFactor ;
217- materialData.metallicFactor = pbr.metallicFactor ;
218- materialData.roughnessFactor = pbr.roughnessFactor ;
219- m_material_ubo.set_sub_data (0 , sizeof (MaterialUBO), &materialData);
220-
221- // Bind textures with sampler - directly from material
222- if (pbr.baseColorTexture && pbr.baseColorTexture ->image ) {
223- ctx.bindTexture (0 , *pbr.baseColorTexture ->image , *m_sampler);
224- }
225- if (pbr.metallicRoughnessTexture &&
226- pbr.metallicRoughnessTexture ->image ) {
227- ctx.bindTexture (1 , *pbr.metallicRoughnessTexture ->image ,
228- *m_sampler);
229- }
230- if (mat->normalTexture && mat->normalTexture ->image ) {
231- ctx.bindTexture (2 , *mat->normalTexture ->image , *m_sampler);
232- }
233- if (mat->emissiveTexture && mat->emissiveTexture ->image ) {
234- ctx.bindTexture (3 , *mat->emissiveTexture ->image , *m_sampler);
235- }
236- if (mat->occlusionTexture && mat->occlusionTexture ->image ) {
237- ctx.bindTexture (4 , *mat->occlusionTexture ->image , *m_sampler);
238- }
233+ if (mat->occlusionTexture && mat->occlusionTexture ->image ) {
234+ ctx.bindTexture (4 , *mat->occlusionTexture ->image , *m_sampler);
239235 }
236+ }
240237
241- ctx.bindUniformBuffer (0 , m_transform_ubo);
242- ctx.bindUniformBuffer (1 , m_camera_ubo);
243- ctx.bindUniformBuffer (2 , m_lighting_ubo);
244- ctx.bindUniformBuffer (3 , m_material_ubo);
238+ ctx.bindUniformBuffer (0 , m_transform_ubo);
239+ ctx.bindUniformBuffer (1 , m_camera_ubo);
240+ ctx.bindUniformBuffer (2 , m_lighting_ubo);
241+ ctx.bindUniformBuffer (3 , m_material_ubo);
245242
246- // Draw the primitive
247- if (primitive.hasIndices ()) {
248- ctx.drawElements (primitive.indexCount , nullptr );
249- } else if (primitive.positions ) {
250- ctx.drawArrays (0 , primitive.vertexCount );
251- }
243+ // Draw the primitive
244+ if (primitive.hasIndices ()) {
245+ ctx.drawElements (primitive.indexCount , nullptr );
246+ } else if (primitive.positions ) {
247+ ctx.drawArrays (0 , primitive.vertexCount );
252248 }
253249 }
254250
0 commit comments