Commit 95211aba authored by Sylvain Thery's avatar Sylvain Thery

finish ExplodeSmoothVolumes

parent 3f6ed5bb
......@@ -57,18 +57,20 @@ protected:
Utils::ShaderExplodeSmoothVolumes* m_shaderS;
bool m_smooth;
bool m_cpf;
bool m_ef;
bool m_smooth;
Utils::ShaderExplodeVolumesLines* m_shaderL;
Utils::VBO* m_vboPos;
Utils::VBO* m_vboColors;
Utils::VBO* m_vboNormals;
Utils::VBO* m_vboPosLine;
/**
......@@ -80,6 +82,13 @@ protected:
Geom::Vec3f m_globalColor;
template<typename PFP>
void updateSmooth(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const VolumeAttribute<typename PFP::VEC3>& colorPerFace, const FunctorSelect& good = allDarts) ;
template<typename PFP>
void updateSmooth(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const FunctorSelect& good = allDarts) ;
public:
/**
* Constructor
......@@ -109,8 +118,8 @@ public:
* @param positions attribute of position vertices
* @param good selector
*/
// template<typename PFP>
// void updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const FunctorSelect& good = allDarts) ;
template<typename PFP>
void updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const FunctorSelect& good = allDarts) ;
/**
* update all drawing buffers
......@@ -123,9 +132,6 @@ public:
void updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const VolumeAttribute<typename PFP::VEC3>& colorPerFace, const FunctorSelect& good = allDarts) ;
template<typename PFP>
void updateSmooth(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const VolumeAttribute<typename PFP::VEC3>& colorPerFace, const FunctorSelect& good = allDarts) ;
/**
* draw edges
*/
......
......@@ -29,6 +29,8 @@
#include "Algo/Geometry/centroid.h"
#include "Topology/generic/autoAttributeHandler.h"
#include "Algo/Geometry/basic.h"
namespace CGoGN
{
......@@ -51,7 +53,6 @@ inline ExplodeVolumeRender::ExplodeVolumeRender(bool withColorPerFace, bool with
m_vboColors = new Utils::VBO();
m_vboColors->setDataSize(3);
m_vboPosLine = new Utils::VBO();
m_vboPosLine->setDataSize(3);
......@@ -59,11 +60,14 @@ inline ExplodeVolumeRender::ExplodeVolumeRender(bool withColorPerFace, bool with
{
m_shaderS = new Utils::ShaderExplodeSmoothVolumes(withColorPerFace,withExplodeFace);
m_shader = NULL;
m_vboNormals = new Utils::VBO();
m_vboNormals->setDataSize(3);
}
else
{
m_shader = new Utils::ShaderExplodeVolumes(withColorPerFace,withExplodeFace);
m_shaderS = NULL;
m_vboNormals = NULL;
}
m_shaderL = new Utils::ShaderExplodeVolumesLines();
......@@ -76,6 +80,9 @@ inline ExplodeVolumeRender::~ExplodeVolumeRender()
delete m_vboPos;
delete m_vboColors;
delete m_vboPosLine;
if (m_vboNormals != NULL)
delete m_vboNormals;
if (m_shader != NULL)
delete m_shader;
if (m_shaderS != NULL)
......@@ -83,98 +90,6 @@ inline ExplodeVolumeRender::~ExplodeVolumeRender()
delete m_shaderL;
}
//template<typename PFP>
//void ExplodeVolumeRender::updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const FunctorSelect& good)
//{
// if (m_cpf)
// {
// CGoGNerr<< "ExplodeVolumeRender: problem wrong update fonction use the other" << CGoGNendl;
// return;
// }
//
// typedef typename PFP::VEC3 VEC3;
// typedef typename PFP::REAL REAL;
//
// VolumeAutoAttribute<VEC3> centerVolumes(map, "centerVolumes");
// Algo::Volume::Geometry::Parallel::computeCentroidVolumes<PFP>(map, positions, centerVolumes, good);
//
// std::vector<VEC3> buffer;
// buffer.reserve(16384);
//
// TraversorCell<typename PFP::MAP, PFP::MAP::FACE_OF_PARENT> traFace(map, good);
//
// for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
// {
// if (m_ef)
// {
// VEC3 centerFace = Algo::Surface::Geometry::faceCentroid<PFP>(map, d, positions);
// 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(centerVolumes[d]);
// buffer.push_back(positions[d]);
// buffer.push_back(positions[b]);
// buffer.push_back(positions[c]);
// buffer.push_back(centerFace); // not used
// buffer.push_back(centerFace);
// b = c;
// c = map.phi1(b);
//
// } while (c != d);
// }
// else
// {
// 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(centerVolumes[d]);
// buffer.push_back(positions[d]);
// buffer.push_back(positions[b]);
// buffer.push_back(positions[c]);
// b = c;
// c = map.phi1(b);
//
// } while (c != d);
// }
// }
//
// m_nbTris = buffer.size()/4;
//
// m_vboPos->allocate(buffer.size());
// VEC3* ptrPos = reinterpret_cast<VEC3*>(m_vboPos->lockPtr());
// memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));
// m_vboPos->releasePtr();
// m_shader->setAttributePosition(m_vboPos);
//
// buffer.clear();
//
// TraversorCell<typename PFP::MAP, PFP::MAP::EDGE_OF_PARENT> traEdge(map, good);
// for (Dart d = traEdge.begin(); d != traEdge.end(); d = traEdge.next())
// {
// buffer.push_back(centerVolumes[d]);
// buffer.push_back(positions[d]);
// buffer.push_back(positions[map.phi1(d)]);
// }
//
// m_nbLines = buffer.size()/3;
//
// m_vboPosLine->allocate(buffer.size());
//
// ptrPos = reinterpret_cast<VEC3*>(m_vboPosLine->lockPtr());
// memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));
//
// m_vboPosLine->releasePtr();
// m_shaderL->setAttributePosition(m_vboPosLine);
//}
template<typename PFP>
......@@ -199,28 +114,30 @@ void ExplodeVolumeRender::updateSmooth(typename PFP::MAP& map, const VertexAttri
TraversorCell<typename PFP::MAP, PFP::MAP::FACE_OF_PARENT> traFace(map, good);
if (m_cpf)
{
for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
{
// compute normals
normals.clear();
VEC3 centerFace(0);
unsigned int nbs=0;
Dart a = d;
do
{
VEC3 v1 = Algo::Surface::Geometry::vectorOutOfDart<PFP>(map,a,position);
VEC3 v1 = Algo::Surface::Geometry::vectorOutOfDart<PFP>(map,a,positions);
Dart e = map.phi1(a);
VEC3 v2 = Algo::Surface::Geometry::vectorOutOfDart<PFP>(map,e,position);
VEC3 v2 = Algo::Surface::Geometry::vectorOutOfDart<PFP>(map,e,positions);
VEC3 N = v1^v2;
N.normalize();
normals.push_back(N);
a = e;
centerFace += positions[d];
nbs++;
} while (a != d);
VEC3 centerFace = Algo::Surface::Geometry::faceCentroid<PFP>(map, d, positions);
std::vector<VEC3>::iterator in = normals.begin();
centerFace /= float(nbs);
typename std::vector<VEC3>::iterator in = normals.begin();
a = d;
Dart b = map.phi1(a);
Dart c = map.phi1(b);
......@@ -238,18 +155,102 @@ void ExplodeVolumeRender::updateSmooth(typename PFP::MAP& map, const VertexAttri
bufferNormals.push_back(*in++);
buffer.push_back(positions[c]);
bufferColors.push_back(colorPerXXX[c]);
bufferNormals.push_back(*in++);
bufferNormals.push_back(*in);
b = c;
c = map.phi1(b);
} while (c != d);
}
}
else
m_nbTris = buffer.size()/4;
m_vboPos->allocate(buffer.size());
VEC3* ptrPos = reinterpret_cast<VEC3*>(m_vboPos->lockPtr());
memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));
m_vboPos->releasePtr();
m_shaderS->setAttributePosition(m_vboPos);
m_vboColors->allocate(bufferColors.size());
VEC3* ptrCol = reinterpret_cast<VEC3*>(m_vboColors->lockPtr());
memcpy(ptrCol,&bufferColors[0],bufferColors.size()*sizeof(VEC3));
m_vboColors->releasePtr();
m_shaderS->setAttributeColor(m_vboColors);
m_vboNormals->allocate(bufferNormals.size());
VEC3* ptrNorm = reinterpret_cast<VEC3*>(m_vboNormals->lockPtr());
memcpy(ptrNorm,&bufferNormals[0],bufferNormals.size()*sizeof(VEC3));
m_vboNormals->releasePtr();
m_shaderS->setAttributeNormal(m_vboNormals);
buffer.clear();
TraversorCell<typename PFP::MAP, PFP::MAP::EDGE_OF_PARENT> traEdge(map, good);
for (Dart d = traEdge.begin(); d != traEdge.end(); d = traEdge.next())
{
buffer.push_back(centerVolumes[d]);
buffer.push_back(positions[d]);
buffer.push_back(positions[map.phi1(d)]);
}
m_nbLines = buffer.size()/3;
m_vboPosLine->allocate(buffer.size());
ptrPos = reinterpret_cast<VEC3*>(m_vboPosLine->lockPtr());
memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));
m_vboPosLine->releasePtr();
m_shaderL->setAttributePosition(m_vboPosLine);
}
template<typename PFP>
void ExplodeVolumeRender::updateSmooth(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const FunctorSelect& good)
{
typedef typename PFP::VEC3 VEC3;
typedef typename PFP::REAL REAL;
VolumeAutoAttribute<VEC3> centerVolumes(map, "centerVolumes");
Algo::Volume::Geometry::Parallel::computeCentroidVolumes<PFP>(map, positions, centerVolumes, good);
std::vector<VEC3> buffer;
buffer.reserve(16384);
std::vector<VEC3> bufferColors;
bufferColors.reserve(16384);
std::vector<VEC3> bufferNormals;
bufferNormals.reserve(16384);
std::vector<VEC3> normals;
bufferNormals.reserve(20);
TraversorCell<typename PFP::MAP, PFP::MAP::FACE_OF_PARENT> traFace(map, good);
for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
{
VEC3 centerFace = Algo::Surface::Geometry::faceCentroid<PFP>(map, d, positions);
// compute normals
normals.clear();
VEC3 centerFace(0);
unsigned int nbs=0;
Dart a = d;
do
{
VEC3 v1 = Algo::Surface::Geometry::vectorOutOfDart<PFP>(map,a,positions);
Dart e = map.phi1(a);
VEC3 v2 = Algo::Surface::Geometry::vectorOutOfDart<PFP>(map,e,positions);
VEC3 N = v1^v2;
N.normalize();
normals.push_back(N);
a = e;
centerFace += positions[d];
nbs++;
} while (a != d);
centerFace /= float(nbs);
typename std::vector<VEC3>::iterator in = normals.begin();
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)
......@@ -257,21 +258,21 @@ void ExplodeVolumeRender::updateSmooth(typename PFP::MAP& map, const VertexAttri
{
buffer.push_back(centerVolumes[d]);
bufferColors.push_back(centerFace);
bufferNormals.push_back(centerFace);
bufferNormals.push_back(centerFace); // unsused just for fill
buffer.push_back(positions[d]);
bufferColors.push_back(m_globalColor);
bufferNormals.push_back();
bufferNormals.push_back(normals.back());
buffer.push_back(positions[b]);
bufferColors.push_back(m_globalColor);
bufferNormals.push_back();
bufferNormals.push_back(*in++);
buffer.push_back(positions[c]);
bufferColors.push_back(m_globalColor);
bufferNormals.push_back();
bufferNormals.push_back(*in);
b = c;
c = map.phi1(b);
} while (c != d);
}
}
m_nbTris = buffer.size()/4;
......@@ -279,13 +280,19 @@ void ExplodeVolumeRender::updateSmooth(typename PFP::MAP& map, const VertexAttri
VEC3* ptrPos = reinterpret_cast<VEC3*>(m_vboPos->lockPtr());
memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));
m_vboPos->releasePtr();
m_shader->setAttributePosition(m_vboPos);
m_shaderS->setAttributePosition(m_vboPos);
m_vboColors->allocate(bufferColors.size());
VEC3* ptrCol = reinterpret_cast<VEC3*>(m_vboColors->lockPtr());
memcpy(ptrCol,&bufferColors[0],bufferColors.size()*sizeof(VEC3));
m_vboColors->releasePtr();
m_shader->setAttributeColor(m_vboColors);
m_shaderS->setAttributeColor(m_vboColors);
m_vboNormals->allocate(bufferNormals.size());
VEC3* ptrNorm = reinterpret_cast<VEC3*>(m_vboNormals->lockPtr());
memcpy(ptrNorm,&bufferNormals[0],bufferNormals.size()*sizeof(VEC3));
m_vboNormals->releasePtr();
m_shaderS->setAttributeNormal(m_vboNormals);
buffer.clear();
......@@ -309,19 +316,21 @@ void ExplodeVolumeRender::updateSmooth(typename PFP::MAP& map, const VertexAttri
}
template<typename PFP>
void ExplodeVolumeRender::updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const VolumeAttribute<typename PFP::VEC3>& colorPerXXX, const FunctorSelect& good)
{
// if (!m_cpf)
// {
// CGoGNerr<< "ExplodeVolumeRender: problem wrong update fonction use the other" << CGoGNendl;
// return;
// }
if (!m_cpf)
{
CGoGNerr<< "ExplodeVolumeRender: problem wrong update fonction use the other (without VolumeAttribute parameter)" << CGoGNendl;
return;
}
if (m_smooth)
{
updateSmooth<PFP>(map,positions,colorPerXXX,good);
return;
}
......@@ -339,8 +348,7 @@ void ExplodeVolumeRender::updateData(typename PFP::MAP& map, const VertexAttribu
bufferColors.reserve(16384);
TraversorCell<typename PFP::MAP, PFP::MAP::FACE_OF_PARENT> traFace(map, good);
if (m_cpf)
{
for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
{
VEC3 centerFace = Algo::Surface::Geometry::faceCentroid<PFP>(map, d, positions);
......@@ -363,12 +371,77 @@ void ExplodeVolumeRender::updateData(typename PFP::MAP& map, const VertexAttribu
c = map.phi1(b);
} while (c != d);
}
m_nbTris = buffer.size()/4;
m_vboPos->allocate(buffer.size());
VEC3* ptrPos = reinterpret_cast<VEC3*>(m_vboPos->lockPtr());
memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));
m_vboPos->releasePtr();
m_shader->setAttributePosition(m_vboPos);
m_vboColors->allocate(bufferColors.size());
VEC3* ptrCol = reinterpret_cast<VEC3*>(m_vboColors->lockPtr());
memcpy(ptrCol,&bufferColors[0],bufferColors.size()*sizeof(VEC3));
m_vboColors->releasePtr();
m_shader->setAttributeColor(m_vboColors);
buffer.clear();
TraversorCell<typename PFP::MAP, PFP::MAP::EDGE_OF_PARENT> traEdge(map, good);
for (Dart d = traEdge.begin(); d != traEdge.end(); d = traEdge.next())
{
buffer.push_back(centerVolumes[d]);
buffer.push_back(positions[d]);
buffer.push_back(positions[map.phi1(d)]);
}
else
m_nbLines = buffer.size()/3;
m_vboPosLine->allocate(buffer.size());
ptrPos = reinterpret_cast<VEC3*>(m_vboPosLine->lockPtr());
memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));
m_vboPosLine->releasePtr();
m_shaderL->setAttributePosition(m_vboPosLine);
}
template<typename PFP>
void ExplodeVolumeRender::updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, const FunctorSelect& good)
{
if (m_cpf)
{
CGoGNerr<< "ExplodeVolumeRender: problem wrong update fonction use the other (without VolumeAttribute parameter)" << CGoGNendl;
return;
}
if (m_smooth)
{
updateSmooth<PFP>(map,positions,good);
return;
}
typedef typename PFP::VEC3 VEC3;
typedef typename PFP::REAL REAL;
VolumeAutoAttribute<VEC3> centerVolumes(map, "centerVolumes");
Algo::Volume::Geometry::Parallel::computeCentroidVolumes<PFP>(map, positions, centerVolumes, good);
std::vector<VEC3> buffer;
buffer.reserve(16384);
std::vector<VEC3> bufferColors;
bufferColors.reserve(16384);
TraversorCell<typename PFP::MAP, PFP::MAP::FACE_OF_PARENT> traFace(map, good);
for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
{
VEC3 centerFace = Algo::Surface::Geometry::faceCentroid<PFP>(map, d, positions);
Dart a = d;
Dart b = map.phi1(a);
Dart c = map.phi1(b);
......@@ -387,7 +460,6 @@ void ExplodeVolumeRender::updateData(typename PFP::MAP& map, const VertexAttribu
c = map.phi1(b);
} while (c != d);
}
}
m_nbTris = buffer.size()/4;
......@@ -424,11 +496,22 @@ void ExplodeVolumeRender::updateData(typename PFP::MAP& map, const VertexAttribu
m_shaderL->setAttributePosition(m_vboPosLine);
}
inline void ExplodeVolumeRender::drawFaces()
{
if (m_smooth)
{
m_shaderS->enableVertexAttribs();
glDrawArrays(GL_LINES_ADJACENCY_EXT , 0 , m_nbTris*4 );
m_shaderS->disableVertexAttribs();
}
else
{
m_shader->enableVertexAttribs();
glDrawArrays(GL_LINES_ADJACENCY_EXT , 0 , m_nbTris*4 );
m_shader->disableVertexAttribs();
}
}
inline void ExplodeVolumeRender::drawEdges()
......@@ -441,17 +524,26 @@ inline void ExplodeVolumeRender::drawEdges()
inline void ExplodeVolumeRender::setExplodeVolumes(float explode)
{
if (m_smooth)
m_shaderS->setExplodeVolumes(explode);
else
m_shader->setExplodeVolumes(explode);
m_shaderL->setExplodeVolumes(explode);
}
inline void ExplodeVolumeRender::setExplodeFaces(float explode)
{
if (m_smooth)
m_shaderS->setExplodeFaces(explode);
else
m_shader->setExplodeFaces(explode);
}
inline void ExplodeVolumeRender::setClippingPlane(const Geom::Vec4f& p)
{
if (m_smooth)
m_shaderS->setClippingPlane(p);
else
m_shader->setClippingPlane(p);
m_shaderL->setClippingPlane(p);
}
......@@ -459,22 +551,34 @@ inline void ExplodeVolumeRender::setClippingPlane(const Geom::Vec4f& p)
inline void ExplodeVolumeRender::setNoClippingPlane()
{
Geom::Vec4f p(1.0f,1.0f,1.0f,-99999999.9f);
if (m_smooth)
m_shaderS->setClippingPlane(p);
else
m_shader->setClippingPlane(p);
m_shaderL->setClippingPlane(p);
}
inline void ExplodeVolumeRender::setAmbiant(const Geom::Vec4f& ambiant)
{
if (m_smooth)
m_shaderS->setAmbiant(ambiant);
else
m_shader->setAmbiant(ambiant);
}
inline void ExplodeVolumeRender::setBackColor(const Geom::Vec4f& color)
{
if (m_smooth)
m_shaderS->setBackColor(color);
else
m_shader->setBackColor(color);
}
inline void ExplodeVolumeRender::setLightPosition(const Geom::Vec3f& lp)
{
if (m_smooth)
m_shaderS->setLightPosition(lp);
else
m_shader->setLightPosition(lp);
}
......@@ -485,6 +589,8 @@ inline void ExplodeVolumeRender::setColorLine(const Geom::Vec4f& col)
inline Utils::GLSLShader* ExplodeVolumeRender::shaderFaces()
{
if (m_smooth)
return m_shaderS;
return m_shader;
}
......
// ShaderExplodeSmoothVolumes::fragmentShaderText
VARYING_FRAG vec4 ColorFS;
uniform vec4 ambient;
uniform vec4 backColor;
VARYING_FRAG vec4 normalFS;
VARYING_FRAG vec4 lightFS;
VARYING_FRAG vec3 colorVert;
void main()
{
gl_FragColor = ColorFS;
float lambertTerm = dot(normalize(normalFS),normalize(lightFS));
if (lambertTerm > 0.0)
gl_FragColor = ambient + vec4(colorVert*lambertTerm, 1.0);
else
gl_FragColor = ambient - backColor*lambertTerm;
}
......@@ -6,15 +6,16 @@ uniform mat4 NormalMatrix;