MeshNode2T.cc 39.9 KB
Newer Older
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
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
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
38
39
 *   $Revision$                                                       *
 *   $Author$                                                      *
 *   $Date$                   *
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 *                                                                           *
\*===========================================================================*/




//=============================================================================
//
//  CLASS MeshNodeT - IMPLEMENTATION
//
//=============================================================================

#define ACG_MESHNODE_C

54
#include <ACG/Geometry/GPUCacheOptimizer.hh>
55
#include <ACG/GL/DrawMesh.hh>
56

57
58
59
60
61
62
63
64
65
66
67
68
69
//== NAMESPACES ===============================================================


namespace ACG {
namespace SceneGraph {
    

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


#include "MeshNode2T.hh"

template<class Mesh>
Jan Möbius's avatar
Jan Möbius committed
70
71
MeshNodeT<Mesh>::
MeshNodeT(Mesh&        _mesh,
72
              BaseNode*    _parent,
Jan Möbius's avatar
Jan Möbius committed
73
74
              std::string  _name ): 
  BaseNode(_parent, _name),
75
  mesh_(_mesh),
76
  drawMesh_(0),
77
78
  enableNormals_(true),
  enableColors_(true),
Jan Möbius's avatar
Jan Möbius committed
79
  enabled_arrays_(0),
80
  updateVertexPicking_(true),
Jan Möbius's avatar
Jan Möbius committed
81
  vertexPickingBaseIndex_(0),
82
  updateEdgePicking_(true),
Jan Möbius's avatar
Jan Möbius committed
83
  edgePickingBaseIndex_(0),
84
  updateFacePicking_(true),
Jan Möbius's avatar
Jan Möbius committed
85
  facePickingBaseIndex_(0),
86
  updateAnyPicking_(true),
87
  anyPickingBaseIndex_(0),
88
  perFaceTextureIndexAvailable_(false),
89
  perFaceTextureCoordsAvailable_(false),
90
  textureMap_(0)
91
{
92
 
93
  /// \todo : Handle vbo not supported
94
  if ( ! checkExtensionSupported("GL_ARB_vertex_buffer_object") ) {
95
96
    std::cerr << "Error! Vertex buffer objects are not supported! The meshNode will not work without them!" << std::endl;
  }
Jan Möbius's avatar
Jan Möbius committed
97
  
98
  drawMesh_ = new DrawMeshT<Mesh>(mesh_);
99
  
100
101
102
}  

template<class Mesh>
Jan Möbius's avatar
Jan Möbius committed
103
MeshNodeT<Mesh>::
104
105
106
~MeshNodeT()
{
  // Delete all allocated buffers
107
  delete drawMesh_;
108
109
110
}

template<class Mesh>
Jan Möbius's avatar
Jan Möbius committed
111
DrawModes::DrawMode
Jan Möbius's avatar
Jan Möbius committed
112
MeshNodeT<Mesh>::
Jan Möbius's avatar
Jan Möbius committed
113
availableDrawModes() const {
Jan Möbius's avatar
Jan Möbius committed
114
  DrawModes::DrawMode drawModes(DrawModes::NONE);
115
  
116
  // We can always render points and a wireframe.
117
  drawModes |= DrawModes::POINTS;
Jan Möbius's avatar
Jan Möbius committed
118
  drawModes |= DrawModes::HIDDENLINE;
119
  drawModes |= DrawModes::WIREFRAME;
120
  drawModes |= DrawModes::HALFEDGES;
121
  
122
123
  if (mesh_.has_vertex_normals())
  {
124
125
    drawModes |= DrawModes::POINTS_SHADED;  
    drawModes |= DrawModes::SOLID_SMOOTH_SHADED;
Jan Möbius's avatar
Jan Möbius committed
126
    drawModes |= DrawModes::SOLID_PHONG_SHADED;
127
128
  }
  
129
130
  if (mesh_.has_face_normals())
    drawModes |= DrawModes::SOLID_FLAT_SHADED;
131
132
133

  if (mesh_.has_halfedge_normals())
    drawModes |= DrawModes::SOLID_SMOOTH_SHADED_FEATURES;
134
  
135
136
137
  if (mesh_.has_vertex_colors())
  {
    drawModes |= DrawModes::POINTS_COLORED;
138
    drawModes |= DrawModes::SOLID_POINTS_COLORED;
139
140
141

    if (mesh_.has_vertex_normals())
      drawModes |= DrawModes::SOLID_POINTS_COLORED_SHADED;
142
  }
143
144
145
146
147

  if(mesh_.has_edge_colors())
  {
    drawModes |= DrawModes::EDGES_COLORED;
  }
148
149
150
151
152

  if(mesh_.has_halfedge_colors())
  {
    drawModes |= DrawModes::HALFEDGES_COLORED;
  }
153
  
154
155
156
157
158
  if (mesh_.has_face_colors()) {
    drawModes |= DrawModes::SOLID_FACES_COLORED;
    
    if( mesh_.has_face_normals() )
      drawModes |= DrawModes::SOLID_FACES_COLORED_FLAT_SHADED;
159
160
161

    if( mesh().has_vertex_normals() )
      drawModes |= DrawModes::SOLID_FACES_COLORED_SMOOTH_SHADED;
162
163
  }
  
164
165
166
  if ( mesh_.has_vertex_texcoords2D() ) {
    drawModes |= DrawModes::SOLID_TEXTURED;
    
167
168
    if (mesh_.has_vertex_normals())
      drawModes |= DrawModes::SOLID_TEXTURED_SHADED; 
169
170
  }
  
171
  if ( perFaceTextureCoordsAvailable_ ) {
172
173
174
175
176
177
    drawModes |= DrawModes::SOLID_2DTEXTURED_FACE;
    
    if (mesh_.has_face_normals())
      drawModes |= DrawModes::SOLID_2DTEXTURED_FACE_SHADED;
  }
  
178
179
180
181
182
  return drawModes;
}

template<class Mesh>
void
Jan Möbius's avatar
Jan Möbius committed
183
MeshNodeT<Mesh>::
184
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax) {
Jan Möbius's avatar
Jan Möbius committed
185
186
  _bbMin.minimize(bbMin_);
  _bbMax.maximize(bbMax_);
187
188
189
190
}

template<class Mesh>
void
Jan Möbius's avatar
Jan Möbius committed
191
MeshNodeT<Mesh>::
192
draw(GLState& _state, const DrawModes::DrawMode& _drawMode) {
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*  
  if ( ( _drawMode & DrawModes::SOLID_FLAT_SHADED ) ||
    ( _drawMode & DrawModes::SOLID_FACES_COLORED_FLAT_SHADED) ||
    ( _drawMode & DrawModes::SOLID_TEXTURED) ||
    ( _drawMode & DrawModes::SOLID_2DTEXTURED_FACE))
  {
    drawMesh_->setFlatShading();
  }
  else
    drawMesh_->setSmoothShading();


  if ( (_drawMode & DrawModes::SOLID_FACES_COLORED ||
    _drawMode & DrawModes::SOLID_FACES_COLORED_FLAT_SHADED))
  {
    drawMesh_->usePerFaceColors();
  }
  else
    drawMesh_->usePerVertexColors();
*/
213
    
214
  GLenum prev_depth = _state.depthFunc();
215
  
216
  unsigned int arrays = NONE;
217
  
218
  glPushAttrib(GL_ENABLE_BIT);
219

220
221
222
223
224
225
226
227
  /// get bound texture buffer and target
  GLuint lastBuffer = ACG::GLState::getBoundTextureBuffer();
  GLenum lastTarget = ACG::GLState::getBoundTextureTarget();

  // Unbind to avoid painting textures on non textured primitives
  ACG::GLState::bindTexture(lastTarget,0);


228
  if ( (_drawMode & DrawModes::POINTS) || (_drawMode & DrawModes::POINTS_COLORED) || (_drawMode & DrawModes::POINTS_SHADED )  ) {
229
    
230
    ACG::GLState::shadeModel(GL_FLAT);
231
232
    
    if ( _drawMode & DrawModes::POINTS_SHADED  ) {
233
      ACG::GLState::enable(GL_LIGHTING);
234
    } else
235
      ACG::GLState::disable(GL_LIGHTING);
236
  
237
238
    // Use Colors in this mode if allowed
    if ( enableColors_ && (_drawMode & DrawModes::POINTS_COLORED) )
239
240
241
    {
      drawMesh_->usePerVertexColors();

242
      // If we have colors and lighting with normals, we have to use colormaterial
243
      if ( enableNormals_ && (_drawMode & DrawModes::POINTS_SHADED ) )
244
        ACG::GLState::enable(GL_COLOR_MATERIAL);
245
      else
246
        ACG::GLState::disable(GL_COLOR_MATERIAL);
247
    }
248
249
    else
      drawMesh_->disableColors();
250
    
251
    // Bring the arrays online
252
//    enable_arrays(arrays);
253
254
    
    // Draw vertices
255
256
257
    draw_vertices();
  }
  
258
259
  
  /// \todo We can render also wireframe shaded and with vertex colors
260
261
  if (_drawMode & DrawModes::WIREFRAME)
  {
262
//    enable_arrays( VERTEX_ARRAY | LINE_INDEX_ARRAY );
263
264
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
265
266
267

    drawMesh_->disableColors();

268
    draw_lines();
Jan Möbius's avatar
Jan Möbius committed
269
  }  
270
  
Jan Möbius's avatar
Jan Möbius committed
271
272
  if (_drawMode & DrawModes::HIDDENLINE)
  {
273
//    enable_arrays(VERTEX_ARRAY);
Jan Möbius's avatar
Jan Möbius committed
274
275
276
277
278
279
280
    
    // First:
    // Render all faces in background color to initialize z-buffer
    Vec4f  clear_color = _state.clear_color();
    Vec4f  base_color  = _state.base_color();
    clear_color[3] = 1.0;
    
281
282
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
Jan Möbius committed
283
284
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    _state.set_base_color(clear_color);
285
286
287

//    drawMesh_->SetFlatShading();
    drawMesh_->disableColors();
Jan Möbius's avatar
Jan Möbius committed
288
    
Jan Möbius's avatar
Jan Möbius committed
289
    ACG::GLState::depthRange(0.01, 1.0);
290
    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
291
    ACG::GLState::depthRange(0.0, 1.0);
Jan Möbius's avatar
Jan Möbius committed
292
293
294
    
    // Second
    // Render the lines. All lines not on the front will be skipped in z-test
295
//    enable_arrays(VERTEX_ARRAY|LINE_INDEX_ARRAY);
296
    ACG::GLState::depthFunc(GL_LEQUAL);
Jan Möbius's avatar
Jan Möbius committed
297
298
    _state.set_base_color(base_color);
    draw_lines();
299
300
    
    //restore depth buffer comparison function for the next draw calls inside this function
301
    ACG::GLState::depthFunc(prev_depth);
Jan Möbius's avatar
Jan Möbius committed
302
303
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  }
304
305
306
307
308


  if (_drawMode & DrawModes::EDGES_COLORED)
  {
    enable_arrays( PER_EDGE_VERTEX_ARRAY | PER_EDGE_COLOR_ARRAY );
309
310
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
311
312
313
    draw_lines();
  }  

314
315
316
  if (_drawMode & DrawModes::HALFEDGES)
  {
    enable_arrays( PER_HALFEDGE_VERTEX_ARRAY);
317
318
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
319
320
321
322
323
324
    draw_halfedges();
  }  

  if (_drawMode & DrawModes::HALFEDGES_COLORED)
  {
    enable_arrays( PER_HALFEDGE_VERTEX_ARRAY | PER_HALFEDGE_COLOR_ARRAY );
325
326
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
327
328
    draw_halfedges();
  }  
329

330
  if ( ( _drawMode & DrawModes::SOLID_POINTS_COLORED ) && mesh_.has_vertex_colors() )
331
332
333
334
335
336
337
338
339
340
341
342
  {
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
    ACG::GLState::depthRange(0.01, 1.0);

    drawMesh_->usePerVertexColors();

    draw_faces();
    ACG::GLState::depthRange(0.0, 1.0);
  }

  if ( ( _drawMode & DrawModes::SOLID_POINTS_COLORED_SHADED ) && mesh_.has_vertex_colors() && mesh_.has_vertex_normals() )
343
  {
344
345
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
Jan Möbius committed
346
    ACG::GLState::depthRange(0.01, 1.0);
347
    if ( enableNormals_ ) {
348
       ACG::GLState::enable(GL_COLOR_MATERIAL);
349
    } else {
350
      ACG::GLState::disable(GL_COLOR_MATERIAL);
351
    }
352

353
354
    drawMesh_->usePerVertexColors();

355
    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
356
    ACG::GLState::depthRange(0.0, 1.0);
357
  }
358

359
  if ( ( _drawMode & DrawModes::SOLID_FLAT_SHADED ) && mesh_.has_face_normals() && mesh_.n_faces() > 0)
360
  {
361
362
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
Jan Möbius committed
363
    ACG::GLState::depthRange(0.01, 1.0);
364
365
366
367
368

    drawMesh_->setFlatShading();
    drawMesh_->disableColors();

    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
369
    ACG::GLState::depthRange(0.0, 1.0);
370
371
372
  }
  
  
Jan Möbius's avatar
Jan Möbius committed
373
374
  if ( ( _drawMode & DrawModes::SOLID_SMOOTH_SHADED ) && mesh_.has_vertex_normals() )
  {
375
376
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
Jan Möbius committed
377
    ACG::GLState::depthRange(0.01, 1.0);
378

379
    drawMesh_->usePerVertexNormals();
380
381
382
    drawMesh_->setSmoothShading();
    drawMesh_->disableColors();
    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
383
    ACG::GLState::depthRange(0.0, 1.0);
Jan Möbius's avatar
Jan Möbius committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
  }
  
  if ( ( _drawMode & DrawModes::SOLID_PHONG_SHADED ) && mesh_.has_vertex_normals() )
  {
    ///\todo Integrate shader here! 
    //     if ( parent() != 0 ) {
    //       if ( parent()->className() == "ShaderNode" ) {
    //
    //         ShaderNode* node = dynamic_cast< ShaderNode* > ( parent() );
    //
    //         GLSL::PtrProgram program = node->getShader( DrawModes::SOLID_PHONG_SHADED );
    //
    //         // Enable own Phong shader
    //         program->use();
398
//    enable_arrays(VERTEX_ARRAY | NORMAL_VERTEX_ARRAY );
Jan Möbius's avatar
Jan Möbius committed
399
    ACG::GLState::disable(GL_LIGHTING);
400
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
Jan Möbius committed
401
    ACG::GLState::depthRange(0.01, 1.0);
402

Jan Möbius's avatar
Jan Möbius committed
403
    drawMesh_->usePerVertexNormals();
404
405
406
407
    drawMesh_->setSmoothShading();
    drawMesh_->disableColors();

    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
408
    ACG::GLState::depthRange(0.0, 1.0);
Jan Möbius's avatar
Jan Möbius committed
409
410
411
412
413
414
415
    
    //disable own Phong shader
    //         program->disable();
    //       }
    //     }
  }
  
416
  
417
  if ( ( _drawMode & DrawModes::SOLID_FACES_COLORED ) && mesh_.has_face_colors()  && mesh_.n_faces() > 0)
418
419
420
  {
    Vec4f base_color_backup = _state.base_color();
    
421
422
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
Jan Möbius committed
423
    ACG::GLState::depthRange(0.01, 1.0);
424
425
426
427
428
//    enable_arrays(PER_FACE_VERTEX_ARRAY | PER_FACE_COLOR_ARRAY);    

    drawMesh_->usePerFaceColors();

    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
429
    ACG::GLState::depthRange(0.0, 1.0);
430
431
432
433
    
    _state.set_base_color(base_color_backup);
  }
  
434
  if ( ( _drawMode & DrawModes::SOLID_FACES_COLORED_SMOOTH_SHADED ) && mesh_.has_face_colors() && mesh_.has_vertex_normals() && mesh_.n_faces() > 0)
435
436
437
  {
    Vec4f base_color_backup = _state.base_color();

438
    ACG::GLState::enable(GL_LIGHTING);
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
    ACG::GLState::shadeModel(GL_SMOOTH);
    ACG::GLState::depthRange(0.01, 1.0);

    drawMesh_->setSmoothShading();
    drawMesh_->usePerVertexNormals();
    drawMesh_->usePerFaceColors();

    draw_faces();
    ACG::GLState::depthRange(0.0, 1.0);

    _state.set_base_color(base_color_backup);
  }

  if ( ( _drawMode & DrawModes::SOLID_SMOOTH_SHADED_FEATURES ) && mesh_.has_halfedge_normals()  && mesh_.n_faces() > 0)
  {
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
    ACG::GLState::depthRange(0.01, 1.0);

    drawMesh_->disableColors();
    drawMesh_->setSmoothShading();
    drawMesh_->usePerHalfedgeNormals();

    draw_faces();

    ACG::GLState::depthRange(0.0, 1.0);
  }
466
  
467
  if ( ( _drawMode & DrawModes::SOLID_FACES_COLORED_FLAT_SHADED ) && mesh_.has_face_colors() && mesh_.has_face_normals()  && mesh_.n_faces() > 0 )
468
469
  {
    Vec4f base_color_backup = _state.base_color();
470
    ACG::GLState::enable(GL_LIGHTING);
471
    
472
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
Jan Möbius committed
473
    ACG::GLState::depthRange(0.01, 1.0);
474
475
476
477
478
479
//    enable_arrays(PER_FACE_VERTEX_ARRAY | PER_FACE_COLOR_ARRAY | PER_FACE_NORMAL_ARRAY );

    drawMesh_->setFlatShading();
    drawMesh_->usePerFaceColors();

    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
480
    ACG::GLState::depthRange(0.0, 1.0);
481
482
483
484
    
    _state.set_base_color(base_color_backup);
  }
  
485
486
  // Rebind the previous texture
  ACG::GLState::bindTexture(lastTarget,lastBuffer);
487
  
488
  if ( ( _drawMode & DrawModes::SOLID_TEXTURED )  && mesh_.has_vertex_texcoords2D())
489
490
  {
    ///\todo enableTexCoords_
491
//    enable_arrays(VERTEX_ARRAY | TEXCOORD_VERTEX_ARRAY );
492
493
494
    ACG::GLState::enable(GL_TEXTURE_2D);
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
Jan Möbius committed
495
    ACG::GLState::depthRange(0.01, 1.0);
496
497

    drawMesh_->disableColors();
498
    drawMesh_->usePerVertexTexcoords();
499
500

    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
501
    ACG::GLState::depthRange(0.0, 1.0);
502
    ACG::GLState::disable(GL_TEXTURE_2D);
503
504
  }
  
505
506
  if ( ( _drawMode & DrawModes::SOLID_TEXTURED_SHADED ) && mesh_.has_vertex_texcoords2D() && mesh_.has_vertex_normals())
  {
507
//    enable_arrays(VERTEX_ARRAY | NORMAL_VERTEX_ARRAY | TEXCOORD_VERTEX_ARRAY);
508
509
510
    ACG::GLState::enable(GL_TEXTURE_2D);
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
Jan Möbius committed
511
    ACG::GLState::depthRange(0.01, 1.0);
512
513
514

    drawMesh_->setSmoothShading();
    drawMesh_->disableColors();
515
    drawMesh_->usePerVertexTexcoords();
516
517

    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
518
    ACG::GLState::depthRange(0.0, 1.0);
519
    ACG::GLState::disable(GL_TEXTURE_2D);
520
521
  }
  
522
    
523
  // Textured by using coordinates stored in halfedges ... arrays generated by stripprocessor
524
  if ( (_drawMode & DrawModes::SOLID_2DTEXTURED_FACE)  && mesh_.n_faces() > 0 )
525
  {
526
    ACG::GLState::enable(GL_TEXTURE_2D);
527
    
528
//    enable_arrays( PER_FACE_VERTEX_ARRAY | PER_FACE_TEXCOORD_ARRAY );
529
    
530
531
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
Jan Möbius committed
532
    ACG::GLState::depthRange(0.01, 1.0);
533
534

    drawMesh_->disableColors();
535
    drawMesh_->usePerHalfedgeTexcoords();
536
537

    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
538
    ACG::GLState::depthRange(0.0, 1.0);
539
    
540
    ACG::GLState::disable(GL_TEXTURE_2D);
541
542
543
  }
  
  // Textured by using coordinates stored in halfedges
544
  if ( ( _drawMode & DrawModes::SOLID_2DTEXTURED_FACE_SHADED ) && mesh_.has_face_normals()  && mesh_.n_faces() > 0)
545
  {
546
    ACG::GLState::enable(GL_TEXTURE_2D);
547
    
548
//    enable_arrays( PER_FACE_VERTEX_ARRAY | PER_FACE_TEXCOORD_ARRAY | PER_FACE_PER_VERTEX_NORMAL_ARRAY );
549

550
551
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
Jan Möbius committed
552
    ACG::GLState::depthRange(0.01, 1.0);
553

554
    drawMesh_->usePerVertexNormals();
555
556
    drawMesh_->setSmoothShading();
    drawMesh_->disableColors();
557
    drawMesh_->usePerHalfedgeTexcoords();
558
559

    draw_faces();
Jan Möbius's avatar
Jan Möbius committed
560
    ACG::GLState::depthRange(0.0, 1.0);
561
    ACG::GLState::disable(GL_TEXTURE_2D);
562
563
564
    
  }
  
Jan Möbius's avatar
Jan Möbius committed
565
566
  enable_arrays(0);
  
567
  // Unbind all remaining buffers
568
  ACG::GLState::bindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB , 0 );
569
  
570
  glPopAttrib();
571
572
}

