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); DefaultLogger::get()->debug(*splitter);
if (TokenMatchI(cmd,"!colour",7)){ if (TokenMatchI(cmd,"!colour",7)){
//name of the color //name of the color
SkipToken(cmd); std::string name = GetNextToken(cmd);
SkipSpaces(&cmd); SkipSpaces(&cmd);
if (TokenMatchI(cmd, "code", 4)){ if (TokenMatchI(cmd, "code", 4)){
SkipSpaces(&cmd); SkipSpaces(&cmd);
@ -179,7 +179,7 @@ void LDrawImporter::ReadMaterials(std::string filename){
cmd += 2; cmd += 2;
edge = edge * (1 / 255.0f); edge = edge * (1 / 255.0f);
//TODO ALPHA and LUMINANCE //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)); materials.insert(std::pair<ColorIndex, LDrawMaterial>(code, mat));
} }
} }
@ -441,18 +441,38 @@ void LDrawImporter::ConvertNode(aiNode* node, LDrawNode* current, std::vector<ai
//we don't know that material //we don't know that material
continue; continue;
} }
aiMaterial* material = new aiMaterial(); aiString *name = new aiString((i->first == 24) ? rawMaterial->name + "-edge" : rawMaterial->name);
if (i->first == 24) //do we already have such a aiMaterial?
material->AddProperty(&rawMaterial->edge, 1, AI_MATKEY_COLOR_DIFFUSE); 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 else
material->AddProperty(&rawMaterial->color, 1, AI_MATKEY_COLOR_DIFFUSE); {
if (rawMaterial->alpha != 1.0f) aiMaterial* material = new aiMaterial();
material->AddProperty(&rawMaterial->alpha, 1, AI_MATKEY_OPACITY); material->AddProperty(name, AI_MATKEY_NAME);
if (rawMaterial->luminance != 0.0f) if (i->first == 24)
material->AddProperty(&(rawMaterial->color * rawMaterial->luminance), 1, AI_MATKEY_COLOR_EMISSIVE); material->AddProperty(&rawMaterial->edge, 1, AI_MATKEY_COLOR_DIFFUSE);
else
material->AddProperty(&rawMaterial->color, 1, AI_MATKEY_COLOR_DIFFUSE);
if (rawMaterial->alpha != 1.0f)
material->AddProperty(&rawMaterial->alpha, 1, AI_MATKEY_OPACITY);
if (rawMaterial->luminance != 0.0f)
material->AddProperty(&(rawMaterial->color * rawMaterial->luminance), 1, AI_MATKEY_COLOR_EMISSIVE);
mesh->mMaterialIndex = aiMaterials->size(); mesh->mMaterialIndex = aiMaterials->size();
aiMaterials->push_back(material); aiMaterials->push_back(material);
}
} }
} }

View File

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