Commit fef28fd4 authored by Jan Möbius's avatar Jan Möbius

Merge branch 'PolyLineCollection' into 'master'

Poly line collection

The PolyLineCollectionNode fills its vbo with PolyLineNode now, to support rendering any kind of vertex data available in the PolyLine.
Picking is updated to render individual picking IDs for vertices and edges.

See merge request !200
parents 0814f42a 6fb27170
Pipeline #3872 passed with stage
in 73 minutes and 7 seconds
......@@ -94,6 +94,17 @@ unsigned int VertexElement::getByteOffset() const
return static_cast<unsigned int>(offset.u);
}
bool VertexElement::operator ==(const VertexElement &_other) const
{
return (type_ == _other.type_) &&
(numElements_ == _other.numElements_) &&
(usage_ == _other.usage_) &&
(shaderInputName_ == _other.shaderInputName_) &&
(pointer_ == _other.pointer_) &&
(divisor_ == _other.divisor_) &&
(vbo_ == _other.vbo_);
}
VertexDeclaration::VertexDeclaration()
: vertexStride_(0), strideUserDefined_(0)
{
......@@ -532,11 +543,17 @@ unsigned int VertexDeclaration::getVertexStride(unsigned int i) const
if (strideUserDefined_)
return vertexStride_;
unsigned int vbo = getElement(i)->vbo_;
std::map<unsigned int, unsigned int>::const_iterator it = vertexStridesVBO_.find(vbo);
const VertexElement* element = getElement(i);
if (element)
{
unsigned int vbo = getElement(i)->vbo_;
std::map<unsigned int, unsigned int>::const_iterator it = vertexStridesVBO_.find(vbo);
return (it != vertexStridesVBO_.end()) ? it->second : vertexStride_;
}
return (it != vertexStridesVBO_.end()) ? it->second : vertexStride_;
// return vertexStride_;
return 0;
}
......@@ -616,6 +633,21 @@ QString VertexDeclaration::toString() const
return result;
}
bool VertexDeclaration::operator ==(const VertexDeclaration &_other) const
{
if (strideUserDefined_ != _other.strideUserDefined_ ||
vertexStride_ != _other.vertexStride_)
return false;
if (elements_ != _other.elements_)
return false;
if (vertexStridesVBO_ != _other.vertexStridesVBO_)
return false;
return true;
}
bool VertexDeclaration::supportsInstancedArrays()
{
static int status_ = -1;
......
......@@ -100,6 +100,14 @@ struct ACGDLLEXPORT VertexElement
/*! interpret pointer_ as byte offset
*/
unsigned int getByteOffset() const;
/*! comparison operator
*/
bool operator == (const VertexElement& _other) const;
/*! comparison operator
*/
bool operator != (const VertexElement& _other) const {return !(*this == _other);}
};
/** \brief Class to define the vertex input layout
......@@ -353,6 +361,15 @@ public:
*/
QString toString() const;
/*! comparison operator
*/
bool operator ==(const VertexDeclaration& _other) const;
/*! comparison operator
*/
bool operator !=(const VertexDeclaration& _other) const {return !(*this == _other);}
private:
/*!
......
......@@ -78,7 +78,6 @@ template <class PolyLine>
PolyLineNodeT<PolyLine>::PolyLineNodeT(PolyLine& _pl, BaseNode* _parent, std::string _name) :
BaseNode(_parent, _name),
polyline_(_pl),
vbo_(0),
updateVBO_(true),
sphere_(0)
......@@ -154,7 +153,7 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode)
ACG::GLState::disable(GL_TEXTURE_2D);
// Bind the vertex array
ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, vbo_);
vbo_.bind();
ACG::Vec4f color = _state.ambient_color() + _state.diffuse_color();
......@@ -263,7 +262,7 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode)
}
// Disable the big buffer and switch to in memory buffer
ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
vbo_.unbind();
ACG::GLState::vertexPointer(&ps[0]);
float line_width_old = _state.line_width();
......@@ -291,7 +290,7 @@ draw(GLState& _state, const DrawModes::DrawMode& _drawMode)
_state.set_line_width(line_width_old);
}
ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
vbo_.unbind();
//Disable the vertex array
ACG::GLState::disableClientState(GL_VERTEX_ARRAY);
......@@ -465,7 +464,7 @@ pick_vertices( GLState& _state )
updateVBO();
// Bind the vertex array
ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, vbo_);
vbo_.bind();
int pickOffsetIndex = int(_state.pick_current_index());
......@@ -484,7 +483,7 @@ pick_vertices( GLState& _state )
pickShader->disable();
ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
vbo_.unbind();
}
else
{
......@@ -577,7 +576,7 @@ pick_edges( GLState& _state, unsigned int _offset)
updateVBO();
// Bind the vertex array
ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, vbo_);
vbo_.bind();
int pickOffsetIndex = int(_state.pick_current_index());
......@@ -596,7 +595,7 @@ pick_edges( GLState& _state, unsigned int _offset)
vertexDecl_.deactivateShaderPipeline(pickShader);
pickShader->disable();
ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
vbo_.unbind();
}
else
{
......@@ -679,9 +678,9 @@ setupVertexDeclaration(VertexDeclaration* _dst, int _colorSource) const {
//----------------------------------------------------------------------------
template <class PolyLine>
void
size_t
PolyLineNodeT<PolyLine>::
updateVBO() {
fillVertexBuffer(void *_buf, size_t _bufSize, bool _addLineStripEndVertex) {
// register custom properties defined in polyline
......@@ -689,7 +688,7 @@ updateVBO() {
typename PolyLine::CustomPropertyHandle proph = polyline_.enumerate_custom_property_handles(i);
const void* propDataBuf = polyline_.get_custom_property_buffer(proph);
......@@ -720,17 +719,55 @@ updateVBO() {
setupVertexDeclaration(&vertexDeclVCol_, 1);
setupVertexDeclaration(&vertexDeclECol_, 2);
// fill buffer
const unsigned int stride = vertexDecl_.getVertexStride();
// create vbo if it does not exist
if (!vbo_)
GLState::genBuffersARB(1, &vbo_);
char* data = static_cast<char*>(_buf);
size_t bytesWritten = 0;
for (unsigned int i = 0 ; i < polyline_.n_vertices() && bytesWritten + stride <= _bufSize; ++i) {
writeVertex(i, data + i * stride);
bytesWritten += stride;
}
if (_addLineStripEndVertex && bytesWritten + stride <= _bufSize) {
// First point is added to the end for a closed loop
writeVertex(0, data + polyline_.n_vertices() * stride);
bytesWritten += stride;
}
return bytesWritten;
}
//----------------------------------------------------------------------------
template <class PolyLine>
void
PolyLineNodeT<PolyLine>::
updateVBO() {
setupVertexDeclaration(&vertexDecl_, 0);
const unsigned int stride = vertexDecl_.getVertexStride();
// size in bytes of vbo, create additional vertex for closed loop indexing
unsigned int bufferSize = stride * (polyline_.n_vertices() + 1);
size_t bufferSize = stride * (polyline_.n_vertices() + 1);
// Create the required array
char* vboData_ = new char[bufferSize];
std::vector<char> vboData(bufferSize);
if (bufferSize > 0) {
size_t bytesWritten = fillVertexBuffer(&vboData[0], bufferSize, true);
if (bytesWritten != bufferSize)
std::cerr << "PolyLineNode: fill vertex buffer only wrote " << bytesWritten << " bytes instead of expected " << bufferSize << " bytes" << std::endl;
// Move data to the buffer in gpu memory
vbo_.upload(bufferSize, &vboData[0], GL_STATIC_DRAW);
vbo_.unbind();
}
// Index buffer for selected vertices
selectedVertexIndexBuffer_.clear();
......@@ -740,8 +777,6 @@ updateVBO() {
for (unsigned int i = 0 ; i < polyline_.n_vertices(); ++i) {
writeVertex(i, vboData_ + i * stride);
// Create an ibo in system memory for vertex selection
if ( polyline_.vertex_selections_available() && polyline_.vertex_selected(i) )
selectedVertexIndexBuffer_.push_back(i);
......@@ -754,16 +789,6 @@ updateVBO() {
}
// First point is added to the end for a closed loop
writeVertex(0, vboData_ + polyline_.n_vertices() * stride);
// Move data to the buffer in gpu memory
GLState::bindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_);
GLState::bufferDataARB(GL_ARRAY_BUFFER_ARB, bufferSize , vboData_ , GL_STATIC_DRAW_ARB);
// Remove the local storage
delete[] vboData_;
// Update done.
updateVBO_ = false;
}
......@@ -880,7 +905,7 @@ getRenderObjects(ACG::IRenderer* _renderer, ACG::GLState& _state , const ACG::S
updateVBO();
// Set to the right vbo
ro.vertexBuffer = vbo_;
ro.vertexBuffer = vbo_.id();
// Set style
ro.debugName = "PolyLine";
......
......@@ -70,6 +70,7 @@
#include <ACG/GL/VertexDeclaration.hh>
#include <ACG/GL/IRenderer.hh>
#include <ACG/GL/GLPrimitives.hh>
#include <ACG/GL/globjects.hh>
//== FORWARDDECLARATIONS ======================================================
......@@ -176,6 +177,33 @@ public:
*/
void setCustomBuffer(int _id, const void* _buffer);
/** \brief Create the vertex declaration
*
* The vertex data in the vbo contains both vertex and edge colors,
* so the vertex declaration decides which bytes to use for colored rendering (if any).
* @param _dst Vertex declaration to initialize
* @param _colorSource 0 - no colors, 1 - vertex colors, 2 - edge colors
*
*/
void setupVertexDeclaration(VertexDeclaration* _dst, int _colorSource) const;
/** \brief Fill a buffer with vertex data
*
* Fill the specified buffer with vertex buffer data.
* The buffer size in bytes can be calculated as vertex stride * num vertices.
* If _addLineStripEndVertex is true, then the buffer size is vertex stride * (num vertices + 1) instead.
* Vertex layout and stride can be queried with via setupVertexDeclaration.
* @param _buf address to buffer to which the vertices are written
* @param _bufSize size in bytes available in the specified buffer
* @param _addLineStripEndVertex add a copy of the first vertex to the end of the buffer (useful to close the line in line_strip rendering mode)
* @return number of bytes written
*
*/
size_t fillVertexBuffer(void* _buf, size_t _bufSize, bool _addLineStripEndVertex);
private:
void pick_vertices ( GLState& _state );
......@@ -211,16 +239,6 @@ private:
*/
void updateVBO();
/** \brief Create the vertex declaration
*
* The vertex data in the vbo contains both vertex and edge colors,
* so the vertex declaration decides which bytes to use for colored rendering (if any).
* @param _dst Vertex declaration to initialize
* @param _colorSource 0 - no colors, 1 - vertex colors, 2 - edge colors
*
*/
void setupVertexDeclaration(VertexDeclaration* _dst, int _colorSource) const;
/** \brief Write vertex data for rendering to a buffer
*
* @param _vertex index of polyline vertex
......@@ -242,7 +260,7 @@ private:
PolyLine& polyline_;
/// VBO used to render the poly line
unsigned int vbo_;
GeometryBuffer vbo_;
/// Index buffer for selected vertices
std::vector<unsigned int> selectedVertexIndexBuffer_;
......
......@@ -58,6 +58,8 @@
#include <ACG/GL/VertexDeclaration.hh>
#include <ACG/GL/IRenderer.hh>
#include <ACG/GL/GLPrimitives.hh>
#include <ACG/GL/globjects.hh>
#include <ObjectTypes/PolyLine/PolyLineNodeT.hh>
//== FORWARDDECLARATIONS ======================================================
......@@ -88,7 +90,7 @@ public:
PolyLineCollectionNodeT(PolyLineCollection& _pl, BaseNode* _parent = 0, std::string _name = "<PolyLineCollectionNode>");
/// Destructor
virtual ~PolyLineCollectionNodeT() {}
virtual ~PolyLineCollectionNodeT();
PolyLineCollection& polyline_collection() { return polyline_collection_; }
......@@ -117,7 +119,7 @@ public:
void getRenderObjects(ACG::IRenderer* _renderer, ACG::GLState& _state , const ACG::SceneGraph::DrawModes::DrawMode& _drawMode , const ACG::SceneGraph::Material* _mat);
/// Trigger an update of the vbo
void update() { updateVBO_ = true; };
void update() { updateVBO_ = true; }
void resetVBO() {offsets_.clear();}
private:
......@@ -129,7 +131,9 @@ private:
PolyLineCollectionNodeT& operator=(const PolyLineCollectionNodeT& _rhs);
/// Buffer organization
ACG::VertexDeclaration vertexDecl_;
ACG::VertexDeclaration vertexDecl_; // layout without colors
ACG::VertexDeclaration vertexDeclVColor_; // layout with vertex colors
ACG::VertexDeclaration vertexDeclEColor_; // layout with edge colors
/** \brief Trigger an update of the vbo
*
......@@ -138,12 +142,9 @@ private:
*/
void updateVBO();
/** \brief Write vertex data for rendering to a buffer
*
* @param _vertex index of polyline vertex
* @param _dst address of vertex in buffer
*/
void writeVertex(typename PolyLineCollection::PolyLine* _polyline, unsigned int _vertex, void* _dst);
void pick_vertices(GLState& _state);
void pick_edges(GLState& _state, unsigned int _offset = 0);
private:
......@@ -151,7 +152,10 @@ private:
PolyLineCollection& polyline_collection_;
/// VBO used to render the poly line
unsigned int vbo_;
GeometryBuffer vbo_;
/// IBO used to render the poly line
IndexBuffer ibo_;
/// Flag to trigger update of vbo
bool updateVBO_;
......@@ -159,9 +163,13 @@ private:
/// Sphere for VertexSphere DrawMode
GLSphere* sphere_;
/// (Offset in vbo, Vertex count in vbo) for each poly line
std::vector<std::pair<size_t, size_t> > offsets_;
std::vector< PolyLineNodeT<typename PolyLineCollection::PolyLine>* > polylineNodes_;
size_t total_vertex_count_;
size_t total_segment_count_;
};
......
......@@ -54,15 +54,13 @@
#include <OpenFlipper/common/Types.hh>
#include <ObjectTypes/PolyLineCollection/PolyLineCollection.hh>
#include <OpenFlipper/BasePlugin/BaseInterface.hh>
#include <OpenFlipper/BasePlugin/LoadSaveInterface.hh>
#include <OpenFlipper/BasePlugin/LoggingInterface.hh>
#include <OpenFlipper/BasePlugin/TypeInterface.hh>
class TypePolyLineCollectionPlugin : public QObject, BaseInterface, LoadSaveInterface, LoggingInterface, TypeInterface
class TypePolyLineCollectionPlugin : public QObject, BaseInterface, LoggingInterface, TypeInterface
{
Q_OBJECT
Q_INTERFACES(BaseInterface)
Q_INTERFACES(LoadSaveInterface)
Q_INTERFACES(LoggingInterface)
Q_INTERFACES(TypeInterface)
......@@ -75,7 +73,7 @@ class TypePolyLineCollectionPlugin : public QObject, BaseInterface, LoadSaveInte
void log(Logtype _type, QString _message);
void log(QString _message);
// LoadSave Interface
// Type Interface
void emptyObjectAdded( int _id );
private slots:
......
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