From cd514af028d5f99a9c1e1996e3261f4142a25da6 Mon Sep 17 00:00:00 2001 From: Sylvain Thery Date: Fri, 17 Feb 2012 09:11:50 +0100 Subject: [PATCH] add rendering algo to fill vbo for color(s) per face (use with any kind of shader that support position & color --- include/Algo/Render/GL2/colorPerFaceRender.h | 95 ++++++++++++++ .../Algo/Render/GL2/colorPerFaceRender.hpp | 122 ++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 include/Algo/Render/GL2/colorPerFaceRender.h create mode 100644 include/Algo/Render/GL2/colorPerFaceRender.hpp diff --git a/include/Algo/Render/GL2/colorPerFaceRender.h b/include/Algo/Render/GL2/colorPerFaceRender.h new file mode 100644 index 00000000..f5a78a5f --- /dev/null +++ b/include/Algo/Render/GL2/colorPerFaceRender.h @@ -0,0 +1,95 @@ +/******************************************************************************* +* 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 _COLOR_PER_FACE_RENDER +#define _COLOR_PER_FACE_RENDER + +#include + +#include "Topology/generic/dart.h" +#include "Topology/generic/attributeHandler.h" +#include "Topology/generic/functor.h" +#include "Utils/vbo.h" +#include "Utils/GLSLShader.h" + + +namespace CGoGN +{ + +namespace Algo +{ + +namespace Render +{ + +namespace GL2 +{ + +/** + * Class that allors + */ +class ColorPerFaceRender +{ +protected: + Utils::VBO& m_vboPos; + + Utils::VBO& m_vboColors; + + GLuint m_nbTris; + + +public: + /** + * Constructor + */ + ColorPerFaceRender() ; + + /** + * update drawing buffers + * @param map the map + * @param positions attribute of position vertices + * @param colorPerFace attribute of color (per face, or per vertex per face) + * @param good selector + */ + template + void updateData(typename PFP::MAP& map, typename PFP::TVEC3& positions, typename PFP::TVEC3& colorPerFace, const FunctorSelect& good = allDarts) ; + + /** + * draw + */ + void draw(Utils::GLSLShader* sh) ; + +}; + +}//end namespace GL2 + +}//end namespace Algo + +}//end namespace Render + +}//end namespace CGoGN + +#include "Algo/Render/GL2/colorPerFaceRender.hpp" + +#endif diff --git a/include/Algo/Render/GL2/colorPerFaceRender.hpp b/include/Algo/Render/GL2/colorPerFaceRender.hpp new file mode 100644 index 00000000..83354472 --- /dev/null +++ b/include/Algo/Render/GL2/colorPerFaceRender.hpp @@ -0,0 +1,122 @@ +/******************************************************************************* +* 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 "Geometry/vector_gen.h" +#include "Topology/generic/dartmarker.h" +#include "Topology/generic/cellmarker.h" +#include "Algo/Geometry/centroid.h" + +namespace CGoGN +{ + +namespace Algo +{ + +namespace Render +{ + +namespace GL2 +{ + + +inline ColorPerFaceRender::ColorPerFaceRender(Utils::VBO& vboPosition, Utils::VBO& vboColor): + m_vboPosition(vboPosition), m_vboColor(vboColor), m_nbTris(0) +{ +} + + +template +void ColorPerFaceRender::updateData(typename PFP::MAP& map, typename PFP::TVEC3& positions, typename PFP::TVEC3& colorPerXXX, const FunctorSelect& good) +{ + typedef typename PFP::VEC3 VEC3; + typedef typename PFP::REAL REAL; + + CellMarker cmv(map,VOLUME); + AutoAttributeHandler centerVolumes(map,VOLUME,"centerVolumes"); + TraversorW traVol(map,good); + for (Dart d=traVol.begin(); d!=traVol.end(); d=traVol.next()) + { + centerVolumes[d] = Algo::Geometry::volumeCentroid(map, d, positions); + } + + std::vector buffer; + buffer.reserve(16384); + + std::vector bufferColors; + bufferColors.reserve(16384); + + TraversorCell traFace(map, PFP::MAP::ORBIT_IN_PARENT(FACE),good); + + for (Dart d=traFace.begin(); d!=traFace.end(); d=traFace.next()) + { + Dart a = d; + Dart b = map.phi1(a); + Dart c = map.phi1(b); + // loop to cut a polygon in triangle on the fly (works only with convex faces) + do + { + buffer.push_back(positions[d]); + bufferColors.push_back(colorPerXXX[d]); + buffer.push_back(positions[b]); + bufferColors.push_back(colorPerXXX[b]); + buffer.push_back(positions[c]); + bufferColors.push_back(colorPerXXX[c]); + b = c; + c = map.phi1(b); + } while (c != d); + } + + m_nbTris = buffer.size()/4; + + m_vboPos->allocate(buffer.size()); + VEC3* ptrPos = reinterpret_cast(m_vboPos->lockPtr()); + memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3)); + m_vboPos->releasePtr(); + + m_vboColors->allocate(bufferColors.size()); + VEC3* ptrCol = reinterpret_cast(m_vboColors->lockPtr()); + memcpy(ptrCol,&bufferColors[0],bufferColors.size()*sizeof(VEC3)); + m_vboColors->releasePtr(); + +} + + +void ColorPerFaceRender::draw(Utils::GLSLShader* sh) +{ + sh->enableVertexAttribs(); + glDrawArrays(GL_TRIANGLES , 0 , m_nbTris*3 ); + sh->disableVertexAttribs(); +} + + + + +}//end namespace VBO + +}//end namespace Algo + +}//end namespace Render + +}//end namespace CGoGN -- GitLab