#include <gmath/gmath.h>
+#include "gfxapi.h"
+
#include "mesh.h"
#include "object.h"
#include "scene.h"
#include "texture.h"
-#include "opengl/mesh-gl.h"
-#include "vulkan/mesh-vk.h"
-
-#include "opengl/texture-gl.h"
-#include "vulkan/texture-vk.h"
-
-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 Material *load_material(const aiScene *ascene, Scene *scene, unsigned int index, const char *tex_fname);
static Mat4 aiMatrix2Mat(aiMatrix4x4 t);
static void create_object(aiNode *node, Mat4 transform, Scene *scene, const aiScene *ascene);
Scene::~Scene()
{
- for(size_t i=0; i<meshes.size(); ++i)
+ for(size_t i=0; i<meshes.size(); i++)
delete meshes[i];
meshes.clear();
- for(size_t i=0; i<materials.size(); ++i)
+ for(size_t i=0; i<materials.size(); i++)
delete materials[i];
materials.clear();
- for(size_t i=0; i<textures.size(); ++i)
+ for(size_t i=0; i<textures.size(); i++)
delete textures[i];
textures.clear();
- for(size_t i=0; i<objects.size(); ++i)
+ for(size_t i=0; i<objects.size(); i++)
delete objects[i];
objects.clear();
}
bool Scene::load(const char *fname)
{
/* loading scene */
- unsigned int ai_flags = aiProcessPreset_TargetRealtime_Quality;
+ /* TODO !!!!!!!!!!!!!!!! flipuvs might need to be removed + fix the cow */
+ unsigned int ai_flags = aiProcessPreset_TargetRealtime_Quality | aiProcess_FlipUVs;
const aiScene *scene = aiImportFile(fname, ai_flags);
if(!scene) {
fprintf(stderr, "Failed to import scene: %s\n", fname);
}
/* load meshes */
- for(unsigned int i=0; i<scene->mNumMeshes; ++i) {
+ for(unsigned int i=0; i<scene->mNumMeshes; i++) {
Mesh *mesh = load_mesh(scene, i);
if(!mesh) {
fprintf(stderr, "Failed to load mesh no: %d.\n", i);
return false;
}
+ mesh->update_vertex_data();
meshes.push_back(mesh);
}
/* load materials */
- for(unsigned int i=0; i<scene->mNumMaterials; ++i) {
- Material *material = load_material(scene, this, i);
+ char *tex_path = new char[strlen(fname) + 1];
+ strcpy(tex_path, fname);
+ char *last_slash = strrchr(tex_path, '/');
+ if(last_slash) {
+ *last_slash = '\0';
+ }
+ else {
+ delete [] tex_path;
+ tex_path = 0;
+ }
+
+ for(unsigned int i=0; i<scene->mNumMaterials; i++) {
+ Material *material = load_material(scene, this, i, tex_path);
if(!material) {
fprintf(stderr, "Failed to load material no: %d", i);
return false;
scene->objects.push_back(object);
}
- for(unsigned int i=0; i<node->mNumChildren; ++i) {
+ for(unsigned int i=0; i<node->mNumChildren; i++) {
create_object(node->mChildren[i], modelling_transform, scene, ascene);
}
}
return 0;
}
- Mesh *mesh;
- if(use_vulkan) {
- mesh = new MeshVK;
- }
- else {
- mesh = new MeshGL;
- }
-
+ Mesh *mesh = gfx_create_mesh();
mesh->name = std::string(amesh->mName.data);
- for(unsigned int i=0; i<amesh->mNumVertices; ++i) {
+ for(unsigned int i=0; i<amesh->mNumVertices; i++) {
/* vertices */
if(amesh->HasPositions()) {
Vec3 vertex = Vec3(amesh->mVertices[i].x, amesh->mVertices[i].y,
mesh->tex_coords.push_back(tex_coord);
}
else {
+ printf("mesh has no texture coordinates.\n");
mesh->tex_coords.push_back(Vec2(0, 0));
}
}
/* indices (called faces in assimp) */
if(amesh->HasFaces()) {
- for(unsigned int i=0; i<amesh->mNumFaces; ++i) {
+ for(unsigned int i=0; i<amesh->mNumFaces; i++) {
mesh->indices.push_back(amesh->mFaces[i].mIndices[0]);
mesh->indices.push_back(amesh->mFaces[i].mIndices[1]);
mesh->indices.push_back(amesh->mFaces[i].mIndices[2]);
return mesh;
}
-static Material *load_material(const aiScene *ascene, Scene *scene, unsigned int index)
+static Material *load_material(const aiScene *ascene, Scene *scene, unsigned int index, const char *tex_path)
{
aiMaterial *amat = ascene->mMaterials[index];
if(!amat) {
mat->diffuse = Vec3(color.r, color.g, color.b);
aiGetMaterialColor(amat, AI_MATKEY_COLOR_SPECULAR, &color);
- float spstr;
- aiGetMaterialFloat(amat, AI_MATKEY_SHININESS_STRENGTH, &spstr);
- mat->specular = spstr * Vec3(color.r, color.g, color.b);
+ // float spstr;
+ // aiGetMaterialFloat(amat, AI_MATKEY_SHININESS_STRENGTH, &spstr);
+ // mat->specular = spstr * Vec3(color.r, color.g, color.b);
+ mat->specular = Vec3(color.r, color.g, color.b);
aiGetMaterialFloat(amat, AI_MATKEY_SHININESS, &mat->shininess);
+ mat->shininess /= 128.0;
+ printf("shininess: %f\n", mat->shininess);
aiString tex_name;
if(aiGetMaterialString(amat, AI_MATKEY_TEXTURE_DIFFUSE(0), &tex_name) == aiReturn_SUCCESS) {
/* different scene objects might use the same texture, we shouldn't store it twice*/
- //mat->dtex = scene->find_texture(tex_name.data);
+
+ std::string tex_fname = tex_path ? std::string(tex_path) + "/" + tex_name.data : tex_name.data;
+
+ mat->dtex = scene->find_texture(tex_fname.c_str());
if(!mat->dtex) {
- if(use_vulkan) {
- mat->dtex = new TextureVK;
- }
- else {
- mat->dtex = new TextureGL;
- }
- if(!mat->dtex->load(tex_name.data)) {
- fprintf(stderr, "Failed to load texture data: %s.\n", tex_name.data);
+ mat->dtex = gfx_create_texture();
+
+ if(!mat->dtex->load(tex_fname.c_str())) {
+ fprintf(stderr, "Failed to load texture data: %s.\n", tex_fname.c_str());
delete mat->dtex;
mat->dtex = 0;
}
+ else {
+ mat->dtex->name = std::string(tex_fname.c_str());
+ }
+ printf("Successfully loaded texture: %s\n", tex_fname.c_str());
scene->textures.push_back(mat->dtex);
}
}
return mat;
}
+
+Mesh *Scene::find_mesh(const char *name)
+{
+ for(size_t i=0; i<meshes.size(); i++) {
+ if(meshes[i]->name == name) {
+ return meshes[i];
+ }
+ }
+ fprintf(stderr, "Mesh %s not found.\n", name);
+ return 0;
+}
+
+Material *Scene::find_material(const char *name)
+{
+ for(size_t i=0; i<materials.size(); i++) {
+ if(materials[i]->name == name) {
+ return materials[i];
+ }
+ }
+ fprintf(stderr, "Material %s not found.\n", name);
+ return 0;
+}
+
+Texture *Scene::find_texture(const char *name)
+{
+ for(size_t i=0; i<textures.size(); i++) {
+ if(textures[i]->name == name) {
+ return textures[i];
+ }
+ }
+ return 0;
+}
\ No newline at end of file