3 #include <assimp/cimport.h>
4 #include <assimp/material.h>
5 #include <assimp/mesh.h>
6 #include <assimp/postprocess.h>
7 #include <assimp/scene.h>
11 #include <gmath/gmath.h>
18 #include "opengl/mesh-gl.h"
19 #include "vulkan/mesh-vk.h"
21 #include "opengl/texture-gl.h"
22 #include "vulkan/texture-vk.h"
24 extern bool use_vulkan;
25 static Mesh *load_mesh(const aiScene *scene, unsigned int index);
26 static Material *load_material(const aiScene *ascene, Scene *scene, unsigned int index);
32 for(size_t i=0; i<meshes.size(); ++i)
36 for(size_t i=0; i<materials.size(); ++i)
40 for(size_t i=0; i<textures.size(); ++i)
44 for(size_t i=0; i<objects.size(); ++i)
49 bool Scene::load(const char *fname)
52 unsigned int ai_flags = aiProcessPreset_TargetRealtime_Quality;
53 const aiScene *scene = aiImportFile(fname, ai_flags);
55 fprintf(stderr, "Failed to import scene: %s\n", fname);
59 /* loading scene meshes */
60 for(unsigned int i=0; i<scene->mNumMeshes; ++i) {
61 Mesh *mesh = load_mesh(scene, i);
63 fprintf(stderr, "Failed to load mesh no: %d.\n", i);
66 meshes.push_back(mesh);
69 /* loading materials */
70 for(unsigned int i=0; i<scene->mNumMaterials; ++i) {
71 Material *material = load_material(scene, this, i);
73 fprintf(stderr, "Failed to load material no: %d", i);
76 materials.push_back(material);
80 for(unsigned int i=0; i<scene->mNumMeshes; ++i) {
81 Object *object = new Object();
82 object->mesh = meshes[i];
83 int idx = meshes[i]->mat_idx;
84 object->material = materials[idx];
85 objects.push_back(object);
88 aiReleaseImport(scene);
92 static Mesh *load_mesh(const aiScene *scene, unsigned int index)
94 /* load mesh with index from scene using assimp */
95 aiMesh *amesh = scene->mMeshes[index];
97 fprintf(stderr, "Failed to load assimp mesh no: %d.\n", index);
109 mesh->name = std::string(amesh->mName.data);
110 mesh->which_mask = 0;
112 for(unsigned int i=0; i<amesh->mNumVertices; ++i) {
114 if(amesh->HasPositions()) {
115 mesh->which_mask |= MESH_VERTEX;
116 Vec3 vertex = Vec3(amesh->mVertices[i].x, amesh->mVertices[i].y,
117 amesh->mVertices[i].z);
119 mesh->vertices.push_back(vertex);
122 fprintf(stderr, "Mesh has no geometry!\n");
128 if(amesh->HasNormals()) {
129 mesh->which_mask |= MESH_NORMAL;
130 Vec3 normal = Vec3(amesh->mNormals[i].x, amesh->mNormals[i].y,
131 amesh->mNormals[i].z);
132 mesh->normals.push_back(normal);
135 fprintf(stderr, "Mesh has no normals!\n");
140 /* texture coordinates */
141 if(amesh->mTextureCoords[0]) {
142 mesh->which_mask |= MESH_TEXTURE;
144 Vec2(amesh->mTextureCoords[0][i].x, amesh->mTextureCoords[0][i].y);
145 mesh->tex_coords.push_back(tex_coord);
148 mesh->tex_coords.push_back(Vec2(0, 0));
152 if(amesh->mTangents) {
153 mesh->which_mask |= MESH_TANGENT;
154 Vec3 tangent = Vec3(amesh->mTangents[i].x, amesh->mTangents[i].y,
155 amesh->mTangents[i].z);
156 mesh->tangents.push_back(tangent);
159 mesh->tangents.push_back(Vec3(1, 0, 0));
162 /* indices (called faces in assimp) */
163 if(amesh->HasFaces()) {
164 mesh->which_mask |= MESH_INDEX;
165 for(unsigned int i=0; i<amesh->mNumFaces; ++i) {
166 mesh->indices.push_back(amesh->mFaces[i].mIndices[0]);
167 mesh->indices.push_back(amesh->mFaces[i].mIndices[1]);
168 mesh->indices.push_back(amesh->mFaces[i].mIndices[2]);
172 fprintf(stderr, "No faces found.\n");
174 mesh->mat_idx = amesh->mMaterialIndex;
175 printf("material idx:%u", mesh->mat_idx);
179 static Material *load_material(const aiScene *ascene, Scene *scene, unsigned int index)
181 aiMaterial *amat = ascene->mMaterials[index];
183 fprintf(stderr, "Failed to load material no: %d.\n", index);
187 Material *mat = new Material;
192 // amat->Get(AI_MATKEY_NAME, name);
193 // mat->name = std::string(name.data);
196 aiGetMaterialColor(amat, AI_MATKEY_COLOR_DIFFUSE, &color);
197 mat->diffuse = Vec3(color.r, color.g, color.b);
199 aiGetMaterialColor(amat, AI_MATKEY_COLOR_SPECULAR, &color);
201 aiGetMaterialFloat(amat, AI_MATKEY_SHININESS_STRENGTH, &spstr);
202 mat->specular = spstr * Vec3(color.r, color.g, color.b);
204 aiGetMaterialFloat(amat, AI_MATKEY_SHININESS, &mat->shininess);
207 if(aiGetMaterialString(amat, AI_MATKEY_TEXTURE_DIFFUSE(0), &tex_name) == aiReturn_SUCCESS) {
208 /* different scene objects might use the same texture, we shouldn't store it twice*/
209 //mat->dtex = scene->find_texture(tex_name.data);
212 mat->dtex = new TextureVK;
215 mat->dtex = new TextureGL;
217 if(!mat->dtex->load(tex_name.data)) {
218 fprintf(stderr, "Failed to load texture data: %s.\n", tex_name.data);
222 scene->textures.push_back(mat->dtex);