[DEV] Add resource loading .obj files

This commit is contained in:
Edouard DUPIN 2012-10-30 18:20:47 +01:00
parent ad78c67548
commit e1d99c0646
11 changed files with 447 additions and 10 deletions

View File

@ -80,6 +80,6 @@ typedef struct {
float h;
}clipping_ts;
#endif
#include <etk/math/math.h>
#endif

View File

@ -25,16 +25,17 @@
#ifndef __ETK_TYPES_MATRIX_H__
#define __ETK_TYPES_MATRIX_H__
#include <etk/DebugInternal.h>
//#include <etk/DebugInternal.h>
#include <etk/math/Vector2D.h>
#include <etk/Vector.h>
namespace etk {
namespace etk
{
template <typename T> class Matrix
{
private:
Vector2D<int32_t> m_size;
Vector<T> m_data;
etk::Vector2D<int32_t> m_size;
etk::Vector<T> m_data;
public:
/*****************************************************
* Constructor

View File

@ -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 <ewol/Debug.h>
#include <ewol/Mesh/Mesh.h>
#include <ewol/ResourceManager.h>
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
}

View File

@ -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 <etk/Types.h>
#include <ewol/Resource.h>
#include <ewol/texture/TextureFile.h>
#include <ewol/openGL/Shader.h>
#include <ewol/openGL/Program.h>
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<uint32_t> m_indices;
etk::Vector< etk::Vector3D<float> > m_vertices;
etk::Vector< etk::Vector2D<float> > m_uvTextures;
etk::Vector< etk::Vector3D<float> > m_normals;
ewol::TextureFile* m_texture1;
#ifdef __VIDEO__OPENGL_ES_2
etk::Vector<draw::Colorf> m_coordColor; //!< internal color of the different point
#else
etk::Vector<draw::Color> 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

View File

@ -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 <ewol/Debug.h>
#include <etk/Vector.h>
#include <etk/os/File.h>
#include <ewol/Mesh/MeshObj.h>
#include <ewol/ResourceManager.h>
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<int32_t> indicesVertices;
etk::Vector<int32_t> indicesUv;
etk::Vector<int32_t> indicesNormal;
etk::Vector< etk::Vector3D<float> > vertices;
etk::Vector< etk::Vector2D<float> > uvTextures;
etk::Vector< etk::Vector3D<float> > 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<float> 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<float> 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<float> 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<int32_t> 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<indicesVertices.Size(); iii++ ){
// Get the indices of its attributes
uint32_t vertexIndex = indicesVertices[iii];
uint32_t uvIndex = indicesUv[iii];
uint32_t normalIndex = indicesNormal[iii];
// Put the attributes in buffers
m_vertices.PushBack(vertices[vertexIndex-1]);
m_uvTextures.PushBack(uvTextures[uvIndex-1]);
m_normals.PushBack(normals[normalIndex-1]);
draw::Color tmpppp(0xFFFFFFFF);
draw::Colorf tmppppp(tmpppp);
m_coordColor.PushBack(tmppppp);
}
}
/*
// Read our .obj file
std::vector vertices;
std::vector uvs;
std::vector normals; // Won't be used at the moment.
bool res = loadOBJ("cube.obj", vertices, uvs, normals);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
*/

View File

@ -0,0 +1,48 @@
/**
*******************************************************************************
* @file ewol/Mesh/MeshObj.h
* @brief ewol Mesh ;obj loader 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_OBJ_H__
#define __MESH_OBJ_H__
#include <etk/Types.h>
#include <etk/UString.h>
#include <ewol/Mesh/Mesh.h>
namespace ewol
{
class MeshObj : public ewol::Mesh
{
public:
MeshObj(etk::UString fileName);
~MeshObj(void) { };
virtual const char* GetType(void) { return "ewol::MeshObj"; };
};
};
#endif

View File

@ -22,13 +22,13 @@
*******************************************************************************
*/
#ifndef __RESOURCES_H__
#define __RESOURCES_H__
#include <etk/Types.h>
#include <etk/UString.h>
#include <ewol/Debug.h>
#ifndef __RESOURCES_H__
#define __RESOURCES_H__
#define MAX_RESOURCE_LEVEL (5)
namespace ewol

View File

@ -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<ewol::Resource*>(object);
Release(object2);
object = NULL;
}

View File

@ -36,6 +36,7 @@
#include <ewol/font/DistantFieldFont.h>
#include <ewol/texture/Texture.h>
#include <ewol/texture/TextureFile.h>
#include <ewol/Mesh/MeshObj.h>
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<int32_t> 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);
}
};

View File

@ -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) {

View File

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