11/*
2- Copyright (C) 2016-2019,2021 Rodrigo Jose Hernandez Cordoba
2+ Copyright (C) 2016-2019,2021,2025 Rodrigo Jose Hernandez Cordoba
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@ limitations under the License.
1616
1717#include < cassert>
1818#include < unordered_map>
19+ #include < algorithm>
1920#include " OpenGLFunctions.h"
2021#include " aeongames/Mesh.h"
2122#include " OpenGLMesh.h"
@@ -62,20 +63,37 @@ namespace AeonGames
6263 {
6364 glBindBuffer ( GL_ARRAY_BUFFER , mVertexBuffer .GetBufferId () );
6465 OPENGL_CHECK_ERROR_THROW ;
65- /* * @todo Find out what is best disable all or only unused */
66- for ( GLuint i = 0 ; i < Mesh::SEMANTIC_COUNT ; ++i )
66+ }
67+
68+ void OpenGLMesh::EnableAttributes ( const std::vector<OpenGLVertexAttribute>& aAttributes ) const
69+ {
70+ for ( GLuint i = 0 ; i < 8 ; ++i )
6771 {
6872 glDisableVertexAttribArray ( i );
6973 }
74+
7075 size_t offset{0 };
7176 for ( auto & attribute : mMesh ->GetAttributes () )
7277 {
73- glEnableVertexAttribArray ( std::get<0 > ( attribute ) );
78+ auto it = std::lower_bound ( aAttributes.begin (), aAttributes.end (), std::get<0 > ( attribute ),
79+ [] ( const OpenGLVertexAttribute & a, uint32_t b )
80+ {
81+ return a.name < b;
82+ } );
83+ if ( it == aAttributes.end () || it->name != std::get<0 > ( attribute ) )
84+ {
85+ /* If we get here, it means the attribute is not present in aAttributes,
86+ but it is present in the mesh, so skip it and continue. */
87+ offset += GetAttributeTotalSize ( attribute );
88+ continue ;
89+ }
90+
91+ glEnableVertexAttribArray ( it->location );
7492 OPENGL_CHECK_ERROR_THROW ;
7593 if ( ! ( std::get<3 > ( attribute ) & Mesh::AttributeFlag::INTEGER ) )
7694 {
7795 glVertexAttribPointer (
78- std::get< 0 > ( attribute ) ,
96+ it-> location ,
7997 std::get<1 > ( attribute ),
8098 MeshTypeToOGL[std::get<2 > ( attribute )],
8199 std::get<3 > ( attribute ) & Mesh::AttributeFlag::NORMALIZED ,
@@ -86,7 +104,7 @@ namespace AeonGames
86104 else
87105 {
88106 glVertexAttribIPointer (
89- std::get< 0 > ( attribute ) ,
107+ it-> location ,
90108 std::get<1 > ( attribute ),
91109 MeshTypeToOGL[std::get<2 > ( attribute )],
92110 static_cast <GLsizei> ( mMesh ->GetStride () ),
@@ -105,4 +123,12 @@ namespace AeonGames
105123 }
106124 OPENGL_CHECK_ERROR_THROW ;
107125 }
126+
127+ void OpenGLMesh::DisableAttributes ( const std::vector<OpenGLVertexAttribute>& aAttributes ) const
128+ {
129+ for ( const auto & attribute : aAttributes )
130+ {
131+ glDisableVertexAttribArray ( attribute.location );
132+ }
133+ }
108134}
0 commit comments