Source

Coin / src / shapenodes / SoIndexedPointSet.cpp

Full commit
  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
 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
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
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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
417
418
419
420
421
422
423
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
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
/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * 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.
 * 
 * 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.
\**************************************************************************/

/*!
  \class SoIndexedPointSet SoIndexedPointSet.h Inventor/nodes/SoIndexedPointSet.h
  \brief The SoIndexedPointSet class is used to display a set of 3D points.
  \ingroup nodes

  This node either uses the coordinates currently on the state
  (typically set up by a leading SoCoordinate3 node in the scenegraph)
  or from a SoVertexProperty node attached to this node to render a
  set of 3D points.

  Here's a simple usage example of SoIndexedPointSet in a scenegraph:

  \verbatim
  #Inventor V2.1 ascii

  Separator {
     Material {
        diffuseColor [
         1 0 0, 0 1 0, 0 0 1, 1 1 0, 1 0 1, 1 1 1, 1 0.8 0.6, 0.6 0.8 1
        ]
     }
     MaterialBinding { value PER_VERTEX_INDEXED }

     Normal {
        vector [
         0 0 1, 1 0 0
        ]
     }
     NormalBinding { value PER_VERTEX_INDEXED }

     Coordinate3 {
        point [
         -1 1 0, -1 -1 0, 1 -1 0, 1 1 0, 0 2 -1, -2 0 -1, 0 -2 -1, 2 0 -1
        ]
     }

     DrawStyle { pointSize 5 }

     IndexedPointSet {
        coordIndex [0, 1, 2, 3, 4, 5, 6, 7]
        normalIndex [0, 1, 0, 1, 0, 1, 0, 1]
     }
  }

  \endverbatim

  <b>FILE FORMAT/DEFAULTS:</b>
  \code
  IndexedPointSet {
    vertexProperty        NULL
    coordIndex        0
    materialIndex        -1
    normalIndex        -1
    textureCoordIndex        -1
  }
  \endcode

  \since TGS Inventor 6.0, Coin 3.1
*/

#include <Inventor/nodes/SoIndexedPointSet.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H

#include <Inventor/misc/SoState.h>
#include <Inventor/bundles/SoTextureCoordinateBundle.h>
#include <Inventor/SoPrimitiveVertex.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/system/gl.h>
#include <Inventor/nodes/SoVertexProperty.h>
#include <Inventor/actions/SoGetPrimitiveCountAction.h>
#include <Inventor/elements/SoGLCoordinateElement.h>
#include <Inventor/elements/SoNormalBindingElement.h>
#include <Inventor/elements/SoMaterialBindingElement.h>
#include <Inventor/elements/SoTextureCoordinateBindingElement.h>
#include <Inventor/bundles/SoMaterialBundle.h>
#include <Inventor/elements/SoGLLazyElement.h>
#include <Inventor/elements/SoGLVBOElement.h>
#include <Inventor/caches/SoNormalCache.h>
#include <Inventor/details/SoPointDetail.h>
#include <Inventor/misc/SoGLDriverDatabase.h>
#if COIN_DEBUG
#include <Inventor/errors/SoDebugError.h>
#endif // COIN_DEBUG

#include "nodes/SoSubNodeP.h"
#include "rendering/SoGL.h"
#include "rendering/SoVBO.h"
#include "rendering/SoVertexArrayIndexer.h"

#define LOCK_VAINDEXER(obj) SoBase::staticDataLock()
#define UNLOCK_VAINDEXER(obj) SoBase::staticDataUnlock()

SO_NODE_SOURCE(SoIndexedPointSet);

/*!
  Constructor.
*/
SoIndexedPointSet::SoIndexedPointSet()
{
  SO_NODE_INTERNAL_CONSTRUCTOR(SoIndexedPointSet);
  this->vaindexer = NULL;
}

/*!
  Destructor.
*/
SoIndexedPointSet::~SoIndexedPointSet()
{
  if (this->vaindexer) delete this->vaindexer;
}

// doc from parent
void
SoIndexedPointSet::initClass(void)
{
  SO_NODE_INTERNAL_INIT_CLASS(SoIndexedPointSet, SO_FROM_INVENTOR_6_0);
}

/*
  Convenience function for fetching the material binding.
  Converts PER_PART* and PER_FACE* to PER_VERTEX*
  Defaults to OVERALL.
*/
SoIndexedPointSet::Binding
SoIndexedPointSet::findMaterialBinding(SoState * const state) const
{
  Binding binding;
  switch (SoMaterialBindingElement::get(state)){
    case SoMaterialBindingElement::PER_PART:
    case SoMaterialBindingElement::PER_FACE:
    case SoMaterialBindingElement::PER_VERTEX:
      binding = PER_VERTEX;
      break;
    case SoMaterialBindingElement::PER_PART_INDEXED:
    case SoMaterialBindingElement::PER_FACE_INDEXED:
    case SoMaterialBindingElement::PER_VERTEX_INDEXED:
      binding = PER_VERTEX_INDEXED;
      break;
    default:
      binding = OVERALL;
  }
  return binding;
}

