Commit 807a3cd1 authored by Sylvain Thery's avatar Sylvain Thery

new VBO updating conversions methods

parent 5ecec1a3
...@@ -71,7 +71,7 @@ void MyQT::createMap() ...@@ -71,7 +71,7 @@ void MyQT::createMap()
// create another attribute on vertices (for edges drawing) // create another attribute on vertices (for edges drawing)
VertexAttribute<int, MAP> colorE = myMap.addAttribute<int, VERTEX, MAP>("colorE"); VertexAttribute<int, MAP> colorE = myMap.addAttribute<int, VERTEX, MAP>("colorE");
colorE[d1] = 0; colorE[d1] = 152;
colorE[PHI1(d1)] = 255; colorE[PHI1(d1)] = 255;
colorE[PHI_1(d1)] = 64; colorE[PHI_1(d1)] = 64;
colorE[PHI<11>(d2)] = 127; colorE[PHI<11>(d2)] = 127;
...@@ -96,21 +96,24 @@ void MyQT::createMap() ...@@ -96,21 +96,24 @@ void MyQT::createMap()
m_positionVBO->updateData(position); m_positionVBO->updateData(position);
// update color edge with on the fly computation of RGB from int // update color edge with on the fly computation of RGB from int
m_colorVBO1->updateDataConversion<int,3>(colorE, [](const float& x) // code writen in lambda
{ // m_colorVBO1->updateDataConversion<int,3>(colorE, [](const float& x)
return Geom::Vec3f(float(x)/255.0f,float(255-x)/255.0f,1.0f); // {
}); // return Geom::Vec3f(float(x)/255.0f,float(x)/255.0f,float(x)/255.0f);
// });
// or with furnished operator of conversion
DataConversion::operatorScalarToRGBf<int> conv2col(0,255);
m_colorVBO1->updateDataConversion<int,3>(colorE,conv2col);
// update color face with on the fly inversion of RGB // update color face with on the fly inversion of RGB
m_colorVBO2->updateDataConversion<PFP::VEC3,3>(colorF, [](const PFP::VEC3& c) m_colorVBO2->updateDataConversion<PFP::VEC3,3>(colorF, [](const PFP::VEC3& c)
{ {
return Geom::Vec3f(float(1.0-c[0]),float(1.0-c[1]),float(1.0-c[2])); return Geom::Vec3f(float(1.0-c[2]),float(1.0-c[1]),float(1.0-c[0]));
}); });
// construct rendering primities // construct rendering primities
m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES); m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES);
m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::LINES); m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::LINES);
...@@ -133,6 +136,8 @@ void MyQT::cb_initGL() ...@@ -133,6 +136,8 @@ void MyQT::cb_initGL()
m_colorVBO1 = new Utils::VBO(); m_colorVBO1 = new Utils::VBO();
m_colorVBO2 = new Utils::VBO(); m_colorVBO2 = new Utils::VBO();
m_scalarVBO = new Utils::VBO();
// using simple shader with color // using simple shader with color
m_shader = new Utils::ShaderSimpleColor(); m_shader = new Utils::ShaderSimpleColor();
m_shader->setAttributePosition(m_positionVBO); m_shader->setAttributePosition(m_positionVBO);
...@@ -152,7 +157,7 @@ void MyQT::cb_redraw() ...@@ -152,7 +157,7 @@ void MyQT::cb_redraw()
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glLineWidth(2.0f); glLineWidth(4.0f);
m_shader2->setAttributeColor(m_colorVBO1); m_shader2->setAttributeColor(m_colorVBO1);
m_render->draw(m_shader2, Algo::Render::GL2::LINES); m_render->draw(m_shader2, Algo::Render::GL2::LINES);
......
...@@ -72,6 +72,8 @@ protected: ...@@ -72,6 +72,8 @@ protected:
Utils::VBO* m_colorVBO1; // color per vertex for face drawing Utils::VBO* m_colorVBO1; // color per vertex for face drawing
Utils::VBO* m_colorVBO2; // color per vertex for edge drawing Utils::VBO* m_colorVBO2; // color per vertex for edge drawing
Utils::VBO* m_scalarVBO; // color per vertex for edge drawing
//shaders //shaders
Utils::ShaderSimpleColor* m_shader; Utils::ShaderSimpleColor* m_shader;
Utils::ShaderColorPerVertex* m_shader2; Utils::ShaderColorPerVertex* m_shader2;
......
...@@ -8,362 +8,447 @@ ...@@ -8,362 +8,447 @@
namespace CGoGN namespace CGoGN
{ {
namespace DataConversion
/**
* Conversion d'un T* en U* en fonction de la surcharge des membres
* Classe abstraite, a surcharger:
* - reserve(), convert(), release() & vectorSize()
*
*/
class ConvertBuffer
{ {
protected:
/**
* size of buffer in bytes
*/
unsigned int m_size;
/**
* size of buffer in number of elements
*/
unsigned int m_nb;
/**
* buffer
*/
void* m_buffer;
public: template< unsigned int D>
/** inline Geom::Vector<D,float> funcVecXdToVecXf(const Geom::Vector<D,double>& x)
* {
*/ Geom::Vector<D,float> v;
ConvertBuffer() : m_size(0),m_nb(0),m_buffer(NULL) {} for (unsigned int j=0; j<D; ++j)
v[j] = float(x[j]);
/** return v;
* }
*/
// virtual ~ConvertBuffer() = 0;
~ConvertBuffer() {}
/**
* set the number of element to convert & reserve memory for buffer
* @param nb nb elements (usually block size when call from VBO)
*/
virtual void reserve(unsigned int nb) = 0;
/**
* release memory buffer ( and set ptrs to null)
*/
virtual void release() = 0;
/**
* convert the buffer
*/
virtual void convert(const void* ptrIn) = 0;
/**
* get the data size in elements (ex: 3 for Vec3f)
*/
virtual unsigned int vectorSize() = 0;
/**
* get the buffer (void*)
*/
void* buffer() { return m_buffer; }
/**
* get the size of buffer in bytes
*/
unsigned int sizeBuffer() { return m_size; }
}; template<typename TYPE_IN>
inline float funcToFloat(const TYPE_IN& x)
{
return float(x);
}
/**
* Convertit simplement en castant chaque element
* Exemple double ou int float
*/
template<typename TYPE_IN> template<typename TYPE_IN>
class ConvertToFloat : public ConvertBuffer class operatorToFloatNormalized
{ {
protected: protected:
float* m_typedBuffer; double m_min;
double m_diff;
public: public:
ConvertToFloat() operatorToFloatNormalized(TYPE_IN min, TYPE_IN max) :
{} m_min(double(min)),
m_diff(double(max-min)) {}
~ConvertToFloat()
{
if (m_typedBuffer)
delete[] m_typedBuffer;
}
void release() inline float operator() (const TYPE_IN& x)
{ {
delete[] m_typedBuffer; double v = (double(x) - m_min)/ m_diff;
m_typedBuffer = NULL; return float(v);
m_buffer = NULL;
m_nb = 0;
m_size = 0;
}
void reserve(unsigned int nb)
{
m_nb = nb; // store number of elements
m_typedBuffer = new float[m_nb]; // allocate buffer
m_buffer = m_typedBuffer; // store void* casted ptr
m_size = m_nb*sizeof(float); // store size of buffer in bytes
}
void convert(const void* ptrIn)
{
// cast ptr in & out with right type
const TYPE_IN* typedIn = reinterpret_cast<const TYPE_IN*>(ptrIn);
float* typedOut = reinterpret_cast<float*>(m_buffer);
// compute conversion
for (unsigned int i = 0; i < m_nb; ++i)
*typedOut++ = float(*typedIn++);
}
unsigned int vectorSize()
{
return 1;
} }
}; };
template<typename TYPE_IN>
/** class operatorScalarToRGBf
* Convertit un type scalaire (char, short, int, ...)
* en un autre (float ou double) en le normalisant
* entre 0 et 1
*/
template<typename TYPE_IN, typename TYPE_OUT>
class ConvertNormalized : public ConvertBuffer
{ {
protected: protected:
TYPE_OUT* m_typedBuffer; double m_min;
double m_diff;
public: public:
ConvertNormalized()/*: operatorScalarToRGBf(TYPE_IN min, TYPE_IN max) :
ConvertBuffer(sizeof(TYPE_IN))*/ m_min(double(min)),
{} m_diff(double(max-min)) {}
~ConvertNormalized()
{
if (m_typedBuffer)
delete[] m_typedBuffer;
}
void release() inline Geom::Vec3f operator() (const TYPE_IN& x)
{ {
if (m_typedBuffer) double h = (360.0 /m_diff) * (double(x) - m_min); // normalize in 0-360
{ int hi = int(floor(h / 60.0)) % 6;
delete[] m_typedBuffer; float f = float((h / 60.0) - floor(h / 60.0));
m_typedBuffer = NULL; float q = 1.0f - f;
m_buffer = NULL; switch(hi)
m_nb = 0;
m_size = 0;
}
}
void reserve(unsigned int nb)
{
m_nb = nb; // store number of elements
m_typedBuffer = new TYPE_OUT[m_nb]; // allocate buffer
m_buffer = m_typedBuffer; // store void* casted ptr
m_size = m_nb*sizeof(TYPE_OUT); // store size of buffer in bytes
}
void convert(const void* ptrIn)
{
// cast ptr in & out with right type
const TYPE_IN* typedIn = reinterpret_cast<const TYPE_IN*>(ptrIn);
TYPE_OUT* typedOut = reinterpret_cast<TYPE_OUT*>(m_buffer);
// compute conversion
for (int i=0; i <m_nb; ++i)
{ {
TYPE_OUT val = (float(*typedIn++) - TYPE_OUT(std::numeric_limits<TYPE_IN>::min())); case 0:
val /= TYPE_OUT(std::numeric_limits<TYPE_IN>::max()) - TYPE_OUT(std::numeric_limits<TYPE_IN>::min()); return Geom::Vec3f(0.0f,f,1.0f);
*typedOut++ = val; break;
case 1:
return Geom::Vec3f(0.0f,1.0f,q);
break;
case 2:
return Geom::Vec3f(f,1.0f,0.0f);
break;
case 3:
return Geom::Vec3f(1.0f,q,0.0f);
break;
case 4:
return Geom::Vec3f(1.0f,0.0f,f);
break;
case 5:
return Geom::Vec3f(q,0.0f,1.0f);
default:
break;
} }
return Geom::Vec3f(0.0f,0.0f,0.0f);
} }
unsigned int vectorSize()
{
return 1;
}
}; };
}
/**
* Convertit un type scalaire (char, short, int, float ..)
* en une couleur variant suivant un schema hsv
* Un min et max donne les valeurs servant à l'interpolation
*/
template<typename TYPE_IN>
class ConvertToRGBf : public ConvertBuffer
{
protected:
float* m_typedBuffer;
TYPE_IN m_min;
TYPE_IN m_diff;
public:
ConvertToRGBf(TYPE_IN min, TYPE_IN max) :
// ConvertBuffer(sizeof(TYPE_IN)),
m_min(min),
m_diff(max-min),
m_typedBuffer(NULL) {}
~ConvertToRGBf() ///**
{ // * Conversion d'un T* en U* en fonction de la surcharge des membres
if (m_typedBuffer) // * Classe abstraite, a surcharger:
delete[] m_typedBuffer; // * - reserve(), convert(), release() & vectorSize()
} // *
// */
void release() //class ConvertBuffer
{ //{
if (m_typedBuffer) //protected:
{ // /**
delete[] m_typedBuffer; // * size of buffer in bytes
m_typedBuffer = NULL; // */
m_buffer = NULL; // unsigned int m_size;
m_nb = 0;
m_size = 0;
}
}
void reserve(unsigned int nb) // /**
{ // * size of buffer in number of elements
m_nb = nb; // store number of elements // */
m_typedBuffer = new float[3*nb]; // allocate buffer // unsigned int m_nb;
m_buffer = m_typedBuffer; // store void* casted ptr
m_size = 3*nb*sizeof(float); // store size of buffer in bytes // /**
} // * buffer
// */
// void* m_buffer;
//public:
// /**
// *
// */
// ConvertBuffer() : m_size(0),m_nb(0),m_buffer(NULL) {}
// /**
// *
// */
//// virtual ~ConvertBuffer() = 0;
// ~ConvertBuffer() {}
// /**
// * set the number of element to convert & reserve memory for buffer
// * @param nb nb elements (usually block size when call from VBO)
// */
// virtual void reserve(unsigned int nb) = 0;
// /**
// * release memory buffer ( and set ptrs to null)
// */
// virtual void release() = 0;
// /**
// * convert the buffer
// */
// virtual void convert(const void* ptrIn) = 0;
// /**
// * get the data size in elements (ex: 3 for Vec3f)
// */
// virtual unsigned int vectorSize() = 0;
// /**
// * get the buffer (void*)
// */
// void* buffer() { return m_buffer; }
// /**
// * get the size of buffer in bytes
// */
// unsigned int sizeBuffer() { return m_size; }
//};
///**
//* Convertit simplement en castant chaque element
//* Exemple double ou int float
//*/
//template<typename TYPE_IN>
//class ConvertToFloat : public ConvertBuffer
//{
//protected:
// float* m_typedBuffer;
//public:
// ConvertToFloat()
// {}
// ~ConvertToFloat()
// {
// if (m_typedBuffer)
// delete[] m_typedBuffer;
// }
// void release()
// {
// delete[] m_typedBuffer;
// m_typedBuffer = NULL;
// m_buffer = NULL;
// m_nb = 0;
// m_size = 0;
// }
void convert(const void* ptrIn) // void reserve(unsigned int nb)
{ // {
// cast ptr in & out with right type // m_nb = nb; // store number of elements
const TYPE_IN* typedIn = reinterpret_cast<const TYPE_IN*>(ptrIn); // m_typedBuffer = new float[m_nb]; // allocate buffer
float* typedOut = reinterpret_cast<float*>(m_buffer); // m_buffer = m_typedBuffer; // store void* casted ptr
// m_size = m_nb*sizeof(float); // store size of buffer in bytes
// }
// void convert(const void* ptrIn)
// {
// // cast ptr in & out with right type
// const TYPE_IN* typedIn = reinterpret_cast<const TYPE_IN*>(ptrIn);
// float* typedOut = reinterpret_cast<float*>(m_buffer);
// // compute conversion
// for (unsigned int i = 0; i < m_nb; ++i)
// *typedOut++ = float(*typedIn++);
// }
// unsigned int vectorSize()
// {
// return 1;
// }
//};
///**
//* Convertit un type scalaire (char, short, int, ...)
//* en un autre (float ou double) en le normalisant
//* entre 0 et 1
//*/
//template<typename TYPE_IN, typename TYPE_OUT>
//class ConvertNormalized : public ConvertBuffer
//{
//protected:
// TYPE_OUT* m_typedBuffer;
//public:
// ConvertNormalized()/*:
// ConvertBuffer(sizeof(TYPE_IN))*/
// {}
// ~ConvertNormalized()
// {
// if (m_typedBuffer)
// delete[] m_typedBuffer;
// }
// void release()
// {
// if (m_typedBuffer)
// {
// delete[] m_typedBuffer;
// m_typedBuffer = NULL;
// m_buffer = NULL;
// m_nb = 0;
// m_size = 0;
// }
// }
// void reserve(unsigned int nb)
// {
// m_nb = nb; // store number of elements
// m_typedBuffer = new TYPE_OUT[m_nb]; // allocate buffer
// m_buffer = m_typedBuffer; // store void* casted ptr
// m_size = m_nb*sizeof(TYPE_OUT); // store size of buffer in bytes
// }
// void convert(const void* ptrIn)
// {
// // cast ptr in & out with right type
// const TYPE_IN* typedIn = reinterpret_cast<const TYPE_IN*>(ptrIn);
// TYPE_OUT* typedOut = reinterpret_cast<TYPE_OUT*>(m_buffer);
// compute conversion // // compute conversion
for (int i=0; i <m_nb; ++i) // for (int i=0; i <m_nb; ++i)
{ // {
float h = (360.0f /(m_diff))*(*typedIn++ - m_min); // normalize in 0-360 // TYPE_OUT val = (float(*typedIn++) - TYPE_OUT(std::numeric_limits<TYPE_IN>::min()));
int hi = int(floor(h / 60.0f)) % 6; // val /= TYPE_OUT(std::numeric_limits<TYPE_IN>::max()) - TYPE_OUT(std::numeric_limits<TYPE_IN>::min());
float f = (h / 60.0) - floor(h / 60.0); // *typedOut++ = val;
float q = 1.0f - f; // }
switch(hi) // }
{
case 0: // unsigned int vectorSize()
*typedOut++ = 0.0f; // {
*typedOut++ = f; // return 1;
*typedOut++ = 1.0f; // }
break;
case 1: //};
*typedOut++ = 0.0f;
*typedOut++ = 1.0f;
*typedOut++ = q; ///**
break; //* Convertit un type scalaire (char, short, int, float ..)
case 2: //* en une couleur variant suivant un schema hsv
*typedOut++ = f; //* Un min et max donne les valeurs servant à l'interpolation
*typedOut++ = 1.0f; //*/
*typedOut++ = 0.0f; //template<typename TYPE_IN>
break; //class ConvertToRGBf : public ConvertBuffer
case 3: //{
*typedOut++ = 1.0f; //protected:
*typedOut++ = q; // float* m_typedBuffer;