[DEV] try to have better mesh management and subdivision try

This commit is contained in:
Edouard DUPIN 2013-03-01 22:28:14 +01:00
parent 4872bfcbf9
commit c53664d973
7 changed files with 141 additions and 87 deletions

14
data/textured3D2.frag Normal file
View File

@ -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);
}

2
data/textured3D2.prog Normal file
View File

@ -0,0 +1,2 @@
textured3D2.vert
textured3D2.frag

18
data/textured3D2.vert Normal file
View File

@ -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;
}

View File

@ -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; iii<m_listFaces.Size() ; iii++) {
m_numberOfElments += m_listFaces[iii].m_nbElement;
// 2 possibilities : triangle or quad :
int32_t indice = 0;
vec3 tmpPos = m_listVertex[m_listFaces[iii].m_vertex[indice]];
vec2 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 = 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();
}

View File

@ -16,8 +16,18 @@
#include <ewol/renderer/resources/Program.h>
#include <ewol/renderer/resources/VirtualBufferObject.h>
// 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<vec3> m_listVertex;
etk::Vector<vec2> m_listUV;
etk::Vector<Face> m_listFaces;
public: // For display storage : (not really usable for mathématical division and other ...
etk::Vector<uint32_t> m_indices;
etk::Vector<vec3> m_vertices;
etk::Vector<vec2> 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);
};
};

View File

@ -31,14 +31,6 @@ ewol::MeshObj::MeshObj(etk::UString _fileName) :
}
char inputDataLine[2048];
etk::Vector<int32_t> indicesVertices;
etk::Vector<int32_t> indicesUv;
etk::Vector<int32_t> 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; iii<indicesVertices.Size(); iii++ ){
// Get the indices of its attributes
uint32_t vertexIndex = indicesVertices[iii];
uint32_t uvIndex = indicesUv[iii];
// Put the attributes in buffers
m_vertices.PushBack(vertices[vertexIndex-1]);
m_verticesVBO->GetRefBuffer(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();
}

View File

@ -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 \