/*
  Convenience function for fetching the normal binding.
  Converts PER_PART* and PER_FACE* to PER_VERTEX*
  Defaults to OVERALL.
*/
SoIndexedPointSet::Binding
SoIndexedPointSet::findNormalBinding(SoState * const state) const

{
  Binding binding;
  switch (SoNormalBindingElement::get(state)){
    case SoNormalBindingElement::PER_PART:
    case SoNormalBindingElement::PER_FACE:
    case SoNormalBindingElement::PER_VERTEX:
      binding = PER_VERTEX;
      break;
    case SoNormalBindingElement::PER_PART_INDEXED:
    case SoNormalBindingElement::PER_FACE_INDEXED:
    case SoNormalBindingElement::PER_VERTEX_INDEXED:
      binding = PER_VERTEX_INDEXED;
      break;
    default:
      binding = OVERALL;
  }
  return binding;
}

/*
  Convenience function for fetching the texture coordinate binding.
*/
SoIndexedPointSet::Binding
SoIndexedPointSet::findTextureBinding(SoState * const state) const

{
  Binding binding;
  switch (SoTextureCoordinateBindingElement::get(state)){
    case SoTextureCoordinateBindingElement::PER_VERTEX:
      binding = PER_VERTEX;
      break;
    default:
      binding = PER_VERTEX_INDEXED;
  }
  return binding;
}

