BSplineSurfaceNodeT.cc 51.6 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           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.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
Jan Möbius's avatar
 
Jan Möbius committed
40
41
42
\*===========================================================================*/

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


//=============================================================================
//
//  CLASS BSplineSurfaceNodeT - IMPLEMENTATION
//  Author:  Ellen Dekkers <dekkers@cs.rwth-aachen.de>
//
//=============================================================================

#define ACG_BSPLINESURFACENODET_C

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

#include "BSplineSurfaceNodeT.hh"
#include <ACG/GL/gl.hh>
64
#include <ACG/GL/GLError.hh>
65
#include <ACG/GL/IRenderer.hh>
66
#include <ACG/Utils/VSToolsT.hh>
Jan Möbius's avatar
 
Jan Möbius committed
67
68
69
#include <vector>


70

Jan Möbius's avatar
 
Jan Möbius committed
71
72
73
74
75
76
77
78
//== NAMESPACES ===============================================================

namespace ACG {
namespace SceneGraph {

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


79
template <class BSplineSurfaceType>
Jan Möbius's avatar
 
Jan Möbius committed
80
void
81
BSplineSurfaceNodeT<BSplineSurfaceType>::
Jan Möbius's avatar
 
Jan Möbius committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
{
  for (unsigned int i = 0; i < bsplineSurface_.n_control_points_m(); ++i)
  {
    for (unsigned int j = 0; j < bsplineSurface_.n_control_points_n(); ++j)
    {
      _bbMin.minimize(bsplineSurface_(i,j));
      _bbMax.maximize(bsplineSurface_(i,j));
    }
  }
}

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

96
template <class BSplineSurfaceType>
Jan Möbius's avatar
 
Jan Möbius committed
97
DrawModes::DrawMode
98
BSplineSurfaceNodeT<BSplineSurfaceType>::
99
availableDrawModes() const
Jan Möbius's avatar
 
Jan Möbius committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{
  DrawModes::DrawMode drawModes(0);

  drawModes |= DrawModes::POINTS;
  drawModes |= DrawModes::WIREFRAME;
  drawModes |= DrawModes::HIDDENLINE;
  drawModes |= DrawModes::SOLID_SMOOTH_SHADED;
  drawModes |= DrawModes::SOLID_FLAT_SHADED;
  drawModes |= DrawModes::SOLID_PHONG_SHADED;
  drawModes |= DrawModes::SOLID_SHADER;
  drawModes |= DrawModes::SOLID_TEXTURED;
  drawModes |= DrawModes::SOLID_1DTEXTURED;

  return drawModes;
}

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

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

template <class BSplineSurfaceType>
void
BSplineSurfaceNodeT<BSplineSurfaceType>::
getRenderObjects(IRenderer* _renderer, GLState& _state, const DrawModes::DrawMode& _drawMode, const Material* _mat)
{
  // check if textures are still valid
  if (    bspline_selection_draw_mode_ == CONTROLPOINT
    && controlPointSelectionTexture_valid_ == false)
    updateControlPointSelectionTexture(_state);
  if (    bspline_selection_draw_mode_ == KNOTVECTOR
    && knotVectorSelectionTexture_valid_ == false)
    updateKnotVectorSelectionTexture(_state);


  for (size_t i = 0; i < _drawMode.getNumLayers(); ++i)
  {
    const DrawModes::DrawModeProperties* props = _drawMode.getLayer(i);


    RenderObject ro;
    ro.initFromState(&_state);
    ro.setupShaderGenFromDrawmode(props);
    ro.depthTest = true;

143
144
145
    // generated texcoords for environment mapping should be computed in fragment shader,
    // because normals aren't available in the vertex shader 
    ro.shaderDesc.texGenPerFragment = true;
146

147
148
149
    if ( props->textured() && arb_texture_idx_)
      ro.addTexture(ACG::RenderObject::Texture(arb_texture_idx_), 0);

150
151
152
153
154
155
156
157
158
159
160
161
162
    if (props->primitive() == DrawModes::PRIMITIVE_POLYGON || props->primitive() == DrawModes::PRIMITIVE_WIREFRAME)
    {
      updateSurfaceMesh();

      ro.vertexBuffer = surfaceVBO_.id();
      ro.indexBuffer = surfaceIBO_.id();
      ro.vertexDecl = &surfaceDecl_;

      if (props->primitive() == DrawModes::PRIMITIVE_WIREFRAME)
        ro.fillMode = GL_LINE;
      else
        ro.fillMode = GL_FILL;

163
164
165
      GLenum roPrimitives = GL_TRIANGLES;

#ifdef GL_ARB_tessellation_shader
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
      bool tessellationMode = ACG::openGLVersion(4, 0) && Texture::supportsTextureBuffer();

      if (tessellationMode)
      {
        // dynamic lod tessellation and spline evaluation on gpu

        if (!controlPointTex_.is_valid())
          updateTexBuffers();

        ro.shaderDesc.tessControlTemplateFile = "BSpline/tesscontrol_lod.glsl";
        ro.shaderDesc.tessEvaluationTemplateFile = "BSpline/tesseval_lod.glsl";


        QString shaderMacro;

        shaderMacro.sprintf("#define BSPLINE_DEGREE_U %i", bsplineSurface_.degree_m());
        ro.shaderDesc.macros.push_back(shaderMacro);

        shaderMacro.sprintf("#define BSPLINE_DEGREE_V %i", bsplineSurface_.degree_n());
        ro.shaderDesc.macros.push_back(shaderMacro);

        shaderMacro.sprintf("#define BSPLINE_KNOTVEC_U %i", bsplineSurface_.degree_m() * 2 + 1);
        ro.shaderDesc.macros.push_back(shaderMacro);

        shaderMacro.sprintf("#define BSPLINE_KNOTVEC_V %i", bsplineSurface_.degree_n() * 2 + 1);
        ro.shaderDesc.macros.push_back(shaderMacro);


        ro.setUniform("controlPointTex", int(1));
        ro.setUniform("knotBufferU", int(2));
        ro.setUniform("knotBufferV", int(3));

        ro.setUniform("uvRange", Vec4f(bsplineSurface_.loweru(), bsplineSurface_.upperu(),
          bsplineSurface_.lowerv(), bsplineSurface_.upperv()));

        ro.addTexture(RenderObject::Texture(controlPointTex_.id(), GL_TEXTURE_2D), 1, false);
        ro.addTexture(RenderObject::Texture(knotTexBufferU_.id(), GL_TEXTURE_BUFFER), 2, false);
        ro.addTexture(RenderObject::Texture(knotTexBufferV_.id(), GL_TEXTURE_BUFFER), 3, false);

205
206
207
        roPrimitives = GL_PATCHES;
      }
      
208
209
      if (tessellationMode)
        ro.patchVertices = 3;
210
#endif
211

212
      ro.glDrawElements(roPrimitives, surfaceIndexCount_, GL_UNSIGNED_INT, 0);
213
214

      _renderer->addRenderObject(&ro);
215

216
217
218
219
220
221
222
223
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
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
    }
  }

  // draw the control net (includes selection on the net)
  if (render_control_net_)
  {
    // update if necessary
    updateControlNetMesh();
    updateControlNetMeshSel();

    // setup base renderobject for unlit point and line rendering
    RenderObject ro;
    ro.initFromState(&_state);
    ro.depthTest = true;
    ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;

    ro.vertexBuffer = controlNetVBO_.id();
    ro.vertexDecl = &controlNetDecl_;

    Vec2f screenSize = Vec2f(_state.viewport_width(), _state.viewport_height());

    // selected control points
    if (controlNetSelIndices_)
    {
      ro.name = "BSplineSurface_ControlPointSel";

      ro.setupPointRendering(10.0f, screenSize);

      Vec4f selColor = generateHighlightColor(controlnet_color_);
      ro.emissive = Vec3f(selColor[0], selColor[1], selColor[2]);

      ro.indexBuffer = controlNetSelIBO_.id();

      ro.glDrawElements(GL_POINTS, controlNetSelIndices_, GL_UNSIGNED_INT, 0);

      _renderer->addRenderObject(&ro);
    }

    // all control points
    {
      ro.name = "BSplineSurface_ControlPoint";
      ro.setupPointRendering(_state.point_size() + 4.0f, screenSize);

      ro.emissive = Vec3f(controlnet_color_[0], controlnet_color_[1], controlnet_color_[2]);

      GLsizei numPoints = bsplineSurface_.n_control_points_m() * bsplineSurface_.n_control_points_n();
      ro.glDrawArrays(GL_POINTS, 0, numPoints);

      _renderer->addRenderObject(&ro);
    }

    ro.resetPointRendering();

    // all line segments
    {
      ro.name = "BSplineSurface_ControlNetLines";
      ro.setupLineRendering(_state.line_width() + 2.0f, screenSize);

      ro.indexBuffer = controlNetLineIBO_.id();

      ro.glDrawElements(GL_LINES, controlNetLineIndices_, GL_UNSIGNED_INT, 0);

      _renderer->addRenderObject(&ro);
    }
  }
}


Jan Möbius's avatar
 
Jan Möbius committed
284
285
//----------------------------------------------------------------------------

286
template <class BSplineSurfaceType>
Jan Möbius's avatar
 
Jan Möbius committed
287
void
288
BSplineSurfaceNodeT<BSplineSurfaceType>::
289
draw(GLState& _state, const DrawModes::DrawMode& _drawMode)
Jan Möbius's avatar
 
Jan Möbius committed
290
{
291
  GLenum prev_depth = _state.depthFunc();
Jan Möbius's avatar
 
Jan Möbius committed
292
293
294

  glPushAttrib(GL_ENABLE_BIT);

295
296
297
298
299
300
301
302
303
  // check if textures are still valid
  if (    bspline_selection_draw_mode_ == CONTROLPOINT
       && controlPointSelectionTexture_valid_ == false)
    updateControlPointSelectionTexture(_state);
  if (    bspline_selection_draw_mode_ == KNOTVECTOR
       && knotVectorSelectionTexture_valid_ == false)
    updateKnotVectorSelectionTexture(_state);
  
  
Jan Möbius's avatar
 
Jan Möbius committed
304
305
  if (_drawMode & DrawModes::POINTS)
  {
306
307
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
 
Jan Möbius committed
308
309
310
311
312
313
314
    render( _state, false);
  }

  if (_drawMode & DrawModes::WIREFRAME)
  {
    glPushAttrib(GL_ENABLE_BIT);

315
316
317
    ACG::GLState::disable( GL_CULL_FACE );
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
 
Jan Möbius committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    render( _state, false);

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glPopAttrib();
  }


  if (_drawMode & DrawModes::HIDDENLINE)
  {
    Vec4f  clear_color = _state.clear_color();
    Vec4f  base_color  = _state.base_color();
    clear_color[3] = 1.0;

333
334
335
    ACG::GLState::enable(GL_DEPTH_TEST);
    ACG::GLState::disable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
 
Jan Möbius committed
336
337
338
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    _state.set_base_color(clear_color);

Jan Möbius's avatar
Jan Möbius committed
339
    ACG::GLState::depthRange(0.01, 1.0);
Jan Möbius's avatar
 
Jan Möbius committed
340
341
342

    render( _state, true);

Jan Möbius's avatar
Jan Möbius committed
343
    ACG::GLState::depthRange(0.0, 1.0);
Jan Möbius's avatar
 
Jan Möbius committed
344
345

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
346
    ACG::GLState::depthFunc(GL_LEQUAL);
Jan Möbius's avatar
 
Jan Möbius committed
347
348
349
350
    _state.set_base_color(base_color);

    render( _state, false);

351
    ACG::GLState::depthFunc(prev_depth);
Jan Möbius's avatar
 
Jan Möbius committed
352
353
354
355
356
357
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  }


  if ( ( _drawMode & DrawModes::SOLID_FLAT_SHADED ))
  {
358
359
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
 
Jan Möbius committed
360

361
362
    ACG::GLState::enable(GL_AUTO_NORMAL);
    ACG::GLState::enable(GL_NORMALIZE);
Jan Möbius's avatar
 
Jan Möbius committed
363

Jan Möbius's avatar
Jan Möbius committed
364
    ACG::GLState::depthRange(0.01, 1.0);
Jan Möbius's avatar
 
Jan Möbius committed
365
366
367

    render( _state, true);

Jan Möbius's avatar
Jan Möbius committed
368
    ACG::GLState::depthRange(0.0, 1.0);
Jan Möbius's avatar
 
Jan Möbius committed
369
370
371
372
373
  }


  if ( ( _drawMode & DrawModes::SOLID_SMOOTH_SHADED ) )
  {
374
375
    ACG::GLState::enable(GL_AUTO_NORMAL);
    ACG::GLState::enable(GL_NORMALIZE);
Jan Möbius's avatar
 
Jan Möbius committed
376

377
378
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
Jan Möbius committed
379
    ACG::GLState::depthRange(0.01, 1.0);
Jan Möbius's avatar
 
Jan Möbius committed
380
381
382

    render( _state, true);

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
  }

  if ( ( _drawMode & DrawModes::SOLID_PHONG_SHADED )  )
  {
388
389
    ACG::GLState::enable(GL_AUTO_NORMAL);
    ACG::GLState::enable(GL_NORMALIZE);
Jan Möbius's avatar
 
Jan Möbius committed
390

391
392
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
Jan Möbius committed
393
    ACG::GLState::depthRange(0.01, 1.0);
Jan Möbius's avatar
 
Jan Möbius committed
394
395
396

    render( _state, true);

Jan Möbius's avatar
Jan Möbius committed
397
    ACG::GLState::depthRange(0.0, 1.0);
Jan Möbius's avatar
 
Jan Möbius committed
398
399
400
401
402
  }


    // If in shader mode, just draw, as the shader has to be set by a shadernode above this node
  if ( (_drawMode & DrawModes::SOLID_SHADER )  ) {
403
404
    ACG::GLState::enable(GL_AUTO_NORMAL);
    ACG::GLState::enable(GL_NORMALIZE);
Jan Möbius's avatar
 
Jan Möbius committed
405

406
407
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
Jan Möbius committed
408
    ACG::GLState::depthRange(0.01, 1.0);
Jan Möbius's avatar
 
Jan Möbius committed
409
410
411
412

//     draw_faces(PER_VERTEX);
    render( _state, true);

Jan Möbius's avatar
Jan Möbius committed
413
    ACG::GLState::depthRange(0.0, 1.0);
Jan Möbius's avatar
 
Jan Möbius committed
414
415
416
  }


417
  if ((_drawMode & DrawModes::SOLID_TEXTURED) || (_drawMode & DrawModes::SOLID_ENV_MAPPED)) {
418
419
    ACG::GLState::enable(GL_AUTO_NORMAL);
    ACG::GLState::enable(GL_NORMALIZE);
420
421
//     ACG::GLState::enable (GL_BLEND); 
//     ACG::GLState::blendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
422
423
    ACG::GLState::enable(GL_LIGHTING);
    ACG::GLState::shadeModel(GL_SMOOTH);
Jan Möbius's avatar
Jan Möbius committed
424
    ACG::GLState::depthRange(0.01, 1.0);
425
426
427
428
429

    arb_texture_used_ = true;
    drawTexturedSurface(_state, arb_texture_idx_ );
    arb_texture_used_ = false;

Jan Möbius's avatar
Jan Möbius committed
430
    ACG::GLState::depthRange(0.0, 1.0);
431
//    ACG::GLState::disable(GL_BLEND);
432
433
  }

Jan Möbius's avatar
 
Jan Möbius committed
434
435
436
437
438
  glPopAttrib();
}

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

439
template <class BSplineSurfaceType>
Jan Möbius's avatar
 
Jan Möbius committed
440
void
441
BSplineSurfaceNodeT<BSplineSurfaceType>::
442
443
444
445
render(GLState& _state, bool _fill)
{
  // draw the control net (includes selection on the net)
  if (render_control_net_)
446
447
448
449
450
451
  {
    if (bspline_draw_mode_ == NORMAL)
      drawControlNet(_state);
    else if (bspline_draw_mode_ == FANCY)
      drawFancyControlNet(_state);
  }
452
  
453
  // draw the spline curve itself, depending on the type of visualization
454
455
456
457
458
459
460
461
462
463
464
465
466
  if (render_bspline_surface_)
  {
    if (bspline_selection_draw_mode_ == NONE)
      drawSurface(_state, _fill);
    else if (bspline_selection_draw_mode_ == CONTROLPOINT)
      drawTexturedSurface(_state, cp_selection_texture_idx_);
    else if (bspline_selection_draw_mode_ == KNOTVECTOR)
      drawTexturedSurface(_state, knot_selection_texture_idx_);
  }
}

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

467
template <class BSplineSurfaceType>
468
void
469
BSplineSurfaceNodeT<BSplineSurfaceType>::
470
drawSurface(GLState& _state, bool _fill)
Jan Möbius's avatar
 
Jan Möbius committed
471
{
472
  updateSurfaceMesh();
Jan Möbius's avatar
 
Jan Möbius committed
473

474
475
  surfaceVBO_.bind();
  surfaceIBO_.bind();
Jan Möbius's avatar
 
Jan Möbius committed
476

477
  surfaceDecl_.activateFixedFunction();
Jan Möbius's avatar
 
Jan Möbius committed
478

479
480
  // draw
  glDrawElements(GL_TRIANGLES, surfaceIndexCount_, GL_UNSIGNED_INT, 0);
Jan Möbius's avatar
 
Jan Möbius committed
481

482
  surfaceDecl_.deactivateFixedFunction();
Jan Möbius's avatar
 
Jan Möbius committed
483

484
485
  surfaceIBO_.unbind();
  surfaceVBO_.unbind();
Jan Möbius's avatar
 
Jan Möbius committed
486
487
}

488
489
//----------------------------------------------------------------------------

490
template <class BSplineSurfaceType>
491
void
492
BSplineSurfaceNodeT<BSplineSurfaceType>::
493
drawTexturedSurface(GLState& _state, GLuint _texture_idx)
494
495
{
  glPushAttrib(GL_ALL_ATTRIB_BITS);
496
497
//  ACG::GLState::enable( GL_COLOR_MATERIAL );
//  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
498
    
499
  ACG::GLState::enable(GL_TEXTURE_2D);
500
  
501
502
503
504
505
506
  // blend colors (otherwise lighting does not affect the texture)
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  // avoid aliasing at patch boundaries
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
507
508
509
510
511
512
513
514

  // repeat if arbitrary texture mode
  if( arb_texture_used_ )
  {
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  }

515
  // GL_MODULATE to include lighting effects
516
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
517
  
518
  ACG::GLState::bindTexture( GL_TEXTURE_2D, _texture_idx);
519

520
  drawSurface( _state);
521

522
523
  ACG::GLState::bindTexture( GL_TEXTURE_2D, 0);
  ACG::GLState::disable(GL_TEXTURE_2D);
524
//  ACG::GLState::disable( GL_COLOR_MATERIAL );
525
526
527
  glPopAttrib( );
}

Jan Möbius's avatar
 
Jan Möbius committed
528
529
//----------------------------------------------------------------------------

530
template <class BSplineSurfaceType>
Jan Möbius's avatar
 
Jan Möbius committed
531
void
532
BSplineSurfaceNodeT<BSplineSurfaceType>::
Jan Möbius's avatar
 
Jan Möbius committed
533
534
drawControlNet(GLState& _state)
{
535
536
537
  // remember old color
  Vec4f base_color_old = _state.base_color();
  
Jan Möbius's avatar
 
Jan Möbius committed
538
539
540
541
  // draw control net
//   glPushAttrib(GL_ENABLE_BIT);
  glPushAttrib(GL_ALL_ATTRIB_BITS);

542
543
544
  ACG::GLState::disable( GL_CULL_FACE );
  ACG::GLState::disable(GL_LIGHTING);
  ACG::GLState::shadeModel(GL_FLAT);
Jan Möbius's avatar
 
Jan Möbius committed
545

546
547
548
549
550
551
552
553
554

  // update controlnet buffers
  updateControlNetMesh();
  updateControlNetMeshSel();

  // bind vbo containing all control points
  controlNetVBO_.bind();
  controlNetDecl_.activateFixedFunction();

Jan Möbius's avatar
 
Jan Möbius committed
555
556
557
  // draw points
  
  // draw selection
558
  if (controlNetSelIndices_)
Jan Möbius's avatar
 
Jan Möbius committed
559
  {
560
    glColor(generateHighlightColor(controlnet_color_));
Jan Möbius's avatar
 
Jan Möbius committed
561
562
    glPointSize(10);

563
564
565
    // selected points are in index buffer
    controlNetSelIBO_.bind();
    glDrawElements(GL_POINTS, controlNetSelIndices_, GL_UNSIGNED_INT, 0);
Jan Möbius's avatar
 
Jan Möbius committed
566
567
568
569
570
571
572
573
  }

  // draw all points
  glColor(controlnet_color_);

  float point_size_old = _state.point_size();
  glPointSize(point_size_old + 4);
  
574
575
  GLsizei numControlPoints = bsplineSurface_.n_control_points_m() * bsplineSurface_.n_control_points_n();
  glDrawArrays(GL_POINTS, 0, numControlPoints);
Jan Möbius's avatar
 
Jan Möbius committed
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
617
618
619

  glPointSize((int)point_size_old);
  

  // draw line segments

  /*
  // draw selection
  if( bsplineSurface_.edge_selections_available())
  {
    // save old values
    Vec4f base_color_old = _state.base_color();
    float line_width_old = _state.line_width();

    glColor(controlnet_highlight_color_);
    glLineWidth(2*line_width_old);

    glBegin(GL_LINES);
    // draw bspline control net
    int num_edges_m = (int)(bsplineSurface_.n_control_points_m()) - 1;
    int num_edges_n = (int)(bsplineSurface_.n_control_points_n()) - 1;
    for (int i = 0; i < num_edges_m; ++i) // #edges
    {
      for (int j = 0; j < num_edges_n; ++j) // #edges
      {
        if( bsplineSurface_.edge_selection(i, j))
        {
          glVertex(bsplineSurface_(i,j));
          glVertex(bsplineSurface_(i+1, j));
        }
      }
    }
    glEnd();

    glLineWidth(line_width_old);
    glColor( base_color_old );
  }
*/
  // draw all line segments
  glColor(controlnet_color_);

  float line_width_old = _state.line_width();
  glLineWidth(line_width_old+2.0);

620
621
  controlNetLineIBO_.bind();
  glDrawElements(GL_LINES, controlNetLineIndices_, GL_UNSIGNED_INT, 0);
Jan Möbius's avatar
 
Jan Möbius committed
622
623


624
625
626
627
  // restore gl states
  controlNetDecl_.deactivateFixedFunction();
  controlNetLineIBO_.unbind();
  controlNetVBO_.unbind();
Jan Möbius's avatar
 
Jan Möbius committed
628
629
630

  glColor( base_color_old );
  glLineWidth(line_width_old);
631
    
Jan Möbius's avatar
 
Jan Möbius committed
632
633
634
635
636
  glPopAttrib();
}

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

637
template <class BSplineSurfaceType>
638
void
639
BSplineSurfaceNodeT<BSplineSurfaceType>::
640
641
drawFancyControlNet(GLState& _state)
{
642
643
644
  // remember old color
  Vec4f base_color_old = _state.base_color();
  
645
646
647
648
  // draw control net
//   glPushAttrib(GL_ENABLE_BIT);
  glPushAttrib(GL_ALL_ATTRIB_BITS);

649
  ACG::GLState::disable( GL_CULL_FACE );
650
651
  
  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
652
  ACG::GLState::enable( GL_COLOR_MATERIAL );
653
  ACG::GLState::enable(GL_LIGHTING);
654
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
655
  ACG::GLState::shadeModel(GL_SMOOTH);
656
657
658
659
660
661
662
663
664


  // draw points
  double sphereRadius = _state.point_size() * 0.05;
  
  // draw selection
  if( bsplineSurface_.controlpoint_selections_available())
  {
    // save old values
665
666
667
    float point_size_old = _state.point_size();

    // save old values
668
669
//     glColor(controlnet_highlight_color_);
    glColor(generateHighlightColor(controlnet_color_));
670
671
672
673
674
675
676

    // draw control polygon
    for (unsigned int i = 0; i < bsplineSurface_.n_control_points_m(); ++i)
    {
      for (unsigned int j = 0; j < bsplineSurface_.n_control_points_n(); ++j)
      {
        if( bsplineSurface_.controlpoint_selection(i, j))
677
          draw_sphere(bsplineSurface_(i, j), sphereRadius, _state, fancySphere_);
678
679
      }
    }
680
681
    
    glPointSize(point_size_old);
682
683
684
685
686
687
688
  }

  // draw all points
  glColor(controlnet_color_);

  for (unsigned int i = 0; i < bsplineSurface_.n_control_points_m(); ++i)
    for (unsigned int j = 0; j < bsplineSurface_.n_control_points_n(); ++j)
689
      draw_sphere(bsplineSurface_(i, j), sphereRadius, _state, fancySphere_);
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


  // draw line segments

  double cylinderRadius = _state.line_width() * 0.05;

  glColor(controlnet_color_);

  // draw bspline control net
  for (unsigned int i = 0; i < bsplineSurface_.n_control_points_m(); ++i)
  {
    for (int j = 0; j < (int)bsplineSurface_.n_control_points_n() - 1; ++j)
    {
      Vec3d p      = bsplineSurface_(i, j);
      Vec3d p_next = bsplineSurface_(i, j+1);
      draw_cylinder(p, p_next - p, cylinderRadius, _state);
    }
  }

  for (int j = 0; j < (int)bsplineSurface_.n_control_points_n(); ++j)
  {
    for (int i = 0; i < (int)bsplineSurface_.n_control_points_m() - 1; ++i)
    {
      Vec3d p = bsplineSurface_(i, j);
      Vec3d p_next = bsplineSurface_(i+1,j);
      draw_cylinder(p, p_next - p, cylinderRadius, _state);
    }
  }

719
  // reset old color
720
721
722
723
724
725
726
  glColor( base_color_old );

  glPopAttrib();
}

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

727
728
729
730
731
732
733
734
735
736
737
template <class BSplineSurfaceType>
void 
BSplineSurfaceNodeT<BSplineSurfaceType>::
updateGeometry()
{
  invalidateSurfaceMesh_ = true;
  invalidateControlNetMesh_ = true;
}

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