git-svn-id: svn://db.shs.com.ru/libs@636 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -81,22 +81,51 @@ Mesh * assimpMesh(const aiMesh * m) {
|
||||
}
|
||||
|
||||
|
||||
ObjectBase * assimpObject(const aiNode * n, const QVector<Mesh * > & meshes) {
|
||||
Light * assimpLight(const aiLight * l) {
|
||||
if (!l) return 0;
|
||||
Light * ret = new Light();
|
||||
ret->setName(l->mName.C_Str());
|
||||
ret->setPos(fromAiVector3D(l->mPosition));
|
||||
ret->direction = fromAiVector3D(l->mDirection);
|
||||
ret->decay_const = l->mAttenuationConstant ;
|
||||
ret->decay_linear = l->mAttenuationLinear ;
|
||||
ret->decay_quadratic = l->mAttenuationQuadratic;
|
||||
ret->angle_start = l->mAngleInnerCone * rad2deg;
|
||||
ret->angle_end = l->mAngleOuterCone * rad2deg;
|
||||
if (l->mType == aiLightSource_SPOT)
|
||||
ret->light_type = Light::Cone;
|
||||
QVector3D col(l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b);
|
||||
ret->intensity = col.length();
|
||||
col /= ret->intensity;
|
||||
ret->setColor(QColor::fromRgbF(col[0], col[1], col[2]));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
ObjectBase * assimpObject(const aiNode * n, const QVector<Mesh * > & meshes, const QMap<QString, Light * > & light_by_name) {
|
||||
if (!n) return 0;
|
||||
ObjectBase * ret = new ObjectBase();
|
||||
ret->setName(n->mName.C_Str());
|
||||
ObjectBase * ret = 0;
|
||||
QString name = n->mName.C_Str();
|
||||
Light * light = light_by_name.value(name, 0);
|
||||
if (light)
|
||||
ret = light->clone();
|
||||
else
|
||||
ret = new ObjectBase();
|
||||
ret->setName(name);
|
||||
ret->setTransform(fromAiMatrix4D(n->mTransformation));
|
||||
//qDebug() << "add object" << ret << ret->name();
|
||||
for (uint i = 0; i < n->mNumMeshes; ++i) {
|
||||
int mi = n->mMeshes[i];
|
||||
if (meshes[mi]) {
|
||||
ret->setMesh(meshes[mi]);
|
||||
//qDebug() << "set mesh" << mi << ret->mesh();
|
||||
break;
|
||||
if (!light) {
|
||||
for (uint i = 0; i < n->mNumMeshes; ++i) {
|
||||
int mi = n->mMeshes[i];
|
||||
if (meshes[mi]) {
|
||||
ret->setMesh(meshes[mi]);
|
||||
//qDebug() << "set mesh" << mi << ret->mesh();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (uint i = 0; i < n->mNumChildren; ++i) {
|
||||
ObjectBase * co = assimpObject(n->mChildren[i], meshes);
|
||||
ObjectBase * co = assimpObject(n->mChildren[i], meshes, light_by_name);
|
||||
if (co) ret->addChild(co);
|
||||
}
|
||||
|
||||
@@ -120,14 +149,23 @@ Scene * loadScene(const QString & filepath) {
|
||||
QVector<Mesh * > meshes;
|
||||
for (uint i = 0; i < ais->mNumMeshes; ++i)
|
||||
meshes << assimpMesh(ais->mMeshes[i]);
|
||||
QVector<Light * > lights;
|
||||
for (uint i = 0; i < ais->mNumLights; ++i)
|
||||
lights << assimpLight(ais->mLights[i]);
|
||||
QMap<QString, Light * > light_by_name;
|
||||
foreach (Light * l, lights)
|
||||
light_by_name[l->name()] = l;
|
||||
|
||||
ObjectBase * root = assimpObject(ais->mRootNode, meshes);
|
||||
ObjectBase * root = assimpObject(ais->mRootNode, meshes, light_by_name);
|
||||
if (!root) return 0;
|
||||
|
||||
Scene * scene = new Scene();
|
||||
scene->setName(root->name());
|
||||
foreach (ObjectBase * o, root->children())
|
||||
scene->addObject(o);
|
||||
//foreach (ObjectBase * o, lights)
|
||||
// scene->addObject(o);
|
||||
qDeleteAll(lights);
|
||||
|
||||
return scene;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user