// doc from parent
void
SoIndexedPointSet::GLRender(SoGLRenderAction * action)
{
  int32_t numpts = this->coordIndex.getNum();
  if (numpts == 0) return;

  SoState * state = action->getState();

  SbBool didpush = FALSE;
  if (this->vertexProperty.getValue()) {
    state->push();
    didpush = TRUE;
    this->vertexProperty.getValue()->GLRender(action);
  }

  if (!this->shouldGLRender(action)){
    if (didpush) state->pop();
    return;
  }

  SoMaterialBundle mb(action);
  SoTextureCoordinateBundle tb(action, TRUE, FALSE);

  const SbVec3f * normals;
  int numindices;
  const int32_t * cindices;
  const int32_t * nindices;
  const int32_t * tindices;
  const int32_t * mindices;
  SbBool doTextures;
  SbBool needNormals;
  const SoCoordinateElement * coords;
  Binding nbind, tbind, mbind;

  SbBool normalCacheUsed;

  doTextures = tb.needCoordinates();
  needNormals = !mb.isColorOnly() || tb.isFunction();

  this->getVertexData(state, coords, normals, cindices,
                      nindices, tindices, mindices, numindices,
                      needNormals, normalCacheUsed);

  if (numindices == 0){
    if (didpush) state->pop();
    return;
  }

  if (normals == NULL && needNormals) {
    needNormals = FALSE;
    if (!didpush) {
      state->push();
      didpush = TRUE;
    }
    SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
  }

  nbind = OVERALL;
  if (needNormals){
    nbind = this->findNormalBinding(state);
  }
  //if we don't have explicit normal indices, use coord indices:
  if (nbind == PER_VERTEX_INDEXED && nindices == NULL) nindices = cindices;

  mbind = this->findMaterialBinding(state);
  //if we don't have explicit material indices, use coord indices:
  if (mbind == PER_VERTEX_INDEXED && mindices == NULL) mindices = cindices;

  tbind = OVERALL;
  if (doTextures) {
    tbind = this->findTextureBinding(state);
    if (tb.isFunction() && !tb.needIndices()) {
      tbind = OVERALL;
      tindices = NULL;//don't send texture coords
    }
    else if (tbind == PER_VERTEX){
      tindices = NULL;//texture coords are sent per vertex
    }
    else {//tbind == PER_VERTEX_INDEXED
      //if we don't have explicit texture coord indices, use coord indices:
      if (tindices == NULL) tindices = cindices;
    }
  }

  const SoGLCoordinateElement * glcoords = dynamic_cast<const SoGLCoordinateElement *>(coords);
  assert(glcoords && "could not cast to SoGLCoordinateElement");

  if (nbind == OVERALL && needNormals) {
    glNormal3fv((const GLfloat *)normals);
  }

  mb.sendFirst(); // always do this, even if mbind != OVERALL

  const cc_glglue * glue = sogl_glue_instance(state);
  const uint32_t contextid = action->getCacheContext();

  SbBool dova =
    SoVBO::shouldRenderAsVertexArrays(state, contextid, numindices) &&
    SoGLDriverDatabase::isSupported(glue, SO_GL_VERTEX_ARRAY) &&
    ((nbind == OVERALL) || ((nbind == PER_VERTEX_INDEXED) && (nindices == cindices))) &&
    ((tbind == OVERALL) || ((tbind == PER_VERTEX_INDEXED) && (tindices == cindices))) &&
    ((mbind == OVERALL) || ((mbind == PER_VERTEX_INDEXED) && (mindices == cindices)));

  if (dova && (mbind == PER_VERTEX_INDEXED)) {
    const SoGLVBOElement * vboelem = SoGLVBOElement::getInstance(state);
    if (vboelem->getColorVBO() == NULL) {
      dova = FALSE;
      // we might be able to do VA-rendering, but need to check the
      // diffuse color type first.
      SoGLLazyElement * lelem = (SoGLLazyElement*) SoLazyElement::getInstance(state);
      if (!lelem->isPacked() && lelem->getNumTransparencies() <= 1) {
        dova = TRUE;
      }
    }
  }
  SbBool didrenderasvbo = FALSE;
  if (dova) {
    SbBool vbo = this->startVertexArray(action,
                                        glcoords,
                                        (needNormals && (nbind == PER_VERTEX_INDEXED)) ? normals : NULL,
                                        doTextures,
                                        mbind == PER_VERTEX_INDEXED);
    didrenderasvbo = vbo;
    LOCK_VAINDEXER(this);
    if (this->vaindexer == NULL) {
      SoVertexArrayIndexer * indexer = new SoVertexArrayIndexer;
      for (int i = 0; i < numindices; i++) {
        int32_t idx = this->coordIndex[i];
        indexer->addPoint(idx);
      }
      indexer->close();
      if (indexer->getNumVertices()) {
        this->vaindexer = indexer;
      }
      else {
        delete indexer;
      }
    }

    if (this->vaindexer) {
      this->vaindexer->render(sogl_glue_instance(state), vbo, contextid);
    }
    UNLOCK_VAINDEXER(this);
    this->finishVertexArray(action, vbo,
                            (needNormals && (nbind == PER_VERTEX_INDEXED)),
                            doTextures,
                            mbind == PER_VERTEX_INDEXED);
  }
  else {//no vertex array rendering
    glBegin(GL_POINTS);
    SbVec3f currnormal = normals ? normals[0] : SbVec3f(0, 0, 1);
    for (int i = 0; i < numindices; i++) {
      int32_t idx = cindices[i];
      if (idx < 0) continue;

      if (mbind == PER_VERTEX_INDEXED) mb.send(mindices[i], TRUE);
      else if (mbind == PER_VERTEX) mb.send(i, TRUE);

      if (nbind == PER_VERTEX_INDEXED) currnormal = normals[nindices[i]];
      else if (nbind == PER_VERTEX) currnormal = normals[i];

      if (needNormals && nbind != OVERALL){
        const GLfloat * ptr = reinterpret_cast<const GLfloat*>(&currnormal);
        glNormal3fv(ptr);
      }

      if (doTextures){
        if (tbind == PER_VERTEX_INDEXED){
          tb.send(tindices[i], glcoords->get3(idx), currnormal);
        }
        else if (tbind == PER_VERTEX){
          tb.send(i, glcoords->get3(idx), currnormal);
        }
      }

      glcoords->send(idx);
    }
    glEnd();
  }
  if (didpush)
    state->pop();

  // send approx number of points for autocache handling. Divide
  // by three so that three points is the same as one triangle.
  sogl_autocache_update(state, numindices/3, didrenderasvbo);
}

// Documented in superclass.
SbBool
SoIndexedPointSet::generateDefaultNormals(SoState *, SoNormalCache * nc)
{
  // Overridden to clear normal cache, as it's not possible to
  // generate a normal for a point.
  nc->set(0, NULL);
  return TRUE;
}

// Documented in superclass.
SbBool
SoIndexedPointSet::generateDefaultNormals(SoState * COIN_UNUSED_ARG(state), SoNormalBundle * COIN_UNUSED_ARG(bundle))
{
  // Overridden to avoid (faulty) compiler warnings with some version
  // of g++.
  return FALSE;
}

// doc from parent
void
SoIndexedPointSet::getBoundingBox(SoGetBoundingBoxAction *action)
{
  inherited::getBoundingBox(action);
}

// doc from parent
void
SoIndexedPointSet::getPrimitiveCount(SoGetPrimitiveCountAction *action)
{
  if (!this->shouldPrimitiveCount(action)) return;
  int num = this->coordIndex.getNum();
  action->addNumPoints(num);
}

// doc from parent
void
SoIndexedPointSet::generatePrimitives(SoAction *action)
{
  int32_t numpts = this->coordIndex.getNum();
  if (numpts == 0) return;

  SoState * state = action->getState();

  if (this->vertexProperty.getValue()) {
    state->push();
    this->vertexProperty.getValue()->doAction(action);
  }

  SoTextureCoordinateBundle tb(action, FALSE, FALSE);

  /*
    FIXME: the following code is almost identical to that in glRender.
    Consider refactoring. wiesener 20090317
  */
  const SbVec3f * normals;
  int numindices;
  const int32_t * cindices;
  const int32_t * nindices;
  const int32_t * tindices;
  const int32_t * mindices;
  SbBool doTextures;
  SbBool needNormals;
  const SoCoordinateElement * coords;
  Binding nbind, tbind, mbind;

  SbBool normalCacheUsed;

  doTextures = tb.needCoordinates();
  needNormals = TRUE;

  this->getVertexData(state, coords, normals, cindices,
                      nindices, tindices, mindices, numindices,
                      needNormals, normalCacheUsed);

  if (numindices == 0){
    if (this->vertexProperty.getValue()) state->pop();
    return;
  }

  if (normals == NULL) {
    needNormals = FALSE;
  }

  nbind = OVERALL;
  if (needNormals){
    nbind = this->findNormalBinding(state);
  }
  //if we don't have explicit normal indices, use coord indices:
  if (nbind == PER_VERTEX_INDEXED && nindices == NULL) nindices = cindices;

  mbind = this->findMaterialBinding(state);
  //if we don't have explicit material indices, use coord indices:
  if (mbind == PER_VERTEX_INDEXED && mindices == NULL) mindices = cindices;

  tbind = OVERALL;
  if (doTextures) {
    tbind = this->findTextureBinding(state);
    if (tb.isFunction() && !tb.needIndices()) {
      tbind = OVERALL;
      tindices = NULL;//don't send texture coords
    }
    else if (tbind == PER_VERTEX){
      tindices = NULL;//texture coords are sent per vertex
    }
    else {//tbind == PER_VERTEX_INDEXED
      //if we don't have explicit texture coord indices, use coord indices:
      if (tindices == NULL) tindices = cindices;
    }
  }

  SoPrimitiveVertex vertex;
  SoPointDetail pointDetail;
  vertex.setDetail(&pointDetail);

  this->beginShape(action, SoShape::POINTS);
  SbVec3f currnormal = normals ? normals[0] : SbVec3f(0, 0, 1);
  for (int i = 0; i < numindices; i++) {
    int32_t idx = cindices[i];
    if (idx < 0) continue;

    if (mbind == PER_VERTEX_INDEXED){
      pointDetail.setMaterialIndex(mindices[i]);
      vertex.setMaterialIndex(mindices[i]);
    }
    else if (mbind == PER_VERTEX){
      pointDetail.setMaterialIndex(i);
      vertex.setMaterialIndex(i);
    }

    int32_t nindex = i;
    if (nbind == PER_VERTEX_INDEXED) nindex = nindices[i];
    if (nbind != OVERALL) currnormal = normals[nindex];
    if (needNormals){
      pointDetail.setNormalIndex(nindex);
      vertex.setNormal(currnormal);
    }

    if (doTextures) {
      if (tb.isFunction()) {
        vertex.setTextureCoords(tb.get(coords->get3(idx), currnormal));
      }
      else {
        int32_t tindex = i;
        if (tbind == PER_VERTEX_INDEXED) tindex = tindices[i];
        pointDetail.setTextureCoordIndex(tindex);
        vertex.setTextureCoords(tb.get(tindex));
      }
    }
    pointDetail.setCoordinateIndex(idx);
    vertex.setPoint(coords->get3(idx));
    this->shapeVertex(&vertex);
  }
  this->endShape();

  if (this->vertexProperty.getValue())
    state->pop();
}

// Documented in superclass.
void
SoIndexedPointSet::notify(SoNotList * list)
{
  SoField * f = list->getLastField();
  if (f == &this->coordIndex) {
    LOCK_VAINDEXER(this);
    if (this->vaindexer) {
      delete this->vaindexer;
      this->vaindexer = NULL;
    }
    UNLOCK_VAINDEXER(this);
  }
  inherited::notify(list);
}

#undef LOCK_VAINDEXER
#undef UNLOCK_VAINDEXER