Commit b217b3af authored by Sylvain Thery's avatar Sylvain Thery

remove BB of groups in OBJ import

parent a8fb63ca
......@@ -35,7 +35,8 @@ ObjView::ObjView():
m_texcoordVBO(NULL),
m_shader(NULL),
m_shader2(NULL),
m_RenderStyle(2)
m_RenderStyle(2),
m_currentGroupDrawn(0xffffffff)
{}
ObjView::~ObjView()
......@@ -69,6 +70,23 @@ void ObjView::cb_keyPress(int k)
case 't':
m_RenderStyle=2;
break;
case 'o':
m_currentGroupDrawn=0xffffffff;
break;
case 'O':
m_currentGroupDrawn=0;
std::cout << "Group "<< m_obj.objGroupName(m_currentGroupDrawn)<< std::endl;
break;
case '-':
m_currentGroupDrawn--;
std::cout << "Group "<< m_obj.objGroupName(m_currentGroupDrawn)<< std::endl;
break;
case '+':
m_currentGroupDrawn++;
std::cout << "Group "<< m_obj.objGroupName(m_currentGroupDrawn)<< std::endl;
break;
default:
break;
}
......@@ -81,6 +99,8 @@ void ObjView::cb_initGL()
// choose to use GL version 2
Utils::GLSLShader::setCurrentOGLVersion(2);
m_dr = new Utils::Drawer;
// create VBO for position
m_positionVBO = new Utils::VBO;
m_texcoordVBO = new Utils::VBO;
......@@ -105,22 +125,130 @@ void ObjView::cb_initGL()
m_obj.createGroupMatVBO_PTN(m_positionVBO,m_texcoordVBO,m_normalVBO);
}
void ObjView::drawBB( const Geom::BoundingBox<VEC3>& bb)
{
const VEC3& mi = bb.min();
const VEC3& ma = bb.max();
m_dr->newList(GL_COMPILE_AND_EXECUTE);
m_dr->lineWidth(3.0f);
m_dr->begin(GL_LINE_LOOP);
m_dr->color3f(1.0f,1.0f,0.0f);
m_dr->vertex3f(mi[0],mi[1],mi[2]);
m_dr->vertex3f(ma[0],mi[1],mi[2]);
m_dr->vertex3f(ma[0],ma[1],mi[2]);
m_dr->vertex3f(mi[0],ma[1],mi[2]);
m_dr->end();
m_dr->begin(GL_LINE_LOOP);
m_dr->color3f(1.0f,1.0f,0.0f);
m_dr->vertex3f(mi[0],mi[1],ma[2]);
m_dr->vertex3f(ma[0],mi[1],ma[2]);
m_dr->vertex3f(ma[0],ma[1],ma[2]);
m_dr->vertex3f(mi[0],ma[1],ma[2]);
m_dr->end();
m_dr->begin(GL_LINES);
m_dr->color3f(1.0f,1.0f,0.0f);
m_dr->vertex3f(mi[0],mi[1],mi[2]);
m_dr->vertex3f(mi[0],mi[1],ma[2]);
m_dr->vertex3f(mi[0],ma[1],mi[2]);
m_dr->vertex3f(mi[0],ma[1],ma[2]);
m_dr->vertex3f(ma[0],ma[1],mi[2]);
m_dr->vertex3f(ma[0],ma[1],ma[2]);
m_dr->vertex3f(ma[0],mi[1],mi[2]);
m_dr->vertex3f(ma[0],mi[1],ma[2]);
m_dr->end();
m_dr->endList();
}
void ObjView::cb_redraw()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
unsigned int nb = m_obj.nbMatGroups();
const std::vector<Algo::Surface::Import::MaterialOBJ*>& mats = m_obj.getMaterials();
unsigned int nb = m_obj.nbObjGroups();
// const std::vector<Algo::Surface::Import::MaterialOBJ*>& mats = m_obj.getMaterials();
if (m_currentGroupDrawn<(nb-1))
{
std::cout << "Draw Object "<< m_currentGroupDrawn << std::endl;
unsigned int i = m_currentGroupDrawn;
drawBB(m_obj.getGroupBB(i));
std::cout << "BB drawn" << std::endl;
switch(m_RenderStyle)
{
case 0: // MONO
for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
{
Geom::Vec4f v(0.2f,1.0f,0.4f,0.0f); // color here green
m_phongShader->setAmbiant(0.2f*v) ;
m_phongShader->setDiffuse(v) ;
m_phongShader->setSpecular(v) ;
m_phongShader->setShininess(10000.0) ;
m_phongShader->enableVertexAttribs();
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
m_phongShader->disableVertexAttribs();
}
break;
case 1: // COLOR
for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
{
const Algo::Surface::Import::MaterialOBJ* mat = m_obj.materialOf(i,j);
Geom::Vec4f v(mat->diffuseColor[0],mat->diffuseColor[1],mat->diffuseColor[2],0.0f);
m_phongShader->setAmbiant(0.2f*v) ;
m_phongShader->setDiffuse(v) ;
m_phongShader->setSpecular(v) ;
m_phongShader->setShininess(10000.0) ;
m_phongShader->enableVertexAttribs();
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
m_phongShader->disableVertexAttribs();
}
default:
for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
{
const Algo::Surface::Import::MaterialOBJ* mat = m_obj.materialOf(i,j);
if (mat->textureDiffuse != NULL)
{
m_shader2->setTexture(mat->textureDiffuse);
m_shader2->setShininess(mat->shininess);
m_shader2->setAmbient(0.8f);
m_shader2->activeTexture();
m_shader2->enableVertexAttribs();
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
m_shader2->disableVertexAttribs();
}
else
{
Geom::Vec4f v;
v[0] = 0.5f; v[1] = 0.5f; v[2] = 0.5f; v[3] = 0.0f;
m_phongShader->setAmbiant(v) ;
v[0] = mat->diffuseColor[0]; v[1] = mat->diffuseColor[1]; v[2] = mat->diffuseColor[2]; v[3] = 0.0f;
m_phongShader->setDiffuse(v) ;
v[0] = mat->specularColor[0]; v[1] = mat->specularColor[1]; v[2] = mat->specularColor[2]; v[3] = 0.0f;
m_phongShader->setSpecular(v) ;
m_phongShader->setShininess(mat->shininess) ;
m_phongShader->enableVertexAttribs();
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
m_phongShader->disableVertexAttribs();
}
}
}
}
else
for (unsigned int i=0; i<nb; ++i)
{
drawBB(m_obj.getGroupBB(i));
switch(m_RenderStyle)
{
case 0: // MONO
{
for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
{
Geom::Vec4f v(0.2f,1.0f,0.4f,0.0f); // color here green
m_phongShader->setAmbiant(0.2f*v) ;
m_phongShader->setDiffuse(v) ;
......@@ -128,33 +256,39 @@ void ObjView::cb_redraw()
m_phongShader->setSpecular(v) ;
m_phongShader->setShininess(10000.0) ;
m_phongShader->enableVertexAttribs();
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i));
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
m_phongShader->disableVertexAttribs();
}
break;
case 1: // COLOR
{
Geom::Vec4f v(mats[i]->diffuseColor[0],mats[i]->diffuseColor[1],mats[i]->diffuseColor[2],0.0f);
for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
{
const Algo::Surface::Import::MaterialOBJ* mat = m_obj.materialOf(i,j);
Geom::Vec4f v(mat->diffuseColor[0],mat->diffuseColor[1],mat->diffuseColor[2],0.0f);
m_phongShader->setAmbiant(0.2f*v) ;
m_phongShader->setDiffuse(v) ;
// v[0] = 1.0f; v[1] = 1.0f; v[2] = 1.0f; v[3] = 0.0f; // use this for specular effected
m_phongShader->setSpecular(v) ;
m_phongShader->setShininess(10000.0) ;
m_phongShader->enableVertexAttribs();
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i));
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
m_phongShader->disableVertexAttribs();
}
default:
{
if (mats[i]->textureDiffuse != NULL)
for (unsigned int j=0; j<m_obj.nbSubGroup(i);++j)
{
const Algo::Surface::Import::MaterialOBJ* mat = m_obj.materialOf(i,j);
if (mat->textureDiffuse != NULL)
{
m_shader2->setTexture(mats[i]->textureDiffuse);
m_shader2->setShininess(mats[i]->shininess);
m_shader2->setTexture(mat->textureDiffuse);
m_shader2->setShininess(mat->shininess);
m_shader2->setAmbient(0.8f);
m_shader2->activeTexture();
m_shader2->enableVertexAttribs();
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i));
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
m_shader2->disableVertexAttribs();
}
else
......@@ -162,13 +296,13 @@ void ObjView::cb_redraw()
Geom::Vec4f v;
v[0] = 0.5f; v[1] = 0.5f; v[2] = 0.5f; v[3] = 0.0f;
m_phongShader->setAmbiant(v) ;
v[0] = mats[i]->diffuseColor[0]; v[1] = mats[i]->diffuseColor[1]; v[2] = mats[i]->diffuseColor[2]; v[3] = 0.0f;
v[0] = mat->diffuseColor[0]; v[1] = mat->diffuseColor[1]; v[2] = mat->diffuseColor[2]; v[3] = 0.0f;
m_phongShader->setDiffuse(v) ;
v[0] = mats[i]->specularColor[0]; v[1] = mats[i]->specularColor[1]; v[2] = mats[i]->specularColor[2]; v[3] = 0.0f;
v[0] = mat->specularColor[0]; v[1] = mat->specularColor[1]; v[2] = mat->specularColor[2]; v[3] = 0.0f;
m_phongShader->setSpecular(v) ;
m_phongShader->setShininess(mats[i]->shininess) ;
m_phongShader->setShininess(mat->shininess) ;
m_phongShader->enableVertexAttribs();
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i), m_obj.nbIndices(i));
glDrawArrays(GL_TRIANGLES, m_obj.beginIndex(i,j), m_obj.nbIndices(i,j));
m_phongShader->disableVertexAttribs();
}
}
......
......@@ -30,6 +30,7 @@
//#include "Utils/Qt/qtSimple.h"
#include "Utils/Qt/qtQGLV.h"
#include "Utils/textures.h"
#include "Utils/drawer.h"
#include "Utils/Shaders/shaderSimpleTexture.h"
#include "Utils/Shaders/shaderPhongTexture.h"
#include "Utils/Shaders/shaderPhong.h"
......@@ -66,6 +67,10 @@ public:
MAP myMap ;
Algo::Surface::Import::OBJModel<PFP> m_obj;
Utils::Drawer* m_dr;
unsigned int m_currentGroupDrawn;
void drawBB( const Geom::BoundingBox<VEC3>& bb);
// VBO
Utils::VBO* m_positionVBO;
Utils::VBO* m_normalVBO;
......
......@@ -91,11 +91,18 @@ class OBJModel
protected:
typename PFP::MAP& m_map;
// infof of sub-groups (group/material)
std::vector<unsigned int> m_beginIndices;
std::vector<unsigned int> m_nbIndices;
std::vector<unsigned int> m_groupIdx;
std::vector<unsigned int> m_sgMat;
std::vector<unsigned int> m_objGroups;
std::vector<unsigned int> m_groupFirstSub;
std::vector<unsigned int> m_groupNbSub;
std::vector<std::string> m_groupNames;
std::vector< Geom::BoundingBox<VEC3> > m_groupBBs;
......@@ -123,7 +130,7 @@ protected:
unsigned int m_tagF ;
void updateGroups(const std::vector<Geom::Vec3f>& pos);
void computeBB(const std::vector<Geom::Vec3f>& pos);
public:
......@@ -256,41 +263,67 @@ public:
* @brief nb group of indices created by createGroupMatVBO_XXX
* @return
*/
unsigned int nbMatGroups() { return m_beginIndices.size(); }
// unsigned int nbMatGroups() { return m_beginIndices.size(); }
/**
* @brief number of sub-group in group
* @param grp id of group
* @return
*/
inline unsigned int nbSubGroup(unsigned int grp) const { return m_groupNbSub[grp];}
/**
* @brief get the begin index of each group in VBOs (for glDrawArrays)
* @brief get the begin index of a sub-group in VBOs (for glDrawArrays)
* @param i id of group
* @param j id of subgroup in group
* @return begin index
*/
unsigned int beginIndex(unsigned int i) const { return m_beginIndices[i]; }
inline unsigned int beginIndex(unsigned int i, unsigned int j) const { return m_beginIndices[ m_groupFirstSub[i]+j ]; }
/**
* @brief get the number of indices of each group in VBOs (for glDrawArrays)
* @brief get the number of indices of a sub-group in VBOs (for glDrawArrays)
* @param i id of group
* @param j id of subgroup in group
* @return number of indices
*/
unsigned int nbIndices(unsigned int i) const { return m_nbIndices[i]; }
inline unsigned int nbIndices(unsigned int i, unsigned int j) const { return m_nbIndices[ m_groupFirstSub[i]+j ]; }
/**
* @brief get the id of group in OBJ file
* @brief material id of a sub-group
* @param i id of group
* @param j id of subgroup in group
* @return id of material
*/
inline unsigned int materialIdOf(unsigned int i, unsigned int j) const { return m_sgMat[ m_groupFirstSub[i]+j ]; }
/**
* @brief material of a sub-group
* @param i id of group
* @param j id of subgroup in group
* @return material ptr
*/
inline const MaterialOBJ* materialOf(unsigned int i, unsigned int j) const { return m_materials[materialIdOf(i,j)]; }
/**
* @brief get the id of group in OBJ file os sub-group
* @param i id of sub-group
* @return obj group index
*/
unsigned int groupIdx(unsigned int i) const { return m_groupIdx[i]; }
inline unsigned int groupIdx(unsigned int i) const { return m_groupIdx[i]; }
/**
* @brief get the number of groups in OBJ file
* @return number of groups
*/
unsigned int nbObjGroups() { return m_objGroups.size()-1; }
unsigned int nbObjGroups() { return m_groupFirstSub.size(); }
/**
* @brief get the index of first group mat of obj
* @param i id of obj group
* @return id of first group mat
*/
unsigned int objGroup(unsigned int i) const { return m_objGroups[i]; }
// unsigned int objGroup(unsigned int i) const { return m_objGroups[i]; }
const Geom::BoundingBox<VEC3>& getGroupBB(unsigned int i) const { return m_groupBBs[i];}
......
......@@ -631,23 +631,145 @@ unsigned int OBJModel<PFP>::createSimpleVBO_PTN(Utils::VBO* positionVBO, Utils::
}
template <typename PFP>
bool OBJModel<PFP>::createGroupMatVBO_P(Utils::VBO* positionVBO)
bool OBJModel<PFP>::createGroupMatVBO_P( Utils::VBO* positionVBO)
{
m_beginIndices.clear();
m_nbIndices.clear();
std::vector<Geom::Vec3f> posBuff;
posBuff.reserve(16384);
if (!m_normals.isValid())
{
CGoGNerr << "no normal attribute "<< CGoGNendl;
return false;
}
if (!m_texCoords.isValid())
{
CGoGNerr << "no tex coords attribute "<< CGoGNendl;
return false;
}
std::vector< std::vector<Dart> > group_faces; //(m_materialNames.size());
group_faces.reserve(16384);
m_groupIdx.reserve(16384);
m_sgMat.reserve(16384);
unsigned int c_sg=0;
group_faces.resize(1);
std::vector< std::vector<Dart> > group_faces(m_materialNames.size());
TraversorF<typename PFP::MAP> traf(m_map);
for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
Dart d=traf.begin();
unsigned int c_grp = m_groups[d];
unsigned int c_mat = m_attMat[d];
m_sgMat.push_back(c_mat);
m_groupFirstSub.push_back(0);
if (m_tagG != 0)
m_groupIdx.push_back(c_grp);
else
m_groupIdx.push_back(0);
while (d!= traf.end())
{
unsigned int g = m_attMat[d];
group_faces[g].push_back(d);
if ((m_groups[d] != c_grp) || (m_attMat[d] != c_mat))
{
c_sg++;
if (m_groups[d] != c_grp)
{
m_groupNbSub.push_back(c_sg-m_groupFirstSub.back());
m_groupFirstSub.push_back(c_sg);
}
c_grp = m_groups[d];
c_mat = m_attMat[d];
m_sgMat.push_back(c_mat);
if (m_tagG != 0)
m_groupIdx.push_back(c_grp);
else
m_groupIdx.push_back(0);
group_faces.resize(c_sg+1);
}
group_faces[c_sg].push_back(d);
d = traf.next();
}
m_groupNbSub.push_back(c_sg+1-m_groupFirstSub.back()); // nb sub-group of last group
// merging same material sub-groups of same group
for (unsigned int g=0; g<m_groupNbSub.size(); ++g)
{
unsigned int fsg = m_groupFirstSub[g];
unsigned int lsg = m_groupFirstSub[g]+m_groupNbSub[g]-1;
for (unsigned int s=fsg; s<=lsg; ++s)
{
if (m_sgMat[s] != 0xffffffff)
{
for (unsigned ss=s+1; ss<=lsg; ++ss)
{
if (m_sgMat[ss] == m_sgMat[s])
{
group_faces[s].insert(group_faces[s].end(),group_faces[ss].begin(),group_faces[ss].end());
group_faces[ss].clear();
m_sgMat[ss] = 0xffffffff;
}
}
}
}
}
// compact group_faces/m_groupIdx/m_sgMat
unsigned int outSg=0;
for (unsigned int inSg=0; inSg<m_sgMat.size(); ++inSg)
{
if (m_sgMat[inSg] != 0xffffffff)
{
if (outSg != inSg)
{
group_faces[outSg].swap(group_faces[inSg]);
m_groupIdx[outSg] = m_groupIdx[inSg];
m_sgMat[outSg] = m_sgMat[inSg];
}
outSg++;
}
}
group_faces.resize(outSg);
m_groupIdx.resize(outSg);
m_sgMat.resize(outSg);
// recreate m_groupFirstSub & m_groupNbSub
unsigned int outGr=0;
m_groupFirstSub[0] = m_groupIdx[0];
for (unsigned int inSg=1; inSg<m_sgMat.size(); ++inSg)
{
if (m_groupIdx[inSg] != m_groupIdx[inSg-1])
{
m_groupNbSub[outGr] = inSg - m_groupFirstSub[outGr];
outGr++;
m_groupFirstSub[outGr] = inSg;
}
}
m_groupNbSub[outGr+1] = m_sgMat.size() - m_groupNbSub[outGr];
// now create VBOs
std::vector<Geom::Vec3f> posBuff;
posBuff.reserve(16384);
unsigned int firstIndex = 0;
unsigned int sz = group_faces.size();
......@@ -668,13 +790,17 @@ bool OBJModel<PFP>::createGroupMatVBO_P(Utils::VBO* positionVBO)
for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
{
Dart d = *id;
Dart e = m_map.phi1(d);
Dart f = m_map.phi1(e);
do
{
posBuff.push_back(m_positions[d]);
posBuff.push_back(m_positions[e]);
posBuff.push_back(m_positions[f]);
e=f;
f = m_map.phi1(e);
nbtris++;
......@@ -682,39 +808,231 @@ bool OBJModel<PFP>::createGroupMatVBO_P(Utils::VBO* positionVBO)
}
m_beginIndices[g] = firstIndex;
m_nbIndices[g] = 3*nbtris;
firstIndex += 3*nbtris;
}
positionVBO->setDataSize(3);
positionVBO->allocate(posBuff.size());
Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
positionVBO->releasePtr();
updateGroups();
// compute BBs ( TO DO: a method ?)
computeBB(posBuff);
return true;
}
//template <typename PFP>
//bool OBJModel<PFP>::createGroupMatVBO_P(Utils::VBO* positionVBO)
//{
// m_beginIndices.clear();
// m_nbIndices.clear();
// std::vector<Geom::Vec3f> posBuff;
// posBuff.reserve(16384);
// std::vector< std::vector<Dart> > group_faces(m_materialNames.size());
// TraversorF<typename PFP::MAP> traf(m_map);
// for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
// {
// unsigned int g = m_attMat[d];
// group_faces[g].push_back(d);
// }
// unsigned int firstIndex = 0;
// unsigned int sz = group_faces.size();
// m_beginIndices.resize(sz);
// m_nbIndices.resize(sz);
// m_groupIdx.resize(sz);
// for (unsigned int g=0; g<sz; ++g)
// {
// unsigned int nbtris = 0;
// std::vector<Dart>& traf = group_faces[g];
// if (m_tagG != 0)
// m_groupIdx[g] = m_groups[traf.front()];
// else
// m_groupIdx[g]=0;
// for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
// {
// Dart d = *id;
// Dart e = m_map.phi1(d);
// Dart f = m_map.phi1(e);
// do
// {
// posBuff.push_back(m_positions[d]);
// posBuff.push_back(m_positions[e]);
// posBuff.push_back(m_positions[f]);
// e=f;
// f = m_map.phi1(e);
// nbtris++;
// }while (f!=d);
// }
// m_beginIndices[g] = firstIndex;
// m_nbIndices[g] = 3*nbtris;
// firstIndex += 3*nbtris;
// }
// positionVBO->setDataSize(3);
// positionVBO->allocate(posBuff.size());
// Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
// memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
// positionVBO->releasePtr();
// updateGroups();
// return true;
//}
template <typename PFP>
bool OBJModel<PFP>::createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO)
bool OBJModel<PFP>::createGroupMatVBO_PT( Utils::VBO* positionVBO,
Utils::VBO* texcoordVBO)
{
m_beginIndices.clear();
m_nbIndices.clear();
if (!m_normals.isValid())
{
CGoGNerr << "no normal attribute "<< CGoGNendl;
return false;
}
if (!m_texCoords.isValid())
{
CGoGNerr << "no tex coords attribute "<< CGoGNendl;
return false;
}
std::vector< std::vector<Dart> > group_faces; //(m_materialNames.size());
group_faces.reserve(16384);
m_groupIdx.reserve(16384);
m_sgMat.reserve(16384);
unsigned int c_sg=0;
group_faces.resize(1);
TraversorF<typename PFP::MAP> traf(m_map);
Dart d=traf.begin();
unsigned int c_grp = m_groups[d];
unsigned int c_mat = m_attMat[d];
m_sgMat.push_back(c_mat);
m_groupFirstSub.push_back(0);