Commit 612c7ed7 authored by Sylvain Thery's avatar Sylvain Thery

VBO update with conversion lambda

parent c926209f
...@@ -69,19 +69,14 @@ void MyQT::createMap() ...@@ -69,19 +69,14 @@ void MyQT::createMap()
colorF[PHI_1(d2)] = Geom::Vec3f(0.0f,1.0f,1.0f); colorF[PHI_1(d2)] = Geom::Vec3f(0.0f,1.0f,1.0f);
// create another attribute on vertices (for edges drawing) // create another attribute on vertices (for edges drawing)
VertexAttribute<VEC3, MAP> colorE = myMap.addAttribute<PFP::VEC3, VERTEX, MAP>("colorE"); VertexAttribute<int, MAP> colorE = myMap.addAttribute<int, VERTEX, MAP>("colorE");
colorE[d1] = Geom::Vec3f(0.0f,0.5f,0.5f); colorE[d1] = 0;
colorE[PHI1(d1)] = Geom::Vec3f(0.5f,0.0f,0.5f); colorE[PHI1(d1)] = 255;
colorE[PHI_1(d1)] = Geom::Vec3f(0.5f,0.5f,0.0f); colorE[PHI_1(d1)] = 64;
colorE[PHI<11>(d2)] = Geom::Vec3f(0.0f,0.5f,0.0f); colorE[PHI<11>(d2)] = 127;
colorE[PHI_1(d2)] = Geom::Vec3f(0.5f,0.0f,0.0f); colorE[PHI_1(d2)] = 192;
// example of attribute on face
// here for example we store the number of edges of faces at construction
FaceAttribute<int, MAP> side = myMap.addAttribute<int, FACE, MAP>("nb_sides");
side[d1] = 3;
side[d2] = 4;
// bounding box of scene // bounding box of scene
Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position); Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
...@@ -94,20 +89,27 @@ void MyQT::createMap() ...@@ -94,20 +89,27 @@ void MyQT::createMap()
// first show for be sure that GL context is binded // first show for be sure that GL context is binded
show(); show();
// update of position VBO (context GL necessary)
// HERE VBO conversion on the fly
// update of position VBO with on the fly conversion from double to float (automatic)
m_positionVBO->updateData(position); m_positionVBO->updateData(position);
// m_colorVBO1->updateData(colorF);
m_colorVBO2->updateData(colorE);
m_colorVBO1->updateData_withLambdaConversion<PFP::VEC3,Geom::Vec3f,3>(colorF, [](const PFP::VEC3& x) // update color edge with on the fly computation of RGB from int
m_colorVBO1->updateDataConversion<int,3>(colorE, [](const float& x)
{
return Geom::Vec3f(float(x)/255.0f,float(255-x)/255.0f,1.0f);
});
// update color face with on the fly inversion of RGB
m_colorVBO2->updateDataConversion<PFP::VEC3,3>(colorF, [](const PFP::VEC3& c)
{ {
return Geom::Vec3f(float(1.0-x[0]),float(1.0-x[1]),float(1.0-x[2])); return Geom::Vec3f(float(1.0-c[0]),float(1.0-c[1]),float(1.0-c[2]));
}); });
// m_colorVBO2->updateData_withLambdaConversion<PFP::VEC3,Geom::Vec3f,3>(colorE, [](const PFP::VEC3& x)
// {
// return Geom::Vec3f(float(1.0-x[0]),float(1.0-x[1]),float(1.0-x[2]));
// });
// construct rendering primities // construct rendering primities
m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES); m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES);
...@@ -140,7 +142,6 @@ void MyQT::cb_initGL() ...@@ -140,7 +142,6 @@ void MyQT::cb_initGL()
m_shader2 = new Utils::ShaderColorPerVertex(); m_shader2 = new Utils::ShaderColorPerVertex();
m_shader2->setAttributePosition(m_positionVBO); m_shader2->setAttributePosition(m_positionVBO);
// m_shader2->setAttributeColor(m_colorVBO1);
// each shader must be registred to allow Qt interface to update matrices uniforms // each shader must be registred to allow Qt interface to update matrices uniforms
registerShader(m_shader2); registerShader(m_shader2);
} }
...@@ -151,7 +152,6 @@ void MyQT::cb_redraw() ...@@ -151,7 +152,6 @@ 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);
// draw yellow points
glLineWidth(2.0f); glLineWidth(2.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);
......
...@@ -74,7 +74,7 @@ protected: ...@@ -74,7 +74,7 @@ protected:
/** /**
* update data from AttributeMultiVectorGen to the vbo, with conversion * update data from AttributeMultiVectorGen to the vbo, with conversion
*/ */
void updateData_withConversion(const AttributeMultiVectorGen* attrib, ConvertBuffer* conv); void updateData_withInternalConversion(const AttributeMultiVectorGen* attrib, ConvertBuffer* conv);
public: public:
/** /**
...@@ -147,9 +147,6 @@ public: ...@@ -147,9 +147,6 @@ public:
void updateData(const AttributeMultiVectorGen* attrib); void updateData(const AttributeMultiVectorGen* attrib);
// template <typename T_IN, typename T_OUT, unsigned int VS, typename CONVFUNC>
// void updateData_withLambdaConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv);
/** /**
* set the converter that convert buffer to float * * set the converter that convert buffer to float *
*/ */
...@@ -172,15 +169,24 @@ public: ...@@ -172,15 +169,24 @@ public:
void allocate(unsigned int nbElts); void allocate(unsigned int nbElts);
/**
template <typename T_IN, typename T_OUT, unsigned int VS, typename CONVFUNC> * update the VBO from Attribute Handler with on the fly conversion
void updateData_withLambdaConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv) * 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<NB_COMPONENTS,float>
*/
template <typename T_IN, unsigned int NB_COMPONENTS, typename CONVFUNC>
void updateDataConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv)
{ {
typedef Geom::Vector<NB_COMPONENTS,float> T_OUT;
const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen(); const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen();
m_name = attrib->getName(); m_name = attrib->getName();
m_typeName = attrib->getTypeName(); m_typeName = attrib->getTypeName();
m_data_size = VS; m_data_size = NB_COMPONENTS;
// alloue la memoire pour le buffer et initialise le conv // alloue la memoire pour le buffer et initialise le conv
T_OUT* typedBuffer = new T_OUT[_BLOCKSIZE_]; T_OUT* typedBuffer = new T_OUT[_BLOCKSIZE_];
...@@ -189,37 +195,33 @@ public: ...@@ -189,37 +195,33 @@ public:
unsigned int byteTableSize; unsigned int byteTableSize;
unsigned int nbb = attrib->getBlocksPointers(addr, byteTableSize); unsigned int nbb = attrib->getBlocksPointers(addr, byteTableSize);
m_nbElts = nbb * _BLOCKSIZE_/(VS*sizeof(T_OUT)); m_nbElts = nbb * _BLOCKSIZE_/(sizeof(T_OUT));
// bind buffer to update
glBindBuffer(GL_ARRAY_BUFFER, *m_id);
glBufferData(GL_ARRAY_BUFFER, nbb * _BLOCKSIZE_, 0, GL_STREAM_DRAW);
unsigned int offset = 0; unsigned int offset = 0;
unsigned int szb = _BLOCKSIZE_*sizeof(T_OUT); unsigned int szb = _BLOCKSIZE_*sizeof(T_OUT);
// bind buffer to update
glBindBuffer(GL_ARRAY_BUFFER, *m_id);
glBufferData(GL_ARRAY_BUFFER, nbb * szb, 0, GL_STREAM_DRAW);
for (unsigned int i = 0; i < nbb; ++i) for (unsigned int i = 0; i < nbb; ++i)
{ {
// convertit les donnees dans le buffer de conv // convertit les donnees dans le buffer de conv
const T_IN* typedIn = reinterpret_cast<const T_IN*>(addr[i]); const T_IN* typedIn = reinterpret_cast<const T_IN*>(addr[i]);
T_OUT* typedOut = typedBuffer; T_OUT* typedOut = typedBuffer;
// compute conversion // compute conversion
for (unsigned int i = 0; i < _BLOCKSIZE_; ++i) for (unsigned int j = 0; j < _BLOCKSIZE_; ++j)
{ *typedOut++ = conv(*typedIn++);
// *typedOut++ = conv(*typedIn++);
std::cout << *typedIn << " -> " ;
*typedOut = conv(*typedIn++);
std::cout << *typedOut++ << std::endl;
}
// update sub-vbo // update sub-vbo
glBufferSubDataARB(GL_ARRAY_BUFFER, offset, szb, typedBuffer); glBufferSubDataARB(GL_ARRAY_BUFFER, offset, szb, reinterpret_cast<void*>(typedBuffer));
// block suivant // block suivant
offset += szb; offset += szb;
} }
// libere la memoire de la conversion // libere la memoire de la conversion
delete[] typedBuffer; delete[] typedBuffer;
} }
......
...@@ -49,7 +49,7 @@ VBO::VBO(const VBO& vbo) : ...@@ -49,7 +49,7 @@ VBO::VBO(const VBO& vbo) :
m_data_size(vbo.m_data_size), m_data_size(vbo.m_data_size),
m_nbElts(vbo.m_nbElts), m_nbElts(vbo.m_nbElts),
m_lock(false), m_lock(false),
m_conv(NULL) m_conv(vbo.m_conv)
{ {
unsigned int nbbytes = sizeof(float) * m_data_size * m_nbElts; unsigned int nbbytes = sizeof(float) * m_data_size * m_nbElts;
...@@ -97,7 +97,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib) ...@@ -97,7 +97,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib)
if (m_conv != NULL) if (m_conv != NULL)
{ {
updateData_withConversion(attrib, m_conv); updateData_withInternalConversion(attrib, m_conv);
return; return;
} }
...@@ -105,7 +105,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib) ...@@ -105,7 +105,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib)
if (amv3 != NULL) if (amv3 != NULL)
{ {
ConvertVec3dToVec3f conv; ConvertVec3dToVec3f conv;
updateData_withConversion(attrib,&conv); updateData_withInternalConversion(attrib,&conv);
return; return;
} }
...@@ -113,7 +113,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib) ...@@ -113,7 +113,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib)
if (amv2 != NULL) if (amv2 != NULL)
{ {
ConvertVec2dToVec2f conv; ConvertVec2dToVec2f conv;
updateData_withConversion(attrib,&conv); updateData_withInternalConversion(attrib,&conv);
return; return;
} }
...@@ -121,7 +121,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib) ...@@ -121,7 +121,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib)
if (amv4 != NULL) if (amv4 != NULL)
{ {
ConvertVec4dToVec4f conv; ConvertVec4dToVec4f conv;
updateData_withConversion(attrib,&conv); updateData_withInternalConversion(attrib,&conv);
return; return;
} }
...@@ -129,7 +129,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib) ...@@ -129,7 +129,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib)
if (amv1 != NULL) if (amv1 != NULL)
{ {
ConvertDoubleToFloat conv; ConvertDoubleToFloat conv;
updateData_withConversion(attrib,&conv); updateData_withInternalConversion(attrib,&conv);
return; return;
} }
...@@ -159,7 +159,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib) ...@@ -159,7 +159,7 @@ void VBO::updateData(const AttributeMultiVectorGen* attrib)
} }
void VBO::updateData_withConversion(const AttributeMultiVectorGen* attrib, ConvertBuffer* conv) void VBO::updateData_withInternalConversion(const AttributeMultiVectorGen* attrib, ConvertBuffer* conv)
{ {
m_name = attrib->getName(); m_name = attrib->getName();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment