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

new VBO updating conversions methods

parent 5ecec1a3
......@@ -71,7 +71,7 @@ void MyQT::createMap()
// create another attribute on vertices (for edges drawing)
VertexAttribute<int, MAP> colorE = myMap.addAttribute<int, VERTEX, MAP>("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<int,3>(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<int,3>(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<int> conv2col(0,255);
m_colorVBO1->updateDataConversion<int,3>(colorE,conv2col);
// 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-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<PFP>(myMap, Algo::Render::GL2::TRIANGLES);
m_render->initPrimitives<PFP>(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);
......
......@@ -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;
......
This diff is collapsed.
......@@ -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<NB_COMPONENTS,float>
* @param conv lambda or function/fonctor 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)
inline void updateDataConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv)
{
typedef Geom::Vector<NB_COMPONENTS,float> T_OUT;
const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen();
updateDataConversion<T_IN,Geom::Vector<NB_COMPONENTS,float>,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<NB_COMPONENTS,float>
*/
template <typename T_IN, typename CONVFUNC>
inline void updateDataConversion(const AttributeHandlerGen& attribHG, CONVFUNC conv)
{
const AttributeMultiVectorGen* attrib = attribHG.getDataVectorGen();
updateDataConversion<T_IN,float,1,CONVFUNC>(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<NB_COMPONENTS,float>
*/
template <typename T_IN, typename T_OUT, unsigned int NB_COMPONENTS, typename CONVFUNC>
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:
}
};
......
......@@ -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();
......
......@@ -26,6 +26,7 @@
#include "Utils/GLSLShader.h"
#include <stdio.h>
#include <string.h>
#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<Geom::Vec3d>* amv3 = dynamic_cast<const AttributeMultiVector<Geom::Vec3d>*>(attrib);
if (amv3 != NULL)
{
ConvertVec3dToVec3f conv;
updateData_withInternalConversion(attrib,&conv);
updateDataConversion<Geom::Vec3d,Geom::Vec3f,3>(attrib,DataConversion::funcVecXdToVecXf<3>);
return;
}
const AttributeMultiVector<Geom::Vec2d>* amv2 = dynamic_cast<const AttributeMultiVector<Geom::Vec2d>*>(attrib);
if (amv2 != NULL)
{
ConvertVec2dToVec2f conv;
updateData_withInternalConversion(attrib,&conv);
updateDataConversion<Geom::Vec2d,Geom::Vec2f,2>(attrib,DataConversion::funcVecXdToVecXf<2>);
return;
}
const AttributeMultiVector<Geom::Vec4d>* amv4 = dynamic_cast<const AttributeMultiVector<Geom::Vec4d>*>(attrib);
if (amv4 != NULL)
{
ConvertVec4dToVec4f conv;
updateData_withInternalConversion(attrib,&conv);
updateDataConversion<Geom::Vec4d,Geom::Vec4f,4>(attrib,DataConversion::funcVecXdToVecXf<4>);
return;
}
const AttributeMultiVector<double>* amv1 = dynamic_cast<const AttributeMultiVector<double>*>(attrib);
if (amv1 != NULL)
{
ConvertDoubleToFloat conv;
updateData_withInternalConversion(attrib,&conv);
updateDataConversion<double,float,1>(attrib,DataConversion::funcToFloat<double>);
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<void*> 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<void*> 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);
}
......
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