Add material names and caching

we are not producing a material for each mesh any more
pull/289/head
diiigle 2014-01-06 12:49:11 +01:00
parent b0039d1a89
commit 4913aefe5f
2 changed files with 36 additions and 14 deletions

View File

@ -147,7 +147,7 @@ void LDrawImporter::ReadMaterials(std::string filename){
DefaultLogger::get()->debug(*splitter);
if (TokenMatchI(cmd,"!colour",7)){
//name of the color
SkipToken(cmd);
std::string name = GetNextToken(cmd);
SkipSpaces(&cmd);
if (TokenMatchI(cmd, "code", 4)){
SkipSpaces(&cmd);
@ -179,7 +179,7 @@ void LDrawImporter::ReadMaterials(std::string filename){
cmd += 2;
edge = edge * (1 / 255.0f);
//TODO ALPHA and LUMINANCE
LDrawMaterial mat = LDrawMaterial(code, value, edge);
LDrawMaterial mat = LDrawMaterial(name,code, value, edge);
materials.insert(std::pair<ColorIndex, LDrawMaterial>(code, mat));
}
}
@ -441,7 +441,26 @@ void LDrawImporter::ConvertNode(aiNode* node, LDrawNode* current, std::vector<ai
//we don't know that material
continue;
}
aiString *name = new aiString((i->first == 24) ? rawMaterial->name + "-edge" : rawMaterial->name);
//do we already have such a aiMaterial?
unsigned int chachedIndex = 0;
for (std::vector<aiMaterial*>::iterator mat = aiMaterials->begin(); mat != aiMaterials->end(); ++mat, ++chachedIndex)
{
aiMaterial* cached = *mat;
aiString cachedName;
cached->Get(AI_MATKEY_NAME, cachedName);
if (cachedName == *name)
break;
}
if (chachedIndex != aiMaterials->size())
{
//we found an already matching
mesh->mMaterialIndex = chachedIndex;
}
else
{
aiMaterial* material = new aiMaterial();
material->AddProperty(name, AI_MATKEY_NAME);
if (i->first == 24)
material->AddProperty(&rawMaterial->edge, 1, AI_MATKEY_COLOR_DIFFUSE);
else
@ -455,6 +474,7 @@ void LDrawImporter::ConvertNode(aiNode* node, LDrawNode* current, std::vector<ai
aiMaterials->push_back(material);
}
}
}
node->mNumChildren = current->children.size();
node->mChildren = new aiNode*[node->mNumChildren];

View File

@ -19,9 +19,11 @@ namespace Assimp{
typedef unsigned int ColorIndex;
struct LDrawMaterial
{
LDrawMaterial(unsigned int code, aiColor3D color, aiColor3D edge) :
code(code), color(color), edge(edge)
LDrawMaterial(std::string name, int code, aiColor3D color, aiColor3D edge) :
name(name), code(code), color(color), edge(edge)
{}
//human readable name
std::string name;
//identification of the color in LDraw files
ColorIndex code;
//the main color of the material