573
574
575
576

template <class Mesh>
void ACG::SceneGraph::MeshNodeT<Mesh>::getRenderObjects( IRenderer* _renderer, GLState& _state, const DrawModes::DrawMode& _drawMode )
{
577
578
  //RenderObject ro = {0}; // zeromem
  // TODO : RIGHT initialization
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
  ro.initFromState(&_state);
 
  // shader gen setup (lighting, shademode, vertex-colors..)
  
  for (unsigned int i = 0; i < _drawMode.getNumLayers(); ++i)
  {
    const DrawModes::DrawModeProperties* props = _drawMode.getLayer(i);

    // reset renderobject
    ro.priority = 0;
    ro.depthRange = Vec2f(0.0f, 1.0f);
    ro.depthTest = true; // some previous node disabled depth testing
    ro.depthWrite = true;
    ro.depthFunc = GL_LESS;

    // ------------------------
    // 1. setup drawMesh based on property source


    if (props->flatShaded())
      drawMesh_->setFlatShading();
    else
      drawMesh_->setSmoothShading();


    ro.shaderDesc.vertexColors = true;

    switch (props->colorSource())
    {
    case DrawModes::COLOR_PER_VERTEX: drawMesh_->usePerVertexColors(); break;
    case DrawModes::COLOR_PER_FACE: drawMesh_->usePerFaceColors(); break;
    default:
      {
        drawMesh_->disableColors(); 
        ro.shaderDesc.vertexColors = false;
      } break;
    }

    switch (props->normalSource())
    {
    case DrawModes::NORMAL_PER_VERTEX: drawMesh_->usePerVertexNormals(); break;
    case DrawModes::NORMAL_PER_HALFEDGE: drawMesh_->usePerHalfedgeNormals(); break;
    default: break;
    }

    ro.shaderDesc.textured = true;

    switch (props->texcoordSource())
    {
    case DrawModes::TEXCOORD_PER_VERTEX: drawMesh_->usePerVertexTexcoords(); break;
    case DrawModes::TEXCOORD_PER_HALFEDGE: drawMesh_->usePerHalfedgeTexcoords(); break;
    default:
      {
        ro.shaderDesc.textured = false;
      }break;
    }

    // ------------------------
    // 2. prepare renderobject


    // enable / disable lighting
    ro.shaderDesc.numLights = props->lighting() ? 0 : -1;

    // TODO: better handling of attribute sources in shader gen
    if (props->flatShaded())
      ro.shaderDesc.shadeMode = SG_SHADE_FLAT;

    switch (props->lightStage())
    {
    case DrawModes::LIGHTSTAGE_SMOOTH: ro.shaderDesc.shadeMode = SG_SHADE_GOURAUD; break;;
    case DrawModes::LIGHTSTAGE_PHONG: ro.shaderDesc.shadeMode = SG_SHADE_PHONG; break;;
    case DrawModes::LIGHTSTAGE_UNLIT: ro.shaderDesc.shadeMode = SG_SHADE_UNLIT; break;;
    }


    // handle 'special' primitives (wireframe, hiddenline, primitives in sysmem buffers)..

    if (props->primitive() == DrawModes::PRIMITIVE_WIREFRAME)
    {
      ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;
      drawMesh_->disableColors();

      add_line_RenderObjects(_renderer, &ro);
    }

    if (props->primitive()  == DrawModes::PRIMITIVE_HIDDENLINE)
    {
      // First:
      // Render all faces in background color to initialize z-buffer

      ro.priority = -1; // priority allows sorting for layers

      ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;

      // color mask = none
      // depth mask = enabled
      ro.glColorMask(0,0,0,0);
      ro.depthTest = true;
      ro.depthWrite = true;
      ro.depthFunc = GL_LESS;

      ro.fillMode = GL_FILL;

      drawMesh_->disableColors();

      ro.depthRange = Vec2f(0.01f, 1.0f);

      add_face_RenderObjects(_renderer, &ro);


      // Second
      // Render the lines. All lines not on the front will be skipped in z-test
      ro.priority = 0; // render after z cullers

      ro.glColorMask(1,1,1,1);
      ro.depthTest = true;
      ro.depthWrite = true;
      ro.depthFunc = GL_LEQUAL;

      ro.depthRange = Vec2f(0.0f, 1.0f);

      add_line_RenderObjects(_renderer, &ro);
    }

    if (props->colored() && props->primitive()  == DrawModes::PRIMITIVE_EDGE)
    {
      ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;
      ro.shaderDesc.vertexColors = true;

      // note: colored edges are in sysmem, so they are directly bound to the VertexDeclaration
      ro.vertexDecl = drawMesh_->getEdgeColoredVertexDeclaration();
      ro.glDrawArrays(GL_LINES, 0, mesh_.n_edges() * 2);

      _renderer->addRenderObject(&ro);

       // skip other edge primitives for this drawmode layer
      continue;
    }  

    if (props->primitive()  == DrawModes::PRIMITIVE_HALFEDGE)
    {
      ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;

      // buffers in sysmem
      if (props->colored())
        ro.vertexDecl = drawMesh_->getHalfedgeVertexDeclaration();
      else
        ro.vertexDecl = drawMesh_->getHalfedgeColoredVertexDeclaration();

      ro.glDrawArrays(GL_LINES, 0, mesh_.n_halfedges() * 2);

      _renderer->addRenderObject(&ro);
    }  


    // -----------------------------------------------------
    // take care of all the other primitives

    ro.depthRange = Vec2f(0.01f, 1.0f);

    switch (props->primitive())
    {
    case DrawModes::PRIMITIVE_POINT: add_point_RenderObjects(_renderer, &ro); break;
    case DrawModes::PRIMITIVE_EDGE: add_line_RenderObjects(_renderer, &ro); break;
    case DrawModes::PRIMITIVE_POLYGON: add_face_RenderObjects(_renderer, &ro); break;
    }
  }

}


