-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
SoBrepFaceSet.cpp
1951 lines (1732 loc) · 71.5 KB
/
SoBrepFaceSet.cpp
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
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef FC_OS_WIN32
#define GL_GLEXT_PROTOTYPES
#endif
#ifndef _PreComp_
# include <float.h>
# include <algorithm>
# include <map>
# include <Python.h>
# include <Inventor/SoPickedPoint.h>
# include <Inventor/SoPrimitiveVertex.h>
# include <Inventor/actions/SoCallbackAction.h>
# include <Inventor/actions/SoGetBoundingBoxAction.h>
# include <Inventor/actions/SoGetPrimitiveCountAction.h>
# include <Inventor/actions/SoGLRenderAction.h>
# include <Inventor/actions/SoPickAction.h>
# include <Inventor/actions/SoWriteAction.h>
# include <Inventor/bundles/SoMaterialBundle.h>
# include <Inventor/bundles/SoTextureCoordinateBundle.h>
# include <Inventor/elements/SoLazyElement.h>
# include <Inventor/elements/SoOverrideElement.h>
# include <Inventor/elements/SoCoordinateElement.h>
# include <Inventor/elements/SoGLCoordinateElement.h>
# include <Inventor/elements/SoGLCacheContextElement.h>
# include <Inventor/elements/SoGLVBOElement.h>
# include <Inventor/elements/SoLineWidthElement.h>
# include <Inventor/elements/SoPointSizeElement.h>
# include <Inventor/errors/SoDebugError.h>
# include <Inventor/errors/SoReadError.h>
# include <Inventor/details/SoFaceDetail.h>
# include <Inventor/details/SoLineDetail.h>
# include <Inventor/misc/SoState.h>
# include <Inventor/misc/SoContextHandler.h>
# include <Inventor/elements/SoShapeStyleElement.h>
# include <Inventor/elements/SoCacheElement.h>
# include <Inventor/elements/SoTextureEnabledElement.h>
# ifdef FC_OS_WIN32
# include <windows.h>
# include <GL/gl.h>
# include <GL/glext.h>
# else
# ifdef FC_OS_MACOSX
# include <OpenGL/gl.h>
# include <OpenGL/glext.h>
# else
# include <GL/gl.h>
# include <GL/glext.h>
# endif //FC_OS_MACOSX
# endif //FC_OS_WIN32
// Should come after glext.h to avoid warnings
# include <Inventor/C/glue/gl.h>
#endif
#include <boost/algorithm/string/predicate.hpp>
#include "SoBrepFaceSet.h"
#include <Gui/SoFCUnifiedSelection.h>
#include <Gui/SoFCSelectionAction.h>
#include <Gui/SoFCInteractiveElement.h>
using namespace PartGui;
SO_NODE_SOURCE(SoBrepFaceSet);
#define PRIVATE(p) ((p)->pimpl)
class SoBrepFaceSet::VBO {
public:
struct Buffer {
uint32_t myvbo[2];
std::size_t vertex_array_size;
std::size_t index_array_size;
bool updateVbo;
bool vboLoaded;
};
static SbBool vboAvailable;
uint32_t indice_array;
std::map<uint32_t, Buffer> vbomap;
VBO()
{
SoContextHandler::addContextDestructionCallback(context_destruction_cb, this);
indice_array = 0;
}
~VBO()
{
SoContextHandler::removeContextDestructionCallback(context_destruction_cb, this);
// schedule delete for all allocated GL resources
std::map<uint32_t, Buffer>::iterator it;
for (it = vbomap.begin(); it != vbomap.end(); ++it) {
void * ptr0 = (void*) ((uintptr_t) it->second.myvbo[0]);
SoGLCacheContextElement::scheduleDeleteCallback(it->first, VBO::vbo_delete, ptr0);
void * ptr1 = (void*) ((uintptr_t) it->second.myvbo[1]);
SoGLCacheContextElement::scheduleDeleteCallback(it->first, VBO::vbo_delete, ptr1);
}
}
void render(SoGLRenderAction * action,
const SoGLCoordinateElement * const vertexlist,
const int32_t *vertexindices,
int num_vertexindices,
const int32_t *partindices,
int num_partindices,
const SbVec3f *normals,
const int32_t *normindices,
SoMaterialBundle *const materials,
const int32_t *matindices,
SoTextureCoordinateBundle * const texcoords,
const int32_t *texindices,
const int nbind,
const int mbind,
const int texture);
static void context_destruction_cb(uint32_t context, void * userdata)
{
VBO * self = static_cast<VBO*>(userdata);
std::map<uint32_t, Buffer>::iterator it = self->vbomap.find(context);
if (it != self->vbomap.end()) {
#ifdef FC_OS_WIN32
const cc_glglue * glue = cc_glglue_instance((int) context);
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)cc_glglue_getprocaddress(glue, "glDeleteBuffersARB");
#endif
//cc_glglue_glDeleteBuffers(glue, buffer.size(), buffer.data());
auto &buffer = it->second;
glDeleteBuffersARB(2, buffer.myvbo);
self->vbomap.erase(it);
}
}
static void vbo_delete(void * closure, uint32_t contextid)
{
const cc_glglue * glue = cc_glglue_instance((int) contextid);
GLuint id = (GLuint) ((uintptr_t) closure);
cc_glglue_glDeleteBuffers(glue, 1, &id);
}
};
SbBool SoBrepFaceSet::VBO::vboAvailable = false;
void SoBrepFaceSet::initClass()
{
SO_NODE_INIT_CLASS(SoBrepFaceSet, SoIndexedFaceSet, "IndexedFaceSet");
}
SoBrepFaceSet::SoBrepFaceSet()
{
SO_NODE_CONSTRUCTOR(SoBrepFaceSet);
SO_NODE_ADD_FIELD(partIndex, (-1));
selContext = std::make_shared<SelContext>();
selContext2 = std::make_shared<SelContext>();
pimpl.reset(new VBO);
}
SoBrepFaceSet::~SoBrepFaceSet()
{
}
void SoBrepFaceSet::doAction(SoAction* action)
{
if (action->getTypeId() == Gui::SoHighlightElementAction::getClassTypeId()) {
Gui::SoHighlightElementAction* hlaction = static_cast<Gui::SoHighlightElementAction*>(action);
selCounter.checkAction(hlaction);
if (!hlaction->isHighlighted()) {
SelContextPtr ctx = Gui::SoFCSelectionRoot::getActionContext(action,this,selContext,false);
if(ctx) {
ctx->highlightIndex = -1;
touch();
}
return;
}
const SoDetail* detail = hlaction->getElement();
if (!detail) {
SelContextPtr ctx = Gui::SoFCSelectionRoot::getActionContext(action,this,selContext);
ctx->highlightIndex = INT_MAX;
ctx->highlightColor = hlaction->getColor();
touch();
}else {
if (!detail->isOfType(SoFaceDetail::getClassTypeId())) {
SelContextPtr ctx = Gui::SoFCSelectionRoot::getActionContext(action,this,selContext,false);
if(ctx) {
ctx->highlightIndex = -1;
touch();
}
}else {
int index = static_cast<const SoFaceDetail*>(detail)->getPartIndex();
SelContextPtr ctx = Gui::SoFCSelectionRoot::getActionContext(action,this,selContext);
ctx->highlightIndex = index;
ctx->highlightColor = hlaction->getColor();
touch();
}
}
return;
}
else if (action->getTypeId() == Gui::SoSelectionElementAction::getClassTypeId()) {
Gui::SoSelectionElementAction* selaction = static_cast<Gui::SoSelectionElementAction*>(action);
switch(selaction->getType()) {
case Gui::SoSelectionElementAction::All: {
SelContextPtr ctx = Gui::SoFCSelectionRoot::getActionContext<SelContext>(action,this,selContext);
selCounter.checkAction(selaction,ctx);
ctx->selectionColor = selaction->getColor();
ctx->selectionIndex.clear();
ctx->selectionIndex.insert(-1);
touch();
return;
} case Gui::SoSelectionElementAction::None:
if(selaction->isSecondary()) {
if(Gui::SoFCSelectionRoot::removeActionContext(action,this))
touch();
}else {
SelContextPtr ctx = Gui::SoFCSelectionRoot::getActionContext(action,this,selContext,false);
if(ctx) {
ctx->selectionIndex.clear();
ctx->colors.clear();
touch();
}
}
return;
case Gui::SoSelectionElementAction::Color:
if(selaction->isSecondary()) {
const auto &colors = selaction->getColors();
auto ctx = Gui::SoFCSelectionRoot::getActionContext(action,this,selContext,false);
if(colors.empty()) {
if(ctx) {
ctx->colors.clear();
if(ctx->isSelectAll())
Gui::SoFCSelectionRoot::removeActionContext(action,this);
touch();
}
return;
}
static std::string element("Face");
if(colors.begin()->first.empty() || colors.lower_bound(element)!=colors.end()) {
if(!ctx) {
ctx = Gui::SoFCSelectionRoot::getActionContext<SelContext>(action,this);
selCounter.checkAction(selaction,ctx);
ctx->selectAll();
}
if(ctx->setColors(selaction->getColors(),element))
touch();
}
}
return;
case Gui::SoSelectionElementAction::Remove:
case Gui::SoSelectionElementAction::Append: {
const SoDetail* detail = selaction->getElement();
if (!detail || !detail->isOfType(SoFaceDetail::getClassTypeId())) {
if(selaction->isSecondary()) {
// For secondary context, a detail of different type means
// the user may want to partial render only other type of
// geometry. So we call below to obtain a action context.
// If no secondary context exist, it will create an empty
// one, and an empty secondary context inhibites drawing
// here.
auto ctx = Gui::SoFCSelectionRoot::getActionContext<SelContext>(action,this);
selCounter.checkAction(selaction,ctx);
touch();
}
return;
}
int index = static_cast<const SoFaceDetail*>(detail)->getPartIndex();
if (selaction->getType() == Gui::SoSelectionElementAction::Append) {
auto ctx = Gui::SoFCSelectionRoot::getActionContext(action,this,selContext);
selCounter.checkAction(selaction,ctx);
ctx->selectionColor = selaction->getColor();
if(ctx->isSelectAll())
ctx->selectionIndex.clear();
if(ctx->selectionIndex.insert(index).second)
touch();
}else{
auto ctx = Gui::SoFCSelectionRoot::getActionContext(action,this,selContext,false);
if(ctx && ctx->removeIndex(index))
touch();
}
break;
} default:
break;
}
return;
}
else if (action->getTypeId() == Gui::SoVRMLAction::getClassTypeId()) {
// update the materialIndex field to match with the number of triangles if needed
SoState * state = action->getState();
Binding mbind = this->findMaterialBinding(state);
if (mbind == PER_PART) {
const SoLazyElement* mat = SoLazyElement::getInstance(state);
int numColor = 1;
int numParts = partIndex.getNum();
if (mat) {
numColor = mat->getNumDiffuse();
if (numColor == numParts) {
int count = 0;
const int32_t * indices = this->partIndex.getValues(0);
for (int i=0; i<numParts; i++) {
count += indices[i];
}
this->materialIndex.setNum(count);
int32_t * matind = this->materialIndex.startEditing();
int32_t k = 0;
for (int i=0; i<numParts; i++) {
for (int j=0; j<indices[i]; j++) {
matind[k++] = i;
}
}
this->materialIndex.finishEditing();
}
}
}
}
// The recommended way to set 'updateVbo' is to reimplement the method 'notify'
// but the base class made this method private so that we can't override it.
// So, the alternative way is to write a custom SoAction class.
else if (action->getTypeId() == Gui::SoUpdateVBOAction::getClassTypeId()) {
for(auto &v : PRIVATE(this)->vbomap) {
v.second.updateVbo = true;
v.second.vboLoaded = false;
}
}
inherited::doAction(action);
}
#ifdef RENDER_GLARRAYS
void SoBrepFaceSet::GLRender(SoGLRenderAction *action)
{
SoState * state = action->getState();
// Disable caching for this node
SoGLCacheContextElement::shouldAutoCache(state, SoGLCacheContextElement::DONT_AUTO_CACHE);
SoMaterialBundle mb(action);
Binding mbind = this->findMaterialBinding(state);
SoTextureCoordinateBundle tb(action, true, false);
SbBool doTextures = tb.needCoordinates();
if (ctx->coordIndex.getNum() < 3)
return;
SelContextPtr ctx2;
SelContextPtr ctx = Gui::SoFCSelectionRoot::getRenderContext<SelContext>(this,selContext,ctx2);
if(ctx2 && ctx2->selectionIndex.empty())
return;
int32_t hl_idx = ctx?ctx->highlightIndex:-1;
int32_t num_selected = ctx?ctx->selectionIndex.size():0;
renderHighlight(action,ctx);
if(ctx && ctx->selectionIndex.size()) {
if(ctx->isSelectAll()) {
if(ctx2 && ctx2->selectionIndex.size()) {
ctx2->selectionColor = ctx->selectionColor;
renderSelection(action,ctx2);
} else
renderSelection(action,ctx);
return;
}
renderSelection(action,ctx);
}
if(ctx2 && ctx2->selectionIndex.size()) {
renderSelection(action,ctx2,false);
}else{
// When setting transparency shouldGLRender() handles the rendering and returns false.
// Therefore generatePrimitives() needs to be re-implemented to handle the materials
// correctly.
if (!this->shouldGLRender(action))
return;
#ifdef RENDER_GLARRAYS
if (!doTextures && index_array.size() && hl_idx < 0 && num_selected <= 0) {
if (mbind == 0) {
mb.sendFirst(); // only one material -> apply it!
renderSimpleArray();
return;
}
else if (mbind == 1) {
renderColoredArray(&mb);
return;
}
}
#endif
Binding nbind = this->findNormalBinding(state);
const SoCoordinateElement * coords;
const SbVec3f * normals;
const int32_t * cindices;
int numindices;
const int32_t * nindices;
const int32_t * tindices;
const int32_t * mindices;
const int32_t * pindices;
int numparts;
SbBool normalCacheUsed;
SbBool sendNormals = !mb.isColorOnly() || tb.isFunction();
this->getVertexData(state, coords, normals, cindices,
nindices, tindices, mindices, numindices,
sendNormals, normalCacheUsed);
mb.sendFirst(); // make sure we have the correct material
// just in case someone forgot
if (!mindices) mindices = cindices;
if (!nindices) nindices = cindices;
pindices = this->partIndex.getValues(0);
numparts = this->partIndex.getNum();
renderShape(state, vboAvailable, static_cast<const SoGLCoordinateElement*>(coords), cindices, numindices,
pindices, numparts, normals, nindices, &mb, mindices, &tb, tindices, nbind, mbind, doTextures?1:0);
if(normalCacheUsed)
this->readUnlockNormalCache();
}
// Workaround for #0000433
//#if !defined(FC_OS_WIN32)
renderHighlight(action,ctx);
renderSelection(action,ctx);
//#endif
}
//****************************************************************************
// renderSimpleArray: normal and coord from vertex_array;
// no texture, color, highlight or selection but highet possible speed;
// all vertices written in one go!
//
void SoBrepFaceSet::renderSimpleArray()
{
int cnt = index_array.size();
if (cnt == 0) return;
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
#if 0
glInterleavedArrays(GL_N3F_V3F, 0, vertex_array.data());
glDrawElements(GL_TRIANGLES, cnt, GL_UNSIGNED_INT, index_array.data());
#else
glInterleavedArrays(GL_N3F_V3F, 0, &(vertex_array[0]));
glDrawElements(GL_TRIANGLES, cnt, GL_UNSIGNED_INT, &(index_array[0]));
#endif
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
//****************************************************************************
// renderColoredArray: normal and coord from vertex_array;
// no texture, highlight or selection but color / material array.
// needs to iterate over parts (i.e. geometry faces)
//
void SoBrepFaceSet::renderColoredArray(SoMaterialBundle *const materials)
{
int num_parts = partIndex.getNum();
int cnt = index_array.size();
if (cnt == 0) return;
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
#if 0
glInterleavedArrays(GL_N3F_V3F, 0, vertex_array.data());
const int32_t* ptr = index_array.data();
#else
glInterleavedArrays(GL_N3F_V3F, 0, &(vertex_array[0]));
const int32_t* ptr = &(index_array[0]);
#endif
for (int part_id = 0; part_id < num_parts; part_id++) {
int tris = partIndex[part_id];
if (tris > 0) {
materials->send(part_id, true);
glDrawElements(GL_TRIANGLES, 3 * tris, GL_UNSIGNED_INT, ptr);
ptr += 3 * tris;
}
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
#else
void SoBrepFaceSet::GLRender(SoGLRenderAction *action)
{
//SoBase::staticDataLock();
static bool init = false;
if (!init) {
std::string ext = (const char*)(glGetString(GL_EXTENSIONS));
PRIVATE(this)->vboAvailable = (ext.find("GL_ARB_vertex_buffer_object") != std::string::npos);
init = true;
}
//SoBase::staticDataUnlock();
if (this->coordIndex.getNum() < 3)
return;
SelContextPtr ctx2;
std::vector<SelContextPtr> ctxs;
SelContextPtr ctx = Gui::SoFCSelectionRoot::getRenderContext(this,selContext,ctx2);
if(ctx2 && ctx2->selectionIndex.empty())
return;
if(selContext2->checkGlobal(ctx))
ctx = selContext2;
if(ctx && (!ctx->selectionIndex.size() && ctx->highlightIndex<0))
ctx.reset();
auto state = action->getState();
selCounter.checkRenderCache(state);
// override material binding to PER_PART_INDEX to achieve
// preselection/selection with transparency
bool pushed = overrideMaterialBinding(action,ctx,ctx2);
if(!pushed){
// for non transparent cases, we still use the old selection rendering
// code, because it can override emission color, which gives a more
// distinguishable selection highlight. The above material binding
// override method can't, because Coin does not support per part
// emission color
// There are a few factors affects the rendering order.
//
// 1) For normal case, the highlight (pre-selection) is the top layer. And since
// the depth buffer clipping is on here, we shall draw highlight first, then
// selection, then the rest part.
//
// 2) If action->isRenderingDelayedPaths() is true, it means we are rendering
// with depth buffer clipping turned off (always on top rendering), so we shall
// draw the top layer last, i.e. renderHighlight() last
//
// 3) If highlightIndex==INT_MAX, it means we are rendering full object highlight
// In order to not obscure selection layer, we shall draw highlight after selection
// if and only if it is not a full object selection.
//
// Transparency complicates stuff even more, but not here. It will be handled inside
// overrideMaterialBinding()
//
if(ctx && ctx->highlightIndex==INT_MAX) {
if(ctx->selectionIndex.empty() || ctx->isSelectAll()) {
if(ctx2) {
ctx2->selectionColor = ctx->highlightColor;
renderSelection(action,ctx2);
} else
renderHighlight(action,ctx);
}else{
if(!action->isRenderingDelayedPaths())
renderSelection(action,ctx);
if(ctx2) {
ctx2->selectionColor = ctx->highlightColor;
renderSelection(action,ctx2);
} else
renderHighlight(action,ctx);
if(action->isRenderingDelayedPaths())
renderSelection(action,ctx);
}
return;
}
if(!action->isRenderingDelayedPaths())
renderHighlight(action,ctx);
if(ctx && ctx->selectionIndex.size()) {
if(ctx->isSelectAll()) {
if(ctx2) {
ctx2->selectionColor = ctx->selectionColor;
renderSelection(action,ctx2);
} else
renderSelection(action,ctx);
if(action->isRenderingDelayedPaths())
renderHighlight(action,ctx);
return;
}
if(!action->isRenderingDelayedPaths())
renderSelection(action,ctx);
}
if(ctx2) {
renderSelection(action,ctx2,false);
if(action->isRenderingDelayedPaths()) {
renderSelection(action,ctx);
renderHighlight(action,ctx);
}
return;
}
}
SoMaterialBundle mb(action);
// It is important to send material before shouldGLRender(), otherwise
// material override with transparncy won't work.
mb.sendFirst();
// When setting transparency shouldGLRender() handles the rendering and returns false.
// Therefore generatePrimitives() needs to be re-implemented to handle the materials
// correctly.
if(this->shouldGLRender(action)) {
Binding mbind = this->findMaterialBinding(state);
Binding nbind = this->findNormalBinding(state);
const SoCoordinateElement * coords;
const SbVec3f * normals;
const int32_t * cindices;
int numindices;
const int32_t * nindices;
const int32_t * tindices;
const int32_t * mindices;
const int32_t * pindices;
int numparts;
SbBool doTextures;
SbBool normalCacheUsed;
SoTextureCoordinateBundle tb(action, true, false);
doTextures = tb.needCoordinates();
SbBool sendNormals = !mb.isColorOnly() || tb.isFunction();
this->getVertexData(state, coords, normals, cindices,
nindices, tindices, mindices, numindices,
sendNormals, normalCacheUsed);
// just in case someone forgot
if (!mindices) mindices = cindices;
if (!nindices) nindices = cindices;
pindices = this->partIndex.getValues(0);
numparts = this->partIndex.getNum();
SbBool hasVBO = !ctx2 && PRIVATE(this)->vboAvailable;
if (hasVBO) {
// get the VBO status of the viewer
Gui::SoGLVBOActivatedElement::get(state, hasVBO);
//
//if (SoGLVBOElement::shouldCreateVBO(state, numindices)) {
// this->startVertexArray(action, coords, normals, false, false);
//}
}
renderShape(action, hasVBO, static_cast<const SoGLCoordinateElement*>(coords), cindices, numindices,
pindices, numparts, normals, nindices, &mb, mindices, &tb, tindices, nbind, mbind, doTextures?1:0);
// if (!hasVBO) {
// // Disable caching for this node
// SoGLCacheContextElement::shouldAutoCache(state, SoGLCacheContextElement::DONT_AUTO_CACHE);
// }else
// SoGLCacheContextElement::setAutoCacheBits(state, SoGLCacheContextElement::DO_AUTO_CACHE);
if (normalCacheUsed)
this->readUnlockNormalCache();
}
if(pushed) {
SbBool notify = enableNotify(FALSE);
materialIndex.setNum(0);
if(notify) enableNotify(notify);
state->pop();
}else if(action->isRenderingDelayedPaths()) {
renderSelection(action,ctx);
renderHighlight(action,ctx);
}
}
#endif
bool SoBrepFaceSet::overrideMaterialBinding(SoGLRenderAction *action, SelContextPtr ctx, SelContextPtr ctx2) {
if(!ctx && !ctx2) return false;
auto state = action->getState();
auto mb = SoMaterialBindingElement::get(state);
auto element = SoLazyElement::getInstance(state);
const SbColor *diffuse = element->getDiffusePointer();
if(!diffuse) return false;
int diffuse_size = element->getNumDiffuse();
const float *trans = element->getTransparencyPointer();
int trans_size = element->getNumTransparencies();
if(!trans || !trans_size) return false;
float trans0=0.0;
bool hasTransparency = false;
for(int i=0;i<trans_size;++i) {
if(trans[i]!=0.0) {
hasTransparency = true;
trans0 = trans[i]>0.5?0.5:trans[i];
break;
}
}
// Override material binding to PER_PART_INDEXED so that we can reuse coin
// rendering for both selection, preselection and partial rendering. The
// main purpose is such that selection and preselection can have correct
// transparency, too.
//
// Criteria of using material binding override:
// 1) original material binding is either overall or per_part. We can
// support others, but omitted here to simplify coding logic, and
// because it seems FC only uses these two.
// 2) either of the following :
// a) has highlight or selection and Selection().needPickPoint, so that
// any preselected/selected part automatically become transparent
// b) has transparency
// c) has color override in secondary context
if((mb==SoMaterialBindingElement::OVERALL ||
(mb==SoMaterialBindingElement::PER_PART && diffuse_size>=partIndex.getNum()))
&&
((ctx && Gui::Selection().needPickedList()) ||
trans0!=0.0 ||
(ctx2 && ctx2->colors.size())))
{
state->push();
packedColors.clear();
if(ctx && Gui::Selection().needPickedList()) {
hasTransparency = true;
if(trans0 < 0.5)
trans0=0.5;
trans_size = 1;
if(ctx2)
ctx2->trans0 = trans0;
}else if(ctx2)
ctx2->trans0 = 0.0;
uint32_t diffuseColor = diffuse[0].getPackedValue(trans0);
int singleColor = 0;
if(ctx && ctx->isHighlightAll()) {
singleColor = 1;
diffuseColor = ctx->highlightColor.getPackedValue(trans0);
}else if(ctx && ctx->isSelectAll()) {
diffuseColor = ctx->selectionColor.getPackedValue(trans0);
singleColor = ctx->isHighlighted()?-1:1;
} else if(ctx2 && ctx2->isSingleColor(diffuseColor,hasTransparency)) {
singleColor = ctx?-1:1;
}
bool partialRender = ctx2 && !ctx2->isSelectAll();
if(singleColor>0 && !partialRender) {
//optimization for single color non-partial rendering
SoMaterialBindingElement::set(state,SoMaterialBindingElement::OVERALL);
SoOverrideElement::setMaterialBindingOverride(state, this, true);
packedColors.push_back(diffuseColor);
SoLazyElement::setPacked(state, this,1, &packedColors[0], hasTransparency);
SoTextureEnabledElement::set(state,this,false);
if(hasTransparency && action->isRenderingDelayedPaths()) {
// rendering delayed paths means we are doing annotation (e.g.
// always on top rendering). To render transparency correctly in
// this case, we shall use openGL transparency blend. Override
// using SoLazyElement::setTransparencyType() doesn't seem to work
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(false);
}
return true;
}
matIndex.clear();
matIndex.reserve(partIndex.getNum());
if(ctx && (ctx->isSelectAll() || ctx->isHighlightAll())) {
matIndex.resize(partIndex.getNum(),0);
if(!partialRender)
packedColors.push_back(diffuseColor);
else {
// default to full transparent
packedColors.push_back(SbColor(1.0,1.0,1.0).getPackedValue(1.0));
packedColors.push_back(diffuseColor);
for(auto idx : ctx2->selectionIndex) {
if(idx>=0 && idx<partIndex.getNum())
matIndex[idx] = packedColors.size()-1; // show only the selected
}
}
if(ctx->highlightIndex>=0 && ctx->highlightIndex<partIndex.getNum()) {
packedColors.push_back(ctx->highlightColor.getPackedValue(trans0));
matIndex[ctx->highlightIndex] = packedColors.size()-1;
}
}else{
if(partialRender) {
packedColors.push_back(SbColor(1.0,1.0,1.0).getPackedValue(1.0));
matIndex.resize(partIndex.getNum(),0);
if(mb == SoMaterialBindingElement::OVERALL || singleColor) {
packedColors.push_back(diffuseColor);
auto cidx = packedColors.size()-1;
for(auto idx : ctx2->selectionIndex) {
if(idx>=0 && idx<partIndex.getNum()) {
if(!singleColor && ctx2->applyColor(idx,packedColors,hasTransparency))
matIndex[idx] = packedColors.size()-1;
else
matIndex[idx] = cidx;
}
}
}else{
assert(diffuse_size >= partIndex.getNum());
for(auto idx : ctx2->selectionIndex) {
if(idx>=0 && idx<partIndex.getNum()) {
if(!ctx2->applyColor(idx,packedColors,hasTransparency)) {
auto t = idx<trans_size?trans[idx]:trans0;
packedColors.push_back(diffuse[idx].getPackedValue(t));
}
matIndex[idx] = packedColors.size()-1;
}
}
}
}else if(mb==SoMaterialBindingElement::OVERALL || singleColor) {
packedColors.push_back(diffuseColor);
matIndex.resize(partIndex.getNum(),0);
if(ctx2 && !singleColor) {
for(auto &v : ctx2->colors) {
int idx = v.first;
if(idx>=0 && idx<partIndex.getNum()) {
packedColors.push_back(ctx2->packColor(v.second,hasTransparency));
matIndex[idx] = packedColors.size()-1;
}
}
}
}else{
assert(diffuse_size >= partIndex.getNum());
packedColors.reserve(diffuse_size+3);
for(int i=0;i<diffuse_size;++i) {
auto t = i<trans_size?trans[i]:trans0;
matIndex.push_back(i);
if(!ctx2 || !ctx2->applyColor(i,packedColors,hasTransparency))
packedColors.push_back(diffuse[i].getPackedValue(t));
}
}
if(ctx && ctx->selectionIndex.size()) {
packedColors.push_back(ctx->selectionColor.getPackedValue(trans0));
for(auto idx : ctx->selectionIndex) {
if(idx>=0 && idx<partIndex.getNum())
matIndex[idx] = packedColors.size()-1;
}
}
if(ctx && ctx->highlightIndex>=0 && ctx->highlightIndex<partIndex.getNum()) {
packedColors.push_back(ctx->highlightColor.getPackedValue(trans0));
matIndex[ctx->highlightIndex] = packedColors.size()-1;
}
}
SbBool notify = enableNotify(FALSE);
materialIndex.setValuesPointer(matIndex.size(),&matIndex[0]);
if(notify) enableNotify(notify);
SoMaterialBindingElement::set(state, this, SoMaterialBindingElement::PER_PART_INDEXED);
SoLazyElement::setPacked(state, this, packedColors.size(), &packedColors[0], hasTransparency);
SoTextureEnabledElement::set(state,this,false);
if(hasTransparency && action->isRenderingDelayedPaths()) {
// rendering delayed paths means we are doing annotation (e.g.
// always on top rendering). To render transparency correctly in
// this case, we shall use openGL transparency blend. Override
// using SoLazyElement::setTransparencyType() doesn't seem to work
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(false);
}
return true;
}
return false;
}
void SoBrepFaceSet::GLRenderBelowPath(SoGLRenderAction * action)
{
inherited::GLRenderBelowPath(action);
}
// this macro actually makes the code below more readable :-)
#define DO_VERTEX(idx) \
if (mbind == PER_VERTEX) { \
pointDetail.setMaterialIndex(matnr); \
vertex.setMaterialIndex(matnr++); \
} \
else if (mbind == PER_VERTEX_INDEXED) { \
pointDetail.setMaterialIndex(*mindices); \
vertex.setMaterialIndex(*mindices++); \
} \
if (nbind == PER_VERTEX) { \
pointDetail.setNormalIndex(normnr); \
currnormal = &normals[normnr++]; \
vertex.setNormal(*currnormal); \
} \
else if (nbind == PER_VERTEX_INDEXED) { \
pointDetail.setNormalIndex(*nindices); \
currnormal = &normals[*nindices++]; \
vertex.setNormal(*currnormal); \
} \
if (tb.isFunction()) { \
vertex.setTextureCoords(tb.get(coords->get3(idx), *currnormal)); \
if (tb.needIndices()) pointDetail.setTextureCoordIndex(tindices ? *tindices++ : texidx++); \
} \
else if (tbind != NONE) { \
pointDetail.setTextureCoordIndex(tindices ? *tindices : texidx); \
vertex.setTextureCoords(tb.get(tindices ? *tindices++ : texidx++)); \
} \
vertex.setPoint(coords->get3(idx)); \
pointDetail.setCoordinateIndex(idx); \
this->shapeVertex(&vertex);
void SoBrepFaceSet::generatePrimitives(SoAction * action)
{
//TODO
#if 0
inherited::generatePrimitives(action);
#else
//This is highly experimental!!!
if (this->coordIndex.getNum() < 3) return;
SoState * state = action->getState();
if (this->vertexProperty.getValue()) {
state->push();
this->vertexProperty.getValue()->doAction(action);
}
Binding mbind = this->findMaterialBinding(state);
Binding nbind = this->findNormalBinding(state);
const SoCoordinateElement * coords;
const SbVec3f * normals;
const int32_t * cindices;
int numindices;
const int32_t * nindices;
const int32_t * tindices;
const int32_t * mindices;
SbBool doTextures;
SbBool sendNormals;
SbBool normalCacheUsed;
sendNormals = true; // always generate normals
this->getVertexData(state, coords, normals, cindices,
nindices, tindices, mindices, numindices,
sendNormals, normalCacheUsed);
SoTextureCoordinateBundle tb(action, false, false);
doTextures = tb.needCoordinates();
if (!sendNormals) nbind = OVERALL;
else if (normalCacheUsed && nbind == PER_VERTEX) {
nbind = PER_VERTEX_INDEXED;
}
else if (normalCacheUsed && nbind == PER_FACE_INDEXED) {
nbind = PER_FACE;
}
if (this->getNodeType() == SoNode::VRML1) {
// For VRML1, PER_VERTEX means per vertex in shape, not PER_VERTEX
// on the state.
if (mbind == PER_VERTEX) {
mbind = PER_VERTEX_INDEXED;
mindices = cindices;
}
if (nbind == PER_VERTEX) {
nbind = PER_VERTEX_INDEXED;
nindices = cindices;
}
}
Binding tbind = NONE;
if (doTextures) {
if (tb.isFunction() && !tb.needIndices()) {
tbind = NONE;
tindices = NULL;
}
// FIXME: just call inherited::areTexCoordsIndexed() instead of
// the if-check? 20020110 mortene.
else if (SoTextureCoordinateBindingElement::get(state) ==
SoTextureCoordinateBindingElement::PER_VERTEX) {
tbind = PER_VERTEX;
tindices = NULL;
}
else {
tbind = PER_VERTEX_INDEXED;
if (tindices == NULL) tindices = cindices;