From 807a3cd176deede22d371aeaf1a150805e1a9aac Mon Sep 17 00:00:00 2001 From: Sylvain Thery Date: Fri, 18 Jul 2014 16:37:22 +0200 Subject: [PATCH] new VBO updating conversions methods --- Apps/Tuto/Attributes/vbo_attribs.cpp | 23 +- Apps/Tuto/Attributes/vbo_attribs.h | 2 + include/Container/convert.h | 737 +++++++++++++++------------ include/Utils/vbo_base.h | 66 ++- src/Utils/GLSLShader.cpp | 1 + src/Utils/vbo.cpp | 106 ++-- 6 files changed, 499 insertions(+), 436 deletions(-) diff --git a/Apps/Tuto/Attributes/vbo_attribs.cpp b/Apps/Tuto/Attributes/vbo_attribs.cpp index 3e19458b..0e4e5a95 100644 --- a/Apps/Tuto/Attributes/vbo_attribs.cpp +++ b/Apps/Tuto/Attributes/vbo_attribs.cpp @@ -71,7 +71,7 @@ void MyQT::createMap() // create another attribute on vertices (for edges drawing) VertexAttribute colorE = myMap.addAttribute("colorE"); - colorE[d1] = 0; + colorE[d1] = 152; colorE[PHI1(d1)] = 255; colorE[PHI_1(d1)] = 64; colorE[PHI<11>(d2)] = 127; @@ -96,21 +96,24 @@ void MyQT::createMap() m_positionVBO->updateData(position); // update color edge with on the fly computation of RGB from int - m_colorVBO1->updateDataConversion(colorE, [](const float& x) - { - return Geom::Vec3f(float(x)/255.0f,float(255-x)/255.0f,1.0f); - }); + // code writen in lambda +// m_colorVBO1->updateDataConversion(colorE, [](const float& x) +// { +// return Geom::Vec3f(float(x)/255.0f,float(x)/255.0f,float(x)/255.0f); +// }); + // or with furnished operator of conversion + DataConversion::operatorScalarToRGBf conv2col(0,255); + m_colorVBO1->updateDataConversion(colorE,conv2col); + // update color face with on the fly inversion of RGB m_colorVBO2->updateDataConversion(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 m_render->initPrimitives(myMap, Algo::Render::GL2::TRIANGLES); m_render->initPrimitives(myMap, Algo::Render::GL2::LINES); @@ -133,6 +136,8 @@ void MyQT::cb_initGL() m_colorVBO1 = new Utils::VBO(); m_colorVBO2 = new Utils::VBO(); + m_scalarVBO = new Utils::VBO(); + // using simple shader with color m_shader = new Utils::ShaderSimpleColor(); m_shader->setAttributePosition(m_positionVBO); @@ -152,7 +157,7 @@ void MyQT::cb_redraw() glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_CULL_FACE); - glLineWidth(2.0f); + glLineWidth(4.0f); m_shader2->setAttributeColor(m_colorVBO1); m_render->draw(m_shader2, Algo::Render::GL2::LINES); diff --git a/Apps/Tuto/Attributes/vbo_attribs.h b/Apps/Tuto/Attributes/vbo_attribs.h index ca6c8cee..10b30185 100644 --- a/Apps/Tuto/Attributes/vbo_attribs.h +++ b/Apps/Tuto/Attributes/vbo_attribs.h @@ -72,6 +72,8 @@ protected: Utils::VBO* m_colorVBO1; // color per vertex for face drawing Utils::VBO* m_colorVBO2; // color per vertex for edge drawing + Utils::VBO* m_scalarVBO; // color per vertex for edge drawing + //shaders Utils::ShaderSimpleColor* m_shader; Utils::ShaderColorPerVertex* m_shader2; diff --git a/include/Container/convert.h b/include/Container/convert.h index d0e11348..e12aae30 100644 --- a/include/Container/convert.h +++ b/include/Container/convert.h @@ -8,362 +8,447 @@ namespace CGoGN { - -/** - * Conversion d'un T* en U* en fonction de la surcharge des membres - * Classe abstraite, a surcharger: - * - reserve(), convert(), release() & vectorSize() - * - */ -class ConvertBuffer +namespace DataConversion { -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: - /** - * - */ - 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; } +template< unsigned int D> +inline Geom::Vector funcVecXdToVecXf(const Geom::Vector& x) +{ + Geom::Vector v; + for (unsigned int j=0; j +inline float funcToFloat(const TYPE_IN& x) +{ + return float(x); +} -/** -* Convertit simplement en castant chaque element -* Exemple double ou int float -*/ template -class ConvertToFloat : public ConvertBuffer +class operatorToFloatNormalized { protected: - float* m_typedBuffer; + double m_min; + double m_diff; public: - ConvertToFloat() - {} - - ~ConvertToFloat() - { - if (m_typedBuffer) - delete[] m_typedBuffer; - } + operatorToFloatNormalized(TYPE_IN min, TYPE_IN max) : + m_min(double(min)), + m_diff(double(max-min)) {} - void release() + inline float operator() (const TYPE_IN& x) { - 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 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(ptrIn); - float* typedOut = reinterpret_cast(m_buffer); - // compute conversion - for (unsigned int i = 0; i < m_nb; ++i) - *typedOut++ = float(*typedIn++); - } - - unsigned int vectorSize() - { - return 1; + double v = (double(x) - m_min)/ m_diff; + return float(v); } }; - -/** -* Convertit un type scalaire (char, short, int, ...) -* en un autre (float ou double) en le normalisant -* entre 0 et 1 -*/ -template -class ConvertNormalized : public ConvertBuffer +template +class operatorScalarToRGBf { protected: - TYPE_OUT* m_typedBuffer; - + double m_min; + double m_diff; public: - ConvertNormalized()/*: - ConvertBuffer(sizeof(TYPE_IN))*/ - {} - - ~ConvertNormalized() - { - if (m_typedBuffer) - delete[] m_typedBuffer; - } + operatorScalarToRGBf(TYPE_IN min, TYPE_IN max) : + m_min(double(min)), + m_diff(double(max-min)) {} - void release() + inline Geom::Vec3f operator() (const TYPE_IN& x) { - 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(ptrIn); - TYPE_OUT* typedOut = reinterpret_cast(m_buffer); - - // compute conversion - for (int i=0; i ::min())); - val /= TYPE_OUT(std::numeric_limits::max()) - TYPE_OUT(std::numeric_limits::min()); - *typedOut++ = val; + case 0: + return Geom::Vec3f(0.0f,f,1.0f); + 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 -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() - { - 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; - } - } +///** +// * 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; - void reserve(unsigned int nb) - { - m_nb = nb; // store number of elements - m_typedBuffer = new float[3*nb]; // allocate buffer - m_buffer = m_typedBuffer; // store void* casted ptr - m_size = 3*nb*sizeof(float); // store size of buffer in bytes - } +// /** +// * size of buffer in number of elements +// */ +// unsigned int m_nb; + +// /** +// * 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 +//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) - { - // cast ptr in & out with right type - const TYPE_IN* typedIn = reinterpret_cast(ptrIn); - float* typedOut = reinterpret_cast(m_buffer); +// 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(ptrIn); +// float* typedOut = reinterpret_cast(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 +//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(ptrIn); +// TYPE_OUT* typedOut = reinterpret_cast(m_buffer); - // compute conversion - for (int i=0; i -class ConvertVecXdToVecXf : public ConvertBuffer -{ -protected: - typedef Geom::Vector VECF; - typedef Geom::Vector VECD; - - VECF* m_typedBuffer; -public: - ConvertVecXdToVecXf(): - - m_typedBuffer(NULL) - {} - - ~ConvertVecXdToVecXf() - { - if (m_typedBuffer) - delete[] m_typedBuffer; - } - - void release() - { - 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 VECF[nb]; // allocate buffer typed (not possible to delete void*) - m_buffer = m_typedBuffer; // store void* casted ptr - m_size = nb*sizeof(VECF); // store size of buffer in bytes - } - - void convert(const void* ptrIn) - { - // cast ptr in & out with right type - const VECD* typedIn = reinterpret_cast(ptrIn); - VECF* typedOut = m_typedBuffer; - // compute conversion - for (unsigned int i = 0; i < m_nb; ++i) - { - const VECD& vd = *typedIn++; - for (unsigned int j=0; j ConvertVec2dToVec2f; -typedef ConvertVecXdToVecXf<3> ConvertVec3dToVec3f; -typedef ConvertVecXdToVecXf<4> ConvertVec4dToVec4f; +// // compute conversion +// for (int i=0; i ::min())); +// val /= TYPE_OUT(std::numeric_limits::max()) - TYPE_OUT(std::numeric_limits::min()); +// *typedOut++ = val; +// } +// } + +// 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 +//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() +// { +// 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 float[3*nb]; // allocate buffer +// m_buffer = m_typedBuffer; // store void* casted ptr +// m_size = 3*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(ptrIn); +// float* typedOut = reinterpret_cast(m_buffer); + +// // compute conversion +// for (int i=0; i +//class ConvertVecXdToVecXf : public ConvertBuffer +//{ +//protected: +// typedef Geom::Vector VECF; +// typedef Geom::Vector VECD; + +// VECF* m_typedBuffer; +//public: +// ConvertVecXdToVecXf(): + +// m_typedBuffer(NULL) +// {} + +// ~ConvertVecXdToVecXf() +// { +// if (m_typedBuffer) +// delete[] m_typedBuffer; +// } + +// void release() +// { +// 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 VECF[nb]; // allocate buffer typed (not possible to delete void*) +// m_buffer = m_typedBuffer; // store void* casted ptr +// m_size = nb*sizeof(VECF); // store size of buffer in bytes +// } + +// void convert(const void* ptrIn) +// { +// // cast ptr in & out with right type +// const VECD* typedIn = reinterpret_cast(ptrIn); +// VECF* typedOut = m_typedBuffer; +// // compute conversion +// for (unsigned int i = 0; i < m_nb; ++i) +// { +// const VECD& vd = *typedIn++; +// for (unsigned int j=0; j ConvertVec2dToVec2f; +//typedef ConvertVecXdToVecXf<3> ConvertVec3dToVec3f; +//typedef ConvertVecXdToVecXf<4> ConvertVec4dToVec4f; + +//typedef ConvertToFloat ConvertDoubleToFloat; -typedef ConvertToFloat ConvertDoubleToFloat; } diff --git a/include/Utils/vbo_base.h b/include/Utils/vbo_base.h index a9074c39..aa0eab3c 100644 --- a/include/Utils/vbo_base.h +++ b/include/Utils/vbo_base.h @@ -67,26 +67,12 @@ protected: /// type name of the last attribute used to fill the VBO std::string m_typeName; - /// pointer to buffer converter - ConvertBuffer* m_conv; - - - /** - * update data from AttributeMultiVectorGen to the vbo, with conversion - */ - void updateData_withInternalConversion(const AttributeMultiVectorGen* attrib, ConvertBuffer* conv); - public: /** * constructor: allocate the OGL VBO */ VBO(const std::string& name = ""); - /** - * constructor with buffer converter: allocate the OGL VBO - */ - VBO(ConvertBuffer* conv, const std::string& name = ""); - /** * copy constructor, new VBO copy content */ @@ -137,6 +123,11 @@ public: */ void sameAllocSameBufferSize(const VBO& vbo); + /** + * update data from attribute multivector to the vbo (automatic conversion if necessary and possible) + */ + void updateData(const AttributeMultiVectorGen* attrib); + /** * update data from attribute handler to the vbo */ @@ -145,13 +136,6 @@ public: updateData(attrib.getDataVectorGen()) ; } - void updateData(const AttributeMultiVectorGen* attrib); - - /** - * set the converter that convert buffer to float * - */ - void setBufferConverter(ConvertBuffer* conv); - /** * update data from given data vector * @warning use only with include vbo.h (not vbo_base.h) @@ -170,20 +154,48 @@ public: void allocate(unsigned int nbElts); /** - * update the VBO from Attribute Handler with on the fly conversion + * update the VBO from Attribute Handler of vectors with on the fly conversion * template paramters: * T_IN input type attribute handler * NB_COMPONENTS 3 for vbo of pos/normal, 2 for texture etc.. * @param attribHG the attribute handler source - * @param conv lambda function that take a const T_IN& and return a Vector + * @param conv lambda or function/fonctor that take a const T_IN& and return a Vector */ template - void updateDataConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv) + inline void updateDataConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv) { - typedef Geom::Vector T_OUT; + const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen(); + updateDataConversion,NB_COMPONENTS,CONVFUNC>(attrib,conv); + } + /** + * update the VBO from Attribute Handler of vectors with on the fly conversion + * template paramters: + * T_IN input type attribute handler + * @param attribHG the attribute handler source + * @param conv lambda or function/fonctor that take a const T_IN& and return a Vector + */ + template + inline void updateDataConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv) + { const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen(); + updateDataConversion(attrib,conv); + } + +protected: + /** + * update the VBO from Attribute Handler with on the fly conversion + * template paramters: + * T_IN input type attribute handler + * NB_COMPONENTS 3 for vbo of pos/normal, 2 for texture etc.. + * @param attrib the attribute multivector source + * @param conv lambda function that take a const T_IN& and return a Vector + */ + template + void updateDataConversion(const AttributeMultiVectorGen* attrib, CONVFUNC conv) + { + unsigned int old_nbb = sizeof(float) * m_data_size * m_nbElts; m_name = attrib->getName(); m_typeName = attrib->getTypeName(); m_data_size = NB_COMPONENTS; @@ -202,7 +214,8 @@ public: // bind buffer to update glBindBuffer(GL_ARRAY_BUFFER, *m_id); - glBufferData(GL_ARRAY_BUFFER, nbb * szb, 0, GL_STREAM_DRAW); + if (nbb!=old_nbb) + glBufferData(GL_ARRAY_BUFFER, nbb * szb, 0, GL_STREAM_DRAW); for (unsigned int i = 0; i < nbb; ++i) { @@ -224,7 +237,6 @@ public: } - }; diff --git a/src/Utils/GLSLShader.cpp b/src/Utils/GLSLShader.cpp index 7c112008..f030e2f7 100644 --- a/src/Utils/GLSLShader.cpp +++ b/src/Utils/GLSLShader.cpp @@ -1090,6 +1090,7 @@ void GLSLShader::enableVertexAttribs(unsigned int stride, unsigned int begin) co { glBindBuffer(GL_ARRAY_BUFFER, it->vbo_ptr->id()); glEnableVertexAttribArray(it->va_id); + assert((it->vbo_ptr->dataSize()!=0) || !"dataSize of VBO is 0 ! could not draw"); glVertexAttribPointer(it->va_id, it->vbo_ptr->dataSize(), GL_FLOAT, false, stride, (const GLvoid*)((unsigned long)(begin))); } // this->unbind(); diff --git a/src/Utils/vbo.cpp b/src/Utils/vbo.cpp index 2b25f4e5..4d8132cf 100644 --- a/src/Utils/vbo.cpp +++ b/src/Utils/vbo.cpp @@ -26,6 +26,7 @@ #include "Utils/GLSLShader.h" #include #include +#include "Container/convert.h" namespace CGoGN { @@ -33,36 +34,33 @@ namespace CGoGN namespace Utils { -VBO::VBO(const std::string& name) : m_nbElts(0), m_lock(false), m_name(name), m_conv(NULL) +VBO::VBO(const std::string& name) : m_data_size(0), m_nbElts(0), m_lock(false), m_name(name)/*, m_conv(NULL)*/ { glGenBuffers(1, &(*m_id)); m_refs.reserve(4); } -VBO::VBO(ConvertBuffer* conv, const std::string& name) : m_nbElts(0), m_lock(false), m_name(name), m_conv(conv) -{ - glGenBuffers(1, &(*m_id)); - m_refs.reserve(4); -} VBO::VBO(const VBO& vbo) : m_data_size(vbo.m_data_size), m_nbElts(vbo.m_nbElts), - m_lock(false), - m_conv(vbo.m_conv) + m_lock(false) { - unsigned int nbbytes = sizeof(float) * m_data_size * m_nbElts; - glGenBuffers(1, &(*m_id)); - vbo.bind(); - void* src = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); + unsigned int nbbytes = sizeof(float) * m_data_size * m_nbElts; - bind(); - glBufferData(GL_ARRAY_BUFFER, nbbytes, src, GL_STREAM_DRAW); + if (nbbytes != 0) + { + vbo.bind(); + void* src = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); - vbo.bind(); - glUnmapBuffer(GL_ARRAY_BUFFER); + bind(); + glBufferData(GL_ARRAY_BUFFER, nbbytes, src, GL_STREAM_DRAW); + + vbo.bind(); + glUnmapBuffer(GL_ARRAY_BUFFER); + } } VBO::~VBO() @@ -72,11 +70,6 @@ VBO::~VBO() glDeleteBuffers(1, &(*m_id)); } -void VBO::setBufferConverter(ConvertBuffer *conv) -{ - m_conv = conv; -} - void VBO::sameAllocSameBufferSize(const VBO& vbo) { m_data_size = vbo.m_data_size; @@ -95,56 +88,50 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib) return; } - if (m_conv != NULL) - { - updateData_withInternalConversion(attrib, m_conv); - return; - } - const AttributeMultiVector* amv3 = dynamic_cast*>(attrib); if (amv3 != NULL) { - ConvertVec3dToVec3f conv; - updateData_withInternalConversion(attrib,&conv); + updateDataConversion(attrib,DataConversion::funcVecXdToVecXf<3>); return; } const AttributeMultiVector* amv2 = dynamic_cast*>(attrib); if (amv2 != NULL) { - ConvertVec2dToVec2f conv; - updateData_withInternalConversion(attrib,&conv); + updateDataConversion(attrib,DataConversion::funcVecXdToVecXf<2>); + return; } const AttributeMultiVector* amv4 = dynamic_cast*>(attrib); if (amv4 != NULL) { - ConvertVec4dToVec4f conv; - updateData_withInternalConversion(attrib,&conv); + updateDataConversion(attrib,DataConversion::funcVecXdToVecXf<4>); + return; } const AttributeMultiVector* amv1 = dynamic_cast*>(attrib); if (amv1 != NULL) { - ConvertDoubleToFloat conv; - updateData_withInternalConversion(attrib,&conv); + updateDataConversion(attrib,DataConversion::funcToFloat); return; } - + unsigned int old_nbb = sizeof(float) * m_data_size * m_nbElts; m_name = attrib->getName(); m_typeName = attrib->getTypeName(); - m_data_size = attrib->getSizeOfType() / sizeof(float); std::vector addr; unsigned int byteTableSize; unsigned int nbb = attrib->getBlocksPointers(addr, byteTableSize); + glBindBuffer(GL_ARRAY_BUFFER, *m_id); - glBufferData(GL_ARRAY_BUFFER, nbb * byteTableSize, 0, GL_STREAM_DRAW); + + if (nbb!=old_nbb) + glBufferData(GL_ARRAY_BUFFER, nbb * byteTableSize, 0, GL_STREAM_DRAW); m_nbElts = nbb * byteTableSize / attrib->getSizeOfType(); @@ -159,42 +146,6 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib) } -void VBO::updateData_withInternalConversion(const AttributeMultiVectorGen* attrib, ConvertBuffer* conv) -{ - - m_name = attrib->getName(); - m_typeName = attrib->getTypeName(); - m_data_size = conv->vectorSize(); - - // alloue la memoire pour le buffer et initialise le conv - conv->reserve(attrib->getBlockSize()); - - std::vector addr; - unsigned int byteTableSize; - unsigned int nbb = attrib->getBlocksPointers(addr, byteTableSize); - - m_nbElts = nbb * attrib->getBlockSize()/(m_data_size*sizeof(float)); - - // bind buffer to update - glBindBuffer(GL_ARRAY_BUFFER, *m_id); - glBufferData(GL_ARRAY_BUFFER, nbb * conv->sizeBuffer(), 0, GL_STREAM_DRAW); - - unsigned int offset = 0; - - for (unsigned int i = 0; i < nbb; ++i) - { - // convertit les donnees dans le buffer de conv - conv->convert(addr[i]); - // update sub-vbo - glBufferSubDataARB(GL_ARRAY_BUFFER, offset, conv->sizeBuffer(), conv->buffer()); - // block suivant - offset += conv->sizeBuffer(); - } - - // libere la memoire de la conversion - conv->release(); -} - void* VBO::lockPtr() { if (m_lock) @@ -237,6 +188,13 @@ void VBO::copyData(void *ptr) const void VBO::allocate(unsigned int nbElts) { m_nbElts = nbElts; + + if (m_data_size ==0) + { + CGoGNerr << "Allocate not possible dataSize = 0 (use setDataSize() first)" << CGoGNendl; + return; + } + glBindBuffer(GL_ARRAY_BUFFER, *m_id); glBufferData(GL_ARRAY_BUFFER, nbElts * m_data_size * sizeof(float), 0, GL_STREAM_DRAW); } -- GitLab