LineNode.cc 16.7 KB
Newer Older
Jan Möbius's avatar
Jan Möbius 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 LineNode - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================
#include <ACG/GL/acg_glew.hh>
#include "LineNode.hh"
#include <ACG/GL/IRenderer.hh>

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

namespace ACG {
namespace SceneGraph {

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

LineNode::LineNode( LineMode     _mode,
                    BaseNode*    _parent,
                     std::string  _name ) :
   MaterialNode(_parent, _name, MaterialNode::BaseColor | MaterialNode::LineWidth),
   picking_line_width_(std::numeric_limits<float>::infinity()),
   line_mode_(_mode),
   draw_always_on_top (false),
   prev_depth_(GL_LESS),
   vbo_(0),
   updateVBO_(true),
   lineNodeName_("")
{
  drawMode(DrawModes::WIREFRAME);
}

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

LineNode::~LineNode() {
  if (vbo_)
90
    glDeleteBuffers(1, &vbo_);
Jan Möbius's avatar
Jan Möbius committed
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

}

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

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;
}

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

void
LineNode::
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
{
  ConstPointIter p_it=points_.begin(), p_end=points_.end();
  for (; p_it!=p_end; ++p_it)
  {
      _bbMax.maximize(*p_it);
      _bbMin.minimize(*p_it);
  }
}


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


DrawModes::DrawMode
LineNode::
availableDrawModes() const
{
  return DrawModes::WIREFRAME;
}


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

void
LineNode::
enter(GLState& _state , const DrawModes::DrawMode& _drawMode)
{
    MaterialNode::enter(_state, _drawMode);
    
    if (alwaysOnTop()) {
	//store current depth comparison function (needed for lasso selection)
	prev_depth_ = _state.depthFunc();
	
	//set depth function and change GLState accordingly
	_state.set_depthFunc(GL_ALWAYS);
    }
}

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

void
LineNode::
draw(GLState&  _state  , const DrawModes::DrawMode& _drawMode)
{
235
236
237
238
239
  if(_state.compatibilityProfile())
    drawCompat(_state, _drawMode);
  else
  {
    /* //Node Based Drawing is not supported on Core profiles
Jan Möbius's avatar
Jan Möbius committed
240
241
242
243
244
245
246
247
248
249
250
251
252
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
284
285
286
287
288
289
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
350
  if (_drawMode & DrawModes::WIREFRAME)
  {
    ACG::GLState::disable(GL_LIGHTING);


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


    if (line_mode_ == LineSegmentsMode)
    {
      // 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;
        }

        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;
          }

          glColor(c);
          glVertex(*p_it);

          ++cnt;
        }

        glEnd();

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

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

      }
      else if ((line_mode_ == LineSegmentsMode) && (points_.size()/2 == colors_.size()) )
      {
        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()) {
          c = *c_it;
        }

        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;
          }

          glColor(c);
          glVertex(*p_it);

          ++cnt;
        }
        glEnd();
      }
      else
      {
        glBegin(GL_LINES);

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

        for (; p_it!=p_end; ++p_it)
        {
          glVertex(*p_it);
        }

        glEnd();
      }
    }
    else
    {
      _state.set_color(_state.base_color());
      glBegin(GL_LINE_STRIP);
      ConstPointIter p_it=points_.begin(), p_end=points_.end();
      for (; p_it!=p_end; ++p_it)
        glVertex(*p_it);
      glEnd();
    }

    //glEnd();
351
  }*/
Jan Möbius's avatar
Jan Möbius committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  }
}
  
//----------------------------------------------------------------------------

void
LineNode::
leave(GLState& _state , const DrawModes::DrawMode& _drawMode)
{
  if (alwaysOnTop()) {
    //restore depth function and change GLState accordingly
    _state.set_depthFunc(prev_depth_);
  }

  MaterialNode::leave(_state, _drawMode);
}

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

void LineNode::pick(GLState&  _state , PickTarget _target)
{
373
374
375
376
  if(_state.compatibilityProfile())
    pickCompat(_state, _target);
  else
  {
Jan Möbius's avatar
Jan Möbius committed
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
  if (n_points() == 0)
    return;

  // Bind the vertex array
  ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);

  const size_t n_edges = n_points() - 1;

  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;
  }
404
  }
Jan Möbius's avatar
Jan Möbius committed
405
406
407
408
409
410
}

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

void LineNode::pick_edges(GLState& _state, unsigned int _offset)
{
411
  //TODO: implement edge picking for lines in CoreProfile
Jan Möbius's avatar
Jan Möbius committed
412
413
414
415
416
417
418
419
420
421
422
}

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

void LineNode::createVBO()
{
  if (!updateVBO_)
    return;

  // create vbo if it does not exist
  if (!vbo_)
423
    glGenBuffers(1, &vbo_);
Jan Möbius's avatar
Jan Möbius committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539

  vertexDecl_.clear();
  vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_POSITION);

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

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

      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;
      }

      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;
      }

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

      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()) {
        c = *c_it;
      }

      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;
        }

        //add position information
        *(vboPtr++) = (*p_it)[0];
        *(vboPtr++) = (*p_it)[1];
        *(vboPtr++) = (*p_it)[2];

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

        ++cnt;
      }

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

      for (; p_it!=p_end; ++p_it)
      {
        *(vboPtr++) = (*p_it)[0];
        *(vboPtr++) = (*p_it)[1];
        *(vboPtr++) = (*p_it)[2];
      }
      //===========
    }


  }
  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];
      }
    }
  }

540
541
  glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo_);
  glBufferData(GL_ARRAY_BUFFER_ARB, vboData.size()*sizeof(float) , &vboData[0] , GL_STATIC_DRAW_ARB);
Jan Möbius's avatar
Jan Möbius committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561

  // Update done.
  updateVBO_ = false;

}

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

  if (points_.empty())
    return;

  // init base render object

  RenderObject ro;
  ro.initFromState(&_state);
  ro.setMaterial(_mat);

  lineNodeName_ = std::string("LineNode: ")+name();
562
  ro.debugName = lineNodeName_;
Jan Möbius's avatar
Jan Möbius committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616

  // draw after scene-meshes
  if (draw_always_on_top)
  {
    ro.priority = 1;
    ro.depthTest = false;
    ro.depthWrite = false;
  }
  else
  {
    ro.depthTest = true;
    ro.depthWrite = true;
  }

  //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;
  }

  // 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());

  createVBO();
  ro.vertexBuffer = vbo_;
  // vertexDecl is defined in createVBO
  ro.vertexDecl = &vertexDecl_;

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


  if (line_mode_ == LineSegmentsMode)
    ro.glDrawArrays(GL_LINES, 0, int( points_.size() ));
  else
    ro.glDrawArrays(GL_LINE_STRIP, 0, int(points_.size()) );

  _renderer->addRenderObject(&ro);

}

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