From 486998fb35a3dfbda9a316268e3a63785a7f12ba Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 23 Oct 2018 22:19:31 +0200 Subject: [PATCH] [DEV] update new etk Uri API --- ege/Material.cpp | 8 +- ege/Material.hpp | 2 +- ege/resource/Mesh.cpp | 25 +++-- ege/resource/Mesh.hpp | 10 +- ege/resource/MeshEmf.cpp | 207 ++++++++++++++++++--------------------- ege/resource/MeshObj.cpp | 2 +- 6 files changed, 122 insertions(+), 132 deletions(-) diff --git a/ege/Material.cpp b/ege/Material.cpp index 0b15d0b..fc9c8dc 100644 --- a/ege/Material.cpp +++ b/ege/Material.cpp @@ -83,14 +83,14 @@ void ege::Material::setShininess(float _val) { m_shininess = _val; } -void ege::Material::setTexture0(const etk::String& _filename) { +void ege::Material::setTexture0(const etk::Uri& _uri) { ivec2 tmpSize(256, 256); - if (_filename != "") { + if (_uri.isEmpty() == false) { // prevent overloard error : ememory::SharedPtr tmpCopy = m_texture0; - m_texture0 = ewol::resource::TextureFile::create(_filename, tmpSize); + m_texture0 = ewol::resource::TextureFile::create(_uri, tmpSize); if (m_texture0 == null) { - EGE_ERROR("Can not load specific texture : " << _filename); + EGE_ERROR("Can not load specific texture : " << _uri); // retreave previous texture: m_texture0 = tmpCopy; if (m_texture0 != null) { diff --git a/ege/Material.hpp b/ege/Material.hpp index d43de11..5ccb061 100644 --- a/ege/Material.hpp +++ b/ege/Material.hpp @@ -49,7 +49,7 @@ namespace ege { void setRenderMode(enum gale::openGL::renderMode _val); enum gale::openGL::renderMode getRenderModeOpenGl(); enum gale::openGL::renderMode getRenderMode(); - void setTexture0(const etk::String& _filename); + void setTexture0(const etk::Uri& _uri); void setTexture0Magic(const ivec2& _size); void setImageSize(const ivec2& _newSize); // get the reference on this image to draw nomething on it ... diff --git a/ege/resource/Mesh.cpp b/ege/resource/Mesh.cpp index 9e58f6e..e73a263 100644 --- a/ege/resource/Mesh.cpp +++ b/ege/resource/Mesh.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include @@ -31,8 +31,8 @@ ege::resource::Mesh::Mesh() : addResourceType("ege::resource::Mesh"); } -void ege::resource::Mesh::init(const etk::String& _fileName, const etk::String& _shaderName) { - gale::Resource::init(_fileName); +void ege::resource::Mesh::init(const etk::Uri& _fileName, const etk::Uri& _shaderName) { + gale::Resource::init(_fileName.getString()); EGE_VERBOSE("Load a new mesh : '" << _fileName << "'"); // get the shader resource : m_GLPosition = 0; @@ -63,18 +63,18 @@ void ege::resource::Mesh::init(const etk::String& _fileName, const etk::String& return; } // TO facilitate some debugs we add a name of the VBO: - m_verticesVBO->setName("[VBO] of " + _fileName); + m_verticesVBO->setName("[VBO] of " + _fileName.getString()); // load the curent file : - etk::String tmpName = etk::toLower(_fileName); + etk::String extention = etk::toLower(_fileName.getPath().getExtention()); // select the corect loader : - if (etk::end_with(tmpName, ".obj") == true) { + if (extention == "obj") { if (loadOBJ(_fileName) == false) { - EGE_ERROR("Error To load OBJ file " << tmpName ); + EGE_ERROR("Error To load OBJ file " << _fileName ); return; } - } else if (etk::end_with(tmpName, ".emf") ) { + } else if (extention == "emf") { if (loadEMF(_fileName) == false) { - EGE_ERROR("Error To load EMF file " << tmpName ); + EGE_ERROR("Error To load EMF file " << _fileName ); return; } //EGE_CRITICAL("Load a new mesh : '" << _fileName << "' (end)"); @@ -407,6 +407,10 @@ void ege::resource::Mesh::generateVBO() { // clean faces indexes : m_listFaces.getValue(kkk).m_index.clear(); int32_t nbIndicInFace = 3; + if (m_materials.exist(m_listFaces.getKey(kkk)) == false) { + EGE_WARNING("missing materials : '" << m_listFaces.getKey(kkk) << "' not in " << m_materials.getKeys() ); + continue; + } if (m_materials[m_listFaces.getKey(kkk)] == null) { EGE_ERROR("Can not get material : " << m_listFaces.getKey(kkk) << " pointer value: " << size_t(m_materials[m_listFaces.getKey(kkk)].get())); continue; @@ -532,7 +536,8 @@ void ege::resource::Mesh::addMaterial(const etk::String& _name, ememory::SharedP EGE_ERROR(" can not add material with no name"); return; } - // really add the material : + // really add the material: + EGE_WARNING("Add material: " << _name); m_materials.add(_name, _data); } diff --git a/ege/resource/Mesh.hpp b/ege/resource/Mesh.hpp index 2a38105..ca65f6b 100644 --- a/ege/resource/Mesh.hpp +++ b/ege/resource/Mesh.hpp @@ -118,13 +118,13 @@ namespace ege { ememory::SharedPtr m_verticesVBO; protected: Mesh(); - void init(const etk::String& _fileName="---", + void init(const etk::Uri& _fileName="---", //const etk::String& _shaderName="DATA:///textured3D2.prog" - const etk::String& _shaderName="DATA:///material3D.prog" + const etk::Uri& _shaderName="DATA:///material3D.prog" ); public: virtual ~Mesh(); - DECLARE_RESOURCE_NAMED_FACTORY(Mesh); + DECLARE_RESOURCE_URI_FACTORY(Mesh); public: virtual void draw(mat4& _positionMatrix, bool _enableDepthTest=true, bool _enableDepthUpdate=true); virtual void draw(mat4& _positionMatrix, @@ -144,8 +144,8 @@ namespace ege { void createViewBox(const etk::String& _materialName,float _size=1.0); void createIcoSphere(const etk::String& _materialName,float _size=1.0, int32_t _subdivision=3); private: - bool loadOBJ(const etk::String& _fileName); - bool loadEMF(const etk::String& _fileName); + bool loadOBJ(const etk::Uri& _fileName); + bool loadEMF(const etk::Uri& _fileName); public: void addMaterial(const etk::String& _name, ememory::SharedPtr _data); public: diff --git a/ege/resource/MeshEmf.cpp b/ege/resource/MeshEmf.cpp index d294d43..0875852 100644 --- a/ege/resource/MeshEmf.cpp +++ b/ege/resource/MeshEmf.cpp @@ -6,25 +6,25 @@ #include #include -#include +#include -static void jumpEndLine(etk::FSNode& _file) { - char current=_file.fileGet(); +static void jumpEndLine(ememory::SharedPtr& _file) { + char current=_file->get(); while( current != '\0' && current != '\n') { //printf("%c", current); - current=_file.fileGet(); + current=_file->get(); } } -static int32_t countIndent(etk::FSNode& _file) { +static int32_t countIndent(ememory::SharedPtr& _file) { int32_t nbIndent=0; int32_t nbSpacesTab=0; int32_t nbChar=0; //EGE_DEBUG(" start count Indent"); - for(char current=_file.fileGet(); current != '\0'; current=_file.fileGet()) { + for(char current=_file->get(); current != '\0'; current=_file->get()) { nbChar++; //EGE_DEBUG("parse : " << current); if (current == '\t') { @@ -46,18 +46,17 @@ static int32_t countIndent(etk::FSNode& _file) { } } //EGE_DEBUG("indent : " << nbIndent); - _file.fileSeek(-nbChar, etk::seekNode_current); + _file->seek(-nbChar, etk::io::SeekMode::Current); return nbIndent; } -static char* loadNextData(char* _elementLine, +static bool loadNextData(etk::String& _elementLine, int64_t _maxData, - etk::FSNode& _file, + ememory::SharedPtr& _file, bool _removeTabs=false, bool _stopColomn=false, bool _stopPipe=true) { - memset(_elementLine, 0, _maxData); - char * element = _elementLine; + _elementLine.clear(); int64_t outSize = 0; /* if (m_zipReadingOffset >= m_zipContent->size()) { @@ -65,12 +64,11 @@ static char* loadNextData(char* _elementLine, return null; } */ - char current = _file.fileGet(); + char current = _file->get(); while (current != '\0') { if( _removeTabs == false - || element != _elementLine) { - *element = current; - element++; + || _elementLine.isEmpty() == false) { + _elementLine.pushBack(current); } if( current == '\n' || current == '\r' @@ -79,42 +77,32 @@ static char* loadNextData(char* _elementLine, || ( current == ':' && _stopColomn == true) ) { - *element = '\0'; //EGE_DEBUG(" plop : '" << _elementLine << "'" ); - return _elementLine; - } else if( element == _elementLine + return true; + } else if( _elementLine.isEmpty() == true && current != '\t') { - *element = current; - element++; + _elementLine.pushBack(current); } // check maxData size ... if (outSize >= _maxData-1) { - *element = '\0'; - return _elementLine; + return true; } - current = _file.fileGet(); + current = _file->get(); } if (outSize == 0) { - return null; + return false; } else { // send last line - return _elementLine; + return true; } - return null; + return false; } -static void removeEndLine(char* _val) { - int32_t len = strlen(_val); - if( len>0 - && ( _val[len-1] == '\n' - || _val[len-1] == '\r' ) ) { - _val[len-1] = '\0'; - } - len--; - if( len>0 - && ( _val[len-1] == '\n' - || _val[len-1] == '\r') ) { - _val[len-1] = '\0'; +static void removeEndLine(etk::String& _value) { + while ( _value.size() > 0 + && ( _value.back() == '\n' + || _value.back() == '\r') ) { + _value.popBack(); } } @@ -139,26 +127,26 @@ enum emfModuleMode { }; // TODO : rework with string line extractor -bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { +bool ege::resource::Mesh::loadEMF(const etk::Uri& _fileName) { m_checkNormal = true; m_normalMode = ege::resource::Mesh::normalMode::none; - etk::FSNode fileName(_fileName); + ememory::SharedPtr fileIO = etk::uri::get(_fileName); // get the fileSize ... - int32_t size = fileName.fileSize(); - if (size == 0 ) { - EGE_ERROR("No data in the file named='" << fileName << "'"); + int32_t size = fileIO->size(); + if (fileIO == null ) { + EGE_ERROR("CAn not get the file named='" << _fileName << "'"); return false; } - if (fileName.fileOpenRead() == false) { - EGE_ERROR("Can not find the file name='" << fileName << "'"); + if (fileIO->open(etk::io::OpenMode::Read) == false) { + EGE_ERROR("Can not find the file name='" << _fileName << "'"); return false; } - char inputDataLine[2048]; + etk::String inputDataLine; // load the first line : - fileName.fileGets(inputDataLine, 2048); - if(strncmp(inputDataLine, "EMF(STRING)", 11) == 0) { + fileIO->gets(inputDataLine); + if (inputDataLine.startWith("EMF(STRING)") == true) { // parse in string mode ... - } else if (strncmp(inputDataLine, "EMF(BINARY)", 11) == 0) { + } else if (inputDataLine.startWith("EMF(BINARY)") == true) { EGE_ERROR(" file binary mode is not supported now : 'EMF(BINARY)'"); return false; } else { @@ -166,7 +154,7 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { return false; } enum emfModuleMode currentMode = EMFModuleNone; - EGE_VERBOSE("Start parsing Mesh file : " << fileName); + EGE_VERBOSE("Start parsing Mesh file : " << _fileName); // mesh global param : etk::String currentMeshName = ""; int32_t meshFaceMaterialID = -1; @@ -181,14 +169,14 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { size_t offsetFaceNormal = 0; size_t offsetVertexNormal = 0; while (1) { - int32_t level = countIndent(fileName); + int32_t level = countIndent(fileIO); if (level == 0) { // new section ... - if (loadNextData(inputDataLine, 2048, fileName) == null) { + if (loadNextData(inputDataLine, 2048, fileIO) == false) { // reach end of file ... break; } - if(strncmp(inputDataLine, "Mesh:", 5) == 0) { + if(inputDataLine.startWith("Mesh:") == true) { currentMode = EMFModuleMesh; removeEndLine(inputDataLine); currentMeshName = inputDataLine + 6; @@ -198,7 +186,7 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { offsetFaceNormal = m_listFacesNormal.size(); offsetVertexNormal = m_listVertexNormal.size(); //EGE_ERROR("new offset: " << offsetVertexId << " " << offsetUV << " " << offsetFaceNormal << " " << offsetVertexNormal); - } else if(strncmp(inputDataLine, "Materials:", 9) == 0) { + } else if(inputDataLine.startWith("Materials:") == true) { currentMode = EMFModuleMaterial; // add previous material: if( materialName != "" @@ -209,9 +197,9 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { } material = ememory::makeShared(); removeEndLine(inputDataLine); - materialName = inputDataLine + 10; + materialName = inputDataLine.extract(10, etk::String::npos); EGE_VERBOSE("Parse Material: " << materialName); - } else if(strncmp(inputDataLine, "Physics:", 8) == 0) { + } else if(inputDataLine.startWith("Physics:") == true) { currentMode = EMFModulePhysics; removeEndLine(inputDataLine); EGE_VERBOSE("Parse global Physics: "); @@ -223,32 +211,32 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { && currentMode <= EMFModuleMesh_END) { if (level == 1) { // In the mesh level 2 the line size must not exced 2048 - if (loadNextData(inputDataLine, 2048, fileName, true) == null) { + if (loadNextData(inputDataLine, 2048, fileIO, true) == false) { // reach end of file ... break; } removeEndLine(inputDataLine); - if(strncmp(inputDataLine, "Vertex", 6) == 0) { + if(inputDataLine.startWith("Vertex") == true) { currentMode = EMFModuleMeshVertex; EGE_VERBOSE(" Vertex ..."); - } else if(strncmp(inputDataLine, "UV-mapping", 10) == 0) { + } else if(inputDataLine.startWith("UV-mapping") == true) { currentMode = EMFModuleMeshUVMapping; haveUVMapping = true; EGE_VERBOSE(" UV-mapping ..."); - } else if(strncmp(inputDataLine, "Normal(vertex)", 14) == 0) { + } else if(inputDataLine.startWith("Normal(vertex)") == true) { currentMode = EMFModuleMeshNormalVertex; EGE_VERBOSE(" Normal(vertex) ..."); - } else if(strncmp(inputDataLine, "Normal(face)", 12) == 0) { + } else if(inputDataLine.startWith("Normal(face)") == true) { currentMode = EMFModuleMeshNormalFace; EGE_VERBOSE(" Normal(face) ..."); - } else if(strncmp(inputDataLine, "Face", 4) == 0) { + } else if(inputDataLine.startWith("Face") == true) { currentMode = EMFModuleMeshFace; EGE_VERBOSE(" Face ..."); - } else if(strncmp(inputDataLine, "Physics", 7) == 0) { + } else if(inputDataLine.startWith("Physics") == true) { currentMode = EMFModuleMeshPhysics; EGE_VERBOSE(" Physics ..."); } else { - EGE_ERROR(" Unknow mesh property '"<parse(inputDataLine) == false) { + if (physics->parse(&inputDataLine[0]) == false) { EGE_ERROR("ERROR when parsing :'" << inputDataLine << "' in physical shape ..."); } } @@ -458,22 +441,22 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { } else if ( currentMode >= EMFModuleMaterial && currentMode <= EMFModuleMaterial_END) { // all material element is stored on 1 line (size < 2048) - if (loadNextData(inputDataLine, 2048, fileName, true) == null) { + if (loadNextData(inputDataLine, 2048, fileIO, true) == false) { // reach end of file ... break; } removeEndLine(inputDataLine); if (material == null) { EGE_ERROR("material allocation error"); - jumpEndLine(fileName); + jumpEndLine(fileIO); continue; } - if(strncmp(inputDataLine,"Ns ",3) == 0) { + if(inputDataLine.startWith("Ns ") == true) { float tmpVal=0; sscanf(&inputDataLine[3], "%f", &tmpVal); material->setShininess(tmpVal); EGE_VERBOSE(" Shininess " << tmpVal); - } else if(strncmp(inputDataLine,"Ka ",3) == 0) { + } else if(inputDataLine.startWith("Ka ") == true) { float tmpVal1=0; float tmpVal2=0; float tmpVal3=0; @@ -481,7 +464,7 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { vec4 tmp(tmpVal1, tmpVal2, tmpVal3, 1); material->setAmbientFactor(tmp); EGE_VERBOSE(" AmbientFactor " << tmp); - } else if(strncmp(inputDataLine,"Kd ",3) == 0) { + } else if(inputDataLine.startWith("Kd ") == true) { float tmpVal1=0; float tmpVal2=0; float tmpVal3=0; @@ -492,7 +475,7 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { vec4 tmp(tmpVal1, tmpVal2, tmpVal3, 1); material->setDiffuseFactor(tmp); EGE_ERROR(" DiffuseFactor " << tmp); - } else if(strncmp(inputDataLine,"Ks ",3) == 0) { + } else if(inputDataLine.startWith("Ks ") == true) { float tmpVal1=0; float tmpVal2=0; float tmpVal3=0; @@ -500,25 +483,27 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { vec4 tmp(tmpVal1, tmpVal2, tmpVal3, 1); material->setSpecularFactor(tmp); EGE_VERBOSE(" SpecularFactor " << tmp); - } else if(strncmp(inputDataLine,"Ni ",3) == 0) { + } else if(inputDataLine.startWith("Ni ") == true) { float tmpVal=0; sscanf(&inputDataLine[3], "%f", &tmpVal); // TODO : ... EGE_VERBOSE(" Ni " << tmpVal); - } else if(strncmp(inputDataLine,"d ",2) == 0) { + } else if(inputDataLine.startWith("d ") == true) { float tmpVal=0; sscanf(&inputDataLine[2], "%f", &tmpVal); // TODO : ... EGE_VERBOSE(" d " << tmpVal); - } else if(strncmp(inputDataLine,"illum ",6) == 0) { + } else if(inputDataLine.startWith("illum ") == true) { int tmpVal=0; sscanf(&inputDataLine[6], "%d", &tmpVal); // TODO : ... EGE_VERBOSE(" illum " << tmpVal); - } else if(strncmp(inputDataLine,"map_Kd ",7) == 0) { - material->setTexture0(fileName.getRelativeFolder() + &inputDataLine[7]); + } else if(inputDataLine.startWith("map_Kd ") == true) { + etk::Uri tmpTexture = _fileName; + tmpTexture.setPath(_fileName.getPath().getParent() / &inputDataLine[7]); + material->setTexture0(tmpTexture); EGE_VERBOSE(" Texture " << &inputDataLine[7]); - } else if(strncmp(inputDataLine,"renderMode ",11) == 0) { + } else if(inputDataLine.startWith("renderMode ") == true) { gale::openGL::renderMode mode; etk::from_string(mode, &inputDataLine[11]); material->setRenderMode(mode); @@ -528,7 +513,7 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { } } else if ( currentMode >= EMFModulePhysics && currentMode <= EMFModulePhysics_END) { - if (loadNextData(inputDataLine, 2048, fileName, true, false, false) == null) { + if (loadNextData(inputDataLine, 2048, fileIO, true, false, false) == false) { // reach end of file ... break; } @@ -548,14 +533,14 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { EGE_ERROR("Can not parse :'" << inputDataLine << "' in physical shape ..."); continue; } - if (physics->parse(inputDataLine) == false) { + if (physics->parse(&inputDataLine[0]) == false) { EGE_ERROR("ERROR when parsing :'" << inputDataLine << "' in physical shape ..."); } } } else { // unknow ... EGE_WARNING("Unknow type of line == > jump end of line ... " << inputDataLine); - jumpEndLine(fileName); + jumpEndLine(fileIO); } } } @@ -568,7 +553,7 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { } EGE_VERBOSE("Stop parsing Mesh file"); - fileName.fileClose(); + fileIO->close(); EGE_VERBOSE("New mesh : "); EGE_VERBOSE(" nb vertex: " << m_listVertex.size()); diff --git a/ege/resource/MeshObj.cpp b/ege/resource/MeshObj.cpp index 10978bb..3930989 100644 --- a/ege/resource/MeshObj.cpp +++ b/ege/resource/MeshObj.cpp @@ -8,7 +8,7 @@ #include -bool ege::resource::Mesh::loadOBJ(const etk::String& _fileName) { +bool ege::resource::Mesh::loadOBJ(const etk::Uri& _fileName) { m_normalMode = ege::resource::Mesh::normalMode::none; #if 0 etk::FSNode fileName(_fileName);