diff --git a/Sources/libetk/etk/Types.h b/Sources/libetk/etk/Types.h index ba533942..7b9f6456 100644 --- a/Sources/libetk/etk/Types.h +++ b/Sources/libetk/etk/Types.h @@ -80,6 +80,6 @@ typedef struct { float h; }clipping_ts; -#endif - #include + +#endif diff --git a/Sources/libetk/etk/math/Matrix.h b/Sources/libetk/etk/math/Matrix.h index 6ee19da9..4ab5aaaf 100644 --- a/Sources/libetk/etk/math/Matrix.h +++ b/Sources/libetk/etk/math/Matrix.h @@ -25,16 +25,17 @@ #ifndef __ETK_TYPES_MATRIX_H__ #define __ETK_TYPES_MATRIX_H__ -#include +//#include #include #include -namespace etk { +namespace etk +{ template class Matrix { private: - Vector2D m_size; - Vector m_data; + etk::Vector2D m_size; + etk::Vector m_data; public: /***************************************************** * Constructor diff --git a/Sources/libewol/ewol/Mesh/Mesh.cpp b/Sources/libewol/ewol/Mesh/Mesh.cpp new file mode 100644 index 00000000..034ae228 --- /dev/null +++ b/Sources/libewol/ewol/Mesh/Mesh.cpp @@ -0,0 +1,123 @@ +/** + ******************************************************************************* + * @file ewol/Mesh/Mesh.cpp + * @brief ewol Mesh system (sources) + * @author Edouard DUPIN + * @date 30/10/2012 + * @par Project + * ewol + * + * @par Copyright + * Copyright 2011 Edouard DUPIN, all right reserved + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY. + * + * Licence summary : + * You can modify and redistribute the sources code and binaries. + * You can send me the bug-fix + * + * Term of the licence in in the file licence.txt. + * + ******************************************************************************* + */ + + +#include +#include +#include + + +ewol::Mesh::Mesh(etk::UString genName) : + ewol::Resource(genName), + m_texture1(NULL) +{ + #ifdef __VIDEO__OPENGL_ES_2 + etk::UString tmpString("textured3D.prog"); + // get the shader resource : + m_GLPosition = 0; + if (true == ewol::resource::Keep(tmpString, m_GLprogram) ) { + m_GLPosition = m_GLprogram->GetAttribute("EW_coord3d"); + m_GLColor = m_GLprogram->GetAttribute("EW_color"); + m_GLtexture = m_GLprogram->GetAttribute("EW_texture2d"); + m_GLMatrix = m_GLprogram->GetUniform("EW_MatrixTransformation"); + m_GLtexID = m_GLprogram->GetUniform("EW_texID"); + } + #endif +} + +ewol::Mesh::~Mesh(void) +{ + // remove dynamics dependencies : + if(NULL!=m_texture1) { + ewol::resource::Release(m_texture1); + } + #ifdef __VIDEO__OPENGL_ES_2 + ewol::resource::Release(m_GLprogram); + #endif +} + + +void ewol::Mesh::Draw(void) +{ + static float rotx = 0; + static float roty = 0; + static float rotz = 0; + rotx += 0.01; + roty += 0.02; + rotz += 0.005; + if (m_vertices.Size()<=0) { + return; + } + if (NULL == m_texture1) { + EWOL_WARNING("Texture does not exist ..."); + return; + } + #ifdef __VIDEO__OPENGL_ES_2 + if (m_GLprogram==NULL) { + EWOL_ERROR("No shader ..."); + return; + } + //EWOL_DEBUG(" Display " << m_coord.Size() << " elements" ); + m_GLprogram->Use(); + // set Matrix : translation/positionMatrix + etk::Matrix4 tmpMatrix = ewol::openGL::GetMatrix(); + tmpMatrix = etk::matrix::Scale(100,100,100) + * etk::matrix::rotate(1,0,0,rotx) + * etk::matrix::rotate(0,1,0,roty) + * etk::matrix::Translate(0.01,0,0) + * etk::matrix::rotate(0,0,1,rotz) + * tmpMatrix; + m_GLprogram->UniformMatrix4fv(m_GLMatrix, 1, tmpMatrix.m_mat); + // TextureID + m_GLprogram->SetTexture0(m_GLtexID, m_texture1->GetId()); + // position : + m_GLprogram->SendAttribute(m_GLPosition, 3/*x,y,z*/, &m_vertices[0]); + // Texture : + m_GLprogram->SendAttribute(m_GLtexture, 2/*u,v*/, &m_uvTextures[0]); + // color : + m_GLprogram->SendAttribute(m_GLColor, 4/*r,g,b,a*/, &m_coordColor[0]); + // Request the draw od the elements : + glDrawArrays(GL_TRIANGLES, 0, m_vertices.Size()); + m_GLprogram->UnUse(); + #else + /* + glColor4f(1.0, 1.0, 1.0, 1.0); + glEnable(GL_TEXTURE_2D); + //EWOL_WARNING("Draw with texture : " << m_textureId << " ==> ogl=" << ewol::texture::GetGLID(m_textureId)); + glBindTexture(GL_TEXTURE_2D, m_resource->GetId() ); + glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays + glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays + glEnableClientState( GL_COLOR_ARRAY ); // Enable Color Arrays + glVertexPointer( 3, GL_FLOAT, 0, &m_coord[0] ); + glTexCoordPointer( 2, GL_FLOAT, 0, &m_coordTex[0] ); + glColorPointer( 4, GL_UNSIGNED_BYTE, 0, &m_coordColor[0] ); + glDrawArrays( GL_TRIANGLES, 0, m_coord.Size()); + //EWOL_DEBUG("request draw of " << m_coord.Size() << " elements"); + glDisableClientState( GL_COLOR_ARRAY ); // Disable Color Arrays + glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays + glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays + glDisable(GL_TEXTURE_2D); + */ + #endif +} diff --git a/Sources/libewol/ewol/Mesh/Mesh.h b/Sources/libewol/ewol/Mesh/Mesh.h new file mode 100644 index 00000000..2b4112d6 --- /dev/null +++ b/Sources/libewol/ewol/Mesh/Mesh.h @@ -0,0 +1,67 @@ +/** + ******************************************************************************* + * @file ewol/Mesh/Mesh.h + * @brief ewol Mesh system (header) + * @author Edouard DUPIN + * @date 30/10/2012 + * @par Project + * ewol + * + * @par Copyright + * Copyright 2011 Edouard DUPIN, all right reserved + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY. + * + * Licence summary : + * You can modify and redistribute the sources code and binaries. + * You can send me the bug-fix + * + * Term of the licence in in the file licence.txt. + * + ******************************************************************************* + */ + +#ifndef __MESH_H__ +#define __MESH_H__ + +#include +#include +#include +#include +#include + +namespace ewol +{ + class Mesh : public ewol::Resource + { + protected: + #ifdef __VIDEO__OPENGL_ES_2 + ewol::Program* m_GLprogram; + int32_t m_GLPosition; + int32_t m_GLMatrix; + int32_t m_GLColor; + int32_t m_GLtexture; + int32_t m_GLtexID; + #endif + etk::Vector m_indices; + etk::Vector< etk::Vector3D > m_vertices; + etk::Vector< etk::Vector2D > m_uvTextures; + etk::Vector< etk::Vector3D > m_normals; + ewol::TextureFile* m_texture1; + #ifdef __VIDEO__OPENGL_ES_2 + etk::Vector m_coordColor; //!< internal color of the different point + #else + etk::Vector m_coordColor; //!< internal color of the different point + #endif + public: + Mesh(etk::UString genName); + virtual ~Mesh(void); + virtual const char* GetType(void) { return "ewol::Mesh"; }; + virtual void Draw(void); + + }; +}; + + +#endif diff --git a/Sources/libewol/ewol/Mesh/MeshObj.cpp b/Sources/libewol/ewol/Mesh/MeshObj.cpp new file mode 100644 index 00000000..dad65a2d --- /dev/null +++ b/Sources/libewol/ewol/Mesh/MeshObj.cpp @@ -0,0 +1,172 @@ +/** + ******************************************************************************* + * @file ewol/Mesh/MeshObj.cpp + * @brief ewol Mesh ;obj loader system (sources) + * @author Edouard DUPIN + * @date 30/10/2012 + * @par Project + * ewol + * + * @par Copyright + * Copyright 2011 Edouard DUPIN, all right reserved + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY. + * + * Licence summary : + * You can modify and redistribute the sources code and binaries. + * You can send me the bug-fix + * + * Term of the licence in in the file licence.txt. + * + ******************************************************************************* + */ + +#include +#include +#include +#include +#include + + +ewol::MeshObj::MeshObj(etk::UString _fileName) : + ewol::Mesh(_fileName) +{ + etk::File fileName(_fileName, etk::FILE_TYPE_DATA); + // Get the fileSize ... + int32_t size = fileName.Size(); + if (size == 0 ) { + EWOL_ERROR("No data in the file named=\"" << fileName << "\""); + return; + } + if(false == fileName.fOpenRead() ) { + EWOL_ERROR("Can not find the file name=\"" << fileName << "\""); + return; + } + char inputDataLine[2018]; + + + etk::Vector indicesVertices; + etk::Vector indicesUv; + etk::Vector indicesNormal; + etk::Vector< etk::Vector3D > vertices; + etk::Vector< etk::Vector2D > uvTextures; + etk::Vector< etk::Vector3D > normals; + + + while (NULL != fileName.fGets(inputDataLine, 2048) ) + { + if (inputDataLine[0]=='v') { + if (inputDataLine[1]=='n') { + // Vertice normal : vn 0.000000 0.000000 -1.000000 + etk::Vector3D vertex; + sscanf(&inputDataLine[3], "%f %f %f", &vertex.x, &vertex.y, &vertex.z ); + normals.PushBack(vertex); + } else if (inputDataLine[1]=='t') { + // Texture position : vt 0.748573 0.750412 + etk::Vector2D vertex; + sscanf(&inputDataLine[3], "%f %f", &vertex.x, &vertex.y); + uvTextures.PushBack(vertex); + } else { + // Vertice position : v 1.000000 -1.000000 -1.000000 + etk::Vector3D vertex; + sscanf(&inputDataLine[2], "%f %f %f", &vertex.x, &vertex.y, &vertex.z ); + vertices.PushBack(vertex); + } + } else if (inputDataLine[0]=='f') { + // face : f 5/1/1 1/2/1 4/3/1* + uint32_t vertexIndex[3], uvIndex[3], normalIndex[3]; + int32_t matches = sscanf(&inputDataLine[2], "%d/%d/%d %d/%d/%d %d/%d/%d\n", + &vertexIndex[0], &uvIndex[0], &normalIndex[0], + &vertexIndex[1], &uvIndex[1], &normalIndex[1], + &vertexIndex[2], &uvIndex[2], &normalIndex[2] ); + if (9 != matches){ + EWOL_ERROR("Parsing error in the .obj files : " << fileName); + continue; + } + indicesVertices.PushBack(vertexIndex[0]); + indicesVertices.PushBack(vertexIndex[1]); + indicesVertices.PushBack(vertexIndex[2]); + indicesUv.PushBack(uvIndex[0]); + indicesUv.PushBack(uvIndex[1]); + indicesUv.PushBack(uvIndex[2]); + indicesNormal.PushBack(normalIndex[0]); + indicesNormal.PushBack(normalIndex[1]); + indicesNormal.PushBack(normalIndex[2]); + + } else if (inputDataLine[0]=='s') { + // ??? : s off + } else if (inputDataLine[0]=='#') { + // comment + // nothing to do ... just go to the new line ... + } else if( inputDataLine[0]=='u' + && inputDataLine[1]=='s' + && inputDataLine[2]=='e' + && inputDataLine[3]=='m' + && inputDataLine[4]=='t' + && inputDataLine[5]=='l' ) { + // Use Material : usemtl imageName.xxx + while( inputDataLine[strlen(inputDataLine)-1] == '\n' + || inputDataLine[strlen(inputDataLine)-1] == '\r' + || inputDataLine[strlen(inputDataLine)-1] == ' ') { + if (1 == strlen(inputDataLine) ){ + break; + } + inputDataLine[strlen(inputDataLine)-1] = '\0'; + } + etk::UString tmpVal(&inputDataLine[7]); + etk::Vector2D tmpSize(256, 256); + if (NULL != m_texture1) { + EWOL_INFO("Release previous loaded texture ... "); + ewol::resource::Release(m_texture1); + } + if (false == ewol::resource::Keep(tmpVal, m_texture1, tmpSize)) { + EWOL_ERROR("Can not load specific texture : " << tmpVal); + } + } else if( inputDataLine[0]=='m' + && inputDataLine[1]=='t' + && inputDataLine[2]=='l' + && inputDataLine[3]=='l' + && inputDataLine[4]=='i' + && inputDataLine[5]=='b' ) { + // ???? : mtllib cube.mtl + } + } + fileName.fClose(); + // For each vertex of each triangle + for( uint32_t iii=0; iii +#include +#include + +namespace ewol +{ + class MeshObj : public ewol::Mesh + { + public: + MeshObj(etk::UString fileName); + ~MeshObj(void) { }; + virtual const char* GetType(void) { return "ewol::MeshObj"; }; + }; +}; + + + + + +#endif + diff --git a/Sources/libewol/ewol/Resource.h b/Sources/libewol/ewol/Resource.h index 246b78b7..1fd41bed 100644 --- a/Sources/libewol/ewol/Resource.h +++ b/Sources/libewol/ewol/Resource.h @@ -22,13 +22,13 @@ ******************************************************************************* */ -#ifndef __RESOURCES_H__ -#define __RESOURCES_H__ - #include #include #include +#ifndef __RESOURCES_H__ +#define __RESOURCES_H__ + #define MAX_RESOURCE_LEVEL (5) namespace ewol diff --git a/Sources/libewol/ewol/ResourceManager.cpp b/Sources/libewol/ewol/ResourceManager.cpp index c14ca7ff..502cc7a7 100644 --- a/Sources/libewol/ewol/ResourceManager.cpp +++ b/Sources/libewol/ewol/ResourceManager.cpp @@ -349,6 +349,18 @@ bool ewol::resource::Keep(etk::UString& filename, ewol::TextureFile*& object, et return true; } +bool ewol::resource::Keep(etk::UString& filename, ewol::MeshObj*& object) +{ + // this element create a new one every time .... + object = new ewol::MeshObj(filename); + if (NULL == object) { + EWOL_ERROR("allocation error of a resource : ??Mesh.obj??"); + return false; + } + LocalAdd(object); + return true; +} + bool ewol::resource::Keep(etk::UString& accesMode, ewol::VirtualBufferObject*& object) { @@ -446,3 +458,9 @@ void ewol::resource::Release(ewol::TextureFile*& object) object = NULL; } +void ewol::resource::Release(ewol::MeshObj*& object) +{ + ewol::Resource* object2 = static_cast(object); + Release(object2); + object = NULL; +} diff --git a/Sources/libewol/ewol/ResourceManager.h b/Sources/libewol/ewol/ResourceManager.h index 3ef83574..1045ba82 100644 --- a/Sources/libewol/ewol/ResourceManager.h +++ b/Sources/libewol/ewol/ResourceManager.h @@ -36,6 +36,7 @@ #include #include #include +#include namespace ewol { @@ -63,6 +64,7 @@ namespace ewol bool Keep(ewol::Texture*& object); // no name needed here ... bool Keep(etk::UString& filename, ewol::TextureFile*& object, etk::Vector2D size); bool Keep(etk::UString& accesMode, ewol::VirtualBufferObject*& object); + bool Keep(etk::UString& filename, ewol::MeshObj*& object); void Release(ewol::Resource*& object); void Release(ewol::TexturedFont*& object); @@ -75,6 +77,7 @@ namespace ewol void Release(ewol::Texture*& object); void Release(ewol::TextureFile*& object); void Release(ewol::VirtualBufferObject*& object); + void Release(ewol::MeshObj*& object); } }; diff --git a/Sources/libewol/ewol/texture/TextureFile.cpp b/Sources/libewol/ewol/texture/TextureFile.cpp index bce62ca0..fd22f49f 100644 --- a/Sources/libewol/ewol/texture/TextureFile.cpp +++ b/Sources/libewol/ewol/texture/TextureFile.cpp @@ -38,7 +38,7 @@ ewol::TextureFile::TextureFile(etk::UString genName, etk::UString tmpfileName, e // load data etk::File fileName(tmpfileName, etk::FILE_TYPE_DATA); if (false == fileName.Exist()) { - EWOL_ERROR("File does not Exist ... " << fileName); + EWOL_ERROR("File does not Exist ... " << fileName << " from : " << tmpfileName); } else { // get the upper paw2 ot the size requested... if (size.x>0 && size.y>0) { diff --git a/Sources/libewol/file.mk b/Sources/libewol/file.mk index 216bdd64..689e0e6f 100644 --- a/Sources/libewol/file.mk +++ b/Sources/libewol/file.mk @@ -47,6 +47,11 @@ FILE_LIST+= ewol/font/FontManager.cpp \ ewol/font/TexturedFont.cpp \ ewol/font/DistantFieldFont.cpp +# Mesh management +FILE_LIST+= ewol/Mesh/Mesh.cpp \ + ewol/Mesh/MeshObj.cpp + + # all widgets FILE_LIST+= ewol/widget/Widget.cpp \ ewol/widget/WidgetManager.cpp \