bool Model::Import3DFromFile( const std::string& pFile) { static bool first = true; if(first) { first = false; setupShaders(); } std::ifstream fin(pFile.c_str()); if(!fin.fail()) { fin.close(); } else{ printf("Couldn't open file: %s\n", pFile.c_str()); printf("%s\n", importer.GetErrorString()); return false; } scene = importer.ReadFile( pFile, aiProcessPreset_TargetRealtime_Quality); if( !scene) { printf("%s\n", importer.GetErrorString()); return false; } // Now we can access the file's contents. printf("Import of scene %s succeeded.\n",pFile.c_str()); #ifdef __APPLE__ boost::filesystem::path p(pFile); std::string basePath = p.parent_path().string()+"/"; #else char path[2000]; wchar_t wpath[2000]; strcpy(path, pFile.c_str()); mbstowcs(wpath, path, strlen(path)+1);//Plus null LPWSTR ptr = wpath; PathRemoveFileSpec(ptr); std::wstring backToString(ptr); std::string basePath(std::string(backToString.begin(),backToString.end())+"\\"); #endif printf("Import textures of scene %s...\n",pFile.c_str()); LoadGLTextures(scene, basePath); printf("Import textures of scene %s done.\n",pFile.c_str()); printf("Import generating VAOs for scene %s...\n",pFile.c_str()); genVAOsAndUniformBuffer(scene); printf("Import generating VAOs for scene %s done.\n",pFile.c_str()); if(!checkForErrors()) { std::cerr << "Problem loading model..." << std::endl; exit(1); } return true; }
int init() { if (!Import3DFromFile(modelname)) return(0); LoadGLTextures(scene); glGetUniformBlockIndex = (PFNGLGETUNIFORMBLOCKINDEXPROC) glutGetProcAddress("glGetUniformBlockIndex"); glUniformBlockBinding = (PFNGLUNIFORMBLOCKBINDINGPROC) glutGetProcAddress("glUniformBlockBinding"); glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC) glutGetProcAddress("glGenVertexArrays"); glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)glutGetProcAddress("glBindVertexArray"); glBindBufferRange = (PFNGLBINDBUFFERRANGEPROC) glutGetProcAddress("glBindBufferRange"); glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC) glutGetProcAddress("glDeleteVertexArrays"); program = setupShaders(); genVAOsAndUniformBuffer(scene); glEnable(GL_DEPTH_TEST); glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // // Uniform Block // glGenBuffers(1,&matricesUniBuffer); glBindBuffer(GL_UNIFORM_BUFFER, matricesUniBuffer); glBufferData(GL_UNIFORM_BUFFER, MatricesUniBufferSize,NULL,GL_DYNAMIC_DRAW); glBindBufferRange(GL_UNIFORM_BUFFER, matricesUniLoc, matricesUniBuffer, 0, MatricesUniBufferSize); //setUniforms(); glBindBuffer(GL_UNIFORM_BUFFER,0); glEnable(GL_MULTISAMPLE); return true; }
bool VSResModelLib::load(std::string filename) { Assimp::Importer importer; //check if file exists std::ifstream fin(filename.c_str()); if(!fin.fail()) fin.close(); else { VSLOG(mLogError, "Unable to open file %s", filename.c_str()); return false; } pScene = importer.ReadFile( filename, aiProcessPreset_TargetRealtime_Quality); // If the import failed, report it if( !pScene) { VSLOG(mLogError, "Failed to import %s", filename.c_str()); return false; } // Get prefix for texture loading size_t index = filename.find_last_of("/\\"); std::string prefix = filename.substr(0, index+1); pTextureIdMap.clear(); bool result = loadTextures(pScene, prefix); genVAOsAndUniformBuffer(pScene); // determine bounding box struct aiVector3D min, max; min.x = min.y = min.z = 1e10f; max.x = max.y = max.z = -1e10f; mVSML->loadIdentity(VSMathLib::AUX0); get_bounding_box_for_node(pScene->mRootNode,&min,&max); float tmp; tmp = max.x - min.x; tmp = max.y - min.y > tmp? max.y - min.y:tmp; tmp = max.z - min.z > tmp? max.z - min.z:tmp; mScaleToUnitCube = 2.0f / tmp; mCenter[0] = min.x + (max.x - min.x) * 0.5f; mCenter[1] = min.y + (max.y - min.y) * 0.5f; mCenter[2] = min.z + (max.z - min.z) * 0.5f; mVSML->loadIdentity(VSMathLib::AUX0); mVSML->scale(VSMathLib::AUX0, mScaleToUnitCube, mScaleToUnitCube, mScaleToUnitCube); mVSML->translate(VSMathLib::AUX0, -mCenter[0], -mCenter[1], -mCenter[2]); for (unsigned int i = 0; i < mMyMeshes.size(); ++i) { mVSML->pushMatrix(mVSML->AUX0); mVSML->multMatrix(mVSML->AUX0, mMyMeshes[i].transform); memcpy(mMyMeshes[i].transform, mVSML->get(mVSML->AUX0), sizeof(float)*16); mVSML->popMatrix(mVSML->AUX0); } // clear texture map pTextureIdMap.clear(); return result; }