template<class Mesh>
void
MeshNodeT<Mesh>::
add_point_RenderObjects(IRenderer* _renderer, const RenderObject* _baseObj) {
  drawMesh_->addPointRenderObjects(_renderer, _baseObj);
}

758
759
template<class Mesh>
void
Jan Möbius's avatar
Jan Möbius committed
760
MeshNodeT<Mesh>::
761
draw_vertices() {
762
  drawMesh_->drawVertices();
763
764
}

765
766
template<class Mesh>
void
Jan Möbius's avatar
Jan Möbius committed
767
MeshNodeT<Mesh>::
768
draw_lines() {
769

770
771
772
  if ((enabled_arrays_ & PER_EDGE_COLOR_ARRAY) && (enabled_arrays_ & PER_EDGE_VERTEX_ARRAY))
  {
    // colored edges still slow
773
    glDrawArrays(GL_LINES, 0, mesh_.n_edges() * 2);
774
  }
775
  else
776
    drawMesh_->drawLines();
777
778
}

779
780
781
782
783
784
785
786
787
788
789
790
791
template<class Mesh>
void
MeshNodeT<Mesh>::
add_line_RenderObjects(IRenderer* _renderer, const RenderObject* _baseObj) {

  if ((enabled_arrays_ & PER_EDGE_COLOR_ARRAY) && (enabled_arrays_ & PER_EDGE_VERTEX_ARRAY))
  {
    // colored edges still slow
    glDrawArrays(GL_LINES, 0, mesh_.n_edges() * 2);
  }
  else
    drawMesh_->addLineRenderObjects(_renderer, _baseObj);
}
792
793
794
795
796
797
798
799
800
801
802
803
804

