diff --git a/Apps/Examples/viewerOBJ.cpp b/Apps/Examples/viewerOBJ.cpp index b65a98b036eed8bb11adff6859760b7498c4d935..19a5021382a0a166d5c7c705931f67d72112a7c7 100644 --- a/Apps/Examples/viewerOBJ.cpp +++ b/Apps/Examples/viewerOBJ.cpp @@ -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& 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& mats = m_obj.getMaterials(); + unsigned int nb = m_obj.nbObjGroups(); +// const std::vector& 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; jsetAmbiant(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; jdiffuseColor[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; jtextureDiffuse != 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; isetAmbiant(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; jdiffuseColor[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; jtextureDiffuse != 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(); } } diff --git a/Apps/Examples/viewerOBJ.h b/Apps/Examples/viewerOBJ.h index 753b696a697ed52f06b85061f72ed2a8a536cd9a..5aba351df51f2e828f2bd6d5c95f50af3ffa9c53 100644 --- a/Apps/Examples/viewerOBJ.h +++ b/Apps/Examples/viewerOBJ.h @@ -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 m_obj; + Utils::Drawer* m_dr; + unsigned int m_currentGroupDrawn; + void drawBB( const Geom::BoundingBox& bb); + // VBO Utils::VBO* m_positionVBO; Utils::VBO* m_normalVBO; diff --git a/Apps/SandBox/CMakeLists.txt b/Apps/SandBox/CMakeLists.txt index 27dde8502bc49d67bffceaf2718c9e232191fb8d..8dd034131d384c57d4ae5503c5f52ab762c41be9 100644 --- a/Apps/SandBox/CMakeLists.txt +++ b/Apps/SandBox/CMakeLists.txt @@ -30,6 +30,12 @@ QT4_WRAP_CPP(tilings_moc tilings.h) add_executable(tilings tilings.cpp ${tilings_moc}) target_link_libraries(tilings ${CGoGN_LIBS_D} ${CGoGN_EXT_LIBS}) +QT4_WRAP_UI( test_traversor2_ui test_traversor2.ui ) +QT4_WRAP_CPP(test_traversor2_moc test_traversor2.h) +add_executable(test_traversor2 test_traversor2.cpp ${test_traversor2_ui} ${test_traversor2_moc}) +target_link_libraries(test_traversor2 ${CGoGN_LIBS_D} ${CGoGN_EXT_LIBS}) + + # Example with Qt # #QT4_WRAP_CPP(tuto1_moc tuto1.h) diff --git a/Apps/SandBox/test_traversor2.cpp b/Apps/SandBox/test_traversor2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89a27ee08dc670a0209840f8531e90441b3363fa --- /dev/null +++ b/Apps/SandBox/test_traversor2.cpp @@ -0,0 +1,422 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg * +* * +* This library is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by the * +* Free Software Foundation; either version 2.1 of the License, or (at your * +* option) any later version. * +* * +* This library is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * +* * +* Web site: http://cgogn.unistra.fr/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#include "test_traversor2.h" +#include "Algo/Geometry/boundingbox.h" +#include "Algo/Tiling/Surface/square.h" +#include "Algo/Geometry/centroid.h" +#include "Algo/Import/import.h" +#include "Algo/Export/export.h" + + +using namespace CGoGN ; + + + +int main(int argc, char **argv) +{ + // // interface + QApplication app(argc, argv); + MyQT sqt; + + + sqt.setDock(& sqt.dock); + sqt.setCallBack( sqt.dock.listTravers, SIGNAL(currentRowChanged(int)), SLOT(traversors(int)) ); + sqt.setCallBack( sqt.dock.withBoundary, SIGNAL(clicked()), SLOT(updateMap()) ); + sqt.setCallBack( sqt.dock.svg, SIGNAL(clicked()), SLOT(svg()) ); + sqt.setCallBack( sqt.dock.widthSlider, SIGNAL(valueChanged(int)), SLOT(width(int)) ); + int n=3; + if (argc==2) + n = atoi(argv[1]); + + // example code itself + sqt.createMap(n); + + sqt.width(5); + + // set help message in menu + sqt.setHelpMsg("First Tuto: \nCreate two faces\nsew them\nand affect positions"); + // final show for redraw + sqt.show(); + // and wait for the end + return app.exec(); +} + + +void MyQT::traversors(int x) +{ + // update all color to grey + for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d)) + { + colorDarts[d] = Geom::Vec3f(0.5f,0.5f,0.5f); + m_render_topo->setDartColor(d,0.5f,0.5f,0.5f); + } + + switch(x) + { + case 0: + { + Traversor2VVaE trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 1: + { + Traversor2VVaF trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 2: + { + Traversor2EEaV trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 3: + { + Traversor2EEaF trav(myMap,m_selected); + float c=0.0f; + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,c); + } + break; + case 4: + { + Traversor2FFaV trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 5: + { + Traversor2FFaE trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 6: + { + Traversor2FV trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 7: + { + Traversor2FE trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 8: + { + Traversor2EV trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 9: + { + Traversor2EF trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 10: + { + Traversor2VE trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + case 11: + { + Traversor2VF trav(myMap,m_selected); + for(Dart b=trav.begin(); b!= trav.end(); b=trav.next()) + m_render_topo->setDartColor(b,0.0f,1.0f,0.0f); + } + break; + default: + break; + } + + updateGL(); +} + +void MyQT::createMap(int n) +{ + position = myMap.addAttribute("position"); + colorDarts = myMap.addAttribute("color"); + + Algo::Surface::Tilings::Square::Grid grid(myMap, n, n, true); + grid.embedIntoGrid(position, 1.,1.,0.); + + // bounding box of scene + Geom::BoundingBox bb = Algo::Geometry::computeBoundingBox(myMap, position) ; + setParamObject(bb.maxSize(), bb.center().data()) ; + m_shift = bb.maxSize()/200.0f; + + // first show for be sure that GL context is binded + show(); + + dm.markAll(); + + m_render_topo->setDartWidth(4.0f); + m_render_topo->setInitialDartsColor(0.5f,0.5f,0.5f); + m_render_topo->setInitialBoundaryDartsColor(0.3f,0.3f,0.3f); + + m_render_topo->updateData(myMap, position, 0.9f, 0.9f, dock.withBoundary->isChecked()); + + for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d)) + { + if (dm.isMarked(d) && (!myMap.isBoundaryMarked2(d))) + { + colorDarts[d] = Geom::Vec3f(0.5f,0.5f,0.5f); + m_render_topo->setDartColor(d,0.5f,0.5f,0.5f); + } + } +} + +void MyQT::updateMap() +{ + m_render_topo->setInitialBoundaryDartsColor(0.0f,0.0f,0.0f); + m_render_topo->updateData(myMap, position, 0.9f, 0.9f, dock.withBoundary->isChecked()); + for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d)) + { + if (dm.isMarked(d) && (!myMap.isBoundaryMarked2(d))) + { + const Geom::Vec3f& C = colorDarts[d]; + if (C*C != 0.0f) + m_render_topo->setDartColor(d,C[0],C[1],C[2]); + } + } + updateGL(); +} + +// initialization GL callback +void MyQT::cb_initGL() +{ + glClearColor(1.0f,1.0f,1.0f,1.0f); + m_render_topo = new Algo::Render::GL2::TopoRender(0.01f) ; +} + +// redraw GL callback (clear and swap already done) +void MyQT::cb_redraw() +{ + glEnable( GL_POLYGON_OFFSET_FILL ); + glPolygonOffset( 1.0f, 1.0f ); + + m_render_topo->drawTopo(); + + glDisable( GL_POLYGON_OFFSET_FILL ); + + if (m_selected != NIL) + m_render_topo->overdrawDart(m_selected, 11, 1.0f, 0.0f, 0.0f); + + if (m_selected2 != NIL) + m_render_topo->overdrawDart(m_selected2, 11, 0.0f, 1.0f, 0.0f); +} + +void MyQT::cb_mousePress(int button, int x, int y) +{ + if (Shift()) + { + Dart d = m_render_topo->picking(myMap, x,y); // nb + if (button == Qt::LeftButton) + { + if (d != Dart::nil()) + m_selected = d; + + if (d != Dart::nil()) + std::cout << "DART="<>8)&0x7f)/255.0f + 0.25f; + float b = float((n>>16)&0x7f)/255.0 + 0.25f; + colorDarts[d] = Geom::Vec3f(r,g,b); + m_render_topo->setDartColor(d,r,g,b); + } + } + break; + case 'g': + for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d)) + { + colorDarts[d] = Geom::Vec3f(0.5f,0.5f,0.5f); + m_render_topo->setDartColor(d,0.5f,0.5f,0.5f); + } + break; + + case 'h': + for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d)) + { + if (!myMap.isBoundaryMarked2(d)) + { + colorDarts[d] = Geom::Vec3f(0.0f,0.0f,0.0f); + m_render_topo->setDartColor(d,0.0f,0.0f,0.0f); + } + } + break; + case Qt::Key_Up: + if (m_selected!=NIL) + position[m_selected][1] += m_shift; + updateMap(); + updateGL(); + break; + case Qt::Key_Down: + if (m_selected!=NIL) + position[m_selected][1] -= m_shift; + updateMap(); + updateGL(); + break; + case Qt::Key_Left: + if (m_selected!=NIL) + position[m_selected][0] -= m_shift; + updateMap(); + updateGL(); + break; + case Qt::Key_Right: + if (m_selected!=NIL) + position[m_selected][0] += m_shift; + updateMap(); + updateGL(); + break; + + default: + break; + } + updateGL(); +} + +void MyQT::svg() +{ + if (m_selected!=NIL) + m_render_topo->setDartColor(m_selected,0.8f,0.0f,0.0f); + if (m_selected2!=NIL) + m_render_topo->setDartColor(m_selected2,0.0f,0.8f,0.0f); + + std::string filename = selectFileSave("snapshot file", ".", "(*.svg)"); + m_render_topo->svgout2D(filename, modelViewMatrix(),projectionMatrix()); +} + +void MyQT::cb_Open() +{ + std::string filters("all (*.*);; trian (*.trian);; off (*.off);; ply (*.ply);; map (*.map)") ; + std::string filename = selectFile("Open Mesh", "", filters) ; + if (!filename.empty()) + importMesh(filename); + updateGL(); +} + +void MyQT::cb_Save() +{ + std::string filename = selectFileSave("Export Off file ",".","(*.off)"); + Algo::Surface::Export::exportOFF(myMap,position,filename.c_str()); + //std::cout << "could not save file : " << filename << std::endl; +} + +void MyQT::importMesh(std::string& filename) +{ + myMap.clear(true) ; + + size_t pos = filename.rfind("."); // position of "." in filename + std::string extension = filename.substr(pos); + + if (extension == std::string(".map")) + { + myMap.loadMapBin(filename); + position = myMap.getAttribute("position") ; + } + else + { + std::vector attrNames ; + if(!Algo::Surface::Import::importMesh(myMap, filename.c_str(), attrNames)) + { + CGoGNerr << "could not import " << filename << CGoGNendl ; + return; + } + position = myMap.getAttribute(attrNames[0]) ; + } + + colorDarts = myMap.getAttribute("color"); + if (!colorDarts.isValid()) + { + colorDarts = myMap.addAttribute("color"); + for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d)) + { + if (dm.isMarked(d) && (!myMap.isBoundaryMarked2(d))) + { + int n = rand(); + float r = float(n&0x7f)/255.0f + 0.25f; + float g = float((n>>8)&0x7f)/255.0f + 0.25f; + float b = float((n>>16)&0x7f)/255.0 + 0.25f; + colorDarts[d] = Geom::Vec3f(r,g,b); + m_render_topo->setDartColor(d,r,g,b); + } + } + } + + m_selected = NIL; + m_selected2 = NIL; + + Geom::BoundingBox bb = Algo::Geometry::computeBoundingBox(myMap, position) ; + setParamObject(bb.maxSize(), bb.center().data()) ; + m_shift = bb.maxSize()/200.0f; + + updateMap(); + updateGLMatrices() ; +} + + +void MyQT::width(int w) +{ + m_render_topo->setDartWidth(w); + m_render_topo->setRelationWidth(w); + updateGL(); +} + diff --git a/Apps/SandBox/test_traversor2.h b/Apps/SandBox/test_traversor2.h new file mode 100644 index 0000000000000000000000000000000000000000..2303c97553ceec89168e54695574076bdb677240 --- /dev/null +++ b/Apps/SandBox/test_traversor2.h @@ -0,0 +1,116 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg * +* * +* This library is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by the * +* Free Software Foundation; either version 2.1 of the License, or (at your * +* option) any later version. * +* * +* This library is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * +* * +* Web site: http://cgogn.unistra.fr/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#ifndef _TEST_TRAVERSOR2_ +#define _TEST_TRAVERSOR2_ + +//#define USE_GMAP + +//#define PRIMAL_TOPO 1 + +#include "Topology/generic/parameters.h" + +#include "Topology/gmap/embeddedGMap2.h" +#include "Topology/map/embeddedMap2.h" + +#include "Algo/Render/GL2/topoRender.h" + +#include "ui_test_traversor2.h" +#include "Utils/Qt/qtui.h" +#include "Utils/Qt/qtSimple.h" +#include "Utils/cgognStream.h" + + +using namespace CGoGN ; + +/** + * Struct that contains some informations about the types of the manipulated objects + * Mainly here to be used by the algorithms that are parameterized by it + */ +struct PFP: public PFP_STANDARD +{ + // definition of the type of the map +#ifdef USE_GMAP + typedef EmbeddedGMap2 MAP; +#else + typedef EmbeddedMap2 MAP; +#endif +}; + + +typedef PFP::MAP MAP ; +typedef PFP::VEC3 VEC3 ; + + +class MyQT: public Utils::QT::SimpleQT +{ + Q_OBJECT +public: + MyQT():m_render_topo(NULL),m_selected(NIL),m_selected2(NIL),dm(myMap),m_shift(0.01f) {} + + void cb_redraw(); + void cb_initGL(); + void cb_mousePress(int button, int x, int y); + void cb_keyPress(int code); + void cb_Open(); + void cb_Save(); + + Utils::QT::uiDockInterface dock; + +protected: + // declaration of the map + MAP myMap; + + VertexAttribute position; + DartAttribute colorDarts; + + // render (for the topo) + Algo::Render::GL2::TopoRender* m_render_topo; + + Dart m_selected; + Dart m_selected2; + DartMarker dm; + float m_shift; + + // just for more compact writing + inline Dart PHI1(Dart d) {return myMap.phi1(d);} + inline Dart PHI_1(Dart d) {return myMap.phi_1(d);} + inline Dart PHI2(Dart d) {return myMap.phi2(d);} + template + Dart PHI(Dart d) {return myMap.phi(d);} + +public: + // example of simple map creation + void createMap(int n); +// void updateMap(); + void importMesh(std::string& filename); + +public slots: + void traversors(int x); + void svg(); + void updateMap(); + void width(int w); +}; + +#endif diff --git a/Apps/SandBox/test_traversor2.ui b/Apps/SandBox/test_traversor2.ui new file mode 100644 index 0000000000000000000000000000000000000000..b723a40f02f9090159d1524e30c2a2ced488b96d --- /dev/null +++ b/Apps/SandBox/test_traversor2.ui @@ -0,0 +1,154 @@ + + + DockWidget + + + + 0 + 0 + 209 + 418 + + + + + 150 + 250 + + + + Interface + + + + true + + + + 0 + 0 + + + + + 150 + 200 + + + + + 2 + + + 4 + + + + + + + Qt::NoFocus + + + + Traversor2VVaE + + + + + Traversor2VVaF + + + + + Traversor2EEaV + + + + + Traversor2EEaF + + + + + Traversor2FFaV + + + + + Traversor2FFaE + + + + + Traversor2FV + + + + + Traverosr2FE + + + + + Traversor2EV + + + + + Traversor2EF + + + + + Traversor2VE + + + + + Traversor2VF + + + + + + + + with boundary + + + true + + + + + + + 1 + + + 10 + + + 5 + + + Qt::Horizontal + + + + + + + SnapshotSVG + + + + + + + + + + + diff --git a/CMakeLists.txt b/CMakeLists.txt index ea0d22039258bc1f3f771fb4a67f0839d9eb5cbc..5ecd70ecd1676b2bbdc540d5523a34eea9d903bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,8 +151,15 @@ IF(WIN32) # set(CMAKE_CONFIGURATION_TYPES "Release Debug" CACHE STRING "Only Release or Debug" FORCE) ELSE(WIN32) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -fPIC") -# SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wclobbered -Wempty-body -Wignored-qualifiers -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -fPIC") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Woverride-init -fPIC") + # remove the 60000+ "no unused local typedefs" warnings with GCC4.8+ + if(CMAKE_COMPILER_IS_GNUCXX) + execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION) + if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-local-typedefs") + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-local-typedefs") + endif(GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8) + endif(CMAKE_COMPILER_IS_GNUCXX) add_subdirectory(Release) add_subdirectory(Debug) add_subdirectory(${CGoGN_ROOT_DIR}/Apps Apps) diff --git a/ThirdParty/OpenNL/CMakeModules/FindSuiteSparse.cmake b/ThirdParty/OpenNL/CMakeModules/FindSuiteSparse.cmake index fb2583c1ab22fe298d46acb20807c17318c28eeb..beae87b7436932f061c68f209a936daba1e97002 100644 --- a/ThirdParty/OpenNL/CMakeModules/FindSuiteSparse.cmake +++ b/ThirdParty/OpenNL/CMakeModules/FindSuiteSparse.cmake @@ -56,9 +56,10 @@ else( WIN32 ) PATH_SUFFIXES cholmod/ CHOLMOD/ ) FIND_PATH( SUITESPARSE_LIBRARY_DIR - NAMES libcholmod.so + NAMES libcholmod.so libcholmod.a PATHS /usr/lib /usr/lib64 + /usr/lib/x86_64-linux-gnu /usr/local/lib ) ENDIF(APPLE) diff --git a/cmake_modules/FindSuiteSparse.cmake b/cmake_modules/FindSuiteSparse.cmake index fb2583c1ab22fe298d46acb20807c17318c28eeb..40439f79ed25d13d70347616b05f26cbbda24386 100644 --- a/cmake_modules/FindSuiteSparse.cmake +++ b/cmake_modules/FindSuiteSparse.cmake @@ -56,9 +56,10 @@ else( WIN32 ) PATH_SUFFIXES cholmod/ CHOLMOD/ ) FIND_PATH( SUITESPARSE_LIBRARY_DIR - NAMES libcholmod.so + NAMES libcholmod.so libcholmod.a PATHS /usr/lib - /usr/lib64 + /usr/lib64 + /usr/lib/x86_64-linux-gnu /usr/local/lib ) ENDIF(APPLE) diff --git a/include/Algo/Geometry/distances.h b/include/Algo/Geometry/distances.h new file mode 100644 index 0000000000000000000000000000000000000000..9cf6b88e819e80be1a48fd279bd8a0288e9919db --- /dev/null +++ b/include/Algo/Geometry/distances.h @@ -0,0 +1,81 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg * +* * +* This library is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by the * +* Free Software Foundation; either version 2.1 of the License, or (at your * +* option) any later version. * +* * +* This library is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * +* * +* Web site: http://cgogn.unistra.fr/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#ifndef __ALGO_GEOMETRY_DISTANCE_H__ +#define __ALGO_GEOMETRY_DISTANCE_H__ + +namespace CGoGN +{ + +namespace Algo +{ + +namespace Geometry +{ + +/** +* compute squared distance from point to the plane of a planar face +* @param map the map +* @param d a dart of the face +* @param P the point +* @return the squared distance to tha plane +*/ +template +typename PFP::REAL squaredDistancePoint2FacePlane(typename PFP::MAP& map, Dart d, const VertexAttribute& position, const VEC3& P) ; + +/** +* compute squared distance from point to face (assuming face is convex) +* Algo: min distance of each subtriangle of face (not optimum ?) +* @param map the map +* @param d a dart of the face +* @param P the point +* @return the squared distance +*/ +template +typename PFP::REAL squaredDistancePoint2Face(typename PFP::MAP& map, Dart d, const VertexAttribute& position, const VEC3& P) ; + + +/** +* compute squared distance from point to an edge +* @param map the map +* @param d a dart of the edge +* @param P the point +* @return the squared distance +*/ +template +typename PFP::REAL squaredDistancePoint2Edge(typename PFP::MAP& map, Dart d, const VertexAttribute& position, const VEC3& P) ; + +template +bool isPlanar(typename PFP::MAP& map, Dart d, const VertexAttribute& position); + + +} // namespace Geometry + +} // namespace Algo + +} // namespace CGoGN + +#include "Algo/Geometry/distances.hpp" + +#endif diff --git a/include/Algo/Geometry/distances.hpp b/include/Algo/Geometry/distances.hpp new file mode 100644 index 0000000000000000000000000000000000000000..4b0a357bfa708b7c08ec7ad333c4331e4fe7dab4 --- /dev/null +++ b/include/Algo/Geometry/distances.hpp @@ -0,0 +1,110 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg * +* * +* This library is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by the * +* Free Software Foundation; either version 2.1 of the License, or (at your * +* option) any later version. * +* * +* This library is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * +* * +* Web site: http://cgogn.unistra.fr/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#include "Geometry/distances.h" +#include "Algo/Geometry/normal.h" +#include + +namespace CGoGN +{ + +namespace Algo +{ + +namespace Geometry +{ + +template +bool isPlanar(typename PFP::MAP& map, Dart d, const VertexAttribute& position) +{ + if (map.phi<111>(d)==d) + return true; + + typename PFP::VEC3 No = Algo::Surface::Geometry::triangleNormal(map, d, position) ; + + Dart e = map.phi<11>(d); + while (e != d) + { + typename PFP::VEC3 n = Algo::Surface::Geometry::triangleNormal(map, e, position) ; + e = map.phi1(e); + if (e!=d) + e = map.phi1(e); + } +} + + + +template +typename PFP::REAL squaredDistancePoint2FacePlane(typename PFP::MAP& map, Dart d, const VertexAttribute& position, const VEC3& P) +{ + const typename PFP::VEC3& A = position[d]; + d = map.phi1(d); + const typename PFP::VEC3& B = position[d]; + d = map.phi1(d); + const typename PFP::VEC3& C = position[d]; + return Geom::squaredDistancePoint2TrianglePlane(P,A,B,C); +} + + +template +typename PFP::REAL squaredDistancePoint2Face(typename PFP::MAP& map, Dart d, const VertexAttribute& position, const VEC3& P) +{ + typedef typename PFP::REAL REAL; + const typename PFP::VEC3& A = position[d]; + + Dart e = map.phi1(d); + Dart f = map.phi1(e); + REAL dist2 = Geom::squaredDistancePoint2Triangle(P,A,position[e],position[f]); + e = f; + f = map.phi1(e); + + while (f != d) + { + REAL d2 = Geom::squaredDistancePoint2Triangle(P,A,position[e],position[f]); + if (d2 < dist2) + dist2 = d2; + e = f; + f = map.phi1(e); + } + return dist2; +} + + +template +typename PFP::REAL squaredDistancePoint2Edge(typename PFP::MAP& map, Dart d, const VertexAttribute& position, const VEC3& P) +{ + const typename PFP::VEC3& A = position[d]; + typename PFP::VEC3& AB = position[map.phi1(d)]-A; + typename PFP::REAL AB2 = AB*AB; + return Geom::squaredDistanceSeg2Point(A,AB,AB2,P) ; +} + + + +} // namespace Geometry + +} // namespace Algo + +} // namespace CGoGN + diff --git a/include/Algo/Import/importObjTex.h b/include/Algo/Import/importObjTex.h index ee52b1dc725e595e0e407f83c0e9aa0881a68062..0d54977de4b4e36a033823fdd52d0985ad8ef1e7 100644 --- a/include/Algo/Import/importObjTex.h +++ b/include/Algo/Import/importObjTex.h @@ -91,11 +91,18 @@ class OBJModel protected: typename PFP::MAP& m_map; + // infof of sub-groups (group/material) std::vector m_beginIndices; std::vector m_nbIndices; std::vector m_groupIdx; + std::vector m_sgMat; std::vector m_objGroups; + + + std::vector m_groupFirstSub; + std::vector m_groupNbSub; + std::vector m_groupNames; std::vector< Geom::BoundingBox > m_groupBBs; @@ -123,7 +130,7 @@ protected: unsigned int m_tagF ; - void updateGroups(const std::vector& pos); + void computeBB(const std::vector& pos); public: @@ -256,44 +263,72 @@ 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& getGroupBB(unsigned int i) const { return m_groupBBs[i];} + Geom::BoundingBox& getGroupBB(unsigned int i) { return m_groupBBs[i];} + const std::string& objGroupName(unsigned int i) const { return m_groupNames[i];} /** @@ -369,7 +404,13 @@ public: */ bool createGroupMatVBO_PTN( Utils::VBO* positionVBO, Utils::VBO* texcoordVBO, Utils::VBO* normalVBO); - + /** + * @brief add a dart by each face of group in a vector + * @param groupId the group to add + * @param dartFaces the vector in which we want to add + * @return the number of faces added. + */ + unsigned int storeFacesOfGroup(unsigned int groupId, std::vector& dartFaces); }; diff --git a/include/Algo/Import/importObjTex.hpp b/include/Algo/Import/importObjTex.hpp index d51b6f92b1e7416b883f03c4b3d134c420328061..3e575e7ce72301e072f318d11928c1a931dff498 100644 --- a/include/Algo/Import/importObjTex.hpp +++ b/include/Algo/Import/importObjTex.hpp @@ -631,23 +631,145 @@ unsigned int OBJModel::createSimpleVBO_PTN(Utils::VBO* positionVBO, Utils:: } + + + + template -bool OBJModel::createGroupMatVBO_P(Utils::VBO* positionVBO) +bool OBJModel::createGroupMatVBO_P( Utils::VBO* positionVBO) { m_beginIndices.clear(); m_nbIndices.clear(); - std::vector 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 > 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 > group_faces(m_materialNames.size()); TraversorF 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 posBuff; + posBuff.reserve(16384); + unsigned int firstIndex = 0; unsigned int sz = group_faces.size(); @@ -668,13 +790,17 @@ bool OBJModel::createGroupMatVBO_P(Utils::VBO* positionVBO) for (std::vector::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::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(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 +//bool OBJModel::createGroupMatVBO_P(Utils::VBO* positionVBO) +//{ +// m_beginIndices.clear(); +// m_nbIndices.clear(); + +// std::vector posBuff; +// posBuff.reserve(16384); + +// std::vector< std::vector > group_faces(m_materialNames.size()); +// TraversorF 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& traf = group_faces[g]; + +// if (m_tagG != 0) +// m_groupIdx[g] = m_groups[traf.front()]; +// else +// m_groupIdx[g]=0; + +// for (std::vector::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(positionVBO->lockPtr()); +// memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f)); +// positionVBO->releasePtr(); + +// updateGroups(); + +// return true; +//} + + + + template -bool OBJModel::createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO) +bool OBJModel::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 > 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 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); + + + if (m_tagG != 0) + m_groupIdx.push_back(c_grp); + else + m_groupIdx.push_back(0); + + + while (d!= traf.end()) + { + 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 posBuff; std::vector TCBuff; posBuff.reserve(16384); TCBuff.reserve(16384); - std::vector< std::vector > group_faces(m_materialNames.size()); - TraversorF 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; @@ -742,22 +1060,29 @@ bool OBJModel::createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* te do { posBuff.push_back(m_positions[d]); - if (m_specialVertices.isMarked(d)) + + if (hasTexCoords()) TCBuff.push_back(m_texCoordsF[d]); else TCBuff.push_back(m_texCoords[d]); + posBuff.push_back(m_positions[e]); - if (m_specialVertices.isMarked(e)) + + if (hasTexCoords()) TCBuff.push_back(m_texCoordsF[e]); else TCBuff.push_back(m_texCoords[e]); + posBuff.push_back(m_positions[f]); - if (m_specialVertices.isMarked(f)) + + if (hasTexCoords()) TCBuff.push_back(m_texCoordsF[f]); else TCBuff.push_back(m_texCoords[f]); + + e=f; f = m_map.phi1(e); nbtris++; @@ -765,6 +1090,7 @@ bool OBJModel::createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* te } m_beginIndices[g] = firstIndex; m_nbIndices[g] = 3*nbtris; + firstIndex += 3*nbtris; } @@ -780,7 +1106,8 @@ bool OBJModel::createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* te memcpy(ptrTC,&TCBuff[0],TCBuff.size()*sizeof(Geom::Vec2f)); texcoordVBO->releasePtr(); - updateGroups(); + // compute BBs ( TO DO: a method ?) + computeBB(posBuff); return true; } @@ -789,26 +1116,140 @@ bool OBJModel::createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* te - template -bool OBJModel::createGroupMatVBO_PN(Utils::VBO* positionVBO, Utils::VBO* normalVBO) +bool OBJModel::createGroupMatVBO_PN( Utils::VBO* positionVBO, + Utils::VBO* normalVBO) { m_beginIndices.clear(); m_nbIndices.clear(); - std::vector posBuff; - std::vector normalBuff; - posBuff.reserve(16384); - normalBuff.reserve(16384); + if (!m_normals.isValid()) + { + CGoGNerr << "no normal attribute "<< CGoGNendl; + return false; + } + + + std::vector< std::vector > 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 > group_faces(m_materialNames.size()); TraversorF 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 posBuff; + std::vector normalBuff; + posBuff.reserve(16384); + normalBuff.reserve(16384); + + unsigned int firstIndex = 0; unsigned int sz = group_faces.size(); @@ -835,34 +1276,29 @@ bool OBJModel::createGroupMatVBO_PN(Utils::VBO* positionVBO, Utils::VBO* no do { posBuff.push_back(m_positions[d]); - if (m_specialVertices.isMarked(d)) - { + + if (hasNormals()) normalBuff.push_back(m_normalsF[d]); - } else - { normalBuff.push_back(m_normals[d]); - } + posBuff.push_back(m_positions[e]); - if (m_specialVertices.isMarked(e)) - { + + if (hasNormals()) normalBuff.push_back(m_normalsF[e]); - } else - { normalBuff.push_back(m_normals[e]); - } + posBuff.push_back(m_positions[f]); - if (m_specialVertices.isMarked(f)) - { + + if (hasNormals()) normalBuff.push_back(m_normalsF[f]); - } else - { normalBuff.push_back(m_normals[f]); - } + + e=f; f = m_map.phi1(e); nbtris++; @@ -870,6 +1306,7 @@ bool OBJModel::createGroupMatVBO_PN(Utils::VBO* positionVBO, Utils::VBO* no } m_beginIndices[g] = firstIndex; m_nbIndices[g] = 3*nbtris; + firstIndex += 3*nbtris; } @@ -885,7 +1322,8 @@ bool OBJModel::createGroupMatVBO_PN(Utils::VBO* positionVBO, Utils::VBO* no memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f)); normalVBO->releasePtr(); - updateGroups(); + // compute BBs ( TO DO: a method ?) + computeBB(posBuff); return true; } @@ -893,6 +1331,7 @@ bool OBJModel::createGroupMatVBO_PN(Utils::VBO* positionVBO, Utils::VBO* no + template bool OBJModel::createGroupMatVBO_PTN( Utils::VBO* positionVBO, Utils::VBO* texcoordVBO, @@ -912,6 +1351,121 @@ bool OBJModel::createGroupMatVBO_PTN( Utils::VBO* positionVBO, return false; } + + std::vector< std::vector > 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 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); + + + if (m_tagG != 0) + m_groupIdx.push_back(c_grp); + else + m_groupIdx.push_back(0); + + + while (d!= traf.end()) + { + 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 posBuff; std::vector TCBuff; std::vector normalBuff; @@ -920,20 +1474,11 @@ bool OBJModel::createGroupMatVBO_PTN( Utils::VBO* positionVBO, normalBuff.reserve(16384); - std::vector< std::vector > group_faces(m_materialNames.size()); - TraversorF 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::createGroupMatVBO_PTN( Utils::VBO* positionVBO, do { posBuff.push_back(m_positions[d]); -// if (m_specialVertices.isMarked(d)) - { - if (hasTexCoords()) - TCBuff.push_back(m_texCoordsF[d]); - else - TCBuff.push_back(m_texCoords[d]); - if (hasNormals()) - normalBuff.push_back(m_normalsF[d]); - else - normalBuff.push_back(m_normals[d]); - } -// else -// { -// TCBuff.push_back(m_texCoords[d]); -// normalBuff.push_back(m_normals[d]); -// } + + if (hasTexCoords()) + TCBuff.push_back(m_texCoordsF[d]); + else + TCBuff.push_back(m_texCoords[d]); + if (hasNormals()) + normalBuff.push_back(m_normalsF[d]); + else + normalBuff.push_back(m_normals[d]); + posBuff.push_back(m_positions[e]); -// if (m_specialVertices.isMarked(e)) - { - if (hasTexCoords()) - TCBuff.push_back(m_texCoordsF[e]); - else - TCBuff.push_back(m_texCoords[e]); - if (hasNormals()) - normalBuff.push_back(m_normalsF[e]); - else - normalBuff.push_back(m_normals[e]); - } -// else -// { -// TCBuff.push_back(m_texCoords[e]); -// normalBuff.push_back(m_normals[e]); -// } + + if (hasTexCoords()) + TCBuff.push_back(m_texCoordsF[e]); + else + TCBuff.push_back(m_texCoords[e]); + if (hasNormals()) + normalBuff.push_back(m_normalsF[e]); + else + normalBuff.push_back(m_normals[e]); + posBuff.push_back(m_positions[f]); -// if (m_specialVertices.isMarked(f)) - { - if (hasTexCoords()) - TCBuff.push_back(m_texCoordsF[f]); - else - TCBuff.push_back(m_texCoords[f]); - if (hasNormals()) - normalBuff.push_back(m_normalsF[f]); - else - normalBuff.push_back(m_normals[f]); - } -// else -// { -// TCBuff.push_back(m_texCoords[f]); -// normalBuff.push_back(m_normals[f]); -// } + + if (hasTexCoords()) + TCBuff.push_back(m_texCoordsF[f]); + else + TCBuff.push_back(m_texCoords[f]); + if (hasNormals()) + normalBuff.push_back(m_normalsF[f]); + else + normalBuff.push_back(m_normals[f]); + e=f; f = m_map.phi1(e); @@ -1037,38 +1564,37 @@ bool OBJModel::createGroupMatVBO_PTN( Utils::VBO* positionVBO, memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f)); normalVBO->releasePtr(); - updateGroups(posBuff); + + // compute BBs ( TO DO: a method ?) + computeBB(posBuff); return true; } + template -void OBJModel::updateGroups(const std::vector& pos) +void OBJModel::computeBB(const std::vector& pos) { - unsigned int sz = m_beginIndices.size(); - unsigned int i=0; - do + m_groupBBs.resize(nbObjGroups()); + + for (unsigned int i=0; i& bb = m_groupBBs.back(); + Geom::BoundingBox& bb = m_groupBBs[i]; + bb.reset(); + + unsigned int begInd = beginIndex(i,0); + unsigned int endInd = begInd; + for (unsigned int j= 0; j< nbSubGroup(i); ++j) + endInd += nbIndices(i,j); - unsigned int grp = m_groupIdx[i]; - while ((i::import( const std::string& filename, std::vector > > vecNormIndPerVertex(m_map, "incidentsN"); VertexAutoAttribute< NoTypeNameAttribute< std::vector > > vecTCIndPerVertex(m_map, "incidentsTC"); - unsigned int currentGroup = 0; + int currentGroup = -1; unsigned int currentMat = 0; unsigned int nextMat = 0; @@ -1269,6 +1795,9 @@ bool OBJModel::import( const std::string& filename, std::vector::import( const std::string& filename, std::vector::import( const std::string& filename, std::vector +unsigned int OBJModel::storeFacesOfGroup(unsigned int groupId, std::vector& dartFaces) +{ + unsigned int nb=dartFaces.size(); + + TraversorF traf(m_map); + for (Dart d=traf.begin(); d!= traf.end(); d = traf.next()) + { + if (m_groups[d] == groupId) + { + dartFaces.push_back(d); + } + } + + return dartFaces.size()-nb; +} } diff --git a/include/Container/attributeMultiVector.hpp b/include/Container/attributeMultiVector.hpp index 395e81682f7911d77e8a1bd019739b44d68fd8bb..db4f9d6f439eb70f298fe9d61dfc7a8571fcd7ab 100644 --- a/include/Container/attributeMultiVector.hpp +++ b/include/Container/attributeMultiVector.hpp @@ -285,7 +285,7 @@ unsigned int AttributeMultiVector::getBlocksPointers(std::vector& addr template inline void AttributeMultiVector::initElt(unsigned int id) { - m_tableData[id / _BLOCKSIZE_][id % _BLOCKSIZE_] = T(0); + m_tableData[id / _BLOCKSIZE_][id % _BLOCKSIZE_] = T(); // T(0); } template diff --git a/include/Topology/generic/traversor2.h b/include/Topology/generic/traversor2.h index f4cf59a9fc7775e7b72f8b30346e81ab7e77a561..28d9de0bdeb1c4e5e71184f81c92eb23c362f0ea 100644 --- a/include/Topology/generic/traversor2.h +++ b/include/Topology/generic/traversor2.h @@ -241,6 +241,7 @@ public: } ; // Traverse the faces adjacent to a given face through sharing a common edge +// Warning mult-incidence is not managed (some faces can be send several times) template class Traversor2FFaE //: public Traversor { diff --git a/include/Topology/generic/traversor2.hpp b/include/Topology/generic/traversor2.hpp index 6c1a56427b626c2b64ccb06cbc907b6942b301e6..acda7b6894398516bd5099a06f0694606adc15a3 100644 --- a/include/Topology/generic/traversor2.hpp +++ b/include/Topology/generic/traversor2.hpp @@ -414,7 +414,8 @@ Dart Traversor2EEaV::next() // Traversor2EEaF template -Traversor2EEaF::Traversor2EEaF(const MAP& map, Dart dart) : m(map),m_QLT(NULL) +Traversor2EEaF::Traversor2EEaF(const MAP& map, Dart dart) : + m(map),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickAdjacentTraversal() ; if (quickTraversal != NULL) @@ -423,9 +424,12 @@ Traversor2EEaF::Traversor2EEaF(const MAP& map, Dart dart) : m(map),m_QLT(NU } else { - start = m.phi1(dart) ; - stop1 = dart ; - stop2 = m.phi2(dart) ; + if (m.isBoundaryMarked2(dart)) + stop1 = m.phi2(dart); + else + stop1 = dart; + stop2 = m.phi2(stop1) ; + start = m.phi1(stop1); } } @@ -458,9 +462,14 @@ Dart Traversor2EEaF::next() if(current != NIL) { current = m.phi1(current) ; - if(current == stop1) - current = m.phi1(stop2) ; - else if(current == stop2) + if (current == stop1) + { + if (!m.isBoundaryMarked2(stop2)) + current = m.phi1(stop2) ; + else + current=NIL; + } + else if (current == stop2) current = NIL ; } return current ; @@ -530,12 +539,12 @@ Traversor2FFaV::Traversor2FFaV(const MAP& map, Dart dart) : m(map),m_QLT(NU else { start = m.phi2(m.phi_1(m.phi2(m.phi_1(dart)))) ; - current = start ; - if(start == dart) + while (start == dart) { - stop = m.phi2(m.phi_1(dart)) ; - start = next() ; + dart = m.phi1(dart); + start = m.phi2(m.phi_1(m.phi2(m.phi_1(dart)))) ; } + current = start ; stop = dart ; if(m.isBoundaryMarked2(start)) start = next() ; @@ -577,12 +586,15 @@ Dart Traversor2FFaV::next() current = m.phi2(m.phi_1(m.phi2(m.phi_1(d)))) ; if(current == d) { - stop = m.phi2(m.phi_1(d)) ; + stop = m.phi1(d); + current = m.phi2(d); return next() ; } stop = d ; - if(m.isBoundaryMarked2(current)) - return next() ; + } + if(m.isBoundaryMarked2(current)) + { + return next() ; } if(current == start) current = NIL ; diff --git a/include/Topology/generic/traversor2Virt.h b/include/Topology/generic/traversor2Virt.h index 56eae57e53cf8b66f39bea9b2019b2ec088884d6..48e5ebf3001c1566874b150727ddfa461493bacf 100644 --- a/include/Topology/generic/traversor2Virt.h +++ b/include/Topology/generic/traversor2Virt.h @@ -40,13 +40,13 @@ template class VTraversor2VE: public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2VE(MAP& map, Dart dart) ; + VTraversor2VE(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -58,13 +58,13 @@ template class VTraversor2VF : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2VF(MAP& map, Dart dart) ; + VTraversor2VF(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -76,13 +76,13 @@ template class VTraversor2VVaE : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2VVaE(MAP& map, Dart dart) ; + VTraversor2VVaE(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -94,7 +94,7 @@ template class VTraversor2VVaF : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; @@ -102,7 +102,7 @@ private: const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2VVaF(MAP& map, Dart dart) ; + VTraversor2VVaF(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -118,13 +118,13 @@ template class VTraversor2EV : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2EV(MAP& map, Dart dart) ; + VTraversor2EV(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -136,13 +136,13 @@ template class VTraversor2EF : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2EF(MAP& map, Dart dart) ; + VTraversor2EF(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -154,7 +154,7 @@ template class VTraversor2EEaV : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; @@ -162,7 +162,7 @@ private: const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2EEaV(MAP& map, Dart dart) ; + VTraversor2EEaV(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -174,7 +174,7 @@ template class VTraversor2EEaF : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; @@ -182,7 +182,7 @@ private: const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2EEaF(MAP& map, Dart dart) ; + VTraversor2EEaF(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -198,13 +198,13 @@ template class VTraversor2FV : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2FV(MAP& map, Dart dart) ; + VTraversor2FV(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -217,7 +217,7 @@ template class VTraversor2FE: public VTraversor2FV { public: - VTraversor2FE(MAP& map, Dart dart):VTraversor2FV(map,dart){} + VTraversor2FE(const MAP& map, Dart dart):VTraversor2FV(map,dart){} } ; // Traverse the faces adjacent to a given face through sharing a common vertex @@ -225,7 +225,7 @@ template class VTraversor2FFaV : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; @@ -233,7 +233,7 @@ private: const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2FFaV(MAP& map, Dart dart) ; + VTraversor2FFaV(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -245,13 +245,13 @@ template class VTraversor2FFaE : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; const std::vector* m_QLT; std::vector::const_iterator m_ItDarts; public: - VTraversor2FFaE(MAP& map, Dart dart) ; + VTraversor2FFaE(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; diff --git a/include/Topology/generic/traversor2Virt.hpp b/include/Topology/generic/traversor2Virt.hpp index d7260cbaf7b8073e1e959d209eea7d26ff48f17c..b4fc80564527e41ccffb15496c53d917a4d10174 100644 --- a/include/Topology/generic/traversor2Virt.hpp +++ b/include/Topology/generic/traversor2Virt.hpp @@ -32,7 +32,7 @@ namespace CGoGN // VTraversor2VE template -VTraversor2VE::VTraversor2VE(MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) +VTraversor2VE::VTraversor2VE(const MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickIncidentTraversal() ; if (quickTraversal != NULL) @@ -81,7 +81,7 @@ Dart VTraversor2VE::next() // VTraversor2VF template -VTraversor2VF::VTraversor2VF(MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) +VTraversor2VF::VTraversor2VF(const MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickIncidentTraversal() ; if (quickTraversal != NULL) @@ -135,7 +135,7 @@ Dart VTraversor2VF::next() // VTraversor2VVaE template -VTraversor2VVaE::VTraversor2VVaE(MAP& map, Dart dart) : m(map),m_QLT(NULL) +VTraversor2VVaE::VTraversor2VVaE(const MAP& map, Dart dart) : m(map),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickAdjacentTraversal() ; if (quickTraversal != NULL) @@ -186,7 +186,7 @@ Dart VTraversor2VVaE::next() // VTraversor2VVaF template -VTraversor2VVaF::VTraversor2VVaF(MAP& map, Dart dart) : m(map),m_QLT(NULL) +VTraversor2VVaF::VTraversor2VVaF(const MAP& map, Dart dart) : m(map),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickAdjacentTraversal() ; if (quickTraversal != NULL) @@ -260,7 +260,7 @@ Dart VTraversor2VVaF::next() // VTraversor2EV template -VTraversor2EV::VTraversor2EV(MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) +VTraversor2EV::VTraversor2EV(const MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickIncidentTraversal() ; if (quickTraversal != NULL) @@ -307,7 +307,7 @@ Dart VTraversor2EV::next() // VTraversor2EF template -VTraversor2EF::VTraversor2EF(MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) +VTraversor2EF::VTraversor2EF(const MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickIncidentTraversal() ; if (quickTraversal != NULL) @@ -359,7 +359,7 @@ Dart VTraversor2EF::next() // VTraversor2EEaV template -VTraversor2EEaV::VTraversor2EEaV(MAP& map, Dart dart) : m(map),m_QLT(NULL) +VTraversor2EEaV::VTraversor2EEaV(const MAP& map, Dart dart) : m(map),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickAdjacentTraversal() ; if (quickTraversal != NULL) @@ -414,7 +414,7 @@ Dart VTraversor2EEaV::next() // VTraversor2EEaF template -VTraversor2EEaF::VTraversor2EEaF(MAP& map, Dart dart) : m(map),m_QLT(NULL) +VTraversor2EEaF::VTraversor2EEaF(const MAP& map, Dart dart) : m(map),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickAdjacentTraversal() ; if (quickTraversal != NULL) @@ -423,9 +423,12 @@ VTraversor2EEaF::VTraversor2EEaF(MAP& map, Dart dart) : m(map),m_QLT(NULL) } else { - start = m.phi1(dart) ; - stop1 = dart ; - stop2 = m.phi2(dart) ; + if (m.isBoundaryMarked2(dart)) + stop1 = m.phi2(dart); + else + stop1 = dart; + stop2 = m.phi2(stop1) ; + start = m.phi1(stop1); } } @@ -458,9 +461,14 @@ Dart VTraversor2EEaF::next() if(current != NIL) { current = m.phi1(current) ; - if(current == stop1) - current = m.phi1(stop2) ; - else if(current == stop2) + if (current == stop1) + { + if (!m.isBoundaryMarked2(stop2)) + current = m.phi1(stop2) ; + else + current=NIL; + } + else if (current == stop2) current = NIL ; } return current ; @@ -473,7 +481,7 @@ Dart VTraversor2EEaF::next() // VTraversor2FV template -VTraversor2FV::VTraversor2FV(MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) +VTraversor2FV::VTraversor2FV(const MAP& map, Dart dart) : m(map), start(dart),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickIncidentTraversal() ; if (quickTraversal != NULL) @@ -520,7 +528,7 @@ Dart VTraversor2FV::next() // VTraversor2FFaV template -VTraversor2FFaV::VTraversor2FFaV(MAP& map, Dart dart) : m(map),m_QLT(NULL) +VTraversor2FFaV::VTraversor2FFaV(const MAP& map, Dart dart) : m(map),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickAdjacentTraversal() ; if (quickTraversal != NULL) @@ -530,12 +538,12 @@ VTraversor2FFaV::VTraversor2FFaV(MAP& map, Dart dart) : m(map),m_QLT(NULL) else { start = m.phi2(m.phi_1(m.phi2(m.phi_1(dart)))) ; - current = start ; - if(start == dart) + while (start == dart) { - stop = m.phi2(m.phi_1(dart)) ; - start = next() ; + dart = m.phi1(dart); + start = m.phi2(m.phi_1(m.phi2(m.phi_1(dart)))) ; } + current = start ; stop = dart ; if(m.isBoundaryMarked2(start)) start = next() ; @@ -577,7 +585,8 @@ Dart VTraversor2FFaV::next() current = m.phi2(m.phi_1(m.phi2(m.phi_1(d)))) ; if(current == d) { - stop = m.phi2(m.phi_1(d)) ; + stop = m.phi1(d); + current = m.phi2(d); return next() ; } stop = d ; @@ -593,7 +602,7 @@ Dart VTraversor2FFaV::next() // VTraversor2FFaE template -VTraversor2FFaE::VTraversor2FFaE(MAP& map, Dart dart) : m(map),m_QLT(NULL) +VTraversor2FFaE::VTraversor2FFaE(const MAP& map, Dart dart) : m(map),m_QLT(NULL) { const AttributeMultiVector > >* quickTraversal = map.template getQuickAdjacentTraversal() ; if (quickTraversal != NULL) diff --git a/include/Topology/generic/traversor2_closed.h b/include/Topology/generic/traversor2_closed.h index ba2d497ccb515c454d780fdebf908c0843425ea6..71d42849c90ed01e96d3ba504bacbd41b04ccf93 100644 --- a/include/Topology/generic/traversor2_closed.h +++ b/include/Topology/generic/traversor2_closed.h @@ -41,15 +41,15 @@ namespace ClosedMap // Traverse the edges incident to a given vertex template -class Traversor2VE: public Traversor +class Traversor2VE: public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; public: - Traversor2VE(MAP& map, Dart dart) ; + Traversor2VE(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -58,15 +58,15 @@ public: // Traverse the faces incident to a given vertex template -class Traversor2VF : public Traversor +class Traversor2VF : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; public: - Traversor2VF(MAP& map, Dart dart) ; + Traversor2VF(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -75,15 +75,15 @@ public: // Traverse the vertices adjacent to a given vertex through sharing a common edge template -class Traversor2VVaE : public Traversor +class Traversor2VVaE : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; public: - Traversor2VVaE(MAP& map, Dart dart) ; + Traversor2VVaE(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -92,17 +92,17 @@ public: // Traverse the vertices adjacent to a given vertex through sharing a common face template -class Traversor2VVaF : public Traversor +class Traversor2VVaF : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; Dart stop ; public: - Traversor2VVaF(MAP& map, Dart dart) ; + Traversor2VVaF(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -115,15 +115,15 @@ public: // Traverse the vertices incident to a given edge template -class Traversor2EV : public Traversor +class Traversor2EV : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; public: - Traversor2EV(MAP& map, Dart dart) ; + Traversor2EV(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -132,15 +132,15 @@ public: // Traverse the faces incident to a given edge template -class Traversor2EF : public Traversor +class Traversor2EF : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; public: - Traversor2EF(MAP& map, Dart dart) ; + Traversor2EF(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -149,17 +149,17 @@ public: // Traverse the edges adjacent to a given edge through sharing a common vertex template -class Traversor2EEaV : public Traversor +class Traversor2EEaV : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; Dart stop1, stop2 ; public: - Traversor2EEaV(MAP& map, Dart dart) ; + Traversor2EEaV(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -168,17 +168,17 @@ public: // Traverse the edges adjacent to a given edge through sharing a common face template -class Traversor2EEaF : public Traversor +class Traversor2EEaF : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; Dart stop1, stop2 ; public: - Traversor2EEaF(MAP& map, Dart dart) ; + Traversor2EEaF(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -191,15 +191,15 @@ public: // Traverse the vertices incident to a given face template -class Traversor2FV : public Traversor +class Traversor2FV : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; public: - Traversor2FV(MAP& map, Dart dart) ; + Traversor2FV(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -212,22 +212,22 @@ template class Traversor2FE: public Traversor2FV { public: - Traversor2FE(MAP& map, Dart dart):Traversor2FV(map,dart){} + Traversor2FE(const MAP& map, Dart dart):Traversor2FV(map,dart){} } ; // Traverse the faces adjacent to a given face through sharing a common vertex template -class Traversor2FFaV : public Traversor +class Traversor2FFaV : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; Dart stop ; public: - Traversor2FFaV(MAP& map, Dart dart) ; + Traversor2FFaV(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; @@ -236,15 +236,15 @@ public: // Traverse the faces adjacent to a given face through sharing a common edge template -class Traversor2FFaE : public Traversor +class Traversor2FFaE : public Traversor { private: - MAP& m ; + const MAP& m ; Dart start ; Dart current ; public: - Traversor2FFaE(MAP& map, Dart dart) ; + Traversor2FFaE(const MAP& map, Dart dart) ; Dart begin() ; Dart end() ; diff --git a/include/Topology/generic/traversor2_closed.hpp b/include/Topology/generic/traversor2_closed.hpp index 8db9c13f921435e3cdb8afbd87deac492cbb0365..09f8d045e825609b27ade6013b744ba88c471ef9 100644 --- a/include/Topology/generic/traversor2_closed.hpp +++ b/include/Topology/generic/traversor2_closed.hpp @@ -35,7 +35,7 @@ namespace ClosedMap // Traversor2VE template -Traversor2VE::Traversor2VE(MAP& map, Dart dart) : m(map), start(dart) +Traversor2VE::Traversor2VE(const MAP& map, Dart dart) : m(map), start(dart) {} template @@ -66,7 +66,7 @@ Dart Traversor2VE::next() // Traversor2VF template -Traversor2VF::Traversor2VF(MAP& map, Dart dart) : m(map), start(dart) +Traversor2VF::Traversor2VF(const MAP& map, Dart dart) : m(map), start(dart) { } @@ -98,7 +98,7 @@ Dart Traversor2VF::next() // Traversor2VVaE template -Traversor2VVaE::Traversor2VVaE(MAP& map, Dart dart) : m(map) +Traversor2VVaE::Traversor2VVaE(const MAP& map, Dart dart) : m(map) { start = m.phi2(dart) ; } @@ -131,7 +131,7 @@ Dart Traversor2VVaE::next() // Traversor2VVaF template -Traversor2VVaF::Traversor2VVaF(MAP& map, Dart dart) : m(map) +Traversor2VVaF::Traversor2VVaF(const MAP& map, Dart dart) : m(map) { start = m.phi1(m.phi1(dart)) ; if(start == dart) @@ -179,7 +179,7 @@ Dart Traversor2VVaF::next() // Traversor2EV template -Traversor2EV::Traversor2EV(MAP& map, Dart dart) : m(map), start(dart) +Traversor2EV::Traversor2EV(const MAP& map, Dart dart) : m(map), start(dart) {} template @@ -210,7 +210,7 @@ Dart Traversor2EV::next() // Traversor2EF template -Traversor2EF::Traversor2EF(MAP& map, Dart dart) : m(map), start(dart) +Traversor2EF::Traversor2EF(const MAP& map, Dart dart) : m(map), start(dart) { } @@ -242,7 +242,7 @@ Dart Traversor2EF::next() // Traversor2EEaV template -Traversor2EEaV::Traversor2EEaV(MAP& map, Dart dart) : m(map) +Traversor2EEaV::Traversor2EEaV(const MAP& map, Dart dart) : m(map) { start = m.phi2(m.phi_1(dart)) ; stop1 = dart ; @@ -279,7 +279,7 @@ Dart Traversor2EEaV::next() // Traversor2EEaF template -Traversor2EEaF::Traversor2EEaF(MAP& map, Dart dart) : m(map) +Traversor2EEaF::Traversor2EEaF(const MAP& map, Dart dart) : m(map) { start = m.phi1(dart) ; stop1 = dart ; @@ -320,7 +320,7 @@ Dart Traversor2EEaF::next() // Traversor2FV template -Traversor2FV::Traversor2FV(MAP& map, Dart dart) : m(map), start(dart) +Traversor2FV::Traversor2FV(const MAP& map, Dart dart) : m(map), start(dart) {} template @@ -351,7 +351,7 @@ Dart Traversor2FV::next() // Traversor2FFaV template -Traversor2FFaV::Traversor2FFaV(MAP& map, Dart dart) : m(map) +Traversor2FFaV::Traversor2FFaV(const MAP& map, Dart dart) : m(map) { start = m.phi2(m.phi_1(m.phi2(m.phi_1(dart)))) ; current = start ; @@ -402,7 +402,7 @@ Dart Traversor2FFaV::next() // Traversor2FFaE template -Traversor2FFaE::Traversor2FFaE(MAP& map, Dart dart) : m(map) +Traversor2FFaE::Traversor2FFaE(const MAP& map, Dart dart) : m(map) { start = m.phi2(dart) ; } diff --git a/include/Utils/Shaders/shaderCustom.frag b/include/Utils/Shaders/shaderCustom.frag new file mode 100644 index 0000000000000000000000000000000000000000..f0c1e42469f6ae9b43e15e77b18aa3953c5e3af0 --- /dev/null +++ b/include/Utils/Shaders/shaderCustom.frag @@ -0,0 +1,8 @@ +// ShaderCustom::fragmentShaderText +VARYING_FRAG vec4 ColorFS; +VARYING_FRAG vec3 N; +void main() +{ + gl_FragData[0] = ColorFS; + gl_FragData[1] = vec4( 0.5*normalize(N)+vec3(0.5), 1.0 ); +} diff --git a/include/Utils/Shaders/shaderCustom.geom b/include/Utils/Shaders/shaderCustom.geom new file mode 100644 index 0000000000000000000000000000000000000000..5ee21083e76e9c0b6625a52f5d0ee86f8fd4af0a --- /dev/null +++ b/include/Utils/Shaders/shaderCustom.geom @@ -0,0 +1,35 @@ +// ShaderCustom::geometryShaderText + +uniform float explode; +uniform mat4 ModelViewProjectionMatrix; +uniform mat4 NormalMatrix; +uniform mat4 ModelViewMatrix; +uniform vec3 lightPosition; +uniform vec4 diffuse; +uniform vec4 ambient; +VARYING_OUT vec4 ColorFS; +VARYING_OUT vec3 N; +VARYING_OUT vec2 texCoord; +void main(void) +{ + vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz; + vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz; + N = cross(v1,v2); + N = normalize (vec3(NormalMatrix*vec4(N,0.0))); + vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz; + center /= 3.0; + vec4 newPos = ModelViewMatrix * vec4(center,0.0); + vec3 L = normalize (lightPosition - newPos.xyz); + float lambertTerm = dot(N,L); + ColorFS = ambient; + if(lambertTerm > 0.0) + ColorFS += diffuse * lambertTerm; + int i; + for(i=0; i< NBVERTS_IN; i++) + { + vec4 pos = explode * POSITION_IN(i) + (1.0-explode)* vec4(center,1.0); + gl_Position = ModelViewProjectionMatrix * pos; + EmitVertex(); + } + EndPrimitive(); +} diff --git a/include/Utils/Shaders/shaderCustom.h b/include/Utils/Shaders/shaderCustom.h new file mode 100644 index 0000000000000000000000000000000000000000..b0636ff4622e90357e3bd6c800023bdda661e5d0 --- /dev/null +++ b/include/Utils/Shaders/shaderCustom.h @@ -0,0 +1,54 @@ +#ifndef __SHADER_CUSTOM__ +#define __SHADER_CUSTOM__ + +#include "Utils/GLSLShader.h" +#include "Geometry/vector_gen.h" +#include "Geometry/matrix.h" + + +using namespace CGoGN; + +class ShaderCustom : public Utils::GLSLShader +{ +protected: + // shader sources + static std::string vertexShaderText; + static std::string fragmentShaderText; + static std::string geometryShaderText; + + // uniform locations + CGoGNGLuint m_unif_ambiant; + CGoGNGLuint m_unif_diffuse; + CGoGNGLuint m_unif_lightPos; + CGoGNGLuint m_unif_explode; + + float m_explode; + Geom::Vec4f m_ambiant; + Geom::Vec4f m_diffuse; + Geom::Vec3f m_light_pos; + + Utils::VBO* m_vboPos; + + void getLocations(); + + void restoreUniformsAttribs(); + +public: + ShaderCustom(); + + void setExplode(float explode); + + void setAmbiant(const Geom::Vec4f& ambiant); + + void setDiffuse(const Geom::Vec4f& diffuse); + + void setLightPosition(const Geom::Vec3f& lp); + + void setParams(float explode, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec3f& lightPos); + + unsigned int setAttributePosition(Utils::VBO* vbo); + + void setTransformation(Geom::Matrix44f t); +}; + +#endif diff --git a/include/Utils/Shaders/shaderCustom.vert b/include/Utils/Shaders/shaderCustom.vert new file mode 100644 index 0000000000000000000000000000000000000000..2b19d55af721685e2d1e1b2f3bdc23720f2a16ed --- /dev/null +++ b/include/Utils/Shaders/shaderCustom.vert @@ -0,0 +1,8 @@ +// ShaderCustom::vertexShaderText +uniform mat4 TransformationMatrix; +uniform mat4 ModelViewProjectionMatrix; +ATTRIBUTE vec3 VertexPosition; +void main() +{ + gl_Position = TransformationMatrix *vec4(VertexPosition, 1.0); +} diff --git a/include/Utils/Shaders/shaderCustomTex.frag b/include/Utils/Shaders/shaderCustomTex.frag new file mode 100644 index 0000000000000000000000000000000000000000..e2aca96473d04d6305ceade0c7b75412698c77d7 --- /dev/null +++ b/include/Utils/Shaders/shaderCustomTex.frag @@ -0,0 +1,13 @@ +// ShaderCustomTex::fragmentShaderText + +PRECISON; +uniform sampler2D textureUnit; +in vec3 N; +in vec2 fragTexCoord; +uniform vec4 ambient; +FRAG_OUT_DEF; +void main() +{ + gl_FragData[0] = ambient*texture2D(textureUnit,fragTexCoord); + gl_FragData[1] = vec4( 0.5*normalize(N)+vec3(0.5), 1.0 ); +} \ No newline at end of file diff --git a/include/Utils/Shaders/shaderCustomTex.geom b/include/Utils/Shaders/shaderCustomTex.geom new file mode 100644 index 0000000000000000000000000000000000000000..b2b0ac6d9adbf4057eabe01276219ecc4f912ea9 --- /dev/null +++ b/include/Utils/Shaders/shaderCustomTex.geom @@ -0,0 +1,24 @@ +// ShaderCustomTex::geometryShaderText + +VARYING_IN vec2 texCoord[]; +VARYING_IN vec3 Normal[]; +VARYING_OUT vec2 fragTexCoord; +VARYING_OUT vec3 N; +void main(void) +{ + //vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz; + //vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz; + //N = cross(v1,v2); + //N = normalize(N); + + + int i; + for(i=0; i< NBVERTS_IN; i++) + { + gl_Position = POSITION_IN(i); + fragTexCoord = texCoord[i]; + N = Normal[i]; + EmitVertex(); + } + EndPrimitive(); +} diff --git a/include/Utils/Shaders/shaderCustomTex.h b/include/Utils/Shaders/shaderCustomTex.h new file mode 100644 index 0000000000000000000000000000000000000000..2b1b930a1199d628c4b29d848ad4c3bedb311159 --- /dev/null +++ b/include/Utils/Shaders/shaderCustomTex.h @@ -0,0 +1,68 @@ +#ifndef __SHADER_CUSTOMTEX__ +#define __SHADER_CUSTOMTEX__ + +#include "Utils/GLSLShader.h" +#include "Geometry/vector_gen.h" + +#include "Utils/clippingShader.h" +#include "Utils/textures.h" +#include "Utils/gl_def.h" +#include "Geometry/matrix.h" + +using namespace CGoGN; + +class ShaderCustomTex : public Utils::ClippingShader +{ +protected: + // shader sources + static std::string vertexShaderText; + static std::string fragmentShaderText; + static std::string geometryShaderText; + + CGoGNGLuint m_unif_unit; + int m_unit; + + Geom::Vec4f m_col; + + Utils::GTexture* m_tex_ptr; + Utils::VBO* m_vboPos; + Utils::VBO* m_vboNormal; + Utils::VBO* m_vboTexCoord; + + void restoreUniformsAttribs(); + +public: + ShaderCustomTex(); + + /** + * choose the texture unit engine to use for this texture + */ + void setTextureUnit(GLenum texture_unit); + + /** + * set the texture to use + */ + void setTexture(Utils::GTexture* tex); + + /** + * activation of texture unit with set texture + */ + void activeTexture(); + + /** + * activation of texture unit with texture id + */ + void activeTexture(CGoGNGLuint texId); + + unsigned int setAttributePosition(Utils::VBO* vbo); + + unsigned int setAttributeNormal(Utils::VBO* vbo); + + unsigned int setAttributeTexCoord(Utils::VBO* vbo); + + void setBaseColor(Geom::Vec4f col); + + void setTransformation(Geom::Matrix44f t); +}; + +#endif diff --git a/include/Utils/Shaders/shaderCustomTex.vert b/include/Utils/Shaders/shaderCustomTex.vert new file mode 100644 index 0000000000000000000000000000000000000000..739c7698d4043555f4a1d3d0ed6fb922810e5908 --- /dev/null +++ b/include/Utils/Shaders/shaderCustomTex.vert @@ -0,0 +1,16 @@ +// ShaderCustomTex::vertexShaderText + +ATTRIBUTE vec3 VertexPosition, VertexNormal; +ATTRIBUTE vec2 VertexTexCoord; +uniform mat4 TransformationMatrix; +uniform mat4 ModelViewProjectionMatrix; +VARYING_VERT vec2 texCoord; +VARYING_VERT vec3 Normal; +INVARIANT_POS; +void main () +{ + Normal = vec3 (ModelViewProjectionMatrix * TransformationMatrix * vec4 (VertexNormal , 1.0)); + gl_Position = ModelViewProjectionMatrix * TransformationMatrix * vec4 (VertexPosition, 1.0); + texCoord = VertexTexCoord; +} + diff --git a/include/Utils/Shaders/shaderMatCustom.frag b/include/Utils/Shaders/shaderMatCustom.frag new file mode 100644 index 0000000000000000000000000000000000000000..67e0014c2e0b9cf59556c96ba9462e3241e698c8 --- /dev/null +++ b/include/Utils/Shaders/shaderMatCustom.frag @@ -0,0 +1,43 @@ +//ShaderMatCustom::fragmentShaderText + +PRECISON; +VARYING_FRAG vec3 EyeVector, Normal, LightDir; +#ifdef WITH_COLOR +VARYING_FRAG vec3 Color; +#endif +uniform vec4 materialDiffuse; +uniform vec4 materialSpecular; +uniform vec4 materialAmbient; +uniform float shininess; +FRAG_OUT_DEF; +void main() +{ + vec3 N = normalize (Normal); + vec3 L = normalize (LightDir); + //float lambertTerm = dot(N,L); + float lambertTerm = 0.15*dot(N,L)+0.80; + + vec4 finalColor = materialAmbient; + + #ifdef DOUBLE_SIDED + if (lambertTerm < 0.0) + { + N = -1.0*N; + lambertTerm = -1.0*lambertTerm; + #else + if (lambertTerm > 0.0) + { + #endif + #ifndef WITH_COLOR + finalColor += materialDiffuse * lambertTerm; + #else + finalColor += vec4((Color*lambertTerm),0.0) ; + #endif + vec3 E = normalize(EyeVector); + vec3 R = reflect(-L, N); + float specular = pow( max(dot(R, E), 0.0), shininess ); + finalColor += materialSpecular * specular; + } + gl_FragColor=finalColor; + //gl_FragColor = vec4(lambertTerm,lambertTerm,lambertTerm,0); +} diff --git a/include/Utils/Shaders/shaderMatCustom.geom b/include/Utils/Shaders/shaderMatCustom.geom new file mode 100644 index 0000000000000000000000000000000000000000..2c666735367c1546824eb282c6e84e34032d1740 --- /dev/null +++ b/include/Utils/Shaders/shaderMatCustom.geom @@ -0,0 +1,35 @@ +// ShaderMatCustom::geometryShaderText + +uniform float explode; +uniform mat4 ModelViewProjectionMatrix; +uniform mat4 NormalMatrix; +uniform mat4 ModelViewMatrix; +uniform vec3 lightPosition; +uniform vec4 diffuse; +uniform vec4 ambient; +//VARYING_OUT vec4 ColorFS; +VARYING_OUT vec3 N; +VARYING_OUT vec2 texCoord; +void main(void) +{ + vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz; + vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz; + N = cross(v1,v2); + N = normalize (vec3(NormalMatrix*vec4(N,0.0))); + vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz; + center /= 3.0; + vec4 newPos = ModelViewMatrix * vec4(center,0.0); + vec3 L = normalize (lightPosition - newPos.xyz); + float lambertTerm = dot(N,L); + ColorFS = ambient; + if(lambertTerm > 0.0) + ColorFS += diffuse * lambertTerm; + int i; + for(i=0; i< NBVERTS_IN; i++) + { + vec4 pos = explode * POSITION_IN(i) + (1.0-explode)* vec4(center,1.0); + gl_Position = ModelViewProjectionMatrix * pos; + EmitVertex(); + } + EndPrimitive(); +} diff --git a/include/Utils/Shaders/shaderMatCustom.h b/include/Utils/Shaders/shaderMatCustom.h new file mode 100644 index 0000000000000000000000000000000000000000..198ff4212a9b520807aacb1613e2623007606f7a --- /dev/null +++ b/include/Utils/Shaders/shaderMatCustom.h @@ -0,0 +1,95 @@ +#ifndef SHADER_MATCUST +#define SHADER_MATCUST + +#include "Utils/GLSLShader.h" +#include "Geometry/vector_gen.h" +#include "Geometry/matrix.h" + +#include + +using namespace CGoGN; + +class ShaderMatCustom : public Utils::GLSLShader +{ +protected: + // flag color per vertex or not + bool m_with_color; + // flag color per vertex or not + bool m_with_eyepos; + + // shader sources OGL3 + static std::string vertexShaderText; + static std::string fragmentShaderText; + static std::string geometryShaderText; + + // uniform locations + CGoGNGLuint m_unif_ambiant; + CGoGNGLuint m_unif_diffuse; + CGoGNGLuint m_unif_specular; + CGoGNGLuint m_unif_shininess; + CGoGNGLuint m_unif_lightPos; + CGoGNGLuint m_unif_eyePos; + + //values + Geom::Vec4f m_ambiant; + Geom::Vec4f m_diffuse; + Geom::Vec4f m_specular; + float m_shininess; + Geom::Vec3f m_lightPos; + Geom::Vec3f m_eyePos; + + Utils::VBO* m_vboPos; + Utils::VBO* m_vboNormal; + Utils::VBO* m_vboColor; + + void getLocations(); + + void sendParams(); + + void restoreUniformsAttribs(); + +public: + ShaderMatCustom(bool doubleSided = false, bool withEyePosition=false); + + // inviduals parameter setting functions + void setAmbiant(const Geom::Vec4f& ambiant); + + void setDiffuse(const Geom::Vec4f& diffuse); + + void setSpecular(const Geom::Vec4f& specular); + + void setShininess(float shininess); + + void setLightPosition(const Geom::Vec3f& lp); + + /// set eye position for VR environement + void setEyePosition(const Geom::Vec3f& ep); + + const Geom::Vec4f& getAmbiant() const { return m_ambiant; } + + const Geom::Vec4f& getDiffuse() const { return m_diffuse; } + + const Geom::Vec4f& getSpecular() const { return m_specular; } + + float getShininess() const { return m_shininess; } + + const Geom::Vec3f& getLightPosition() const { return m_lightPos; } + + /** + * set all parameter in on call (one bind also) + */ + void setParams(const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec4f& specular, float shininess, const Geom::Vec3f& lightPos); + + // attributes + unsigned int setAttributePosition(Utils::VBO* vbo); + + unsigned int setAttributeNormal(Utils::VBO* vbo); + + // optional attributes + unsigned int setAttributeColor(::Utils::VBO* vbo); + void unsetAttributeColor(); + + void setTransformation(Geom::Matrix44f t); +}; + +#endif diff --git a/include/Utils/Shaders/shaderMatCustom.vert b/include/Utils/Shaders/shaderMatCustom.vert new file mode 100644 index 0000000000000000000000000000000000000000..02aa71452b4928e85af54143c4d83fc5df53e390 --- /dev/null +++ b/include/Utils/Shaders/shaderMatCustom.vert @@ -0,0 +1,37 @@ +//ShaderMatCustom::vertexShaderText + +ATTRIBUTE vec3 VertexPosition, VertexNormal; +#ifdef WITH_COLOR +ATTRIBUTE vec3 VertexColor; +#endif +uniform mat4 TransformationMatrix; +uniform mat4 ModelViewProjectionMatrix; +uniform mat4 ModelViewMatrix; +uniform mat4 NormalMatrix; +uniform vec3 lightPosition; +VARYING_VERT vec3 EyeVector, Normal, LightDir; + +#ifdef WITH_COLOR +VARYING_VERT vec3 Color; +#endif + +#ifdef WITH_EYEPOSITION +uniform vec3 eyePosition; +#endif + +INVARIANT_POS; +void main () +{ + Normal = vec3 (NormalMatrix * TransformationMatrix * vec4 (VertexNormal, 0.0)); + vec3 Position = vec3 (ModelViewMatrix * TransformationMatrix * vec4 (VertexPosition, 1.0)); + LightDir = lightPosition - Position; + #ifdef WITH_EYEPOSITION + EyeVector = eyePosition-Position; + #else + EyeVector = -Position; + #endif + #ifdef WITH_COLOR + Color = VertexColor; + #endif + gl_Position = ModelViewProjectionMatrix * (TransformationMatrix * vec4 (VertexPosition, 1.0)); +} diff --git a/include/Utils/filename.h b/include/Utils/filename.h new file mode 100644 index 0000000000000000000000000000000000000000..0a0426493d5961206d0524e6b4d362f39563e84f --- /dev/null +++ b/include/Utils/filename.h @@ -0,0 +1,98 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg * +* * +* This library is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by the * +* Free Software Foundation; either version 2.1 of the License, or (at your * +* option) any later version. * +* * +* This library is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * +* * +* Web site: http://cgogn.unistra.fr/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#ifndef _CGOGN_FILENAME_H_ +#define _CGOGN_FILENAME_H_ + +#include + +namespace CGoGN +{ +namespace Utils +{ +/** + * @brief check if filename has extension and add it if not + * @param filename + * @param extension (with . example ".svg") + * @return the modified (or not) filename + */ +inline std::string checkFileNameExtension(const std::string &filename, const std::string extension) +{ + std::size_t found = filename.rfind(extension); + if ( (found==std::string::npos) || ((found+extension.length()) != filename.length()) ) + { + if (filename[filename.size()-1]=='.') + return filename.substr(0,filename.size()-1) + extension; + + return filename + extension; + } + return filename; +} + +/** + * @brief extract the path from a file-name + * @param filename + * @return the path (with ending /) if there is a / (or \) in filename + */ +inline std::string extractPathFromFileName(const std::string &filename) +{ + std::size_t found = filename.rfind('/'); + + if (found == std::string::npos) + found = filename.rfind('\\'); // welcome on NTFS ;) + + if (found == std::string::npos) + return ""; + + return filename.substr(0,found+1); +} + +/** + * @brief extract the name from a file-name + * @param filename + * @return the name of file (string behind last / (or /)) + */ +inline std::string extractNameFromFileName(const std::string &filename) +{ + std::size_t found = filename.rfind('/'); + + if (found == std::string::npos) + found = filename.rfind('\\'); // welcome on NTFS ;) + + if (found == std::string::npos) + return filename; + + return filename.substr(found+1); +} + + + +} +} + + + + +#endif + diff --git a/include/Utils/gl_matrices.h b/include/Utils/gl_matrices.h index 152732926b392c878ae49abe20a2451acdf18007..a25cd7afc18f1a66f07bf9f0797b1d9fdc61d227 100644 --- a/include/Utils/gl_matrices.h +++ b/include/Utils/gl_matrices.h @@ -70,25 +70,26 @@ public: void rotate(float angle, const Geom::Vec3f& Axis) { - glm::mat4 X = glm::rotate(m_matrices[2], angle, glm::vec3(Axis[0],Axis[1],Axis[2])) * m_matrices[2]; + glm::mat4 X = glm::rotate(glm::mat4(1.f), angle, glm::vec3(Axis[0],Axis[1],Axis[2])) * m_matrices[2]; m_matrices[2] = X; } void translate(const Geom::Vec3f& P) { - glm::mat4 X = glm::translate(m_matrices[2], glm::vec3(P[0],P[1],P[2])) * m_matrices[2]; + + glm::mat4 X = glm::translate(glm::mat4(1.f), glm::vec3(P[0],P[1],P[2])) * m_matrices[2]; m_matrices[2] = X; } void scale(const Geom::Vec3f& S) { - glm::mat4 X = glm::scale(m_matrices[2], glm::vec3(S[0],S[1],S[2])) * m_matrices[2]; + glm::mat4 X = glm::scale(glm::mat4(1.f), glm::vec3(S[0],S[1],S[2])) * m_matrices[2]; m_matrices[2] = X; } void scale(float s) { - glm::mat4 X = glm::scale(m_matrices[2], glm::vec3(s,s,s)) * m_matrices[2]; + glm::mat4 X = glm::scale(glm::mat4(1.f), glm::vec3(s,s,s)) * m_matrices[2]; m_matrices[2] = X; } diff --git a/src/Topology/gmap/embeddedGMap2.cpp b/src/Topology/gmap/embeddedGMap2.cpp index 06cdd5507ff50e4a178ed207ca953514a7268090..896a32df80ed0916b1ba4ee0505bef06f7afb703 100644 --- a/src/Topology/gmap/embeddedGMap2.cpp +++ b/src/Topology/gmap/embeddedGMap2.cpp @@ -57,19 +57,20 @@ Dart EmbeddedGMap2::newFace(unsigned int nbEdges, bool withBoundary) initOrbitEmbeddingNewCell(phi2(d)) ; } } - else - { - if (isOrbitEmbedded()) - { - Traversor2FV t(*this, d); - for(Dart it = t.begin(); it != t.end(); it = t.next()) - initOrbitEmbeddingNewCell(it) ; - } - if(isOrbitEmbedded()) - initOrbitEmbeddingNewCell(d) ; +// else +// { +// if (isOrbitEmbedded()) +// { +// Traversor2FV t(*this, d); +// for(Dart it = t.begin(); it != t.end(); it = t.next()) +// initOrbitEmbeddingNewCell(it) ; +// } - } +// if(isOrbitEmbedded()) +// initOrbitEmbeddingNewCell(d) ; + +// } return d ; } diff --git a/src/Utils/Shaders/shaderCustom.cpp b/src/Utils/Shaders/shaderCustom.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0226b848b2ce4f0a16598db4f01f775bb4967ee9 --- /dev/null +++ b/src/Utils/Shaders/shaderCustom.cpp @@ -0,0 +1,137 @@ +#include +#include "Utils/Shaders/shaderCustom.h" + + +#include "shaderCustom.vert" +#include "shaderCustom.frag" +#include "shaderCustom.geom" + + +ShaderCustom::ShaderCustom() +{ + m_nameVS = "ShaderCustom_vs"; + m_nameFS = "ShaderCustom_fs"; + m_nameGS = "ShaderCustom_gs"; + + std::string glxvert(*GLSLShader::DEFINES_GL); + glxvert.append(vertexShaderText); + + std::string glxgeom = GLSLShader::defines_Geom("triangles", "triangle_strip", 3); + glxgeom.append(geometryShaderText); + + std::string glxfrag(*GLSLShader::DEFINES_GL); + glxfrag.append(fragmentShaderText); + + loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_TRIANGLE_STRIP, 3); + + bind(); + getLocations(); + unbind(); + + //Default values + m_explode = 1.0f; + m_ambiant = Geom::Vec4f(0.05f, 0.05f, 0.1f, 0.0f); + m_diffuse = Geom::Vec4f(0.1f, 1.0f, 0.1f, 0.0f); + m_light_pos = Geom::Vec3f(10.0f, 10.0f, 1000.0f); + + Geom::Matrix44f id; + id.identity(); + setTransformation(id); + + setParams(m_explode, m_ambiant, m_diffuse, m_light_pos); +} + +void ShaderCustom::getLocations() +{ + *m_unif_explode = glGetUniformLocation(program_handler(), "explode"); + *m_unif_ambiant = glGetUniformLocation(program_handler(), "ambient"); + *m_unif_diffuse = glGetUniformLocation(program_handler(), "diffuse"); + *m_unif_lightPos = glGetUniformLocation(program_handler(), "lightPosition"); +} + +unsigned int ShaderCustom::setAttributePosition(Utils::VBO* vbo) +{ + m_vboPos = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexPosition", vbo); + unbind(); + return id; +} + +void ShaderCustom::setParams(float expl, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec3f& lightPos) +{ + m_explode = expl; + m_ambiant = ambiant; + m_diffuse = diffuse; + m_light_pos = lightPos; + + bind(); + + glUniform1f(*m_unif_explode, expl); + glUniform4fv(*m_unif_ambiant, 1, ambiant.data()); + glUniform4fv(*m_unif_diffuse, 1, diffuse.data()); + glUniform3fv(*m_unif_lightPos, 1, lightPos.data()); + + unbind(); +} + +void ShaderCustom::setExplode(float explode) +{ + m_explode = explode; + bind(); + glUniform1f(*m_unif_explode, explode); + unbind(); +} + +void ShaderCustom::setAmbiant(const Geom::Vec4f& ambiant) +{ + m_ambiant = ambiant; + bind(); + glUniform4fv(*m_unif_ambiant,1, ambiant.data()); + unbind(); +} + +void ShaderCustom::setDiffuse(const Geom::Vec4f& diffuse) +{ + m_diffuse = diffuse; + bind(); + glUniform4fv(*m_unif_diffuse,1, diffuse.data()); + unbind(); +} + +void ShaderCustom::setLightPosition(const Geom::Vec3f& lp) +{ + m_light_pos = lp; + bind(); + glUniform3fv(*m_unif_lightPos,1,lp.data()); + unbind(); +} + +void ShaderCustom::restoreUniformsAttribs() +{ + *m_unif_explode = glGetUniformLocation(program_handler(),"explode"); + *m_unif_ambiant = glGetUniformLocation(program_handler(),"ambient"); + *m_unif_diffuse = glGetUniformLocation(program_handler(),"diffuse"); + *m_unif_lightPos = glGetUniformLocation(program_handler(),"lightPosition"); + + bind(); + + glUniform1f (*m_unif_explode, m_explode); + glUniform4fv(*m_unif_ambiant, 1, m_ambiant.data()); + glUniform4fv(*m_unif_diffuse, 1, m_diffuse.data()); + glUniform3fv(*m_unif_lightPos, 1, m_light_pos.data()); + + bindVA_VBO("VertexPosition", m_vboPos); + + unbind(); +} + +void ShaderCustom::setTransformation(Geom::Matrix44f t) +{ + bind(); + CGoGNGLuint m_transf; + *m_transf = glGetUniformLocation(program_handler(),"TransformationMatrix"); + glUniformMatrix4fv(*m_transf, 1, false, &t(0,0)); + unbind(); +} + diff --git a/src/Utils/Shaders/shaderCustomTex.cpp b/src/Utils/Shaders/shaderCustomTex.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cae72ee6ddd66f0db8c894fc873185e1b0cb8d8f --- /dev/null +++ b/src/Utils/Shaders/shaderCustomTex.cpp @@ -0,0 +1,167 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg * +* * +* This library is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by the * +* Free Software Foundation; either version 2.1 of the License, or (at your * +* option) any later version. * +* * +* This library is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * +* * +* Web site: http://cgogn.unistra.fr/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#include +#include "Utils/Shaders/shaderCustomTex.h" + +#include "shaderCustomTex.vert" +#include "shaderCustomTex.frag" +#include "shaderCustomTex.geom" + +//std::string ShaderCustomTex::vertexShaderText = +// "ATTRIBUTE vec3 VertexPosition;\n" +// "ATTRIBUTE vec2 VertexTexCoord;\n" +// "uniform mat4 ModelViewProjectionMatrix;\n" +// "VARYING_VERT vec2 texCoord;\n" +// "INVARIANT_POS;\n" +// "void main ()\n" +// "{\n" +// " gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);\n" +// " texCoord = VertexTexCoord;\n" +// "}"; +// +// +//std::string ShaderCustomTex::fragmentShaderText = +// "PRECISON;\n" +// "VARYING_FRAG vec2 texCoord;\n" +// "uniform sampler2D textureUnit;\n" +// "FRAG_OUT_DEF;\n" +// "void main()\n" +// "{\n" +// " gl_FragColor=texture2D(textureUnit,texCoord);\n" +// "}"; + + +ShaderCustomTex::ShaderCustomTex() : +m_col(1) +{ + m_nameVS = "ShaderCustomTex_vs"; + m_nameFS = "ShaderCustomTex_fs"; + m_nameGS = "ShaderCustomTex_gs"; + + std::string glxvert(*GLSLShader::DEFINES_GL); + glxvert.append(vertexShaderText); + + std::string glxgeom = GLSLShader::defines_Geom("triangles", "triangle_strip", 3); + glxgeom.append(geometryShaderText); + + std::string glxfrag(*GLSLShader::DEFINES_GL); + glxfrag.append(fragmentShaderText); + +// loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str()); + loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_TRIANGLE_STRIP, 3); + + m_unif_unit = glGetUniformLocation(this->program_handler(), "textureUnit"); + + setBaseColor(m_col); + + Geom::Matrix44f id; + id.identity(); + setTransformation(id); + +} + +void ShaderCustomTex::setTextureUnit(GLenum texture_unit) +{ + this->bind(); + int unit = texture_unit - GL_TEXTURE0; + glUniform1iARB(*m_unif_unit,unit); + m_unit = unit; +} + +void ShaderCustomTex::setTexture(Utils::GTexture* tex) +{ + m_tex_ptr = tex; +} + +void ShaderCustomTex::setBaseColor(Geom::Vec4f col) +{ + m_col = col; + + bind(); + CGoGNGLuint m_unif_ambiant; + *m_unif_ambiant = glGetUniformLocation(program_handler(),"ambient"); + glUniform4fv(*m_unif_ambiant, 1, m_col.data()); + unbind(); +} + +void ShaderCustomTex::setTransformation(Geom::Matrix44f t) +{ + bind(); + CGoGNGLuint m_transf; + *m_transf = glGetUniformLocation(program_handler(),"TransformationMatrix"); + glUniformMatrix4fv(*m_transf, 1, false, &t(0,0)); + unbind(); +} + +void ShaderCustomTex::activeTexture() +{ + glActiveTexture(GL_TEXTURE0 + m_unit); + m_tex_ptr->bind(); +} + +void ShaderCustomTex::activeTexture(CGoGNGLuint texId) +{ + glActiveTexture(GL_TEXTURE0 + m_unit); + glBindTexture(GL_TEXTURE_2D, *texId); +} + +unsigned int ShaderCustomTex::setAttributePosition(Utils::VBO* vbo) +{ + m_vboPos = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexPosition", vbo); + unbind(); + return id; +} + +unsigned int ShaderCustomTex::setAttributeNormal(Utils::VBO* vbo) +{ + m_vboNormal = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexNormal", vbo); + unbind(); + return id; +} + +unsigned int ShaderCustomTex::setAttributeTexCoord(Utils::VBO* vbo) +{ + m_vboTexCoord = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexTexCoord", vbo); + unbind(); + return id; +} + +void ShaderCustomTex::restoreUniformsAttribs() +{ + m_unif_unit = glGetUniformLocation(this->program_handler(), "textureUnit"); + + bindVA_VBO("VertexPosition", m_vboPos); + bindVA_VBO("VertexTexCoord", m_vboTexCoord); + + this->bind(); + glUniform1iARB(*m_unif_unit,m_unit); + this->unbind(); +} diff --git a/src/Utils/Shaders/shaderMatCustom.cpp b/src/Utils/Shaders/shaderMatCustom.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2490871424c8617821e0def37ceb6b11ca5746ec --- /dev/null +++ b/src/Utils/Shaders/shaderMatCustom.cpp @@ -0,0 +1,228 @@ +#include +#include +#include "Utils/Shaders/shaderMatCustom.h" + +#include "shaderMatCustom.vert" +#include "shaderMatCustom.frag" +//#include "shaderMatCustom.geom" + +ShaderMatCustom::ShaderMatCustom(bool doubleSided, bool withEyePosition): + m_with_color(false), + m_with_eyepos(withEyePosition), + m_ambiant(Geom::Vec4f(0.05f,0.05f,0.1f,0.0f)), + m_diffuse(Geom::Vec4f(0.1f,1.0f,0.1f,0.0f)), + m_specular(Geom::Vec4f(1.0f,1.0f,1.0f,0.0f)), + m_shininess(100.0f), + m_lightPos(Geom::Vec3f(10.0f,10.0f,1000.0f)), + m_vboPos(NULL), + m_vboNormal(NULL), + m_vboColor(NULL) +{ + m_nameVS = "ShaderMatCustom_vs"; + m_nameFS = "ShaderMatCustom_fs"; + m_nameGS = "ShaderMatCustom_gs"; + + std::string glxvert(*GLSLShader::DEFINES_GL); + // get choose GL defines (2 or 3) + // ans compile shaders + if (m_with_eyepos) + glxvert.append("#define WITH_EYEPOSITION"); + glxvert.append(vertexShaderText); + +// std::string glxgeom = GLSLShader::defines_Geom("triangles", "triangle_strip", 3); +// glxgeom.append(geometryShaderText); + + std::string glxfrag(*GLSLShader::DEFINES_GL); + // Use double sided lighting if set + if (doubleSided) + glxfrag.append("#define DOUBLE_SIDED\n"); + glxfrag.append(fragmentShaderText); + + loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str()); +// loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_TRIANGLE_STRIP, 3); + + // and get and fill uniforms + getLocations(); + + Geom::Matrix44f id; + id.identity(); + setTransformation(id); + + sendParams(); +} + +void ShaderMatCustom::getLocations() +{ + bind(); + *m_unif_ambiant = glGetUniformLocation(this->program_handler(), "materialAmbient"); + *m_unif_diffuse = glGetUniformLocation(this->program_handler(), "materialDiffuse"); + *m_unif_specular = glGetUniformLocation(this->program_handler(), "materialSpecular"); + *m_unif_shininess = glGetUniformLocation(this->program_handler(), "shininess"); + *m_unif_lightPos = glGetUniformLocation(this->program_handler(), "lightPosition"); + if (m_with_eyepos) + *m_unif_eyePos = glGetUniformLocation(this->program_handler(), "eyePosition"); + unbind(); +} + +void ShaderMatCustom::sendParams() +{ + bind(); + glUniform4fv(*m_unif_ambiant, 1, m_ambiant.data()); + glUniform4fv(*m_unif_diffuse, 1, m_diffuse.data()); + glUniform4fv(*m_unif_specular, 1, m_specular.data()); + glUniform1f(*m_unif_shininess, m_shininess); + glUniform3fv(*m_unif_lightPos, 1, m_lightPos.data()); + if (m_with_eyepos) + glUniform3fv(*m_unif_eyePos, 1, m_eyePos.data()); + unbind(); +} + +void ShaderMatCustom::setAmbiant(const Geom::Vec4f& ambiant) +{ + bind(); + glUniform4fv(*m_unif_ambiant,1, ambiant.data()); + m_ambiant = ambiant; + unbind(); +} + +void ShaderMatCustom::setDiffuse(const Geom::Vec4f& diffuse) +{ + bind(); + glUniform4fv(*m_unif_diffuse,1, diffuse.data()); + m_diffuse = diffuse; + unbind(); +} + +void ShaderMatCustom::setSpecular(const Geom::Vec4f& specular) +{ + bind(); + glUniform4fv(*m_unif_specular,1,specular.data()); + m_specular = specular; + unbind(); +} + +void ShaderMatCustom::setShininess(float shininess) +{ + bind(); + glUniform1f (*m_unif_shininess, shininess); + m_shininess = shininess; + unbind(); +} + +void ShaderMatCustom::setLightPosition(const Geom::Vec3f& lightPos) +{ + bind(); + glUniform3fv(*m_unif_lightPos,1,lightPos.data()); + m_lightPos = lightPos; + unbind(); +} + +void ShaderMatCustom::setEyePosition(const Geom::Vec3f& eyePos) +{ + if (m_with_eyepos) + { + bind(); + glUniform3fv(*m_unif_eyePos,1,eyePos.data()); + m_eyePos = eyePos; + unbind(); + } +} + +void ShaderMatCustom::setParams(const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec4f& specular, float shininess, const Geom::Vec3f& lightPos) +{ + m_ambiant = ambiant; + m_diffuse = diffuse; + m_specular = specular; + m_shininess = shininess; + m_lightPos = lightPos; + sendParams(); +} + +unsigned int ShaderMatCustom::setAttributeColor(CGoGN::Utils::VBO* vbo) +{ + m_vboColor = vbo; + if (!m_with_color) + { + m_with_color=true; + // set the define and recompile shader + std::string gl3vert(*GLSLShader::DEFINES_GL); + gl3vert.append("#define WITH_COLOR 1\n"); + gl3vert.append(vertexShaderText); + std::string gl3frag(*GLSLShader::DEFINES_GL); + gl3frag.append("#define WITH_COLOR 1\n"); + gl3frag.append(fragmentShaderText); + loadShadersFromMemory(gl3vert.c_str(), gl3frag.c_str()); + + // and treat uniforms + getLocations(); + sendParams(); + } + // bind th VA with WBO + bind(); + unsigned int id = bindVA_VBO("VertexColor", vbo); + unbind(); + return id; +} + +void ShaderMatCustom::unsetAttributeColor() +{ + m_vboColor = NULL; + if (m_with_color) + { + m_with_color = false; + // unbind the VA + bind(); + unbindVA("VertexColor"); + unbind(); + // recompile shader + std::string gl3vert(*GLSLShader::DEFINES_GL); + gl3vert.append(vertexShaderText); + std::string gl3frag(*GLSLShader::DEFINES_GL); + gl3frag.append(fragmentShaderText); + loadShadersFromMemory(gl3vert.c_str(), gl3frag.c_str()); + // and treat uniforms + getLocations(); + sendParams(); + } +} + +void ShaderMatCustom::restoreUniformsAttribs() +{ + getLocations(); + sendParams(); + + bind(); + bindVA_VBO("VertexPosition", m_vboPos); + bindVA_VBO("VertexNormal", m_vboNormal); + if (m_vboColor) + bindVA_VBO("VertexColor", m_vboColor); + + unbind(); +} + +unsigned int ShaderMatCustom::setAttributePosition(CGoGN::Utils::VBO* vbo) +{ + m_vboPos = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexPosition", vbo); + unbind(); + return id; +} + +unsigned int ShaderMatCustom::setAttributeNormal(CGoGN::Utils::VBO* vbo) +{ + m_vboNormal = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexNormal", vbo); + unbind(); + return id; +} + +void ShaderMatCustom::setTransformation(Geom::Matrix44f t) +{ + bind(); + CGoGNGLuint m_transf; + *m_transf = glGetUniformLocation(program_handler(),"TransformationMatrix"); + glUniformMatrix4fv(*m_transf, 1, false, &t(0,0)); + unbind(); +}