Commit bac3af1b authored by Sylvain Thery's avatar Sylvain Thery
Browse files

import & render of obj with tex (in texture example)

parent 2c3b7fb1
...@@ -28,18 +28,18 @@ ...@@ -28,18 +28,18 @@
#include "Utils/vbo.h" #include "Utils/vbo.h"
TexView::TexView(): TexView::TexView():
m_render(NULL), m_obj(myMap),
m_positionVBO(NULL), m_positionVBO(NULL),
m_texcoordVBO(NULL), m_texcoordVBO(NULL),
m_nbIndices(0),
m_texture(NULL), m_texture(NULL),
m_shader(NULL), m_shader(NULL),
m_modeMask(false), m_shader2(NULL),
m_fileName("") m_phong(true)
{} {}
TexView::~TexView() TexView::~TexView()
{ {
delete m_render;
delete m_shader; delete m_shader;
delete m_positionVBO; delete m_positionVBO;
delete m_texcoordVBO; delete m_texcoordVBO;
...@@ -51,21 +51,21 @@ void TexView::cb_initGL() ...@@ -51,21 +51,21 @@ void TexView::cb_initGL()
// choose to use GL version 2 // choose to use GL version 2
Utils::GLSLShader::setCurrentOGLVersion(2); Utils::GLSLShader::setCurrentOGLVersion(2);
// create the render
m_render = new Algo::Render::GL2::MapRender();
// create VBO for position // create VBO for position
m_positionVBO = new Utils::VBO; m_positionVBO = new Utils::VBO;
m_texcoordVBO = new Utils::VBO; m_texcoordVBO = new Utils::VBO;
m_normalVBO = new Utils::VBO;
m_texture = new Utils::Texture<2,Geom::Vec3uc>(GL_UNSIGNED_BYTE); m_texture = new Utils::Texture<2,Geom::Vec3uc>(GL_UNSIGNED_BYTE);
computeImage();
m_texture->update();
m_mask = new Utils::Texture<2,float>(GL_FLOAT); if (m_texture->load(m_fileNameTex))
m_mask->create(Geom::Vec2ui(256,256)); m_texture->update();
createMask(8); else
m_mask->update(); computeImage();
// CGoGNerr << "Problem loading image"<< CGoGNendl;
m_texture->setWrapping(GL_CLAMP_TO_EDGE);
m_texture->update();
m_shader = new Utils::ShaderSimpleTexture(); m_shader = new Utils::ShaderSimpleTexture();
m_shader->setAttributePosition(m_positionVBO); m_shader->setAttributePosition(m_positionVBO);
...@@ -74,125 +74,115 @@ void TexView::cb_initGL() ...@@ -74,125 +74,115 @@ void TexView::cb_initGL()
m_shader->setTexture(m_texture); m_shader->setTexture(m_texture);
registerShader(m_shader); registerShader(m_shader);
m_shader2 = new Utils::ShaderTextureMask(); m_shader2 = new Utils::ShaderPhongTexture();
m_shader2->setAttributePosition(m_positionVBO); m_shader2->setAttributePosition(m_positionVBO);
m_shader2->setAttributeTexCoord(m_texcoordVBO); m_shader2->setAttributeTexCoord(m_texcoordVBO);
m_shader2->setTextureUnits(GL_TEXTURE0,GL_TEXTURE1); m_shader2->setAttributeNormal(m_normalVBO);
m_shader2->setTextures(m_texture,m_mask); m_shader2->setTextureUnit(GL_TEXTURE1);
m_shader2->setTexture(m_texture);
m_shader2->setShininess(10.0f);
m_shader2->setAmbient(0.1f);
m_shader2->setSpecular(Geom::Vec4f(0.5));
registerShader(m_shader2); registerShader(m_shader2);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES); if (!m_obj.hasNormals())
{
VertexAttribute<Geom::Vec3f> normal = myMap.getAttribute<VEC3, VERTEX>("normal") ;
if(!normal.isValid())
normal = myMap.addAttribute<VEC3, VERTEX>("normal") ;
Algo::Surface::Geometry::computeNormalVertices<PFP>(myMap, m_obj.m_positions, normal) ;
m_obj.setNormalAttribute(normal);
}
m_nbIndices = m_obj.createSimpleVBO_PTN(m_positionVBO,m_texcoordVBO,m_normalVBO);
} }
void TexView::cb_redraw() void TexView::cb_redraw()
{ {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_LIGHTING);
if (m_shader) if (m_phong)
{ {
if (m_modeMask) m_shader2->activeTexture();
{ m_shader2->enableVertexAttribs();
m_shader2->activeTextures(); glDrawArrays(GL_TRIANGLES, 0, m_nbIndices);
m_render->draw(m_shader2, Algo::Render::GL2::TRIANGLES); m_shader2->disableVertexAttribs();
}
else
{
m_shader->activeTexture();
m_render->draw(m_shader, Algo::Render::GL2::TRIANGLES);
}
} }
else
{
m_shader->activeTexture();
m_shader->enableVertexAttribs();
glDrawArrays(GL_TRIANGLES, 0, m_nbIndices);
m_shader->disableVertexAttribs();
}
} }
void TexView::cb_keyPress(int code) void TexView::cb_keyPress(int code)
{ {
switch(code) switch(code)
{ {
case 'p':
m_phong = !m_phong;
case 'l': case 'l':
m_texture->setFiltering(GL_LINEAR); m_texture->setFiltering(GL_LINEAR);
break; break;
case 'n': case 'n':
m_texture->setFiltering(GL_NEAREST); m_texture->setFiltering(GL_NEAREST);
break; break;
case 'm': case '1':
m_modeMask = !m_modeMask; m_shader2->setShininess(10.0f);
if (m_modeMask)
{
createMask(16);
m_mask->update();
}
break; break;
case 'M': case '2':
m_modeMask = !m_modeMask; m_shader2->setShininess(100.0f);
if (m_modeMask)
{
createMask(8);
m_mask->update();
}
break; break;
case 's': case '3':
m_texture->subSample2<Geom::Vec3d>(); m_shader2->setShininess(500.0f);
m_texture->update();
break; break;
case 't': case '4':
m_texture->rotate90(3); m_shader2->setShininess(25000.0f);
m_texture->update();
break; break;
case '5':
m_shader2->setShininess(1.0f);
case 'r': m_shader2->setSpecular(Geom::Vec4f(0));
m_texture->load(m_fileName);
m_texture->update();
break; break;
} }
updateGL(); updateGL();
} }
void TexView::cb_Open() void TexView::init(char *fnm, char* fnt)
{ {
std::string filename = selectFile("Open Image","/tmp"); if (fnm == NULL)
if (!filename.empty())
{ {
m_fileName = filename; computeTore();
if (m_texture->load(filename)) m_fileNameTex = std::string(fnt);
{
m_texture->update();
updateGL();
}
else
CGoGNerr << "Problem loading image"<< CGoGNendl;
} }
else else
{ {
computeImage(); m_fileNameMesh = std::string(fnm);
m_texture->update(); m_fileNameTex = std::string(fnt);
updateGL(); std::vector<std::string> attrNames;
m_obj.import(m_fileNameMesh,attrNames);
} }
}
void TexView::createMask(unsigned int nb) Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, m_obj.m_positions);
{ float lWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
if (nb ==0) Geom::Vec3f lPosObj = (bb.min() + bb.max()) / PFP::REAL(2);
return;
unsigned int sz0 = m_mask->size()[0]/nb; // send BB info to interface for centering on GL screen
unsigned int sz1 = m_mask->size()[1]/nb; setParamObject(lWidthObj, lPosObj.data());
for (unsigned int j=0; j<m_mask->size()[1]; ++j)
{
for (unsigned int i=0; i<m_mask->size()[0]; ++i)
{
bool b1 = (i/sz0)%2 ==0;
bool b2 = (j/sz1)%2 ==0;
if (b1!=b2)
(*m_mask)(i,j)=1.0f;
else
(*m_mask)(i,j)= 0.0f;
}
}
} }
void TexView::computeImage() void TexView::computeImage()
{ {
std::vector<Geom::Vec3f> colorTable; std::vector<Geom::Vec3f> colorTable;
...@@ -219,23 +209,15 @@ void TexView::computeImage() ...@@ -219,23 +209,15 @@ void TexView::computeImage()
#undef WIDTHCHECKER #undef WIDTHCHECKER
} }
int main(int argc, char**argv) void TexView::computeTore()
{ {
// interface:
QApplication app(argc, argv);
TexView tv;
PFP::MAP& m = tv.myMap;
VertexAttribute<VEC3> position = m.addAttribute<VEC3, VERTEX>("position");
VertexAttribute<Geom::Vec2f> texcoord = m.addAttribute<Geom::Vec2f, VERTEX>("texcoord");
#define NB 96 #define NB 96
Algo::Surface::Modelisation::Polyhedron<PFP> prim(m, position); VertexAttribute<VEC3> position = myMap.addAttribute<VEC3, VERTEX>("position");
VertexAttribute<Geom::Vec2f> texcoord = myMap.addAttribute<Geom::Vec2f, VERTEX>("texcoord");
Algo::Surface::Modelisation::Polyhedron<PFP> prim(myMap, position);
prim.tore_topo(NB, NB); prim.tore_topo(NB, NB);
prim.embedTore(40.0f,20.0f); prim.embedTore(40.0f,20.0f);
Dart d = prim.getDart(); Dart d = prim.getDart();
for(unsigned int i=0; i<NB; ++i) for(unsigned int i=0; i<NB; ++i)
{ {
...@@ -253,30 +235,42 @@ int main(int argc, char**argv) ...@@ -253,30 +235,42 @@ int main(int argc, char**argv)
b =(2.0f/NB)*(NB-j); b =(2.0f/NB)*(NB-j);
texcoord[d] = Geom::Vec2f(a,b); texcoord[d] = Geom::Vec2f(a,b);
d = m.phi<121>(d); d = myMap.phi<121>(d);
} }
d = m.phi<211>(d); d = myMap.phi<211>(d);
} }
#undef NB #undef NB
// bounding box m_obj.setPositionAttribute(position);
Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(m, position); m_obj.setTexCoordAttribute(texcoord);
float lWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2)); }
Geom::Vec3f lPosObj = (bb.min() + bb.max()) / PFP::REAL(2);
// envoit info BB a l'interface
tv.setParamObject(lWidthObj, lPosObj.data());
// show 1 pour GL context
tv.show();
// update des VBO (position et texture coord)
tv.m_positionVBO->updateData(position);
tv.m_texcoordVBO->updateData(texcoord);
// show final pour premier redraw
int main(int argc, char**argv)
{
// interface:
QApplication app(argc, argv);
TexView tv;
if (argc == 3)
{
tv.init(argv[1], argv[2]);
}
else if (argc == 2)
{
tv.init(NULL, argv[1]);
}
else
{
tv.init(NULL,"x");
}
tv.show(); tv.show();
// et on attend la fin. // et on attend la fin.
return app.exec(); return app.exec();
} }
...@@ -30,17 +30,17 @@ ...@@ -30,17 +30,17 @@
#include "Utils/Qt/qtSimple.h" #include "Utils/Qt/qtSimple.h"
#include "Utils/textures.h" #include "Utils/textures.h"
#include "Utils/Shaders/shaderSimpleTexture.h" #include "Utils/Shaders/shaderSimpleTexture.h"
#include "Utils/Shaders/shaderTextureMask.h" #include "Utils/Shaders/shaderPhongTexture.h"
#include "Topology/generic/parameters.h" #include "Topology/generic/parameters.h"
#include "Topology/map/embeddedMap2.h" #include "Topology/map/embeddedMap2.h"
#include "Algo/Render/GL2/mapRender.h" #include "Algo/Render/GL2/mapRender.h"
#include "Algo/Import/importObjTex.h"
// forward definitions (minimize includes) // forward definitions (minimize includes)
namespace CGoGN { namespace Algo { namespace Render { namespace GL2 { class MapRender; }}}} namespace CGoGN { namespace Algo { namespace Render { namespace GL2 { class MapRender; }}}}
namespace CGoGN { namespace Utils { class VBO; } } namespace CGoGN { namespace Utils { class VBO; } }
using namespace CGoGN ; using namespace CGoGN ;
struct PFP: public PFP_STANDARD struct PFP: public PFP_STANDARD
...@@ -59,36 +59,38 @@ class TexView: public Utils::QT::SimpleQT ...@@ -59,36 +59,38 @@ class TexView: public Utils::QT::SimpleQT
{ {
Q_OBJECT Q_OBJECT
protected: protected:
void createMask(unsigned int nb);
void computeImage(); void computeImage();
void computeTore();
public: public:
MAP myMap ; MAP myMap ;
Algo::Surface::Import::OBJModel<PFP> m_obj;
// render
Algo::Render::GL2::MapRender* m_render;
// VBO // VBO
Utils::VBO* m_positionVBO; Utils::VBO* m_positionVBO;
Utils::VBO* m_texcoordVBO; Utils::VBO* m_texcoordVBO;
Utils::VBO* m_normalVBO;
unsigned int m_nbIndices;
Utils::Texture<2,Geom::Vec3uc>* m_texture; Utils::Texture<2,Geom::Vec3uc>* m_texture;
Utils::Texture<2,float>* m_mask;
//2 shaders // shader simple texture
Utils::ShaderSimpleTexture* m_shader; Utils::ShaderSimpleTexture* m_shader;
Utils::ShaderTextureMask* m_shader2; Utils::ShaderPhongTexture* m_shader2;
//with mask or not bool m_phong;
bool m_modeMask;
/// filename of last loaded texture /// filename of loaded mesh
std::string m_fileName; std::string m_fileNameMesh;
/// filename of loaded texture
std::string m_fileNameTex;
TexView(); TexView();
~TexView(); ~TexView();
void init(char* fnm, char* fnt);
// callbacks of simpleQT to overdefine: // callbacks of simpleQT to overdefine:
void cb_redraw(); void cb_redraw();
...@@ -96,7 +98,6 @@ public: ...@@ -96,7 +98,6 @@ public:
void cb_keyPress(int code); void cb_keyPress(int code);
void cb_Open();
}; };
#endif #endif
...@@ -70,6 +70,12 @@ protected: ...@@ -70,6 +70,12 @@ protected:
/// read face line with different indices v v/t v//n v/t/n /// read face line with different indices v v/t v//n v/t/n
short readObjLine(std::stringstream& oss, std::vector<unsigned int>& indices); short readObjLine(std::stringstream& oss, std::vector<unsigned int>& indices);
unsigned int m_tagV ;
unsigned int m_tagVT ;
unsigned int m_tagVN ;
unsigned int m_tagG ;
unsigned int m_tagF ;
public: public:
/// marker for special vertices (with several normals & tex coords) /// marker for special vertices (with several normals & tex coords)
...@@ -97,6 +103,31 @@ public: ...@@ -97,6 +103,31 @@ public:
*/ */
OBJModel(typename PFP::MAP& map); OBJModel(typename PFP::MAP& map);
/**
* @brief set position attribute
* @param position attribute
*/
void setPositionAttribute(VertexAttribute<Geom::Vec3f> position);
/**
* @brief set position attribute
* @param position attribute
*/
void setNormalAttribute(VertexAttribute<Geom::Vec3f> normal);
/**
* @brief set texture coordinate attribute
* @param texcoord attribute
*/
void setTexCoordAttribute(VertexAttribute<Geom::Vec2f>texcoord);
bool hasTexCoords() const { return m_tagVT!=0; }
bool hasNormals() const { return m_tagVN!=0; }
bool hasGroups() const { return m_tagG!=0; }
/** /**
* @brief import * @brief import
* @param filename * @param filename
......
...@@ -38,7 +38,9 @@ namespace Import ...@@ -38,7 +38,9 @@ namespace Import
template <typename PFP> template <typename PFP>
OBJModel<PFP>::OBJModel(typename PFP::MAP& map): OBJModel<PFP>::OBJModel(typename PFP::MAP& map):
m_map(map),m_specialVertices(map),m_dirtyEdges(map) m_map(map),
m_tagV(0),m_tagVT(0),m_tagVN(0),m_tagG(0),m_tagF(0),
m_specialVertices(map),m_dirtyEdges(map)
{ {
} }
...@@ -73,6 +75,25 @@ std::vector<std::string>& OBJModel<PFP>::getMaterialNames() ...@@ -73,6 +75,25 @@ std::vector<std::string>& OBJModel<PFP>::getMaterialNames()
return m_materialNames; return m_materialNames;
} }
template <typename PFP>
void OBJModel<PFP>::setPositionAttribute(VertexAttribute<Geom::Vec3f> position)
{
m_positions = position;
}
template <typename PFP>
void OBJModel<PFP>::setNormalAttribute(VertexAttribute<Geom::Vec3f> normal)
{
m_normals = normal;
}
template <typename PFP>
void OBJModel<PFP>::setTexCoordAttribute(VertexAttribute<Geom::Vec2f>texcoord)
{
m_texCoords = texcoord;
}
template <typename PFP> template <typename PFP>
...@@ -337,10 +358,22 @@ unsigned int OBJModel<PFP>::createSimpleVBO_PT(Utils::VBO* positionVBO, Utils::V ...@@ -337,10 +358,22 @@ unsigned int OBJModel<PFP>::createSimpleVBO_PT(Utils::VBO* positionVBO, Utils::V
template <typename