14 const std::vector<int>& material_buffer;
16 compareMaterial(
const std::vector<int>& _buffer ) : material_buffer(_buffer) {}
18 bool operator() (
const int& a,
const int& b )
const
20 return material_buffer[a] < material_buffer[b];
33 MeshVertex( ) : material(-1), position(-1), texcoord(-1), normal(-1) {}
34 MeshVertex(
const int m,
const int p,
const int t,
const int n ) : material(m), position(p), texcoord(t), normal(n) {}
39 if(material != b.material)
return material < b.material;
40 if(position != b.position)
return position < b.position;
41 if(texcoord != b.texcoord)
return texcoord < b.texcoord;
42 if(normal != b.normal)
return normal < b.normal;
55 mesh.positions.reserve(data.positions.size());
56 mesh.texcoords.reserve(data.positions.size());
57 mesh.normals.reserve(data.positions.size());
60 std::vector<int> triangles;
61 triangles.reserve(data.material_indices.size());
62 for(
int i= 0; i < (int) data.material_indices.size(); i++)
63 triangles.push_back(i);
65 std::sort(triangles.begin(), triangles.end(),
compareMaterial(data.material_indices));
68 int material_id= data.material_indices[triangles[0]];
69 mesh.material_groups.push_back(
MeshGroup(material_id, 0) );
75 std::map<MeshVertex, int> remap;
76 for(
int i= 0; i < (int) triangles.size(); i++)
82 if(material_id != data.material_indices[triangles[i]])
85 material_id= data.material_indices[triangles[i]];
87 mesh.material_groups.back().count= 3*i - mesh.material_groups.back().first;
88 mesh.material_groups.push_back(
MeshGroup(material_id, 3*i) );
91 for(
int k= 0; k < 3; k++)
94 int index= 3*triangles[i] + k;
95 MeshVertex vertex=
MeshVertex(material_id, data.position_indices[index], data.texcoord_indices[index], data.normal_indices[index]);
97 auto found= remap.insert( std::make_pair(
vertex, remap.size()) );
101 assert(data.position_indices[index] != -1);
102 mesh.positions.push_back( data.positions[data.position_indices[index]] );
104 if(data.texcoord_indices[index] != -1)
106 mesh.texcoords.push_back( data.texcoords[data.texcoord_indices[index]] );
109 mesh.texcoords.push_back(
vec2() );
111 if(data.normal_indices[index] != -1)
113 mesh.normals.push_back( data.normals[data.normal_indices[index]] );
116 mesh.normals.push_back(
vec3() );
120 mesh.indices.push_back(found.first->second);
125 mesh.material_groups.back().count= 3*triangles.size() - mesh.material_groups.back().first;
127 printf(
"buffers : %d positions, %d texcoords, %d normals, %d indices, %d groups\n",
128 (
int) mesh.positions.size(), (
int) mesh.texcoords.size(), (
int) mesh.normals.size(), (
int) mesh.indices.size(), (
int) mesh.material_groups.size());
const std::vector< unsigned int > & material_indices() const
renvoie les indices des matieres des triangles.
const Materials & materials() const
renvoie la description des matieres.
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
MeshBuffer buffers(const MeshData &data)
construction a partir des donnees d'un maillage.
representation d'un objet openGL.
representation des donnees d'un fichier wavefront .obj
representation d'un objet.
representation d'une sequence de triangles associes a la meme matiere
representation de l'indexation complete d'un sommet
vecteur generique, utilitaire.
vecteur generique, utilitaire.
representation de l'indexation complete d'un sommet
bool has_normals(const Hit &hit, const GLTFScene &scene)
verifie la presence des normales par sommet.
bool has_texcoords(const Hit &hit, const GLTFScene &scene)
verifie la presence des coordonnees de texture...