LineNode.cc 17.6 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4 5 6 7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
Jan Möbius's avatar
Jan Möbius committed
8 9
 *                                                                           *
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
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
 * 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.              *
Jan Möbius's avatar
Jan Möbius committed
39 40 41 42 43
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
44
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
45 46 47 48
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
49 50 51 52 53 54 55 56 57 58 59




//=============================================================================
//
//  CLASS LineNode - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================
60
#include <ACG/GL/acg_glew.hh>
Jan Möbius's avatar
 
Jan Möbius committed
61
#include "LineNode.hh"
62
#include <ACG/GL/IRenderer.hh>
Jan Möbius's avatar
 
Jan Möbius committed
63 64 65 66 67 68 69 70

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

namespace ACG {
namespace SceneGraph {

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

71 72 73 74
LineNode::LineNode( LineMode     _mode,
                    BaseNode*    _parent,
                     std::string  _name ) :
   MaterialNode(_parent, _name, MaterialNode::BaseColor | MaterialNode::LineWidth),
75
   picking_line_width_(std::numeric_limits<float>::infinity()),
76 77 78 79
   line_mode_(_mode),
   draw_always_on_top (false),
   prev_depth_(GL_LESS),
   vbo_(0),
Matthias Möller's avatar
Matthias Möller committed
80 81
   updateVBO_(true),
   lineNodeName_("")
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
  drawMode(DrawModes::WIREFRAME);
}

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

LineNode::~LineNode() {
  if (vbo_)
    glDeleteBuffersARB(1, &vbo_);

}

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

void LineNode::set_line_mode(LineMode _mode)
{
  // Set the new line mode
  line_mode_ = _mode;

  // Force an update of the vbo
  updateVBO_ = true;
}

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

void LineNode::clear()
{
  clear_points();
  clear_colors();
}

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

void LineNode::clear_points()
{
  points_.clear();

  // Force an update of the vbo
  updateVBO_ = true;
}

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

void LineNode::clear_colors()
{
  colors_.clear();
  colors4f_.clear();

  // Force an update of the vbo
  updateVBO_ = true;
}

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

void LineNode::set_color(const Vec4f& _c)
{
  clear_colors();
  add_color(ACG::Vec3uc((char) (((int) _c[0]) * 255),
                        (char) (((int) _c[1]) * 255),
                        (char) (((int) _c[2]) * 255)));
  MaterialNode::set_color(_c);
}

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

void LineNode::add_point(const Vec3d& _v)
{
  points_.push_back(_v);

  // Force an update of the vbo
  updateVBO_ = true;
}

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

void LineNode::add_line(const Vec3d& _v0, const Vec3d& _v1)
{
  add_point(_v0);
  add_point(_v1);

  // Force an update of the vbo
  updateVBO_ = true;
}

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

void LineNode::add_color(const ACG::Vec3uc& _c)
{
  colors_.push_back(_c);

  // Force an update of the vbo
  updateVBO_ = true;
}

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

void LineNode::add_color(const Color4f _c)
{
  colors4f_.push_back(_c);

  // Force an update of the vbo
  updateVBO_ = true;
}

//----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
187 188 189

void
LineNode::
190
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
Jan Möbius's avatar
 
Jan Möbius committed
191 192 193 194
{
  ConstPointIter p_it=points_.begin(), p_end=points_.end();
  for (; p_it!=p_end; ++p_it)
  {
195 196
      _bbMax.maximize(*p_it);
      _bbMin.minimize(*p_it);
Jan Möbius's avatar
 
Jan Möbius committed
197 198 199 200 201 202 203
  }
}


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


Jan Möbius's avatar
Jan Möbius committed
204
DrawModes::DrawMode
Jan Möbius's avatar
 
Jan Möbius committed
205
LineNode::
Jan Möbius's avatar
Jan Möbius committed
206
availableDrawModes() const
Jan Möbius's avatar
 
Jan Möbius committed
207 208 209 210 211 212 213
{
  return DrawModes::WIREFRAME;
}


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

214 215
void
LineNode::
216
enter(GLState& _state , const DrawModes::DrawMode& _drawMode)
217 218 219 220 221
{
    MaterialNode::enter(_state, _drawMode);
    
    if (alwaysOnTop()) {
	//store current depth comparison function (needed for lasso selection)
222
	prev_depth_ = _state.depthFunc();
223 224
	
	//set depth function and change GLState accordingly
225
	_state.set_depthFunc(GL_ALWAYS);
226 227 228 229
    }
}

//----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
230 231 232

void
LineNode::
233
draw(GLState&  _state  , const DrawModes::DrawMode& _drawMode)
Jan Möbius's avatar
 
Jan Möbius committed
234 235 236
{
  if (_drawMode & DrawModes::WIREFRAME)
  {
237
    ACG::GLState::disable(GL_LIGHTING);
Jan Möbius's avatar
 
Jan Möbius committed
238

239 240 241 242 243 244 245

   // if (line_mode_ == LineSegmentsMode)
   //   glBegin(GL_LINES);
   // else
   //   glBegin(GL_LINE_STRIP);


246
    if (line_mode_ == LineSegmentsMode)
247
    {
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      // first check if (new standard) 4-channel colors are specified
      if( (points_.size()/2 == colors4f_.size()) )
      {
        // enable blending of lines
        GLboolean blendb;
        glGetBooleanv( GL_BLEND, &blendb);
        glEnable(GL_BLEND);
        // blend ontop of prev. drawn mesh
        GLboolean depthmaskb;
        glGetBooleanv( GL_DEPTH_WRITEMASK, &depthmaskb);
        glDepthMask(GL_FALSE);

        glBegin(GL_LINES);

        ConstPointIter p_it=points_.begin(), p_end=points_.end();
        ConstColor4fIter c_it=colors4f_.begin();

        Color4f c(1.0f,1.0f,1.0f,1.0f);
        if(c_it != colors4f_.end()) {
          c = *c_it;
        }
269

270 271 272 273 274 275 276
        int cnt = 0;
        for (; p_it!=p_end; ++p_it)
        {
          if ((cnt > 0) && (cnt % 2 == 0) && (c_it+1) != colors4f_.end()) {
            ++c_it;
            c = *c_it;
          }
277

278 279
          glColor(c);
          glVertex(*p_it);
280

281 282
          ++cnt;
        }
Jan Möbius's avatar
 
Jan Möbius committed
283

284 285 286 287 288 289 290 291 292
        glEnd();

        // disable blending of lines
        if( blendb == GL_FALSE )
          glDisable(GL_BLEND);

        // enable depth mask
        if( depthmaskb == GL_TRUE )
          glDepthMask(GL_TRUE);
293 294

      }
295
      else if ((line_mode_ == LineSegmentsMode) && (points_.size()/2 == colors_.size()) )
296
      {
297 298 299 300 301 302
        glBegin(GL_LINES);
        ConstPointIter p_it=points_.begin(), p_end=points_.end();
        ConstColorIter c_it=colors_.begin();

        Color c((char)255, (char)255, (char)255);
        if(c_it != colors_.end()) {
303 304 305
          c = *c_it;
        }

306 307 308 309 310 311 312
        int cnt = 0;
        for (; p_it!=p_end; ++p_it)
        {
          if ((cnt > 0) && (cnt % 2 == 0) && (c_it+1) != colors_.end()) {
            ++c_it;
            c = *c_it;
          }
313

314 315
          glColor(c);
          glVertex(*p_it);
316

317 318 319
          ++cnt;
        }
        glEnd();
320
      }
321
      else
Jan Möbius's avatar
 
Jan Möbius committed
322
      {
323 324 325 326 327 328 329
        glBegin(GL_LINES);

        ConstPointIter p_it=points_.begin(), p_end=points_.end();

        for (; p_it!=p_end; ++p_it)
        {
          glVertex(*p_it);
330
        }
Jan Möbius's avatar
 
Jan Möbius committed
331

332
        glEnd();
Jan Möbius's avatar
 
Jan Möbius committed
333 334 335 336
      }
    }
    else
    {
337
      _state.set_color(_state.base_color());
338
      glBegin(GL_LINE_STRIP);
Jan Möbius's avatar
 
Jan Möbius committed
339 340 341
      ConstPointIter p_it=points_.begin(), p_end=points_.end();
      for (; p_it!=p_end; ++p_it)
        glVertex(*p_it);
342
      glEnd();
Jan Möbius's avatar
 
Jan Möbius committed
343 344
    }

345
    //glEnd();
Jan Möbius's avatar
 
Jan Möbius committed
346 347
  }
}
348 349
  
//----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
350

351 352
void
LineNode::
353
leave(GLState& _state , const DrawModes::DrawMode& _drawMode)
354
{
355 356 357 358 359 360
  if (alwaysOnTop()) {
    //restore depth function and change GLState accordingly
    _state.set_depthFunc(prev_depth_);
  }

  MaterialNode::leave(_state, _drawMode);
361
}
Jan Möbius's avatar
 
Jan Möbius committed
362

363 364
//----------------------------------------------------------------------------

365 366 367 368 369 370 371 372 373 374
void LineNode::pick(GLState&  _state , PickTarget _target)
{
  if (n_points() == 0)
    return;

  // Bind the vertex array
  ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
  ACG::GLState::vertexPointer( &(points_)[0] );
  ACG::GLState::enableClientState(GL_VERTEX_ARRAY);

Jan Möbius's avatar
Jan Möbius committed
375
  const size_t n_edges = n_points() - 1;
376 377 378 379 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

  switch (_target)
  {
    case PICK_EDGE:
    {
      _state.pick_set_maximum (n_edges);
      pick_edges(_state, 0);
      break;
    }

    case PICK_ANYTHING:
    {
      _state.pick_set_maximum (n_edges);
      pick_edges(_state, 0);
      break;
    }

    default:
      break;
  }

  //Disable the vertex array
  ACG::GLState::disableClientState(GL_VERTEX_ARRAY);
}

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

void LineNode::pick_edges(GLState& _state, unsigned int _offset)
{
  // Check if we have any edges to draw (% 0 causes division by zero on windows)
  if (n_points() < 2)
    return;

  const float line_width_old = _state.line_width();
  _state.set_line_width(picking_line_width());
  _state.pick_set_name (0);

  glDepthRange(0.0, 0.99);

  if (line_mode_ == PolygonMode)
  {
Jan Möbius's avatar
Jan Möbius committed
417 418
    const size_t n_edges = n_points() - 1;
    for (size_t i = 0; i < n_edges; ++i)
419 420 421
    {
      _state.pick_set_name(i + _offset);
      glBegin(GL_LINES);
Jan Möbius's avatar
Jan Möbius committed
422 423
      glArrayElement(static_cast<GLint>(i));
      glArrayElement(static_cast<GLint>(i + 1));
424 425 426 427 428
      glEnd();
    }
  }
  else if (line_mode_ == LineSegmentsMode)
  {
Jan Möbius's avatar
Jan Möbius committed
429 430
    const size_t n_edges = n_points() / 2;
    for (size_t i = 0; i < n_edges; ++i)
431 432 433
    {
      _state.pick_set_name(i + _offset);
      glBegin(GL_LINES);
Jan Möbius's avatar
Jan Möbius committed
434 435
      glArrayElement(static_cast<GLint>(2*i));
      glArrayElement(static_cast<GLint>(2*i + 1));
436 437 438 439 440 441 442 443 444 445 446
      glEnd();
    }
  }

  glDepthRange(0.0, 1.0);

  _state.set_line_width(line_width_old);
}

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

447 448 449 450
void LineNode::createVBO()
{
  if (!updateVBO_)
    return;
451

452 453 454
  // create vbo if it does not exist
  if (!vbo_)
    glGenBuffersARB(1, &vbo_);
455

456 457 458
  vertexDecl_.clear();
  vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_POSITION);

459 460
  //3 coordinates per vertex
  std::vector<float> vboData(3*points_.size(),0.f);
461

462 463
  if (line_mode_ == LineSegmentsMode)
  {
464 465 466
    if( (points_.size()/2 == colors4f_.size()) )
    {
      //   === One color entry per line segment (alpha channel available ) ===
467
      vertexDecl_.addElement(GL_FLOAT, 4, VERTEX_USAGE_COLOR);
468 469
      vboData.resize(vboData.size() + 4 * points_.size());
      float* vboPtr = &vboData[0];
470

471 472
      ConstPointIter p_it=points_.begin(), p_end=points_.end();
      ConstColor4fIter c_it=colors4f_.begin();
Jan Möbius's avatar
Jan Möbius committed
473

474 475 476 477
      Color4f c(1.0f,1.0f,1.0f,1.0f);
      if(c_it != colors4f_.end()) {
        c = *c_it;
      }
Jan Möbius's avatar
Jan Möbius committed
478

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
      int cnt = 0;
      for (; p_it!=p_end; ++p_it)
      {
        if ((cnt > 0) && (cnt % 2 == 0) && (c_it+1) != colors4f_.end()) {
          ++c_it;
          c = *c_it;
        }
        //add position information
        *(vboPtr++) = (*p_it)[0];
        *(vboPtr++) = (*p_it)[1];
        *(vboPtr++) = (*p_it)[2];

        //add color information
        *(vboPtr++) = c[0];
        *(vboPtr++) = c[1];
        *(vboPtr++) = c[2];
        *(vboPtr++) = c[3];

        ++cnt;
      }
499

500 501 502 503
      //====================
    } else if ( points_.size()/2 == colors_.size() )
    {
      //=== One color entry per line segment (no alpha channel available and uchars as colors) ===
504
      vertexDecl_.addElement(GL_FLOAT, 4, VERTEX_USAGE_COLOR);
505 506 507
      //add 4 colors for each vertex
      vboData.resize(vboData.size() + 4 * points_.size());
      float* vboPtr = &vboData[0];
508

509 510
      ConstPointIter p_it=points_.begin(), p_end=points_.end();
      ConstColorIter c_it=colors_.begin();
511

512 513 514 515
      Color c((char)255, (char)255, (char)255);
      if(c_it != colors_.end()) {
        c = *c_it;
      }
516

517 518 519 520 521 522 523
      int cnt = 0;
      for (; p_it!=p_end; ++p_it)
      {
        if ((cnt > 0) && (cnt % 2 == 0) && (c_it+1) != colors_.end()) {
          ++c_it;
          c = *c_it;
        }
524

525 526 527 528
        //add position information
        *(vboPtr++) = (*p_it)[0];
        *(vboPtr++) = (*p_it)[1];
        *(vboPtr++) = (*p_it)[2];
529

530 531 532 533 534
        //add color information
        *(vboPtr++) = c[0]/255.f;
        *(vboPtr++) = c[1]/255.f;
        *(vboPtr++) = c[2]/255.f;
        *(vboPtr++) = 1.f;
535

536 537
        ++cnt;
      }
538

539 540 541 542 543 544
      //===========
    } else
    {
      //=== No colors. Just draw the segments ===
      ConstPointIter p_it=points_.begin(), p_end=points_.end();
      float* vboPtr = &vboData[0];
545

546 547 548 549 550
      for (; p_it!=p_end; ++p_it)
      {
        *(vboPtr++) = (*p_it)[0];
        *(vboPtr++) = (*p_it)[1];
        *(vboPtr++) = (*p_it)[2];
551
      }
552 553
      //===========
    }
554

555

556 557 558 559 560 561 562 563 564 565 566 567
  }
  else
  {
    // === No colors (Use material) and one continuous line ===
    // Pointer to it for easier copy operation
    float* pPoints = &vboData[0];

    // Copy from internal storage to vbo in memory
    for (unsigned int  i = 0 ; i < points_.size(); ++i) {
      for ( unsigned int j = 0 ; j < 3 ; ++j) {
        *(pPoints++) = points_[i][j];
      }
568
    }
569
  }
570

571 572
  glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_);
  glBufferDataARB(GL_ARRAY_BUFFER_ARB, vboData.size()*sizeof(float) , &vboData[0] , GL_STATIC_DRAW_ARB);
