PolyLineCollectionNodeT.cc 24.7 KB
Newer Older
Christopher Tenter's avatar
Christopher Tenter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                       *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/




//=============================================================================
//
//  CLASS PolyLineNodeT - IMPLEMENTATION
//
//=============================================================================

#define ACG_POLYLINECOLLECTIONNODET_C

//== INCLUDES =================================================================



#include "PolyLineCollectionNodeT.hh"
#include <ACG/GL/gl.hh>
#include <ACG/Utils/VSToolsT.hh>
#include <vector>
#include <OpenMesh/Core/Utils/vector_cast.hh>

#ifdef _WIN32
#include <cstdint>
#else
#include <stdint.h>
#endif

//== NAMESPACES ===============================================================

namespace ACG {
namespace SceneGraph {

//== IMPLEMENTATION ==========================================================

/// Constructor
template <class PolyLineCollection>
PolyLineCollectionNodeT<PolyLineCollection>::PolyLineCollectionNodeT(PolyLineCollection& _pl, BaseNode* _parent, std::string _name) :
        BaseNode(_parent, _name),
        polyline_collection_(_pl),
        updateVBO_(true),
90 91
        sphere_(0),
        total_vertex_count_(0)
Christopher Tenter's avatar
Christopher Tenter committed
92 93 94 95 96 97 98 99

{
  // Initial default draw mode
  drawMode(DrawModes::WIREFRAME | DrawModes::POINTS );
}

//----------------------------------------------------------------------------

100 101 102 103 104 105 106 107 108
template <class PolyLineCollection>
PolyLineCollectionNodeT<PolyLineCollection>::~PolyLineCollectionNodeT()
{
  for (size_t i = 0; i < polylineNodes_.size(); ++i)
    delete polylineNodes_[i];
}

//----------------------------------------------------------------------------

Christopher Tenter's avatar
Christopher Tenter committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
template <class PolyLineCollection>
void
PolyLineCollectionNodeT<PolyLineCollection>::
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
{
    for(typename PolyLineCollection::iterator it = polyline_collection_.iter(); it; ++it){
        typename PolyLineCollection::PolyLine* polyline = *it;

        if(polyline){
            for (unsigned int i=0; i< polyline->n_vertices(); ++i)
            {
                _bbMin.minimize(polyline->point(i));
                _bbMax.maximize(polyline->point(i));
            }
        }
    }
}


//----------------------------------------------------------------------------


template <class PolyLineCollection>
DrawModes::DrawMode
PolyLineCollectionNodeT<PolyLineCollection>::
availableDrawModes() const
{
136
  return (DrawModes::WIREFRAME | DrawModes::POINTS | DrawModes::POINTS_COLORED | DrawModes::EDGES_COLORED );
Christopher Tenter's avatar
Christopher Tenter committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}


//----------------------------------------------------------------------------


template <class PolyLineCollection>
void
PolyLineCollectionNodeT<PolyLineCollection>::
draw(GLState& _state, const DrawModes::DrawMode& _drawMode)
{
    // Block if we do not have any polylines
    if ( polyline_collection_.n_polylines() == 0 )
        return;

    // Update the vbo only if required.
    if ( updateVBO_ )
        updateVBO();

    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::disable(GL_TEXTURE_2D);

    // Bind the vertex array
160
    vbo_.bind();
Christopher Tenter's avatar
Christopher Tenter committed
161 162 163 164
    vertexDecl_.activateFixedFunction();

    ACG::Vec4f color = _state.ambient_color()  + _state.diffuse_color();

165 166
    bool vertexColors = _drawMode & DrawModes::POINTS_COLORED;

Christopher Tenter's avatar
Christopher Tenter committed
167
    // draw points
168
    if (_drawMode & DrawModes::POINTS || vertexColors)
Christopher Tenter's avatar
Christopher Tenter committed
169 170 171
    {
        _state.set_color( color );

172 173 174 175 176
        if (vertexColors) {
          vertexDecl_.deactivateFixedFunction();
          vertexDeclVColor_.activateFixedFunction();
        }

Christopher Tenter's avatar
Christopher Tenter committed
177
        // Draw all vertices (don't care about selection)
178
        glDrawArrays(GL_POINTS, 0, total_vertex_count_);
Christopher Tenter's avatar
Christopher Tenter committed
179

180 181 182
        if (vertexColors) {
          vertexDeclVColor_.deactivateFixedFunction();
          vertexDecl_.activateFixedFunction();
Christopher Tenter's avatar
Christopher Tenter committed
183 184 185 186 187 188
        }


        float point_size_old = _state.point_size();
        _state.set_point_size(point_size_old+4.0f);

189
        _state.set_color( Vec4f(1.0f,0.0f,0.0f,1.0f) );
Christopher Tenter's avatar
Christopher Tenter committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203

        // Draw selected polylines
        for(typename PolyLineCollection::index_iterator it = polyline_collection_.selected_iter(); it; ++it){
            typename PolyLineCollection::PolyLine* polyline = *it;

            if(polyline && polyline->n_vertices() > 0){
                glDrawArrays(GL_POINTS, offsets_[it.idx()].first, offsets_[it.idx()].second-1);
            }
        }

        _state.set_point_size(point_size_old);

    }

204 205 206

    bool edgeColors = _drawMode & DrawModes::EDGES_COLORED;

Christopher Tenter's avatar
Christopher Tenter committed
207
    // draw line segments
208
    if (_drawMode & DrawModes::WIREFRAME || edgeColors) {
Christopher Tenter's avatar
Christopher Tenter committed
209 210 211

        _state.set_color( color );

212 213 214 215
        if (edgeColors) {
          vertexDecl_.deactivateFixedFunction();
          vertexDeclEColor_.activateFixedFunction();
        }
Christopher Tenter's avatar
Christopher Tenter committed
216

217 218 219
        ibo_.bind();
        glDrawElements(GL_LINES, total_segment_count_ * 2, GL_UNSIGNED_INT, 0);
        ibo_.unbind();
Christopher Tenter's avatar
Christopher Tenter committed
220

221 222 223
        if (edgeColors) {
          vertexDeclEColor_.deactivateFixedFunction();
          vertexDecl_.activateFixedFunction();
Christopher Tenter's avatar
Christopher Tenter committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        }

        float line_width_old = _state.line_width();
        _state.set_color(Vec4f(1, 0, 0, 1));
        _state.set_line_width(2 * line_width_old);

        for(typename PolyLineCollection::index_iterator it = polyline_collection_.selected_iter(); it; ++it){
            typename PolyLineCollection::PolyLine* polyline = *it;

            if(polyline && polyline->n_vertices() > 0){
                if ( polyline->is_closed() ){
                    glDrawArrays(GL_LINE_STRIP, offsets_[it.idx()].first, offsets_[it.idx()].second);
                }else{
                    glDrawArrays(GL_LINE_STRIP, offsets_[it.idx()].first, offsets_[it.idx()].second-1);
                }

                //offset += polyline->n_vertices() + 1;
            }
        }

        // Draw selected edges
        std::vector<int> selected_indices;
        for(typename PolyLineCollection::index_iterator it = polyline_collection_.visible_iter(); it; ++it){
            typename PolyLineCollection::PolyLine* polyline = *it;


            if(polyline){
                int offset = offsets_[it.idx()].first;
252
                for(size_t i = 0; i < polyline->n_vertices(); ++i){
Christopher Tenter's avatar
Christopher Tenter committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
                    if(!polyline->is_closed() && i == polyline->n_vertices() - 1){
                        continue;
                    }
                    if(polyline->edge_selected(i)){
                        selected_indices.push_back(offset + i);
                        selected_indices.push_back(offset + i + 1);
                    }
                }
            }
        }

        if(selected_indices.size() > 0){
            glDrawElements(GL_LINES, selected_indices.size(), GL_UNSIGNED_INT, selected_indices.data());
        }

        _state.set_line_width(line_width_old);

    }

    vertexDecl_.deactivateFixedFunction();
    ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
}

//----------------------------------------------------------------------------


template <class PolyLineCollection>
void
PolyLineCollectionNodeT<PolyLineCollection>::
pick(GLState& _state, PickTarget _target)
{
284
  size_t numPolyLines = polyline_collection_.n_polylines();
285

286
  if (  numPolyLines == 0 )
Christopher Tenter's avatar
Christopher Tenter committed
287 288 289
    return;


290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  switch (_target)
  {
    case PICK_VERTEX:
    {
      size_t numPickIDs = 0;

      for (size_t i = 0; i < numPolyLines; ++i)
      {
        typename PolyLineCollection::PolyLine* line = polyline_collection_.polyline(i);

        if (line)
          numPickIDs += line->n_vertices();
      }

      _state.pick_set_maximum (numPickIDs);
      pick_vertices( _state);
      break;
    }

    case PICK_EDGE:
    {
      size_t numPickIDs = 0;

      for (size_t i = 0; i < numPolyLines; ++i)
      {
        typename PolyLineCollection::PolyLine* line = polyline_collection_.polyline(i);

        if (line)
          numPickIDs += line->n_edges();
      }

      _state.pick_set_maximum (numPickIDs);
      pick_edges(_state, 0);
      break;
    }

    case PICK_ANYTHING:
    {
      size_t numPickVertices = 0;
      size_t numPickIDs = 0;

      for (size_t i = 0; i < numPolyLines; ++i)
      {
        typename PolyLineCollection::PolyLine* line = polyline_collection_.polyline(i);

        if (line)
        {
          numPickIDs += line->n_vertices();
          numPickIDs += line->n_edges();

          numPickVertices += line->n_vertices();
        }
      }

      _state.pick_set_maximum (numPickIDs);

      pick_vertices( _state);
      pick_edges( _state, static_cast<unsigned int>(numPickVertices));
      break;
    }
Christopher Tenter's avatar
Christopher Tenter committed
350

351 352 353 354 355 356 357 358 359 360 361 362 363
    default:
      break;
  }
}

//----------------------------------------------------------------------------

template <class PolyLineCollection>
void
PolyLineCollectionNodeT<PolyLineCollection>::
pick_vertices(GLState &_state)
{
  size_t numPolyLines = polyline_collection_.n_polylines();
Christopher Tenter's avatar
Christopher Tenter committed
364

365
  // draw vertex pick IDs
Christopher Tenter's avatar
Christopher Tenter committed
366 367 368

  float point_size_old = _state.point_size();
  glPointSize(point_size_old+3.0f);
369 370 371 372 373


  GLSL::Program* pickVertexShader = ACG::ShaderCache::getInstance()->getProgram("Picking/pick_vertices_vs.glsl", "Picking/pick_vertices_fs.glsl");

  if (pickVertexShader && pickVertexShader->isLinked())
Christopher Tenter's avatar
Christopher Tenter committed
374
  {
375 376 377
    // shader needs picking ID offset and transform matrix
    int pickVertexOffset = int(_state.pick_current_index());
    GLMatrixf mWVP = _state.projection() * _state.modelview();
Christopher Tenter's avatar
Christopher Tenter committed
378

379
    pickVertexShader->use();
Christopher Tenter's avatar
Christopher Tenter committed
380

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    pickVertexShader->setUniform("mWVP", mWVP);

    vbo_.bind();

    vertexDecl_.activateShaderPipeline(pickVertexShader);


    for (size_t i = 0; i < numPolyLines; ++i)
    {
      typename PolyLineCollection::PolyLine* line = polyline_collection_.polyline(i);

      if (line && line->n_vertices() > 0)
      {
        pickVertexShader->setUniform("pickVertexOffset", pickVertexOffset);

        glDrawArrays(GL_POINTS, offsets_[i].first, offsets_[i].second-1);

        pickVertexOffset += int(offsets_[i].second - 1);
      }
    }

    vertexDecl_.deactivateShaderPipeline(pickVertexShader);

    vbo_.unbind();

    pickVertexShader->disable();
  }
  else
  {
    // fallback implementation: draw picking ids without shader
    glBegin(GL_POINTS);

    size_t curPickID = 0;

    for (size_t i = 0; i < numPolyLines; ++i)
    {
      typename PolyLineCollection::PolyLine* line = polyline_collection_.polyline(i);

      if (line && line->n_vertices() > 0)
      {
        for (size_t v = 0; v < line->n_vertices(); ++v)
        {
          _state.pick_set_name(curPickID++);

          glVertex3d(line->point(v)[0], line->point(v)[1], line->point(v)[2]);
        }
Christopher Tenter's avatar
Christopher Tenter committed
427
      }
428 429 430
    }

    glEnd();
Christopher Tenter's avatar
Christopher Tenter committed
431
  }
432

Christopher Tenter's avatar
Christopher Tenter committed
433
  glPointSize(point_size_old);
434
}
Christopher Tenter's avatar
Christopher Tenter committed
435

436 437 438 439 440 441 442 443 444 445
//----------------------------------------------------------------------------

template <class PolyLineCollection>
void
PolyLineCollectionNodeT<PolyLineCollection>::
pick_edges(GLState &_state, unsigned int _offset)
{
  size_t numPolyLines = polyline_collection_.n_polylines();

  // draw edge picking IDs
Christopher Tenter's avatar
Christopher Tenter committed
446 447 448
  float line_width_old = _state.line_width();
  glLineWidth(line_width_old+3.0f);

449
  GLSL::Program* pickEdgeShader = ACG::ShaderCache::getInstance()->getProgram("Picking/vertex.glsl", "Picking/pick_vertices_fs2.glsl");
Christopher Tenter's avatar
Christopher Tenter committed
450

451 452 453 454 455
  if (pickEdgeShader && pickEdgeShader->isLinked())
  {
    // shader needs picking ID offset and transform matrix
    int pickVertexOffset = int(_state.pick_current_index() + _offset);
    GLMatrixf mWVP = _state.projection() * _state.modelview();
Christopher Tenter's avatar
Christopher Tenter committed
456

457
    pickEdgeShader->use();
Christopher Tenter's avatar
Christopher Tenter committed
458

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
    pickEdgeShader->setUniform("mWVP", mWVP);

    vbo_.bind();

    vertexDecl_.activateShaderPipeline(pickEdgeShader);

    for (size_t i = 0; i < numPolyLines; ++i)
    {
      typename PolyLineCollection::PolyLine* line = polyline_collection_.polyline(i);

      if (line && line->n_vertices() > 0)
      {
        pickEdgeShader->setUniform("pickVertexOffset", pickVertexOffset);

        size_t numIndices = line->is_closed() ? offsets_[i].second : offsets_[i].second - 1;
        glDrawArrays(GL_LINE_STRIP, offsets_[i].first, numIndices);

        pickVertexOffset += int(line->n_edges());
Christopher Tenter's avatar
Christopher Tenter committed
477
      }
478 479 480 481 482 483 484
    }

    vertexDecl_.deactivateShaderPipeline(pickEdgeShader);

    vbo_.unbind();

    pickEdgeShader->disable();
Christopher Tenter's avatar
Christopher Tenter committed
485
  }
486 487 488 489 490 491
  else
  {
    // fallback implementation: draw picking ids without shader
    glBegin(GL_LINES);

    size_t curPickID = _offset;
Christopher Tenter's avatar
Christopher Tenter committed
492

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    for (size_t i = 0; i < numPolyLines; ++i)
    {
      typename PolyLineCollection::PolyLine* line = polyline_collection_.polyline(i);

      if (line)
      {
        size_t nv = line->n_vertices();

        for (size_t v = 0; v < line->n_edges(); ++v)
        {
          _state.pick_set_name(curPickID++);

          glVertex3d(line->point(v)[0], line->point(v)[1], line->point(v)[2]);

          size_t e = (v+1)%nv;
          glVertex3d(line->point(e)[0], line->point(e)[1], line->point(e)[2]);
        }
      }
    }

    glEnd();
  }

  glLineWidth(line_width_old);
Christopher Tenter's avatar
Christopher Tenter committed
517 518 519 520 521 522 523 524 525 526 527
}

//----------------------------------------------------------------------------

template <class PolyLineCollection>
void
PolyLineCollectionNodeT<PolyLineCollection>::
updateVBO() {

    bool lines_did_change = false;

528
    if( offsets_.size() != polyline_collection_.n_polylines() ){
Christopher Tenter's avatar
Christopher Tenter committed
529 530 531 532 533 534
        offsets_.resize(polyline_collection_.n_polylines());
        lines_did_change = true;
    }

    int offset = 0;
    total_vertex_count_ = 0;
535
    total_segment_count_ = 0;
Christopher Tenter's avatar
Christopher Tenter committed
536
    for(typename PolyLineCollection::iterator it = polyline_collection_.iter(); it; ++it){
537
        std::pair<size_t, size_t> current_offset;
Christopher Tenter's avatar
Christopher Tenter committed
538 539
        current_offset.first = offset;
        if(*it){
540
            current_offset.second = it->n_vertices() + 1;
Christopher Tenter's avatar
Christopher Tenter committed
541 542 543 544 545 546 547 548 549 550
        }else{
            current_offset.second = 0;
        }

        if(current_offset != offsets_[it.idx()]){
            lines_did_change = true;
        }

        offsets_[it.idx()] = current_offset;
        total_vertex_count_ += current_offset.second;
551
        total_segment_count_ += it->n_edges();
Christopher Tenter's avatar
Christopher Tenter committed
552 553 554 555 556 557 558

        offset += current_offset.second;
    }

    if(lines_did_change){


559 560
      // update internal line node
      // these are used exclusively used to fill the vertex buffer
Christopher Tenter's avatar
Christopher Tenter committed
561 562


563 564
      size_t prevLineCount = polylineNodes_.size();
      size_t curLineCount = polyline_collection_.n_polylines();
Christopher Tenter's avatar
Christopher Tenter committed
565 566


567 568 569 570 571 572
      // free memory for removed lines
      if (curLineCount < prevLineCount)
      {
        for (size_t i = curLineCount; i < prevLineCount; ++i)
          delete polylineNodes_[i];
      }
Christopher Tenter's avatar
Christopher Tenter committed
573

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
      polylineNodes_.resize(curLineCount);


      for(size_t i = 0; i < curLineCount; ++i) {
        typename PolyLineCollection::PolyLine* polyline = polyline_collection_.polyline(i);
        polylineNodes_[i] = new PolyLineNodeT<typename PolyLineCollection::PolyLine>(*polyline);
      }



      // Update the vertex declaration based on the input data:
      vertexDecl_.clear();
      vertexDeclVColor_.clear();
      vertexDeclEColor_.clear();


      if (curLineCount > 0) {
        polylineNodes_[0]->setupVertexDeclaration(&vertexDecl_, 0);
592 593
        polylineNodes_[0]->setupVertexDeclaration(&vertexDeclVColor_, 1);
        polylineNodes_[0]->setupVertexDeclaration(&vertexDeclEColor_, 2);
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609


        // make sure that all polylines have the same vertex format
        bool equalVertexFormat = true;

        for (size_t i = 1; i < curLineCount; ++i) {

          VertexDeclaration decl[3];

          for (int k = 0; k < 3; ++k)
            polylineNodes_[i]->setupVertexDeclaration(&decl[k], k);

          if (decl[0] != vertexDecl_ ||
              decl[1] != vertexDeclVColor_ ||
              decl[2] != vertexDeclEColor_)
            equalVertexFormat = false;
Christopher Tenter's avatar
Christopher Tenter committed
610 611 612
        }


613 614 615
        if (!equalVertexFormat)
          std::cerr << "PolyLineCollection error: polylines do not have the same vertex format, so the collection vbo could not be created" << std::endl;
        else {
Christopher Tenter's avatar
Christopher Tenter committed
616

617 618 619
          // size in bytes of vbo,  create additional vertex for closed loop indexing
          size_t stride = vertexDecl_.getVertexStride();
          size_t bufferSize = stride * offset;
Christopher Tenter's avatar
Christopher Tenter committed
620

621 622
          if (bufferSize > 0) {
            std::vector<char> vboData(bufferSize);
623
            std::vector<unsigned int> iboData(total_segment_count_ * 2);
Christopher Tenter's avatar
Christopher Tenter committed
624

625
            size_t offsetSegment = 0;
Christopher Tenter's avatar
Christopher Tenter committed
626

627 628 629 630 631 632
            for(size_t i = 0; i < curLineCount; ++i) {
              typename PolyLineCollection::PolyLine* polyline = polyline_collection_.polyline(i);


              if (polyline && polylineNodes_[i] && polyline->n_vertices() > 0) {

633
                // fill vertex buffer data
634 635 636 637
                size_t offset = offsets_[i].first;

                polylineNodes_[i]->fillVertexBuffer(&vboData[(offset) * stride], bufferSize, true);

638 639 640 641 642 643 644
                // fill index buffer data
                for (size_t k = 0; k < polyline->n_edges(); ++k) {
                  iboData[(offsetSegment + k) * 2] = offset + k;
                  iboData[(offsetSegment + k) * 2 + 1] = offset + (k + 1) % polyline->n_vertices();
                }
                offsetSegment += polyline->n_edges();

645
              }
Christopher Tenter's avatar
Christopher Tenter committed
646

647 648 649
            }

            // Move data to the buffer in gpu memory
650
            vbo_.upload(vboData.size() * sizeof(vboData[0]), &vboData[0], GL_STATIC_DRAW);
651
            vbo_.unbind();
652 653 654

            ibo_.upload(iboData.size() * sizeof(iboData[0]), &iboData[0], GL_STATIC_DRAW);
            ibo_.unbind();
655
          }
Christopher Tenter's avatar
Christopher Tenter committed
656

657 658 659 660 661 662 663 664 665
        }

      }
    }



    // Update done.
    updateVBO_ = false;
Christopher Tenter's avatar
Christopher Tenter committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
}

//----------------------------------------------------------------------------

template <class PolyLineCollection>
void
PolyLineCollectionNodeT<PolyLineCollection>::
getRenderObjects(ACG::IRenderer* _renderer, ACG::GLState&  _state , const ACG::SceneGraph::DrawModes::DrawMode&  _drawMode , const ACG::SceneGraph::Material* _mat) {

  // Block if we do not have any vertices
  if ( polyline_collection_.n_polylines() == 0 )
    return;

  // init base render object
  ACG::RenderObject ro;

  _state.enable(GL_COLOR_MATERIAL);
  _state.enable(GL_LIGHTING);
  ro.initFromState(&_state);

  ro.setMaterial(_mat);

  // draw after scene-meshes
  ro.priority = 1;

  // Update the vbo only if required.
  if ( updateVBO_ )
    updateVBO();

  // Set to the right vbo
696
  ro.vertexBuffer = vbo_.id();
Christopher Tenter's avatar
Christopher Tenter committed
697 698 699 700 701 702 703 704 705 706 707 708 709

  // Set style
  ro.debugName = "PolyLineCollection";
  ro.blending = false;
  ro.depthTest = true;

  // Default color
  ACG::Vec4f defaultColor   = _state.ambient_color()  + _state.diffuse_color();
  ACG::Vec4f selectionColor = ACG::Vec4f(1.0,0.0,0.0,1.0);

  // Viewport size
  ACG::Vec2f screenSize(float(_state.viewport_width()), float(_state.viewport_height()));

710
  for (size_t i = 0; i < _drawMode.getNumLayers(); ++i) {
Christopher Tenter's avatar
Christopher Tenter committed
711 712 713 714 715 716 717
    ACG::SceneGraph::Material localMaterial = *_mat;

    const ACG::SceneGraph::DrawModes::DrawModeProperties* props = _drawMode.getLayer(i);

    ro.setupShaderGenFromDrawmode(props);
    ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

    if (props->colored())
    {
      if (props->colorSource() == ACG::SceneGraph::DrawModes::COLOR_PER_EDGE)
      {
        ro.vertexDecl = &vertexDeclEColor_;
        ro.shaderDesc.vertexColorsInterpolator = "flat";
      }
      else
      {
        ro.vertexDecl = &vertexDeclVColor_;
        ro.shaderDesc.vertexColorsInterpolator.clear();
      }

      ro.shaderDesc.vertexColors = true;
    }
    else
    {
      ro.vertexDecl = &vertexDecl_;
      ro.shaderDesc.vertexColors = false;
    }

Christopher Tenter's avatar
Christopher Tenter committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
    //---------------------------------------------------
    // No lighting!
    // Therefore we need some emissive color
    //---------------------------------------------------
    localMaterial.baseColor(defaultColor);
    ro.setMaterial(&localMaterial);


    if(props->primitive() == ACG::SceneGraph::DrawModes::PRIMITIVE_POINT){
        // Render all vertices which are selected via an index buffer
        ro.debugName = "polyline.Points.selected";
        localMaterial.baseColor(selectionColor);
        ro.setMaterial(&localMaterial);

        // Point Size geometry shader
        ro.setupPointRendering(_mat->pointSize(), screenSize);

        // Render all vertices (ignore selection here!)
        ro.debugName = "polylinecollection.Points";
        localMaterial.baseColor(defaultColor);
        ro.setMaterial(&localMaterial);


763 764
        // Point Size geometry shader
        ro.setupPointRendering(_mat->pointSize(), screenSize);
Christopher Tenter's avatar
Christopher Tenter committed
765

766 767
        // apply user settings
        applyRenderObjectSettings(props->primitive(), &ro);
768

Christopher Tenter's avatar
Christopher Tenter committed
769

770 771
        ro.glDrawArrays(GL_POINTS, 0, total_vertex_count_);
        _renderer->addRenderObject(&ro);
Christopher Tenter's avatar
Christopher Tenter committed
772

773 774
    }else if(props->primitive() == ACG::SceneGraph::DrawModes::PRIMITIVE_WIREFRAME ||
      props->primitive() == ACG::SceneGraph::DrawModes::PRIMITIVE_EDGE){
Christopher Tenter's avatar
Christopher Tenter committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788
        // Render all edges which are selected via an index buffer
        ro.debugName = "polyline.Wireframe.selected";
        localMaterial.baseColor(selectionColor);
        ro.setMaterial(&localMaterial);

        // Line Width geometry shader
        ro.setupLineRendering(_state.line_width(), screenSize);

        ro.debugName = "polylinecollection.Wireframe";
        localMaterial.baseColor(defaultColor);
        ro.setMaterial(&localMaterial);
        // The first point is mapped to an additional last point in buffer, so we can
        // just Render one point more to get a closed line

789

790 791
        // Line Width geometry shader
        ro.setupLineRendering(_state.line_width(), screenSize);
792

793 794
        // apply user settings
        applyRenderObjectSettings(props->primitive(), &ro);
Christopher Tenter's avatar
Christopher Tenter committed
795 796


797 798
        ro.indexBuffer = ibo_.id();
        ro.glDrawElements(GL_LINES, total_segment_count_ * 2, GL_UNSIGNED_INT, 0);
799

800 801
        _renderer->addRenderObject(&ro);
        ro.indexBuffer = 0;
Christopher Tenter's avatar
Christopher Tenter committed
802 803 804 805 806 807 808 809 810 811

    }
  }

}

//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================