double RunAverage( const char* name, const char* fbxFile, int reps ) { Timer timer(name, reps); for( int i=0; i<reps; i++ ) { FBXFile file; file.load(fbxFile); } return timer.GetAverage(); }
bool nsfw::Assets::LoadFBX(const char * name, const char * path) { FBXFile file; std::vector<Vertex> vertices; std::vector<unsigned> indices; bool success = file.load(path, FBXFile::UNITS_CENTIMETER, true, false, true); if (!success) { std::cout << "Error loading FBX file:\n"; assert(false); return false; } //load meshes assert(file.getMeshCount() > 0); for (int meshIndex = 0; meshIndex < file.getMeshCount(); meshIndex++) { FBXMeshNode* mesh = file.getMeshByIndex(meshIndex); for (int verticesIndex = 0; verticesIndex < mesh->m_vertices.size(); verticesIndex++) { auto xVert = mesh->m_vertices[verticesIndex]; Vertex v; v.position = xVert.position; v.normal = xVert.normal; v.tangent = xVert.tangent; v.texCoord = xVert.texCoord1; vertices.push_back(v); } indices = mesh->m_indices; MakeVAO(mesh->m_name.c_str(), vertices.data(), vertices.size(), indices.data(), indices.size()); } //load textures using fbx file, its already loaded so why not for (int i = 0; i < file.getTextureCount(); i++) { FBXTexture* tex = file.getTextureByIndex(i); assert(nullptr != tex->data && "error loading texture.\n"); uint imageFormat = tex->format; switch (imageFormat) { case 1: imageFormat = GL_RED; break; case 2: imageFormat = GL_RG; break; case 3: imageFormat = GL_RGB; break; case 4: imageFormat = GL_RGBA; break; } MakeTexture(tex->name.c_str(), tex->width, tex->height, imageFormat, (char*)tex->data); } file.unload(); return true; }
bool AssetLibrary::AssetManager::loadFBX(const char* name, const char* path) { FBXFile file; file.load(path); // file.initialiseOpenGLTextures(); printf("Load FBX Start:\n"); for (unsigned int i = 0; i < file.getMeshCount(); i++) { auto m = file.getMeshByIndex(i); //load vertecies std::vector<Vertex> fbxData(m->m_vertices.size()); fbxData.data(); for (unsigned j = 0; j < m->m_vertices.size(); j++) { fbxData[j].position = m->m_vertices[j].position; fbxData[j].normal = m->m_vertices[j].normal; fbxData[j].tangent = m->m_vertices[j].tangent; fbxData[j].texcoord = m->m_vertices[j].texCoord1; } buildVAO(name, fbxData.data(), m->m_vertices.size(), m->m_indices.data(), m->m_indices.size()); //Diffuse Map if (m->m_material->textures[FBXMaterial::DiffuseTexture] != NULL) { std::string nameT; nameT = name + std::string("Diffuse"); loadTexture(nameT.c_str(), m->m_material->textures[FBXMaterial::DiffuseTexture]->path.c_str()); } //Normal Map if (m->m_material->textures[FBXMaterial::NormalTexture] != NULL) { std::string nameT; nameT = name + std::string("Normal"); loadTexture(nameT.c_str(), m->m_material->textures[FBXMaterial::NormalTexture]->path.c_str()); } //Specular Map if (m->m_material->textures[FBXMaterial::SpecularTexture] != NULL) { std::string nameT; nameT = name + std::string("Specular"); loadTexture(nameT.c_str(), m->m_material->textures[FBXMaterial::SpecularTexture]->path.c_str()); } } file.unload(); printf("Load FBX End.\n"); return true; }
FBXFile* ResourceManager::LoadFBX(const char* a_ccFile, int a_iScale) { if(m_pFBXMap.find(a_ccFile) == m_pFBXMap.end()) { FBXFile* pFBX = new FBXFile(); if (!(pFBX->load(a_ccFile, FBXFile::UNIT_SCALE(a_iScale)))) { printf("Failed to load file: %s \n", a_ccFile); return nullptr; } m_pFBXMap.insert(std::pair<const char*, FBXFile*>(a_ccFile, pFBX)); return pFBX; } else { return m_pFBXMap[a_ccFile]; } }
Object::Object( const char* fbxFilePath ) : vao( 0 ), vbo( 0 ), ibo( 0 ), textureID( nullptr ), textureCount( 0 ) { FBXFile* file = new FBXFile(); file->load( fbxFilePath ); textureCount = file->getTextureCount(); glGenVertexArrays( 1, &vao ); glGenBuffers( 1, &vbo ); glGenBuffers( 1, &ibo ); if ( textureCount > 0 ) { textureID = new unsigned int[textureCount]; glGenTextures( textureCount, textureID ); for ( unsigned int n = 0; n < textureCount; ++n ) { glBindTexture( GL_TEXTURE_2D, textureID[n] ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, file->getTextureByIndex( n )->width, file->getTextureByIndex( n )->height, 1, GL_RGB, GL_UNSIGNED_BYTE, file->getTextureByIndex( n )->data ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glBindTexture( GL_TEXTURE_2D, 0 ); } } FBXMeshNode* mesh = file->getMeshByIndex( 0 ); Vert* verts = new Vert[mesh->m_vertices.size()]; for ( unsigned int n = 0; n < mesh->m_vertices.size(); ++n ) { verts[n].position = mesh->m_vertices[n].position; verts[n].texCoord = mesh->m_vertices[n].texCoord1; } glBindVertexArray( vao ); // VBO handling glBindBuffer( GL_ARRAY_BUFFER, vbo ); glBufferData( GL_ARRAY_BUFFER, sizeof( Vert ) * mesh->m_vertices.size(), verts, GL_STATIC_DRAW ); // IBO handling glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo ); glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( unsigned int ) * mesh->m_indices.size(), mesh->m_indices.data(), GL_STATIC_DRAW ); indexCount = mesh->m_indices.size(); // Position glEnableVertexAttribArray( 0 ); glVertexAttribPointer( 0, 4, GL_FLOAT, GL_FALSE, sizeof( Vert ), nullptr ); // Texture coord glEnableVertexAttribArray( 1 ); glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, sizeof( Vert ), ( void* )sizeof( glm::vec4 ) ); // Done buffering data glBindVertexArray( 0 ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); delete[]( verts ); file->unload(); delete( file ); }
bool GLFramework::LoadModel(const char * path) { bool success = true; //find extension std::string sPath(path); std::string ext = sPath.substr(sPath.find_last_of('.')); Geometry geometry; if (ext == ".obj") { std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::material_t> materials; std::string err = tinyobj::LoadObj(shapes, materials, path); if (err.length() != 0) { std::cout << "Error loading OBJ file:\n" << err << std::endl; success = false; } //hard coding only using first shape, can change to loop here if (success) { auto shape = shapes[0]; auto mesh = shape.mesh; geometry.vertices.resize(mesh.positions.size()); uint posIndex = 0; uint normalIndex = 0; uint UVIndex = 0; bool hasNormals = mesh.normals.size() == mesh.positions.size(); bool hasUVs = mesh.texcoords.size() == mesh.positions.size(); //obj has vectors of floats, my struct and shaders uses glm vecs so need to build myself for (uint vertexCount = 0; posIndex < mesh.positions.size(); vertexCount++) { float x = mesh.positions[posIndex++]; float y = mesh.positions[posIndex++]; float z = mesh.positions[posIndex++]; geometry.vertices[vertexCount].position = vec4(x, y, z, 1); if (hasNormals) { x = mesh.normals[normalIndex++]; y = mesh.normals[normalIndex++]; z = mesh.normals[normalIndex++]; geometry.vertices[vertexCount].normal = vec4(x, y, z, 1); } if (hasUVs) { x = mesh.texcoords[UVIndex++]; y = mesh.texcoords[UVIndex++]; geometry.vertices[vertexCount].UV = vec2(x, y); } } geometry.indices = mesh.indices; } } else if (ext == ".fbx") { FBXFile file; success = file.load(path, FBXFile::UNITS_METER, false, false, false); if (!success) { std::cout << "Error loading FBX file:\n"; } else { //hardcoding to use single mesh, can loop here if needed. FBXMeshNode* mesh = file.getMeshByIndex(0); geometry.vertices.resize(mesh->m_vertices.size()); for (int i = 0; i < mesh->m_vertices.size(); i++) { auto xVert = mesh->m_vertices[i]; geometry.vertices[i].position = xVert.position; geometry.vertices[i].color = xVert.colour; geometry.vertices[i].normal = xVert.normal; geometry.vertices[i].UV = xVert.texCoord1; } geometry.indices = mesh->m_indices; file.unload(); } } else { std::cout << "Unsupported format. Only support .obj or .fbx files.\n"; success = false; } if (!success) { return false; } LoadModel(geometry); return true; }