diff --git a/include/Utils/clippingPresets.h b/include/Utils/clippingPresets.h new file mode 100644 index 0000000000000000000000000000000000000000..d710bf6fed75241d6a360ca2482ec0789a4b49d4 --- /dev/null +++ b/include/Utils/clippingPresets.h @@ -0,0 +1,153 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009-2011, 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.u-strasbg.fr/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#ifndef _CGoGN_CLIPPINGPRESETS_H_ +#define _CGoGN_CLIPPINGPRESETS_H_ + +#include "Utils/clippingShader.h" +#include "Geometry/vector_gen.h" +#include + +namespace CGoGN +{ + +namespace Utils +{ + +class ClippingPreset +{ + + + /*********************************************** + * + * Constructors + * + ***********************************************/ + +public : + + /// public static constructor + static ClippingPreset* CreateEmptyPreset(); + + /** + * public static constructor + * @param center center between planes + * @param distance distance between planes + * @param axis axis on which planes are aligned (0 for x, 1 for y, 2 for z) + * @param facing true means having facing planes + */ + static ClippingPreset* CreateDualPlanesPreset(Geom::Vec3f center, float distance, int axis, bool facing); + + /** + * public static constructor + * @param center center between planes + * @param distance distance between planes + * @param facing true means having facing planes + */ + static ClippingPreset* CreateCubePreset(Geom::Vec3f center, float distance, bool facing); + +private : + + /// private constructor (used by public static constructors) + ClippingPreset(); + + + /*********************************************** + * + * Preset settings + * + ***********************************************/ + +public : + + /** + * adds a clip plane to the preset + * @param normal clip plane normal + * @param origin clip plane origin + */ + void addClipPlane(Geom::Vec3f normal, Geom::Vec3f origin); + + /** + * adds a clip sphere to the preset + * @param center clip sphere center + * @param radius clip sphere radius + */ + void addClipSphere(Geom::Vec3f center, float radius); + + /** + * sets the clipping mode + * @param clipMode clipping mode + */ + void setClippingMode(ClippingShader::clippingMode clipMode); + +private : + + /// clip planes structure + struct clipPlane + { + Geom::Vec3f normal, origin; + }; + + /// clip planes array + std::vector m_clipPlanes; + + /// clip spheres structure + struct clipSphere + { + Geom::Vec3f center; + float radius; + }; + + /// clip spheres array + std::vector m_clipSpheres; + + /// clipping mode + ClippingShader::clippingMode m_clipMode; + + + /*********************************************** + * + * Preset application + * + ***********************************************/ + +public : + + /** + * applies the preset on a clipping shader + * @param clipShader pointer to the clipping shader object + * @param planesIds returns the new added planes ids + * @param spheresIds returns the new added spheres ids + * @warning planesIds and spheresIds must not be NULL, otherwise the function does nothing + */ + void apply(ClippingShader* clipShader, std::vector *planesIds, std::vector *spheresIds); + + +}; + +} // namespace Utils + +} // namespace CGoGN + +#endif diff --git a/include/Utils/clippingShader.h b/include/Utils/clippingShader.h index 11fa9f3280c8557322f0018d3d191f7ed3e16cff..e2d39d90bcaed329b796d85fed6773b3ed155355 100644 --- a/include/Utils/clippingShader.h +++ b/include/Utils/clippingShader.h @@ -69,7 +69,7 @@ public: * @return clip plane id (positive value on success, else -1) * @warning insertClippingCode must be called first */ - int addClipPlane(); + unsigned int addClipPlane(); /* * deletes a clip plane @@ -77,6 +77,9 @@ public: */ void deleteClipPlane(unsigned int id); + /// deletes all clip planes + void deleteAllClipPlanes(); + /// gets the clip planes count int getClipPlanesCount(); @@ -164,7 +167,7 @@ public: * @return clip sphere id (positive value on success, else -1) * @warning insertClippingCode must be called first */ - int addClipSphere(); + unsigned int addClipSphere(); /* * deletes a clip sphere @@ -172,6 +175,9 @@ public: */ void deleteClipSphere(unsigned int id); + /// deletes all clip spheres + void deleteAllClipSpheres(); + /// gets the clip spheres count int getClipSpheresCount(); diff --git a/src/Utils/clippingPresets.cpp b/src/Utils/clippingPresets.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef1602576a242d3b8f22db7049eac34117c51fd5 --- /dev/null +++ b/src/Utils/clippingPresets.cpp @@ -0,0 +1,175 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009-2011, 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.u-strasbg.fr/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#include "Utils/clippingPresets.h" + +namespace CGoGN +{ + +namespace Utils +{ + + +/*********************************************** + * + * Constructors + * + ***********************************************/ + + +ClippingPreset* ClippingPreset::CreateEmptyPreset() +{ + ClippingPreset *preset = new ClippingPreset; + + return preset; +} + +ClippingPreset* ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f center, float distance, int axis, bool facing) +{ + ClippingPreset *preset = new ClippingPreset; + + // Axis on which planes will be aligned + if ((axis < 0) || (axis > 2)) + axis = 0; + Geom::Vec3f positDir (0.0, 0.0, 0.0); + positDir[axis] = 1.0; + Geom::Vec3f negDir (0.0, 0.0, 0.0); + negDir[axis] = -1.0; + + // Add planes to preset + preset->addClipPlane(positDir, center + positDir*(distance / 2.0)); + preset->addClipPlane(negDir, center + negDir*(distance / 2.0)); + + // Set clipping mode + preset->setClippingMode(ClippingShader::CLIPPING_MODE_AND); + + return preset; +} + +ClippingPreset* ClippingPreset::CreateCubePreset(Geom::Vec3f center, float distance, bool facing) +{ + ClippingPreset *preset = new ClippingPreset; + + // Directions + Geom::Vec3f xAxisPos = Geom::Vec3f (1.0, 0.0, 0.0); + Geom::Vec3f xAxisNeg = Geom::Vec3f (-1.0, 0.0, 0.0); + Geom::Vec3f yAxisPos = Geom::Vec3f (0.0, 1.0, 0.0); + Geom::Vec3f yAxisNeg = Geom::Vec3f (0.0, -1.0, 0.0); + Geom::Vec3f zAxisPos = Geom::Vec3f (0.0, 0.0, 1.0); + Geom::Vec3f zAxisNeg = Geom::Vec3f (0.0, 0.0, -1.0); + + // Add planes to preset + preset->addClipPlane(xAxisPos, center + xAxisPos*(distance / 2.0)); + preset->addClipPlane(xAxisNeg, center + xAxisNeg*(distance / 2.0)); + preset->addClipPlane(yAxisPos, center + yAxisPos*(distance / 2.0)); + preset->addClipPlane(yAxisNeg, center + yAxisNeg*(distance / 2.0)); + preset->addClipPlane(zAxisPos, center + zAxisPos*(distance / 2.0)); + preset->addClipPlane(zAxisNeg, center + zAxisNeg*(distance / 2.0)); + + // Set clipping mode + preset->setClippingMode(ClippingShader::CLIPPING_MODE_AND); + + return preset; +} + +ClippingPreset::ClippingPreset(): + m_clipMode (ClippingShader::CLIPPING_MODE_AND) +{ + +} + + +/*********************************************** + * + * Preset settings + * + ***********************************************/ + + +void ClippingPreset::addClipPlane(Geom::Vec3f normal, Geom::Vec3f origin) +{ + clipPlane newPlane; + newPlane.normal = normal; + newPlane.origin = origin; + + m_clipPlanes.push_back(newPlane); +} + +void ClippingPreset::addClipSphere(Geom::Vec3f center, float radius) +{ + clipSphere newSphere; + newSphere.center = center; + newSphere.radius = radius; + + m_clipSpheres.push_back(newSphere); +} + +void ClippingPreset::setClippingMode(ClippingShader::clippingMode clipMode) +{ + m_clipMode = clipMode; +} + + +/*********************************************** + * + * Preset application + * + ***********************************************/ + + +void ClippingPreset::apply(ClippingShader* clipShader, std::vector *planesIds, std::vector *spheresIds) +{ + if ( (clipShader == NULL) || (planesIds == NULL) || (spheresIds == NULL) ) + return; + + // Delete any remaining clip planes or spheres + clipShader->deleteAllClipPlanes(); + clipShader->deleteAllClipSpheres(); + + // Initialize ids arrays (they should already be of zero size) + planesIds->resize(0); + spheresIds->resize(0); + + // Add planes and spheres and get their ids + for (size_t i = 0; i < m_clipPlanes.size(); i++) + { + unsigned int id = clipShader->addClipPlane(); + planesIds->push_back(id); + clipShader->setClipPlaneParamsAll(id, m_clipPlanes[i].normal, m_clipPlanes[i].origin); + } + for (size_t i = 0; i < m_clipSpheres.size(); i++) + { + unsigned int id = clipShader->addClipSphere(); + spheresIds->push_back(id); + clipShader->setClipSphereParamsAll(id, m_clipSpheres[i].center, m_clipSpheres[i].radius); + } + + // Set new clipping mode + clipShader->setClipMode(m_clipMode); + +} + +} // namespace Utils + +} // namespace CGoGN diff --git a/src/Utils/clippingShader.cpp b/src/Utils/clippingShader.cpp index 97547facebf1c9dce55e2a4456fdd3023d5d76ed..b017209d722f6435e19274e11816078203fa0915 100644 --- a/src/Utils/clippingShader.cpp +++ b/src/Utils/clippingShader.cpp @@ -61,7 +61,7 @@ ClippingShader::ClippingShader(): ***********************************************/ -int ClippingShader::addClipPlane() +unsigned int ClippingShader::addClipPlane() { // Check if the clipping code has been inserted into shader if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::addClipPlane")) @@ -159,6 +159,38 @@ void ClippingShader::deleteClipPlane(unsigned int id) recompile(); } +void ClippingShader::deleteAllClipPlanes() +{ + // Check if the clipping code has been inserted into shader + if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::deleteAllClipPlanes")) + return; + + // Shader name string + std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS; + + // Use a shader mutator + ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc()); + + // Modify the clip planes count constant in both shader + if (errorRaiseShaderMutatorFailure( + (!SM.changeIntConstantValue(ShaderMutator::VERTEX_SHADER, "CLIP_PLANES_COUNT", 0)) + || (!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_PLANES_COUNT", 0)), + "ClippingShader::deleteAllClipPlanes")) + return; + + // Reload both shaders + reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str()); + reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str()); + + // Rearrange planes arrays + m_clipPlanes.resize(0); + m_clipPlanesIds.resize(0); + m_clipPlanesEquations.resize(0); + + // Recompile shaders (automatically calls updateClippingUniforms) + recompile(); +} + int ClippingShader::getClipPlanesCount() { return (int)m_clipPlanes.size(); @@ -323,7 +355,7 @@ void ClippingShader::updateClipPlaneUniformsArray(unsigned int id) ***********************************************/ -int ClippingShader::addClipSphere() +unsigned int ClippingShader::addClipSphere() { // Check if the clipping code has been inserted into shader if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::addClipSphere")) @@ -421,6 +453,38 @@ void ClippingShader::deleteClipSphere(unsigned int id) recompile(); } +void ClippingShader::deleteAllClipSpheres() +{ + // Check if the clipping code has been inserted into shader + if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::deleteAllClipSpheres")) + return; + + // Shader name string + std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS; + + // Use a shader mutator + ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc()); + + // Modify the clip spheres count constant in both shader + if (errorRaiseShaderMutatorFailure( + (!SM.changeIntConstantValue(ShaderMutator::VERTEX_SHADER, "CLIP_SPHERES_COUNT", 0)) + || (!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_SPHERES_COUNT", 0)), + "ClippingShader::deleteAllClipSpheres")) + return; + + // Reload both shaders + reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str()); + reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str()); + + // Rearrange spheres arrays + m_clipSpheres.resize(0); + m_clipSpheresIds.resize(0); + m_clipSpheresCentersAndRadiuses.resize(0); + + // Recompile shaders (automatically calls updateClippingUniforms) + recompile(); +} + int ClippingShader::getClipSpheresCount() { return (int)m_clipSpheres.size();