59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
#include <vector>
|
|
#include <map>
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include "vboindexer.hpp"
|
|
|
|
bool getSimilarVertexIndex_fast(
|
|
const buffer::PackedVertexData &packed,
|
|
std::map<buffer::PackedVertexData, GLushort> &VertexToOutIndex,
|
|
GLushort &result)
|
|
{
|
|
std::map<buffer::PackedVertexData, GLushort>::iterator it = VertexToOutIndex.find(packed);
|
|
if ( it == VertexToOutIndex.end() ){
|
|
return false;
|
|
}else{
|
|
result = it->second;
|
|
return true;
|
|
}
|
|
}
|
|
bool getSimilarVertexIndex_fast(
|
|
const buffer::PackedVertexData &packed,
|
|
std::map<buffer::PackedVertexData, unsigned int> &VertexToOutIndex,
|
|
unsigned int &result)
|
|
{
|
|
std::map<buffer::PackedVertexData, unsigned int>::iterator it = VertexToOutIndex.find(packed);
|
|
if ( it == VertexToOutIndex.end() ){
|
|
return false;
|
|
}else{
|
|
result = it->second;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void indexVBO(
|
|
const std::vector<buffer::PackedVertexData> &in_vertices,
|
|
|
|
std::vector<GLushort> &out_indices,
|
|
std::vector<buffer::PackedVertexData> &out_vertices)
|
|
{
|
|
std::map<buffer::PackedVertexData, GLushort> VertexToOutIndex;
|
|
|
|
out_indices.reserve(in_vertices.size());
|
|
// For each input vertex
|
|
for (size_t i=0; i<in_vertices.size(); i++ ){
|
|
// Try to find a similar vertex in out_XXXX
|
|
GLushort index;
|
|
bool found = getSimilarVertexIndex_fast(in_vertices[i], VertexToOutIndex, index);
|
|
|
|
if ( found ){ // A similar vertex is already in the VBO, use it instead !
|
|
out_indices.push_back( index );
|
|
}else{ // If not, it needs to be added in the output data.
|
|
out_vertices.push_back(in_vertices[i]);
|
|
GLushort newindex = (GLushort)out_vertices.size() - 1;
|
|
out_indices.push_back( newindex );
|
|
VertexToOutIndex[ in_vertices[i] ] = newindex;
|
|
}
|
|
}
|
|
} |