53#include "ACG/GL/GLError.hh"
54#define ACG_POLYLINECOLLECTIONNODET_C
60#include "PolyLineCollectionNodeT.hh"
61#include <ACG/GL/gl.hh>
62#include <ACG/Utils/VSToolsT.hh>
64#include <OpenMesh/Core/Utils/vector_cast.hh>
80template <
class PolyLineCollection>
83 polyline_collection_(_pl),
86 total_vertex_count_(0),
87 total_segment_count_(0)
96template <
class PolyLineCollection>
99 for (
size_t i = 0; i < polylineNodes_.size(); ++i)
100 delete polylineNodes_[i];
105template <
class PolyLineCollection>
114 for (
unsigned int i=0; i< polyline->
n_vertices(); ++i)
127template <
class PolyLineCollection>
139template <
class PolyLineCollection>
145 if ( polyline_collection_.n_polylines() == 0 )
157 vertexDecl_.activateFixedFunction();
169 vertexDecl_.deactivateFixedFunction();
170 vertexDeclVColor_.activateFixedFunction();
174 glDrawArrays(GL_POINTS, 0, total_vertex_count_);
177 vertexDeclVColor_.deactivateFixedFunction();
178 vertexDecl_.activateFixedFunction();
192 glDrawArrays(GL_POINTS, offsets_[it.idx()].first, offsets_[it.idx()].second-1);
209 vertexDecl_.deactivateFixedFunction();
210 vertexDeclEColor_.activateFixedFunction();
214 glDrawElements(GL_LINES, total_segment_count_ * 2, GL_UNSIGNED_INT, 0);
218 vertexDeclEColor_.deactivateFixedFunction();
219 vertexDecl_.activateFixedFunction();
231 glDrawArrays(GL_LINE_STRIP, offsets_[it.idx()].first, offsets_[it.idx()].second);
233 glDrawArrays(GL_LINE_STRIP, offsets_[it.idx()].first, offsets_[it.idx()].second-1);
241 std::vector<int> selected_indices;
247 int offset = offsets_[it.idx()].first;
248 for(
size_t i = 0; i < polyline->
n_vertices(); ++i){
252 if(polyline->edge_selected(i)){
253 selected_indices.push_back(offset + i);
254 selected_indices.push_back(offset + i + 1);
260 if(selected_indices.size() > 0){
261 glDrawElements(GL_LINES, selected_indices.size(), GL_UNSIGNED_INT, selected_indices.data());
268 vertexDecl_.deactivateFixedFunction();
275template <
class PolyLineCollection>
280 size_t numPolyLines = polyline_collection_.n_polylines();
282 if ( numPolyLines == 0 )
290 size_t numPickIDs = 0;
292 for (
size_t i = 0; i < numPolyLines; ++i)
301 pick_vertices( _state);
307 size_t numPickIDs = 0;
309 for (
size_t i = 0; i < numPolyLines; ++i)
318 pick_edges(_state, 0);
324 size_t numPickVertices = 0;
325 size_t numPickIDs = 0;
327 for (
size_t i = 0; i < numPolyLines; ++i)
343 pick_vertices( _state);
344 pick_edges( _state,
static_cast<unsigned int>(numPickVertices));
355template <
class PolyLineCollection>
360 size_t numPolyLines = polyline_collection_.n_polylines();
365 glPointSize(point_size_old+3.0f);
368 desc.vertexTemplateFile =
"Picking/pick_vertices_vs.glsl";
369 desc.fragmentTemplateFile =
"Picking/pick_vertices_fs.glsl";
372 if (pickVertexShader && pickVertexShader->
isLinked())
378 pickVertexShader->
use();
384 vertexDecl_.activateShaderPipeline(pickVertexShader);
387 for (
size_t i = 0; i < numPolyLines; ++i)
393 pickVertexShader->
setUniform(
"pickVertexOffset", pickVertexOffset);
395 glDrawArrays(GL_POINTS, offsets_[i].first, offsets_[i].second-1);
397 pickVertexOffset += int(offsets_[i].second - 1);
401 vertexDecl_.deactivateShaderPipeline(pickVertexShader);
412 size_t curPickID = 0;
414 for (
size_t i = 0; i < numPolyLines; ++i)
420 for (
size_t v = 0; v < line->
n_vertices(); ++v)
432 glPointSize(point_size_old);
437template <
class PolyLineCollection>
439PolyLineCollectionNodeT<PolyLineCollection>::
440pick_edges(GLState &_state,
unsigned int _offset)
442 size_t numPolyLines = polyline_collection_.n_polylines();
444 float line_width_old = _state.line_width();
445 _state.set_line_width(line_width_old + 3.0f);
447 static ShaderGenDesc desc;
450 desc.vertexTemplateFile =
"Picking/vertex.glsl";
451 desc.fragmentTemplateFile =
"Picking/pick_vertices_fs2.glsl";
455 desc.vertexTemplateFile =
"Picking/pick_vertices_vs.glsl";
456 desc.fragmentTemplateFile =
"Picking/pick_vertices_fs.glsl";
461 if (pickEdgeShader && pickEdgeShader->
isLinked())
464 int pickVertexOffset = int(_state.pick_current_index() + _offset);
465 GLMatrixf mWVP = _state.projection() * _state.modelview();
467 pickEdgeShader->
use();
473 vertexDecl_.activateShaderPipeline(pickEdgeShader);
475 for (
size_t i = 0; i < numPolyLines; ++i)
481 pickEdgeShader->
setUniform(
"pickVertexOffset", pickVertexOffset);
483 size_t numIndices = line->
is_closed() ? offsets_[i].second : offsets_[i].second - 1;
484 glDrawArrays(GL_LINE_STRIP, offsets_[i].first, numIndices);
486 pickVertexOffset += int(line->
n_edges());
490 vertexDecl_.deactivateShaderPipeline(pickEdgeShader);
501 size_t curPickID = _offset;
503 for (
size_t i = 0; i < numPolyLines; ++i)
511 for (
size_t v = 0; v < line->
n_edges(); ++v)
513 _state.pick_set_name(curPickID++);
526 _state.set_line_width(line_width_old);
532template <
class PolyLineCollection>
537 bool lines_did_change =
false;
539 if( offsets_.size() != polyline_collection_.n_polylines() ){
540 offsets_.resize(polyline_collection_.n_polylines());
541 lines_did_change =
true;
545 total_vertex_count_ = 0;
546 total_segment_count_ = 0;
548 std::pair<size_t, size_t> current_offset;
549 current_offset.first = offset;
551 current_offset.second = it->n_vertices() + 1;
553 current_offset.second = 0;
556 if(current_offset != offsets_[it.idx()]){
557 lines_did_change =
true;
560 offsets_[it.idx()] = current_offset;
561 total_vertex_count_ += current_offset.second;
562 total_segment_count_ += it->n_edges();
564 offset += current_offset.second;
567 if(lines_did_change){
574 size_t prevLineCount = polylineNodes_.size();
575 size_t curLineCount = polyline_collection_.n_polylines();
579 if (curLineCount < prevLineCount)
581 for (
size_t i = curLineCount; i < prevLineCount; ++i)
582 delete polylineNodes_[i];
585 polylineNodes_.resize(curLineCount);
588 for(
size_t i = 0; i < curLineCount; ++i) {
597 vertexDeclVColor_.clear();
598 vertexDeclEColor_.clear();
601 if (curLineCount > 0) {
602 polylineNodes_[0]->setupVertexDeclaration(&vertexDecl_, 0);
603 polylineNodes_[0]->setupVertexDeclaration(&vertexDeclVColor_, 1);
604 polylineNodes_[0]->setupVertexDeclaration(&vertexDeclEColor_, 2);
608 bool equalVertexFormat =
true;
610 for (
size_t i = 1; i < curLineCount; ++i) {
614 for (
int k = 0; k < 3; ++k)
615 polylineNodes_[i]->setupVertexDeclaration(&decl[k], k);
617 if (decl[0] != vertexDecl_ ||
618 decl[1] != vertexDeclVColor_ ||
619 decl[2] != vertexDeclEColor_)
620 equalVertexFormat =
false;
624 if (!equalVertexFormat)
625 std::cerr <<
"PolyLineCollection error: polylines do not have the same vertex format, so the collection vbo could not be created" << std::endl;
630 size_t bufferSize = stride * offset;
632 if (bufferSize > 0) {
633 std::vector<char> vboData(bufferSize);
634 std::vector<unsigned int> iboData(total_segment_count_ * 2);
636 size_t offsetSegment = 0;
638 for(
size_t i = 0; i < curLineCount; ++i) {
642 if (polyline && polylineNodes_[i] && polyline->
n_vertices() > 0) {
645 size_t offset = offsets_[i].first;
647 polylineNodes_[i]->fillVertexBuffer(&vboData[(offset) * stride], bufferSize,
true);
650 for (
size_t k = 0; k < polyline->
n_edges(); ++k) {
651 iboData[(offsetSegment + k) * 2] = offset + k;
652 iboData[(offsetSegment + k) * 2 + 1] = offset + (k + 1) % polyline->
n_vertices();
654 offsetSegment += polyline->
n_edges();
661 vbo_.upload(vboData.size() *
sizeof(vboData[0]), &vboData[0], GL_STATIC_DRAW);
664 ibo_.upload(iboData.size() *
sizeof(iboData[0]), &iboData[0], GL_STATIC_DRAW);
681template <
class PolyLineCollection>
687 if ( polyline_collection_.n_polylines() == 0 )
694 ro.setMaterial(_mat);
707 ro.debugName =
"PolyLineCollection";
753 ro.setMaterial(&localMaterial);
756 if(props->primitive() == ACG::SceneGraph::DrawModes::PRIMITIVE_POINT){
758 ro.debugName =
"polyline.Points.selected";
760 ro.setMaterial(&localMaterial);
766 ro.debugName =
"polylinecollection.Points";
768 ro.setMaterial(&localMaterial);
775 applyRenderObjectSettings(props->primitive(), &ro);
778 ro.glDrawArrays(GL_POINTS, 0, total_vertex_count_);
781 }
else if(props->primitive() == ACG::SceneGraph::DrawModes::PRIMITIVE_WIREFRAME ||
782 props->primitive() == ACG::SceneGraph::DrawModes::PRIMITIVE_EDGE){
784 ro.debugName =
"polyline.Wireframe.selected";
786 ro.setMaterial(&localMaterial);
791 ro.debugName =
"polylinecollection.Wireframe";
793 ro.setMaterial(&localMaterial);
802 applyRenderObjectSettings(props->primitive(), &ro);
806 ro.glDrawElements(GL_LINES, total_segment_count_ * 2, GL_UNSIGNED_INT, 0);
const GLMatrixd & modelview() const
get modelview matrix
int viewport_width() const
get viewport width
void pick_set_name(size_t _idx)
sets the current name/color (like glLoadName(_idx))
bool pick_set_maximum(size_t _idx)
Set the maximal number of primitives/components of your object.
const GLMatrixd & projection() const
get projection matrix
void set_color(const Vec4f &_col)
set color
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
void set_line_width(float _f)
set line width
const Vec4f & ambient_color() const
get ambient color
void set_point_size(float _f)
set point size
size_t pick_current_index() const
Returns the current color picking index (can be used for caching)
int viewport_height() const
get viewport height
float line_width() const
get line width
const Vec4f & diffuse_color() const
get diffuse color
static void bindBuffer(GLenum _target, GLuint _buffer)
replaces glBindBuffer, supports locking
float point_size() const
get point size
virtual void addRenderObject(RenderObject *_renderObject)
Callback for the scenegraph nodes, which send new render objects via this function.
size_t n_vertices() const
Get number of vertices.
Point & point(unsigned int _i)
Get a point of the polyline.
size_t n_edges() const
Get number of edges.
bool is_closed() const
Check if the polyline is marked as closed.
DrawModeProperties stores a set of properties that defines, how to render an object.
bool colored() const
Are colors used?
const DrawModeProperties * getLayer(unsigned int _i) const
returns the property set at layer i
size_t getNumLayers() const
returns the layer count
void baseColor(const Vec4f &_c)
set the base color (Sets the baseColor which is the same as the emission(const Vec4f& _c) )
void pointSize(float _sz)
set point size (default: 1.0)
DrawModes::DrawMode availableDrawModes() const override
return available draw modes
void updateVBO()
Trigger an update of the vbo.
void pick(GLState &_state, PickTarget _target) override
picking
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax) override
update bounding box
PolyLineCollectionNodeT(PolyLineCollection &_pl, BaseNode *_parent=0, std::string _name="<PolyLineCollectionNode>")
Constructor.
void getRenderObjects(ACG::IRenderer *_renderer, ACG::GLState &_state, const ACG::SceneGraph::DrawModes::DrawMode &_drawMode, const ACG::SceneGraph::Material *_mat) override
Add the objects to the given renderer.
virtual ~PolyLineCollectionNodeT()
Destructor.
void draw(GLState &, const DrawModes::DrawMode &_drawMode) override
draw lines and normals
static ShaderCache * getInstance()
Return instance of the ShaderCache singleton.
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > &_mods)
Query a dynamically generated program from cache.
QString vertexColorsInterpolator
interpolation qualifier for input vertex colors: "flat", "smooth", "noperspective"
Class to define the vertex input layout.
unsigned int getVertexStride(unsigned int i=0) const
bool isLinked()
Returns if the program object has been succesfully linked.
void disable()
Resets to standard rendering pipeline.
void use()
Enables the program object for using.
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
DrawMode POINTS_COLORED
draw colored, but not lighted points (requires point colors)
DrawMode WIREFRAME
draw wireframe
DrawMode POINTS
draw unlighted points using the default base color
DrawMode EDGES_COLORED
draw edges with colors (without shading)
PickTarget
What target to use for picking.
@ PICK_EDGE
picks edges (may not be implemented for all nodes)
@ PICK_ANYTHING
pick any of the prior targets (should be implemented for all nodes)
@ PICK_VERTEX
picks verices (may not be implemented for all nodes)
Namespace providing different geometric functions concerning angles.
GLMatrixT< float > GLMatrixf
typedef
VectorT< float, 4 > Vec4f
bool ACGDLLEXPORT openGLVersionTest(const int _major, const int _minor)
Interface class between scenegraph and renderer.
ShaderGenDesc shaderDesc
Drawmode and other shader params.
void setupLineRendering(float _lineWidth, const Vec2f &_screenSize)
Setup rendering of thick lines.
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
void setupShaderGenFromDrawmode(const SceneGraph::DrawModes::DrawModeProperties *_props)
Fills out ShaderGenDesc parameters based on Drawmode properties.
GLuint indexBuffer
Use vertex array object.
int priority
Priority to allow sorting of objects.
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
void setupPointRendering(float _pointSize, const Vec2f &_screenSize)
Setup rendering of circle points.