From c53664d973ac170569a05d892813b34bc6870e9d Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 1 Mar 2013 22:28:14 +0100 Subject: [PATCH] [DEV] try to have better mesh management and subdivision try --- data/textured3D2.frag | 14 +++ data/textured3D2.prog | 2 + data/textured3D2.vert | 18 ++++ sources/ewol/renderer/resources/Mesh.cpp | 73 ++++++++++++++- sources/ewol/renderer/resources/Mesh.h | 18 +++- sources/ewol/renderer/resources/MeshObj.cpp | 99 ++++----------------- sources/file.mk | 4 + 7 files changed, 141 insertions(+), 87 deletions(-) create mode 100644 data/textured3D2.frag create mode 100644 data/textured3D2.prog create mode 100644 data/textured3D2.vert diff --git a/data/textured3D2.frag b/data/textured3D2.frag new file mode 100644 index 00000000..350ae29c --- /dev/null +++ b/data/textured3D2.frag @@ -0,0 +1,14 @@ +#ifdef GL_ES +precision mediump float; +precision mediump int; +#endif + +// Input : +uniform sampler2D EW_texID; + +varying vec2 f_texcoord; + +void main(void) { + gl_FragColor = texture2D(EW_texID, f_texcoord); + //gl_FragColor = vec4(1.0,1.0,0.2,0.6); +} diff --git a/data/textured3D2.prog b/data/textured3D2.prog new file mode 100644 index 00000000..e7330f06 --- /dev/null +++ b/data/textured3D2.prog @@ -0,0 +1,2 @@ +textured3D2.vert +textured3D2.frag \ No newline at end of file diff --git a/data/textured3D2.vert b/data/textured3D2.vert new file mode 100644 index 00000000..54297fa7 --- /dev/null +++ b/data/textured3D2.vert @@ -0,0 +1,18 @@ +#ifdef GL_ES +precision mediump float; +precision mediump int; +#endif +// Input : +attribute vec3 EW_coord3d; +attribute vec2 EW_texture2d; +uniform mat4 EW_MatrixTransformation; + +// output : +varying vec2 f_texcoord; + +void main(void) { + gl_Position = EW_MatrixTransformation * vec4(EW_coord3d, 1.0); + //gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(EW_coord3d, 1.0); + // set texture output coord + f_texcoord = EW_texture2d; +} diff --git a/sources/ewol/renderer/resources/Mesh.cpp b/sources/ewol/renderer/resources/Mesh.cpp index 143b6803..13721244 100644 --- a/sources/ewol/renderer/resources/Mesh.cpp +++ b/sources/ewol/renderer/resources/Mesh.cpp @@ -16,12 +16,12 @@ ewol::Mesh::Mesh(etk::UString genName) : m_numberOfElments(0), m_texture1(NULL) { - etk::UString tmpString("DATA:textured3D.prog"); + etk::UString tmpString("DATA:textured3D2.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_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"); @@ -69,7 +69,7 @@ void ewol::Mesh::Draw(mat4& positionMatrix) // Texture : m_GLprogram->SendAttributePointer(m_GLtexture, 2/*u,v*/, m_verticesVBO, 1); // color : - m_GLprogram->SendAttributePointer(m_GLColor, 4/*r,g,b,a*/, m_verticesVBO, 2); + //m_GLprogram->SendAttributePointer(m_GLColor, 4/*r,g,b,a*/, m_verticesVBO, 2); // Request the draw od the elements : glDrawArrays(GL_TRIANGLES, 0, m_numberOfElments); m_GLprogram->UnUse(); @@ -77,3 +77,70 @@ void ewol::Mesh::Draw(mat4& positionMatrix) glBindBuffer(GL_ARRAY_BUFFER,0); } +void ewol::Mesh::GenerateVBO(void) +{ + m_numberOfElments = 0; + // TODO : Set a better display system, this one is the worst I known ... + for (int32_t iii=0; iiiGetRefBuffer(0).PushBack(tmpPos.x()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.y()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.z()); + m_verticesVBO->GetRefBuffer(1).PushBack(tmpUV.x()); + m_verticesVBO->GetRefBuffer(1).PushBack(1.0f-tmpUV.y()); + + indice = 1; + tmpPos = m_listVertex[m_listFaces[iii].m_vertex[indice]]; + tmpUV = m_listUV[m_listFaces[iii].m_uv[indice]]; + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.x()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.y()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.z()); + m_verticesVBO->GetRefBuffer(1).PushBack(tmpUV.x()); + m_verticesVBO->GetRefBuffer(1).PushBack(1.0f-tmpUV.y()); + + indice = 2; + tmpPos = m_listVertex[m_listFaces[iii].m_vertex[indice]]; + tmpUV = m_listUV[m_listFaces[iii].m_uv[indice]]; + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.x()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.y()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.z()); + m_verticesVBO->GetRefBuffer(1).PushBack(tmpUV.x()); + m_verticesVBO->GetRefBuffer(1).PushBack(1.0f-tmpUV.y()); + + if (m_listFaces[iii].m_nbElement==4) { + indice = 0; + tmpPos = m_listVertex[m_listFaces[iii].m_vertex[indice]]; + tmpUV = m_listUV[m_listFaces[iii].m_uv[indice]]; + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.x()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.y()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.z()); + m_verticesVBO->GetRefBuffer(1).PushBack(tmpUV.x()); + m_verticesVBO->GetRefBuffer(1).PushBack(1.0f-tmpUV.y()); + + indice = 2; + tmpPos = m_listVertex[m_listFaces[iii].m_vertex[indice]]; + tmpUV = m_listUV[m_listFaces[iii].m_uv[indice]]; + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.x()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.y()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.z()); + m_verticesVBO->GetRefBuffer(1).PushBack(tmpUV.x()); + m_verticesVBO->GetRefBuffer(1).PushBack(1.0f-tmpUV.y()); + + indice = 3; + tmpPos = m_listVertex[m_listFaces[iii].m_vertex[indice]]; + tmpUV = m_listUV[m_listFaces[iii].m_uv[indice]]; + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.x()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.y()); + m_verticesVBO->GetRefBuffer(0).PushBack(tmpPos.z()); + m_verticesVBO->GetRefBuffer(1).PushBack(tmpUV.x()); + m_verticesVBO->GetRefBuffer(1).PushBack(1.0f-tmpUV.y()); + } + } + // update all the VBO elements ... + m_verticesVBO->Flush(); +} + diff --git a/sources/ewol/renderer/resources/Mesh.h b/sources/ewol/renderer/resources/Mesh.h index 6a17e107..debe1739 100644 --- a/sources/ewol/renderer/resources/Mesh.h +++ b/sources/ewol/renderer/resources/Mesh.h @@ -16,8 +16,18 @@ #include #include +// note using modify Half-Edge system to store data (modify is for storing UV mapping too +// help on : http://www.flipcode.com/archives/The_Half-Edge_Data_Structure.shtml + namespace ewol { + class Face + { + public: + int32_t m_nbElement; + int32_t m_vertex[4]; + int32_t m_uv[4]; + }; class Mesh : public ewol::Resource { // 3 "float" elements @@ -38,7 +48,12 @@ namespace ewol int32_t m_GLtexID; int32_t m_bufferOfset; int32_t m_numberOfElments; - public: + protected: + etk::Vector m_listVertex; + etk::Vector m_listUV; + etk::Vector m_listFaces; + + public: // For display storage : (not really usable for mathématical division and other ... etk::Vector m_indices; etk::Vector m_vertices; etk::Vector m_uvTextures; @@ -52,6 +67,7 @@ namespace ewol virtual ~Mesh(void); virtual const char* GetType(void) { return "ewol::Mesh"; }; virtual void Draw(mat4& positionMatrix); + void GenerateVBO(void); }; }; diff --git a/sources/ewol/renderer/resources/MeshObj.cpp b/sources/ewol/renderer/resources/MeshObj.cpp index 2568cbd5..12a9429b 100644 --- a/sources/ewol/renderer/resources/MeshObj.cpp +++ b/sources/ewol/renderer/resources/MeshObj.cpp @@ -31,14 +31,6 @@ ewol::MeshObj::MeshObj(etk::UString _fileName) : } char inputDataLine[2048]; - - etk::Vector indicesVertices; - etk::Vector indicesUv; - etk::Vector indicesNormal; - etk::Vector< vec3 > vertices; - etk::Vector< vec2 > uvTextures; - etk::Vector< vec3 > normals; - int32_t lineID = 0; while (NULL != fileName.FileGets(inputDataLine, 2048) ) { @@ -46,19 +38,17 @@ ewol::MeshObj::MeshObj(etk::UString _fileName) : if (inputDataLine[0]=='v') { if (inputDataLine[1]=='n') { // Vertice normal : vn 0.000000 0.000000 -1.000000 - vec3 vertex(0,0,0); - sscanf(&inputDataLine[3], "%f %f %f", &vertex.m_floats[0], &vertex.m_floats[1], &vertex.m_floats[2] ); - normals.PushBack(vertex); + // we did not use normal ==> we recalculated it if needed (some .obj does not export normal, then it is simple like this ... } else if (inputDataLine[1]=='t') { // Texture position : vt 0.748573 0.750412 vec2 vertex(0,0); sscanf(&inputDataLine[3], "%f %f", &vertex.m_floats[0], &vertex.m_floats[1]); - uvTextures.PushBack(vertex); + m_listUV.PushBack(vertex); } else { // Vertice position : v 1.000000 -1.000000 -1.000000 vec3 vertex(0,0,0); sscanf(&inputDataLine[2], "%f %f %f", &vertex.m_floats[0], &vertex.m_floats[1], &vertex.m_floats[2] ); - vertices.PushBack(vertex); + m_listVertex.PushBack(vertex); } } else if (inputDataLine[0]=='f') { // face : f 5/1/1 1/2/1 4/3/1* @@ -95,44 +85,20 @@ ewol::MeshObj::MeshObj(etk::UString _fileName) : } } } + Face tmpFace; + tmpFace.m_nbElement = 3; + tmpFace.m_vertex[0] = vertexIndex[0]; + tmpFace.m_uv[0] = uvIndex[0]; + tmpFace.m_vertex[1] = vertexIndex[1]; + tmpFace.m_uv[1] = uvIndex[1]; + tmpFace.m_vertex[2] = vertexIndex[2]; + tmpFace.m_uv[2] = uvIndex[2]; if (true==quadMode) { - indicesVertices.PushBack(vertexIndex[0]); - indicesVertices.PushBack(vertexIndex[1]); - indicesVertices.PushBack(vertexIndex[2]); - // second triangle - indicesVertices.PushBack(vertexIndex[0]); - indicesVertices.PushBack(vertexIndex[2]); - indicesVertices.PushBack(vertexIndex[3]); - indicesUv.PushBack(uvIndex[0]); - indicesUv.PushBack(uvIndex[1]); - indicesUv.PushBack(uvIndex[2]); - // second triangle - indicesUv.PushBack(uvIndex[0]); - indicesUv.PushBack(uvIndex[2]); - indicesUv.PushBack(uvIndex[3]); - if (12 == matches) { - indicesNormal.PushBack(normalIndex[0]); - indicesNormal.PushBack(normalIndex[1]); - indicesNormal.PushBack(normalIndex[2]); - // second triangle - indicesNormal.PushBack(normalIndex[0]); - indicesNormal.PushBack(normalIndex[2]); - indicesNormal.PushBack(normalIndex[3]); - } - } else { - indicesVertices.PushBack(vertexIndex[0]); - indicesVertices.PushBack(vertexIndex[1]); - indicesVertices.PushBack(vertexIndex[2]); - indicesUv.PushBack(uvIndex[0]); - indicesUv.PushBack(uvIndex[1]); - indicesUv.PushBack(uvIndex[2]); - if (9 == matches) { - indicesNormal.PushBack(normalIndex[0]); - indicesNormal.PushBack(normalIndex[1]); - indicesNormal.PushBack(normalIndex[2]); - } + tmpFace.m_nbElement++; + tmpFace.m_vertex[3] = vertexIndex[3]; + tmpFace.m_uv[3] = uvIndex[3]; } - // TODO : Calculate normal when none is provided ... + m_listFaces.PushBack(tmpFace); } else if (inputDataLine[0]=='s') { // ??? : s off } else if (inputDataLine[0]=='#') { @@ -173,40 +139,7 @@ ewol::MeshObj::MeshObj(etk::UString _fileName) : } } fileName.FileClose(); - // update the number of element in the display ... - m_numberOfElments = indicesVertices.Size(); - // For each vertex of each triangle - for( uint32_t iii=0; iiiGetRefBuffer(0).PushBack(vertices[vertexIndex-1].x()); - m_verticesVBO->GetRefBuffer(0).PushBack(vertices[vertexIndex-1].y()); - m_verticesVBO->GetRefBuffer(0).PushBack(vertices[vertexIndex-1].z()); - m_uvTextures.PushBack(uvTextures[uvIndex-1]); - m_verticesVBO->GetRefBuffer(1).PushBack(uvTextures[uvIndex-1].x()); - m_verticesVBO->GetRefBuffer(1).PushBack(1.0f-uvTextures[uvIndex-1].y()); - draw::Color tmpppp(0xFFFFFFFF); - draw::Colorf tmppppp(tmpppp); - m_coordColor.PushBack(tmppppp); - m_verticesVBO->GetRefBuffer(2).PushBack(tmppppp.r); - m_verticesVBO->GetRefBuffer(2).PushBack(tmppppp.g); - m_verticesVBO->GetRefBuffer(2).PushBack(tmppppp.b); - m_verticesVBO->GetRefBuffer(2).PushBack(tmppppp.a); - - if (indicesNormal.Size()>iii) { - uint32_t normalIndex = indicesNormal[iii]; - m_normals.PushBack(normals[normalIndex-1]); - m_verticesVBO->GetRefBuffer(3).PushBack(normals[normalIndex-1].x()); - m_verticesVBO->GetRefBuffer(3).PushBack(normals[normalIndex-1].y()); - m_verticesVBO->GetRefBuffer(3).PushBack(normals[normalIndex-1].z()); - } - } - // update all the VBO elements ... - m_verticesVBO->Flush(); + GenerateVBO(); } diff --git a/sources/file.mk b/sources/file.mk index 92057f64..b925dfeb 100644 --- a/sources/file.mk +++ b/sources/file.mk @@ -90,6 +90,10 @@ LOCAL_COPY_FILES := ../data/textured3D.prog:textured3D.prog \ ../data/textured3D.frag:textured3D.frag \ ../data/textured3D.vert:textured3D.vert \ \ + ../data/textured3D.prog:textured3D2.prog \ + ../data/textured3D.frag:textured3D2.frag \ + ../data/textured3D.vert:textured3D2.vert \ + \ ../data/color.prog:color.prog \ ../data/color.frag:color.frag \ ../data/color.vert:color.vert \