virtual ShaderMedia *load(const File &file, bool force = false) { GLuint id = 0; GLint compileStatus = GL_TRUE; if (file.getExtension() == "vert") id = glCreateShader(GL_VERTEX_SHADER); else if (file.getExtension() == "pix") id = glCreateShader(GL_FRAGMENT_SHADER); if (id == 0 || !glIsShader(id)) throw LoadingFailed(file.getFullName(), "ShaderLoader failed to create shader."); const std::string source = file.getFileContent(); const char *sourceChar = source.c_str(); glShaderSource(id, 1, &sourceChar, NULL); glCompileShader(id); glGetShaderiv(id, GL_COMPILE_STATUS, &compileStatus); if (compileStatus != GL_TRUE) { GLint l; glGetShaderiv(id, GL_INFO_LOG_LENGTH, &l); std::string log(l + 1, '\0'); glGetShaderInfoLog(id, l, &l, &log[0]); std::cout << std::endl << log; throw LoadingFailed(file.getFullName(), "ShaderLoader failed to compile shader.\n"); } return new ShaderMedia(id, file.getFileName(), force); }
Plugin::Plugin(const std::string& Filename) { m_Library = LoadLibrary(Filename.c_str()); if (!m_Library) throw LoadingFailed(Filename, "Impossible de charger la bibliothèque dynamique"); PtrFunc LoadFunc = reinterpret_cast<PtrFunc>(GetProcAddress(m_Library, "LoadPlugin")); if (!LoadFunc) throw LoadingFailed(Filename, "Impossible de trouver la fonction 'LoadPlugin' dans la bibliothèque dynamique"); LoadFunc(); }
virtual TextureMedia *load(const File &file, bool force = false) { std::string line; std::ifstream myfile(file.getFullName().c_str()); std::vector<std::string> list; ImageMediaPtr image; GLuint bufferId; std::vector<glm::vec2> coords; if (!myfile.is_open()) { throw LoadingFailed(file.getFullName(), "TextureLoader failed to load texture."); } while (myfile.good()) { std::getline(myfile, line); split(line, list, " "); if (list.empty()) break; if (list[0] == "IMG" && list.size() == 2) { image = ResourceManager::getInstance().get<ImageMedia>(list[1]); } else if (list[0] == "C" && list.size() == 4) { for (unsigned int i = 1; i < 4; ++i) { std::vector<std::string> sub; split(list[i], sub, ","); if (sub.size() < 2) throw LoadingFailed(file.getFullName(), "TextureLoader error, coords size is not correct."); coords.push_back(glm::vec2(std::atof(sub[0].c_str()), std::atof(sub[1].c_str()))); } } } if (coords.size() == 0 || (coords.size() % 3) != 0) { throw LoadingFailed(file.getFullName(), "TextureLoader error, coords size is not correct."); } glGenBuffers(1, &bufferId); glBindBuffer(GL_ARRAY_BUFFER, bufferId); glBufferData(GL_ARRAY_BUFFER, coords.size() * sizeof(glm::vec2), &coords[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); return new TextureMedia(bufferId, coords.size(), image, file.getFileName(), force); }
virtual ImageMedia *load(const File &file, bool force = false) { ALLEGRO_BITMAP *bmp; GLuint tex; bmp = al_load_bitmap(file.getFullName().c_str()); if (!bmp) throw LoadingFailed(file.getFullName(), "ImageLoader failed to load image."); // al_convert_mask_to_alpha(bmp, al_map_rgb(255, 0, 0)); tex = al_get_opengl_texture(bmp); if (tex == 0) throw LoadingFailed(file.getFullName(), "ImageLoader failed to load texture."); return new ImageMedia(bmp, tex, file.getFileName(), force); }
FileLogger::FileLogger(const std::string& file) : _file(file){ if(!_file){ throw LoadingFailed(file); } _file << "Log started : " << getCurrentDate() << " " << getCurrentTime() << std::endl; _file << "================================" << std::endl; }
Model* MeshLoader::LoadFromFile(const std::string& filename) { std::ifstream file(filename,std::ios::in); if (!file) throw LoadingFailed(filename,"Erreur lors du chargement du fichier (ObjLoader)"); a_vector<Model::TVertex> vertices; a_vector<Model::TIndex> indices; int vertices_count, index; file >> vertices_count; for(int i = 0; i < vertices_count; i++) { Model::TVertex vertex; vertex.normal = vec3(0,0,1); file >> vertex.position.x >> vertex.position.y >> vertex.position.z >> vertex.normal.x;// >> vertex.normal.y >> vertex.normal.z >> vertex.texCoords.x >> vertex.texCoords.y; int c; //file >> c >> c >> c >> c; vertices.push_back(vertex); } file >> index; for(int i = 0; i < index; i++) { Model::TIndex index; file >> index; indices.push_back(index); file >> index; indices.push_back(index); file >> index; indices.push_back(index); } return new Model(&vertices[0],vertices.size(),&indices[0],indices.size(),PT_TRIANGLELIST); }
std::string ShaderProgramsLoader::LoadShader(const std::string& filename, const std::string& parentdir) { std::ifstream file; if(parentdir.empty()) file.open(filename, std::ios::in); else { File f(parentdir); file.open(f.Path()+"\\"+filename, std::ios::in); } std::string str_shader = "", buffer = ""; if(!file) throw LoadingFailed(filename,"Erreur lors du chargement du fichier (ShaderProgramsLoader)"); char cbuffer = 0; while(file.read(&cbuffer,1)) { std::string instruction = ""; std::string value = ""; switch(cbuffer) { case '#': while(file.read(&cbuffer,1) && cbuffer != ' ') instruction += cbuffer; while(file.read(&cbuffer,1) && cbuffer != '\n') value += cbuffer; value += '\n'; switch(preprocessor(instruction)) { case PREPROCESSOR_INCLUDE: buffer += LoadShader(value.substr(value.find_first_of('"')+1, value.find_last_of('"')-1),filename); break; default: buffer += "#" + instruction + " " + value + "\n"; break; } break; case '\n': buffer +=cbuffer; break; default: buffer +=cbuffer; while(file.read(&cbuffer,1) && cbuffer != '\n') buffer +=cbuffer; buffer += '\n'; break; } str_shader += buffer; buffer = ""; } return str_shader; }
File MediaManager::FindMedia(const File& filename) const { for (std::set<std::string>::const_iterator itr = m_Paths.begin(); itr != m_Paths.end(); ++itr) { File retFile = *itr + filename.Fullname(); if (retFile.Exists()) return retFile; } throw LoadingFailed(filename.Fullname(), "Fichier introuvable dans les répertoires de recherche"); }
Plugin::~Plugin() { PtrFunc UnloadFunc = reinterpret_cast<PtrFunc>(GetProcAddress(m_Library, "UnloadPlugin")); if (!UnloadFunc) throw LoadingFailed("Module", "Impossible de trouver la fonction 'UnloadPlugin' dans la bibliothèque dynamique"); UnloadFunc(); if (m_Library) FreeLibrary(m_Library); }
inline T *find(const std::string &name) const { t_const_iter it; it = this->list_.find(name); if (it != this->list_.end()) { return static_cast<T*>(it->second); } throw LoadingFailed(name, "This file is not loaded and can not be found."); return NULL; }
inline Loader<T> &findLoader(const File &file) const { typename MediaHolder<T>::LoaderMap::const_iterator it; // todo tolower it = MediaHolder<T>::loaders.find(file.getExtension()); if (it != MediaHolder<T>::loaders.end() && it->second) { return *(it->second); } throw LoadingFailed(file.getFullName(), "No loader handle this type of file."); }
void ImagesLoader::saveToFile(const Image* object, const std::string& fileName){ Image img(object->getSize(), PXF_A8R8G8B8); img.copyImage(*object); img.flip(); ILuint tx; ilGenImages(1, &tx); ilBindImage(tx); const glm::ivec2& size = img.getSize(); if(!ilTexImage(size.x, size.y, 1, getBytesPerPixel(img.getFormat()), IL_BGRA, IL_UNSIGNED_BYTE, (void*)img.getData())) throw LoadingFailed(fileName); if(!ilSaveImage(const_cast<ILstring>(fileName.c_str()))) throw LoadingFailed(fileName); ilBindImage(0); ilDeleteImages(1, &tx); }
File findMedia(const File &file) const { std::set<std::string>::const_iterator it; it = this->paths_.begin(); while (it != this->paths_.end()) { File f; f = *it + file.getFileName(); if (f.exists()) return f; ++it; } throw LoadingFailed(file.getFullName(), "File cannot be found."); }
Image* ImagesLoader::loadFromFile(const std::string& file){ ILuint texture; ilGenImages(1, &texture); ilBindImage(texture); if(!(ilLoadImage(const_cast<ILstring>(file.c_str())))) throw LoadingFailed(file); glm::ivec2 size; size.x = ilGetInteger(IL_IMAGE_WIDTH); size.y = ilGetInteger(IL_IMAGE_HEIGHT); const unsigned char* pix = ilGetData(); Image* img = new Image(size, PXF_A8R8G8B8, pix); img->flip(); return img; }
char* TxtLoader::LoadFromFile(const std::string& filename) { char *src = NULL; a_uint32 size; std::ifstream file(filename, std::ios::in); if (!file) throw LoadingFailed(filename,"Erreur lors du chargement du fichier (TxtLoader)"); file.seekg(0,std::ios_base::end); size = (a_uint32)file.tellg(); file.seekg(0,std::ios_base::beg); src = new char[size+1]; file.read(src,size); src[size] = '\0'; file.close(); return src; }
Model* M2Loader::LoadFromFile(const std::string& filename) { std::ifstream file(filename,std::ios::in | std::ios::ate | std::ios::binary); if (!file) throw LoadingFailed(filename,"Erreur lors du chargement du fichier (ObjLoader)"); uint32 size = (uint32)file.tellg(); file.seekg(0, std::ios::beg); std::vector<Model::TVertex> vertices; std::vector<Model::TIndex> indices; M2ModelHeader header; if(size < sizeof(header)) return NULL; M2ModelVertex* m2vertex; int8* buffer = new int8[size]; file.read(buffer,size); file.close(); std::memcpy(&header,buffer,sizeof(header)); M2RenderFlags* flags = (M2RenderFlags*)(buffer+header.ofsTexFlags); m2vertex = new M2ModelVertex[header.nVertices]; std::memcpy(m2vertex,buffer+header.ofsVertices,sizeof(M2ModelVertex)*header.nVertices); vertices.resize(header.nVertices); std::ifstream file2(filename+".skin",std::ios::in | std::ios::ate | std::ios::binary); if (!file2) throw LoadingFailed(filename+".skin","Erreur lors du chargement du fichier (ObjLoader)"); uint32 size2 = (uint32)file2.tellg(); file2.seekg(0, std::ios::beg); int8* buffer2 = new int8[size2]; file2.read(buffer2,size2); file2.close(); M2SkinHeader _header; std::memcpy(&_header,buffer2,sizeof(_header)); M2ModelTextureDef *texdef = (M2ModelTextureDef*)(buffer + header.ofsTextures); uint16* indexlook = (uint16*)(buffer2+_header.ofsIndices); uint16* triangles = (uint16*)(buffer2+_header.ofsTriangles); for(uint32 i = 0; i < _header.nTriangles; i++) indices.push_back(indexlook[triangles[i]]); for(uint32 i = 0; i < header.nVertices; i++) { vertices[i].position = m2vertex[i].pos; vertices[i].normal = normalize(m2vertex[i].normal); vertices[i].texCoords = m2vertex[i].texcoords; vertices[i].color = -1; } delete[] buffer; delete[] buffer2; delete[] m2vertex; Model* model = new Model(&vertices[0],vertices.size(),&indices[0],indices.size()); Texture tex; tex.CreateFromFile(filename + ".png",PXF_A8R8G8B8); return model; }
Model* MD2Loader::loadFromFile(const std::string& name) { std::ifstream stream(name.c_str(), std::ios_base::binary); Header head; stream.read(reinterpret_cast<char*>(&head), sizeof(Header)); if(head.magic != _magicId) throw LoadingFailed(name + " magic"); if(head.version != _MD2Version) throw LoadingFailed(name + " version"); std::vector<Texture> textures(head.nbTex); std::vector<TexCoord> texCoord(head.nbTexCoord); std::vector<Triangle> triangles(head.nbTriangles); std::vector<Frame> frames(head.nbFrames); stream.seekg(head.offsetTextures, std::ios_base::beg); stream.read(reinterpret_cast<char*>(&textures[0]), sizeof(Texture)*head.nbTex); stream.seekg(head.offsetTexCoords, std::ios_base::beg); stream.read(reinterpret_cast<char*>(&texCoord[0]), sizeof(TexCoord)*head.nbTexCoord); stream.seekg(head.offsetTriangles, std::ios_base::beg); stream.read(reinterpret_cast<char*>(&triangles[0]), sizeof(Triangle)*head.nbTriangles); stream.seekg(head.offsetFrames, std::ios_base::beg); frames[0].vertices.resize(head.nbVert); stream.read(reinterpret_cast<char*>(&frames[0].scale), sizeof(frames[0].scale)); stream.read(reinterpret_cast<char*>(&frames[0].translate), sizeof(frames[0].translate)); stream.read(reinterpret_cast<char*>(&frames[0].name), sizeof(frames[0].name)); stream.read(reinterpret_cast<char*>(&frames[0].vertices[0]), sizeof(Point)*head.nbVert); std::vector<Model::Vertex> vertices; std::vector<Model::Index> indices; for(int i = 0; i < head.nbTriangles; ++i) { for(int j=0; j<3; ++j) { Model::Vertex v; const Point& vert = frames[0].vertices[triangles[i].vertices[j]]; const TexCoord& texC = texCoord[triangles[i].texCoords[j]]; v.px = vert.x*frames[0].scale.x+frames[0].translate.x; v.py = vert.y*frames[0].scale.y+frames[0].translate.y; v.pz = vert.z*frames[0].scale.z+frames[0].translate.z; v.nx = _normalTable[vert.n].x; v.ny = _normalTable[vert.n].y; v.nz = _normalTable[vert.n].z; v.color = Graphics::D3::Renderer::getInstance().convertColor(Graphics::Color::White); v.tx = texC.u/static_cast<float>(head.texW); v.ty = texC.v/static_cast<float>(head.texH); vertices.push_back(v); indices.push_back(i*3+2-j); } } std::string texName = ""; if(textures[0].name != nullptr) texName = textures[0].name; Model* m = new Model(name, &vertices[0], (int)vertices.size(), &indices[0], (int)indices.size(), texName); return m; }
virtual void save(const ShaderProgramMedia *, const std::string &name) { throw LoadingFailed(name, "ShaderProgramLoader doesn't support SAVE."); }
virtual ShaderProgramMedia *load(const File &file, bool force = false) { GLuint id; std::string line; std::ifstream myfile(file.getFullName().c_str()); std::vector<std::string> list; std::map<std::string, std::string> textures; ShaderMediaPtr shader; ShaderProgramMedia *program; GLint linkStatus = GL_TRUE; if (!myfile.is_open()) { throw LoadingFailed(file.getFullName(), "ShaderProgramLoader failed to load shader."); } id = glCreateProgram(); if (id == 0) throw LoadingFailed(file.getFullName(), "ShaderProgramLoader failed to create shader."); program = new ShaderProgramMedia(id, file.getFileName(), force); while (myfile.good()) { std::getline(myfile, line); split(line, list, " "); if (list.size() != 2) continue; if (list[0] == "VERTEX" || list[0] == "PIXEL") { shader = ResourceManager::getInstance().get<ShaderMedia>(list[1]); glAttachShader(id, shader->getId()); program->add(shader->getId()); } else if (list[0] == "TEXTURES" && list.size() == 2) { std::vector<std::string> sub; split(list[1], sub, ","); for (unsigned int i = 0; i < sub.size(); ++i) { std::vector<std::string> val; split(sub[i], val, ":"); if (val.size() <= 1) continue; textures.insert(std::pair<std::string, std::string>(val[0], val[1])); } } } program->setTextures(textures); glLinkProgram(id); if (linkStatus != GL_TRUE) { GLint l; glGetProgramiv(id, GL_INFO_LOG_LENGTH, &l); std::string log(l + 1, '\0'); glGetProgramInfoLog(id, l, &l, &log[0]); std::cout << std::endl << log; throw LoadingFailed(file.getFullName(), "ShaderProgramLoader failed to link program.\n"); } return program; }
virtual void save(const Skybox *, const std::string &name) { throw LoadingFailed(name, "SkyboxLoader doesn't support SAVE."); }
virtual void save(const ImageMedia *, const std::string &name) { throw LoadingFailed(name, "ImageLoader doesn't support SAVE."); }
virtual Skybox *load(const File &file, bool force = false) { // // A terminer // http://raptor.developpez.com/tutorial/opengl/skybox/ ALLEGRO_FS_ENTRY *fs; ALLEGRO_FS_ENTRY *content; ALLEGRO_BITMAP **bmps = new ALLEGRO_BITMAP*[6]; int fileCnt = 0; GLuint cube_map_texture_ID; GLenum cube_map_target[6] = { GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, GL_TEXTURE_CUBE_MAP_POSITIVE_Z }; std::map<std::string, ALLEGRO_LOCKED_REGION*> texture_image; fs = al_create_fs_entry(file.getFullName().c_str()); if (!fs) { throw LoadingFailed(file.getFullName(), "SkyboxLoader failed to find or open skybox folder."); } if (!al_open_directory(fs)) { throw LoadingFailed(file.getFullName(), "SkyboxLoader failed to open skybox folder."); } if (!al_fs_entry_exists(fs)) { throw LoadingFailed(file.getFullName(), "SkyboxLoader can't find folder."); } al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); while ((content = al_read_directory(fs))) { const char *name = al_get_fs_entry_name(content); ALLEGRO_LOCKED_REGION *r; std::cout << name << std::endl; bmps[fileCnt] = al_load_bitmap(name); if (!bmps[fileCnt]) throw LoadingFailed(name, "SkyboxLoader can't load image."); r = al_lock_bitmap(bmps[fileCnt], ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READWRITE); if (!r) throw LoadingFailed(name, "SkyboxLoader can't lock bitmap."); texture_image.insert(std::pair<std::string, ALLEGRO_LOCKED_REGION*>(std::string(name), r)); al_destroy_fs_entry(content); ++fileCnt; } al_destroy_fs_entry(fs); al_set_new_bitmap_flags(~ALLEGRO_MEMORY_BITMAP); // Génération d'une texture CubeMap glGenTextures(1, &cube_map_texture_ID); if (cube_map_texture_ID == 0) throw LoadingFailed(file.getFullName(), "SkyboxLoader error generating a texture."); // Configuration de la texture glBindTexture(GL_TEXTURE_CUBE_MAP, cube_map_texture_ID); std::map<std::string, ALLEGRO_LOCKED_REGION*>::iterator it = texture_image.begin(); int i = 0; while (it != texture_image.end()) { glTexImage2D(cube_map_target[i], 0, it->second->pixel_size, al_get_bitmap_width(bmps[i]), al_get_bitmap_height(bmps[i]), 0, GL_BGRA, GL_UNSIGNED_BYTE, it->second->data); al_unlock_bitmap(bmps[i]); ++i; ++it; } glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP); return new Skybox(cube_map_texture_ID, bmps, file.getFileName(), force); }