git-svn-id: svn://db.shs.com.ru/libs@681 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -81,6 +81,46 @@ Mesh * assimpMesh(const aiMesh * m) {
|
||||
}
|
||||
|
||||
|
||||
QColor aiMatColor(const aiMaterial * m, const char * key, uint s0, uint s1, const QColor & def = Qt::white) {
|
||||
aiColor4D col;
|
||||
aiReturn r = m->Get(key, s0, s1, col);
|
||||
//qDebug() << key << r << col.r << col.g << col.b;
|
||||
if (r != aiReturn_SUCCESS) return def;
|
||||
return QColor::fromRgbF(col.r, col.g, col.b);
|
||||
}
|
||||
float aiMatFloat(const aiMaterial * m, const char * key, uint s0, uint s1, float def = 0.f) {
|
||||
float ret;
|
||||
aiReturn r = m->Get(key, s0, s1, ret);
|
||||
if (r != aiReturn_SUCCESS) return def;
|
||||
return ret;
|
||||
}
|
||||
QString aiMatMap(const aiMaterial * m, aiTextureType texture) {
|
||||
aiString p;
|
||||
aiReturn r = const_cast<aiMaterial*>(m)->GetTexture(texture, 0, &p);
|
||||
if (r != aiReturn_SUCCESS) return QString(p.C_Str());
|
||||
return QString();
|
||||
}
|
||||
Material * assimpMaterial(const aiMaterial * m) {
|
||||
if (!m) return 0;
|
||||
Material * ret = new Material();
|
||||
ret->name = const_cast<aiMaterial*>(m)->GetName().C_Str();
|
||||
//qDebug() << "mat" << ret->name;
|
||||
//for (int i = 0; i < m->mNumProperties; ++i)
|
||||
// qDebug()<< m->mProperties[i]->mKey.C_Str();
|
||||
ret->color_diffuse = aiMatColor(m, AI_MATKEY_COLOR_DIFFUSE);
|
||||
ret->color_specular = aiMatColor(m, AI_MATKEY_COLOR_SPECULAR);
|
||||
ret->color_emission = aiMatColor(m, AI_MATKEY_COLOR_EMISSIVE);
|
||||
ret->map_diffuse .bitmap_path = aiMatMap(m, aiTextureType_DIFFUSE);
|
||||
ret->map_normal .bitmap_path = aiMatMap(m, aiTextureType_NORMALS);
|
||||
ret->map_specular .bitmap_path = aiMatMap(m, aiTextureType_SPECULAR);
|
||||
ret->map_roughness.bitmap_path = aiMatMap(m, aiTextureType_DIFFUSE_ROUGHNESS);
|
||||
ret->map_emission .bitmap_path = aiMatMap(m, aiTextureType_EMISSIVE);
|
||||
ret->transparency = 1.f - aiMatFloat(m, AI_MATKEY_OPACITY, 1.f);
|
||||
ret->setTypes();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Light * assimpLight(const aiLight * l) {
|
||||
if (!l) return 0;
|
||||
Light * ret = new Light();
|
||||
@@ -102,7 +142,9 @@ Light * assimpLight(const aiLight * l) {
|
||||
}
|
||||
|
||||
|
||||
ObjectBase * assimpObject(const aiNode * n, const QVector<Mesh * > & meshes, const QMap<QString, Light * > & light_by_name, QVector<Light*> & out_lights) {
|
||||
ObjectBase * assimpObject(const aiNode * n, const QVector<Mesh * > & meshes, aiMesh ** ai_meshes,
|
||||
const QVector<Material*> & materials,
|
||||
const QMap<QString, Light * > & light_by_name, QVector<Light*> & out_lights) {
|
||||
if (!n) return 0;
|
||||
ObjectBase * ret = 0;
|
||||
QString name = n->mName.C_Str();
|
||||
@@ -120,8 +162,12 @@ ObjectBase * assimpObject(const aiNode * n, const QVector<Mesh * > & meshes, con
|
||||
for (uint i = 0; i < n->mNumMeshes; ++i) {
|
||||
int mi = n->mMeshes[i];
|
||||
if (meshes[mi]) {
|
||||
if (!ret->mesh())
|
||||
if (!ret->mesh()) {
|
||||
ret->setMesh(new Mesh());
|
||||
int mati = ai_meshes[mi]->mMaterialIndex;
|
||||
if (mati >= 0 || mati < materials.size())
|
||||
ret->setMaterial(materials[mati]);
|
||||
}
|
||||
ret->mesh()->append(meshes[mi]);
|
||||
//ret->setMesh(meshes[mi]);
|
||||
//qDebug() << "set mesh" << mi << ret->mesh();
|
||||
@@ -130,7 +176,7 @@ ObjectBase * assimpObject(const aiNode * n, const QVector<Mesh * > & meshes, con
|
||||
}
|
||||
}
|
||||
for (uint i = 0; i < n->mNumChildren; ++i) {
|
||||
ObjectBase * co = assimpObject(n->mChildren[i], meshes, light_by_name, out_lights);
|
||||
ObjectBase * co = assimpObject(n->mChildren[i], meshes, ai_meshes, materials, light_by_name, out_lights);
|
||||
if (co) ret->addChild(co);
|
||||
}
|
||||
|
||||
@@ -154,6 +200,9 @@ Scene * loadScene(const QString & filepath) {
|
||||
QVector<Mesh * > meshes;
|
||||
for (uint i = 0; i < ais->mNumMeshes; ++i)
|
||||
meshes << assimpMesh(ais->mMeshes[i]);
|
||||
QVector<Material * > materials;
|
||||
for (uint i = 0; i < ais->mNumMaterials; ++i)
|
||||
materials << assimpMaterial(ais->mMaterials[i]);
|
||||
QVector<Light * > lights;
|
||||
for (uint i = 0; i < ais->mNumLights; ++i)
|
||||
lights << assimpLight(ais->mLights[i]);
|
||||
@@ -162,7 +211,7 @@ Scene * loadScene(const QString & filepath) {
|
||||
light_by_name[l->name()] = l;
|
||||
|
||||
QVector<Light*> out_lights;
|
||||
ObjectBase * root = assimpObject(ais->mRootNode, meshes, light_by_name, out_lights);
|
||||
ObjectBase * root = assimpObject(ais->mRootNode, meshes, ais->mMeshes, materials, light_by_name, out_lights);
|
||||
if (!root) return 0;
|
||||
|
||||
ObjectBaseList rcl = root->children(true);
|
||||
|
||||
Reference in New Issue
Block a user