template<class Mesh>
void
MeshNodeT<Mesh>::
draw_halfedges() {
  // If we are rendering per edge per vertex attributes, we need to use a seperated vertex buffer!
  if ( enabled_arrays_ & PER_HALFEDGE_VERTEX_ARRAY )
    glDrawArrays(GL_LINES, 0, mesh_.n_halfedges() * 2);
  // Something went wrong here!
  else
    std::cerr << "Unable to Draw! halfedge array configuration is invalid!!" << std::endl;
}

805
806
template<class Mesh>
void
Jan Möbius's avatar
Jan Möbius committed
807
MeshNodeT<Mesh>::
808
809
draw_faces() {
  drawMesh_->draw(textureMap_);
810
811
}

812
813
814
815
816
817
818
template<class Mesh>
void
MeshNodeT<Mesh>::
add_face_RenderObjects(IRenderer* _renderer, const RenderObject* _baseObj) {
  drawMesh_->addTriRenderObjects(_renderer, _baseObj, textureMap_);
}

819
820
template<class Mesh>
void
Jan Möbius's avatar
Jan Möbius committed
821
MeshNodeT<Mesh>::
822
enable_arrays(unsigned int _arrays) {
823

Jan Möbius's avatar
Jan Möbius committed
824
  // Unbind everything to ensure sane settings
825
826
  ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
  ACG::GLState::bindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
827

828
829
830
831
832
833
834
835
836
837
838
839
  //===================================================================
  // per Edge Vertex Array
  //===================================================================  
  
  // Check if we should enable the per face vertex array
  if (_arrays & PER_EDGE_VERTEX_ARRAY)  {
    
    // Check if its already enabled
    if (!(enabled_arrays_ & PER_EDGE_VERTEX_ARRAY)) {
      enabled_arrays_ |= PER_EDGE_VERTEX_ARRAY;
      
      // For this version we load the colors directly not from vbo
840
841
      ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
      ACG::GLState::vertexPointer( drawMesh_->perEdgeVertexBuffer() );   
842
      
843
      ACG::GLState::enableClientState(GL_VERTEX_ARRAY);
844
845
846
847
848
      
    }
  } else if (enabled_arrays_ & PER_EDGE_VERTEX_ARRAY) {
    // Disable Vertex array
    enabled_arrays_ &= ~PER_EDGE_VERTEX_ARRAY;
849
    ACG::GLState::disableClientState(GL_VERTEX_ARRAY);
850
851
852
853
854
855
856
857
858
859
860
861
862
863
  } 
  
  //===================================================================
  // per Edge Color Array
  //===================================================================  
  
  // Check if we should enable the per face vertex array
  if ( mesh_.has_edge_colors()  && ( _arrays & PER_EDGE_COLOR_ARRAY) )  {
    
    // Check if its already enabled
    if (!(enabled_arrays_ & PER_EDGE_COLOR_ARRAY)) {
      enabled_arrays_ |= PER_EDGE_COLOR_ARRAY;
      
      // For this version we load the colors directly not from vbo
864
865
      ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
      ACG::GLState::colorPointer( drawMesh_->perEdgeColorBuffer() );
866
      
867
      ACG::GLState::enableClientState(GL_COLOR_ARRAY);
868
869
870
871
872
      
    }
  } else if (enabled_arrays_ & PER_EDGE_COLOR_ARRAY) {
    // Disable Vertex array
    enabled_arrays_ &= ~PER_EDGE_COLOR_ARRAY;
873
    ACG::GLState::disableClientState(GL_COLOR_ARRAY);
874
  }   
875
876
877
878
879
880
881
882
883
884
885
886
887
888


  //===================================================================
  // per Halfedge Vertex Array
  //===================================================================  
  
  // Check if we should enable the per face vertex array
  if (_arrays & PER_HALFEDGE_VERTEX_ARRAY)  {
    
    // Check if its already enabled
    if (!(enabled_arrays_ & PER_HALFEDGE_VERTEX_ARRAY)) {
      enabled_arrays_ |= PER_HALFEDGE_VERTEX_ARRAY;
      
      // For this version we load the colors directly not from vbo
889
890
      ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
      ACG::GLState::vertexPointer( drawMesh_->perHalfedgeVertexBuffer() );   
891
      
892
      ACG::GLState::enableClientState(GL_VERTEX_ARRAY);
893
894
895
896
897
      
    }
  } else if (enabled_arrays_ & PER_HALFEDGE_VERTEX_ARRAY) {
    // Disable Vertex array
    enabled_arrays_ &= ~PER_HALFEDGE_VERTEX_ARRAY;
898
    ACG::GLState::disableClientState(GL_VERTEX_ARRAY);
899
900
901
902
903
904
905
906
907
908
909
910
911
912
  } 
  
  //===================================================================
  // per Halfedge Color Array
  //===================================================================  
  
  // Check if we should enable the per face vertex array
  if ( mesh_.has_halfedge_colors()  && ( _arrays & PER_HALFEDGE_COLOR_ARRAY) )  {
    
    // Check if its already enabled
    if (!(enabled_arrays_ & PER_HALFEDGE_COLOR_ARRAY)) {
      enabled_arrays_ |= PER_HALFEDGE_COLOR_ARRAY;
      
      // For this version we load the colors directly not from vbo
913
914
      ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
      ACG::GLState::colorPointer( drawMesh_->perHalfedgeColorBuffer() );
915
      
916
      ACG::GLState::enableClientState(GL_COLOR_ARRAY);
917
918
919
920
921
      
    }
  } else if (enabled_arrays_ & PER_HALFEDGE_COLOR_ARRAY) {
    // Disable Vertex array
    enabled_arrays_ &= ~PER_HALFEDGE_COLOR_ARRAY;
922
    ACG::GLState::disableClientState(GL_COLOR_ARRAY);
923
  }   
924

925
926
927
928
  //===================================================================
  // Check for OpenGL Errors
  //===================================================================    
  glCheckErrors();
929
930
931
932
}

template<class Mesh>
void
Jan Möbius's avatar
Jan Möbius committed
933
MeshNodeT<Mesh>::
934
pick(GLState& _state, PickTarget _target) {
935
936
937
938
939
940
941
942
943
944