X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fscene.cc;h=c6408471f1163582c845b1523327214bdbd75a64;hb=1ede01ab671f707338b2cc13517877cccd4a4f7f;hp=7f9a879e208a07f3fb086dafc4c050177b57648e;hpb=46cc932f7ddb0c81f352bc847973dae6b327ebeb;p=demo diff --git a/src/scene.cc b/src/scene.cc index 7f9a879..c640847 100644 --- a/src/scene.cc +++ b/src/scene.cc @@ -25,6 +25,9 @@ extern bool use_vulkan; static Mesh *load_mesh(const aiScene *scene, unsigned int index); static Material *load_material(const aiScene *ascene, Scene *scene, unsigned int index); +static Mat4 aiMatrix2Mat(aiMatrix4x4 t); +static void create_object(aiNode *node, Mat4 transform, Scene *scene, const aiScene *ascene); + Scene::Scene() {} Scene::~Scene() @@ -56,7 +59,7 @@ bool Scene::load(const char *fname) return false; } - /* loading scene meshes */ + /* load meshes */ for(unsigned int i=0; imNumMeshes; ++i) { Mesh *mesh = load_mesh(scene, i); if(!mesh) { @@ -66,7 +69,7 @@ bool Scene::load(const char *fname) meshes.push_back(mesh); } - /* loading materials */ + /* load materials */ for(unsigned int i=0; imNumMaterials; ++i) { Material *material = load_material(scene, this, i); if(!material) { @@ -77,16 +80,51 @@ bool Scene::load(const char *fname) } /* create objects */ - for(unsigned int i=0; imNumMeshes; ++i) { - Object *object = new Object(); - object->mesh = meshes[i]; - int idx = meshes[i]->mat_idx; - object->material = materials[idx]; - objects.push_back(object); + aiNode *node = scene->mRootNode; + Mat4 transform = Mat4::identity; + create_object(node, transform, this, scene); + return true; +} + +static Mat4 aiMatrix2Mat(aiMatrix4x4 t) +{ + return Mat4(t.a1, t.a2, t.a3, t.a4, + t.b1, t.b2, t.b3, t.b4, + t.c1, t.c2, t.c3, t.c4, + t.d1, t.d2, t.d3, t.d4); +} + +static void create_object(aiNode *node, Mat4 parent_transform, Scene *scene, const aiScene *ascene) +{ + /* Note: + The 99% of the scenes have 1 mesh per node => for simplicity we only check the 1st one. + Also: the 3D models we are going to use for this demo, have flat structure (no hierarchy) + but just in case we need to replace them later, we calculate the transform by assuming that we + have a node hierarchy. This => that each object's modelling transformation is the + product of its local transformation (mTransformation) with the acc parent nodes transformations + (parent_transform) + */ + Mat4 modelling_transform = parent_transform * aiMatrix2Mat(node->mTransformation); + + if(node->mNumMeshes > 0) { + Object *object = new Object; + + // get the mesh index from node + int mesh_idx = node->mMeshes[0]; + object->mesh = scene->meshes[mesh_idx]; + + // get the material index from the mesh that is assigned to this node + aiMesh *amesh = ascene->mMeshes[mesh_idx]; + object->material = scene->materials[amesh->mMaterialIndex]; + + // set the object's transformation + object->transform = modelling_transform; + scene->objects.push_back(object); } - aiReleaseImport(scene); - return true; + for(unsigned int i=0; imNumChildren; ++i) { + create_object(node->mChildren[i], modelling_transform, scene, ascene); + } } static Mesh *load_mesh(const aiScene *scene, unsigned int index) @@ -107,12 +145,10 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) } mesh->name = std::string(amesh->mName.data); - mesh->which_mask = 0; for(unsigned int i=0; imNumVertices; ++i) { /* vertices */ if(amesh->HasPositions()) { - mesh->which_mask |= MESH_VERTEX; Vec3 vertex = Vec3(amesh->mVertices[i].x, amesh->mVertices[i].y, amesh->mVertices[i].z); @@ -126,7 +162,6 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) /* normals */ if(amesh->HasNormals()) { - mesh->which_mask |= MESH_NORMAL; Vec3 normal = Vec3(amesh->mNormals[i].x, amesh->mNormals[i].y, amesh->mNormals[i].z); mesh->normals.push_back(normal); @@ -139,9 +174,7 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) /* texture coordinates */ if(amesh->mTextureCoords[0]) { - mesh->which_mask |= MESH_TEXTURE; - Vec2 tex_coord = - Vec2(amesh->mTextureCoords[0][i].x, amesh->mTextureCoords[0][i].y); + Vec2 tex_coord = Vec2(amesh->mTextureCoords[0][i].x, amesh->mTextureCoords[0][i].y); mesh->tex_coords.push_back(tex_coord); } else { @@ -149,19 +182,18 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) } /* tangents */ - if(amesh->mTangents) { - mesh->which_mask |= MESH_TANGENT; - Vec3 tangent = Vec3(amesh->mTangents[i].x, amesh->mTangents[i].y, - amesh->mTangents[i].z); - mesh->tangents.push_back(tangent); - } - else { - mesh->tangents.push_back(Vec3(1, 0, 0)); - } + // if(amesh->mTangents) { + // mesh->which_mask |= MESH_TANGENT; + // Vec3 tangent = Vec3(amesh->mTangents[i].x, amesh->mTangents[i].y, + // amesh->mTangents[i].z); + // mesh->tangents.push_back(tangent); + // } + // else { + // mesh->tangents.push_back(Vec3(1, 0, 0)); + // } } /* indices (called faces in assimp) */ if(amesh->HasFaces()) { - mesh->which_mask |= MESH_INDEX; for(unsigned int i=0; imNumFaces; ++i) { mesh->indices.push_back(amesh->mFaces[i].mIndices[0]); mesh->indices.push_back(amesh->mFaces[i].mIndices[1]); @@ -172,7 +204,6 @@ static Mesh *load_mesh(const aiScene *scene, unsigned int index) fprintf(stderr, "No faces found.\n"); mesh->mat_idx = amesh->mMaterialIndex; - printf("material idx:%u", mesh->mat_idx); return mesh; } @@ -188,9 +219,9 @@ static Material *load_material(const aiScene *ascene, Scene *scene, unsigned int mat->dtex = 0; mat->shininess = 40; - // aiString name; - // amat->Get(AI_MATKEY_NAME, name); - // mat->name = std::string(name.data); + aiString name; + amat->Get(AI_MATKEY_NAME, name); + mat->name = std::string(name.data); aiColor4D color; aiGetMaterialColor(amat, AI_MATKEY_COLOR_DIFFUSE, &color);