573

574 575
  // Update done.
  updateVBO_ = false;
576

577
}
578

579 580 581
void
LineNode::
getRenderObjects(IRenderer* _renderer, GLState&  _state , const DrawModes::DrawMode&  _drawMode , const ACG::SceneGraph::Material* _mat) {
582

Matthias Möller's avatar
Matthias Möller committed
583
  if (points_.empty())
584
    return;
585

586
  // init base render object
587

588 589 590 591
  RenderObject ro;
  ro.initFromState(&_state);
  ro.setMaterial(_mat);

Matthias Möller's avatar
Matthias Möller committed
592 593
  lineNodeName_ = std::string("LineNode: ")+name();
  ro.debugName = lineNodeName_.c_str();
594 595 596

  // draw after scene-meshes
  if (draw_always_on_top)
Matthias Möller's avatar
Matthias Möller committed
597
  {
598
    ro.priority = 1;
Matthias Möller's avatar
Matthias Möller committed
599 600 601 602 603 604 605 606
    ro.depthTest = false;
    ro.depthWrite = false;
  }
  else
  {
    ro.depthTest = true;
    ro.depthWrite = true;
  }
607 608 609 610 611 612 613

  //set blending
  if ((line_mode_ == LineSegmentsMode) && (points_.size()/2 == colors4f_.size()))
  {
    ro.blending = true;
    ro.blendSrc = GL_SRC_ALPHA;
    ro.blendDest = GL_ONE_MINUS_SRC_ALPHA;
614 615
  }

616 617 618 619 620 621 622 623 624
  // simulate line width via quad extrusion in geometry shader
  QString geomTemplate = ShaderProgGenerator::getShaderDir();
  geomTemplate += "Wireframe/geom_line2quad.tpl";

  ro.shaderDesc.geometryTemplateFile = geomTemplate;

  ro.setUniform("screenSize", Vec2f((float)_state.viewport_width(), (float)_state.viewport_height()));
  ro.setUniform("lineWidth", _state.line_width());

625 626
  createVBO();
  ro.vertexBuffer = vbo_;
627 628
  // vertexDecl is defined in createVBO
  ro.vertexDecl = &vertexDecl_;
629

630 631
  //besides of the position, colors are saved so we can show them
  if (vertexDecl_.getNumElements() > 1)
632 633 634 635
    ro.shaderDesc.vertexColors = true;


  if (line_mode_ == LineSegmentsMode)
Jan Möbius's avatar
size_t  
Jan Möbius committed
636
    ro.glDrawArrays(GL_LINES, 0, int( points_.size() ));
637
  else
Jan Möbius's avatar
size_t  
Jan Möbius committed
638
    ro.glDrawArrays(GL_LINE_STRIP, 0, int(points_.size()) );
639 640 641

  _renderer->addRenderObject(&ro);

642
}
Jan Möbius's avatar
 
Jan Möbius committed
643 644 645 646 647

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