opencsg-sys 1.0.0

FFI bindings to OpenCSG (image-based CSG rendering for OpenGL)
Documentation
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
// OpenCSG - library for image-based CSG rendering for OpenGL
// Copyright (C) 2002-2026, Florian Kirsch,
// Hasso-Plattner-Institute at the University of Potsdam, Germany
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA.

//
// renderSCS.cpp
//
// stuff specific for the SCS algorithm
//

#include "opencsgConfig.h"
#include <opencsg.h>
#include "opencsgRender.h"
#include "batch.h"
#include "channelManager.h"
#include "context.h"
#include "occlusionQuery.h"
#include "openglHelper.h"
#include "primitiveHelper.h"
#include "scissorMemo.h"
#include "sequencer.h"
#include "settings.h"

#include <algorithm>
#include <map>
#include <string>

namespace OpenCSG {

    namespace {

        ScissorMemo* scissor;

        struct IdBufferId {
            GLubyte r;
            GLubyte g;
            GLubyte b;
            GLubyte a;
            GLubyte* vec() {
                return &r;
            }
        };

        struct RenderData {
            IdBufferId bufferId;
        };

        std::map<Primitive*, RenderData> gRenderInfo;

        RenderData* getRenderData(Primitive* primitive) {
            RenderData* dta = &(gRenderInfo.find(primitive))->second;
            return dta;
        }

        // Stores Ids in the alpha buffer only -> only 255 primitives are possible
        class SCSChannelManagerAlphaOnly : public ChannelManagerForBatches {
        public:
            virtual void merge();
        };

        void SCSChannelManagerAlphaOnly::merge() {

            ProjTextureSetup setup = FixedFunction;
            setupProjectiveTexture(setup);

            glEnable(GL_ALPHA_TEST);
            glEnable(GL_CULL_FACE);
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LESS);
            glDepthMask(GL_TRUE);
            glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

            std::vector<Channel> channels = occupied();
            for (std::vector<Channel>::const_iterator c = channels.begin(); c!=channels.end(); ++c) {

                setupTexEnv(*c);
                scissor->recall(*c);
                scissor->enableScissor();

                const std::vector<Primitive*> primitives = getPrimitives(*c);
                for (std::vector<Primitive*>::const_iterator j = primitives.begin(); j != primitives.end(); ++j) {
                    glCullFace((*j)->getOperation() == Intersection ? GL_BACK : GL_FRONT);
                    RenderData* primitiveData = getRenderData(*j);
                    GLubyte id = primitiveData->bufferId.a;

                    // Here is an interesting bug, which happened on an ATI HD4670, but actually
                    // might happen on every hardware. I am not sure whether it can be solved 
                    // correctly.

                    // Problem is that in optimized mode with some compilers (VC6, Visual Studio 2003
                    // in particular), when setting the alpha func as follows:
                    // glAlphaFunc(GL_EQUAL, static_cast<float>(id) / 255.0f);
                    // the division is optimized as multiplication with 1.0f/255.0f.
                    // This is a fine and valid optimization. Unfortunately, the results
                    // are not exactly the same as the direct division for some ids.
                    // Which is apparently what the ATI driver is doing internally.
                    // So with comparison with GL_EQUAL fails. 

                    // Fortunately the OpenGL standard enforces that the mapping of color byte
                    // values to float fragment values be done by division. So if the
                    // solution found below (just working at double precision) proves
                    // to work once, it should work forever, such that a precompiling
                    // lookup table containing the correct alpha reference values is 
                    // not required. However a bad feeling remains.

                    // The SCSChannelManagerFragmentProgram path implemented below should fix this.

                    double alpha = static_cast<double>(id) / 255.0;
                    GLfloat fAlpha = static_cast<float>(alpha);
                    glAlphaFunc(GL_EQUAL, fAlpha);

                    (*j)->render();
                }
            }

            scissor->disableScissor();

            glDisable(GL_ALPHA_TEST);
            glDisable(GL_CULL_FACE);
            glDepthFunc(GL_LEQUAL);

            resetProjectiveTexture(setup);

            clear();
        }

        // Stores Ids in the all components of the color buffer
        // -> in theory, 2^32-1 primitives are possible
        // ARB variant
        class SCSChannelManagerARBProgram : public ChannelManagerForBatches {
        public:
            SCSChannelManagerARBProgram(ProjTextureSetup);
            virtual Channel request();
            virtual void merge();

        private:
            ProjTextureSetup mProjTextureSetup;
        };

        SCSChannelManagerARBProgram::SCSChannelManagerARBProgram(ProjTextureSetup setup)
          : mProjTextureSetup(setup)
        {
        }

        Channel SCSChannelManagerARBProgram::request() {
            ChannelManagerForBatches::request();
            mCurrentChannel = AllChannels;
            mOccupiedChannels = mCurrentChannel;
            return mCurrentChannel;
        }

        // Emulates eye texgen
        static const char mergeARBVertexProgram[] =
"!!ARBvp1.0 OPTION ARB_position_invariant;\n"
"ATTRIB  pos = vertex.position;\n"
"ATTRIB  col = vertex.color;\n"
"OUTPUT  outCol = result.color;\n"
"OUTPUT  outTex0 = result.texcoord[0];\n"
"PARAM   mvpmat[4] = { state.matrix.mvp };\n"
"PARAM   texmat[4] = { state.matrix.texture[0] };\n"
"TEMP    eye;\n"
"TEMP    tex;\n"
"DP4     eye.x, mvpmat[0], pos;\n"
"DP4     eye.y, mvpmat[1], pos;\n"
"DP4     eye.z, mvpmat[2], pos;\n"
"DP4     eye.w, mvpmat[3], pos;\n"
"DP4     tex.x, texmat[0], eye;\n"
"DP4     tex.y, texmat[1], eye;\n"
"DP4     tex.z, texmat[2], eye;\n"
"DP4     tex.w, texmat[3], eye;\n"
"MOV     outTex0, tex;\n"
"MOV     outCol, col;\n"
"END";

        // Subtract color from texture value, takes the absolute value
        // and adds all components into each channel of the result, scaled by 2.0f.
        // This way, all 32-bits of the color channel can be used
        // for an 'equal' alpha test, i.e, to check if value in texture
        // and color are equal.

        // Note that 1.0f/255.0f cannot be the result of the above computation.
        // Either the result is 0 (if all components were equal, disregarding
        // numerical errors), or larger/equal than 2.0f/255.0f. The alpha function
        // then is actually set to  GL_LESS, 1.0f/255.0f. This is robust,
        // in contract to the GL_EQUAL in SCSChannelManagerAlphaOnly above.
        // The scaling by 2.0f is required for NVidia hardware, which considers
        // the alpha function GL_LESS, 0.5f/255.0f as GL_LESS, 0.0f for some reason.
        static const char mergeARBFragmentProgramRect[] =
"!!ARBfp1.0\n"
"TEMP    temp;\n"
"ATTRIB  tex0 = fragment.texcoord[0];\n"
"ATTRIB  col0 = fragment.color;\n"
"PARAM   scaleByTwo = { 2.0, 2.0, 2.0, 2.0 };\n"
"OUTPUT  out = result.color;\n"
"TXP     temp, tex0, texture[0], RECT;\n"
"SUB     temp, temp, col0;\n"
"ABS     temp, temp;\n"
"DP4     out, temp, scaleByTwo;\n"
"END";

        static const char mergeARBFragmentProgram2D[] =
"!!ARBfp1.0\n"
"TEMP    temp;\n"
"ATTRIB  tex0 = fragment.texcoord[0];\n"
"ATTRIB  col0 = fragment.color;\n"
"PARAM   scaleByTwo = { 2.0, 2.0, 2.0, 2.0 };\n"
"OUTPUT  out = result.color;\n"
"TXP     temp, tex0, texture[0], 2D;\n"
"SUB     temp, temp, col0;\n"
"ABS     temp, temp;\n"
"DP4     out, temp, scaleByTwo;\n"
"END";

        void SCSChannelManagerARBProgram::merge()
        {
            if (mProjTextureSetup == ARBShader)
            {
                GLuint vId = OpenGL::getARBVertexProgram(mergeARBVertexProgram, (sizeof(mergeARBVertexProgram) / sizeof(mergeARBVertexProgram[0])) - 1);
                glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vId);
                glEnable(GL_VERTEX_PROGRAM_ARB);
            }

            GLuint fId =
                isRectangularTexture()
                  ? OpenGL::getARBFragmentProgram(mergeARBFragmentProgramRect, (sizeof(mergeARBFragmentProgramRect) / sizeof(mergeARBFragmentProgramRect[0])) - 1)
                  : OpenGL::getARBFragmentProgram(mergeARBFragmentProgram2D, (sizeof(mergeARBFragmentProgram2D) / sizeof(mergeARBFragmentProgram2D[0])) - 1);
            glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fId);
            glEnable(GL_FRAGMENT_PROGRAM_ARB);

            setupProjectiveTexture(mProjTextureSetup);

            glEnable(GL_ALPHA_TEST);
            glEnable(GL_CULL_FACE);
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LESS);
            glDepthMask(GL_TRUE);
            glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

            glAlphaFunc(GL_LESS, 1.0f / 255.0f);

            std::vector<Channel> channels;
            channels.push_back(AllChannels);
            for (std::vector<Channel>::const_iterator c = channels.begin(); c!=channels.end(); ++c) {

                scissor->recall(*c);
                scissor->enableScissor();

                const std::vector<Primitive*> primitives = getPrimitives(*c);
                for (std::vector<Primitive*>::const_iterator j = primitives.begin(); j != primitives.end(); ++j) {
                    glCullFace((*j)->getOperation() == Intersection ? GL_BACK : GL_FRONT);
                    RenderData* primitiveData = getRenderData(*j);
                    GLubyte * id = primitiveData->bufferId.vec();
                    glColor4ubv(id);
                    (*j)->render();
                }
            }

            scissor->disableScissor();

            glDisable(GL_ALPHA_TEST);
            glDisable(GL_CULL_FACE);
            glDepthFunc(GL_LEQUAL);
            glDisable(GL_FRAGMENT_PROGRAM_ARB);

            if (mProjTextureSetup == ARBShader)
            {
                glDisable(GL_VERTEX_PROGRAM_ARB);
            }

            resetProjectiveTexture(mProjTextureSetup);

            clear();
        }

        // Stores Ids in the all components of the color buffer
        // -> in theory, 2^32-1 primitives are possible
        // GLSL variant
        class SCSChannelManagerGLSLProgram : public ChannelManagerForBatches {
        public:
            virtual Channel request();
            virtual void merge();
        };

        Channel SCSChannelManagerGLSLProgram::request() {
            ChannelManagerForBatches::request();
            mCurrentChannel = AllChannels;
            mOccupiedChannels = mCurrentChannel;
            return mCurrentChannel;
        }

        // Subtract color from texture value, and if the result is not 0
        // (including some epsilon, which is less than 1.0/(256*256),
        // discards the fragment.
        // This way, all 32-bits of the color channel can be used
        // for an 'equal' alpha test, i.e, to check if value in texture
        // and color are equal.
        static const char mergeFragmentProgramRect[] =
            "#version 110\n"
            "#extension GL_ARB_texture_rectangle : enable\n"
            "uniform sampler2DRect texture0;\n"
            "uniform vec4 color;\n"
            "void main() {\n"
            "    vec4 temp = texture2DRect(texture0, gl_FragCoord.xy);\n"
            "    temp = temp - color;\n"
            "    if (dot(temp, temp) > 0.000001)\n"
            "        discard;\n"
            "    gl_FragColor = color;\n"
            "}\n";

        static const char mergeFragmentProgram2D[] =
            "#version 110\n"
            "uniform sampler2D texture0;\n"
            "uniform vec2 texSizeInv;\n"
            "uniform vec4 color;\n"
            "void main() {\n"
            "    vec2 texCoord = vec2(gl_FragCoord.x * texSizeInv.x, gl_FragCoord.y * texSizeInv.y);\n"
            "    vec4 temp = texture2D(texture0, texCoord);\n"
            "    temp = temp - color;\n"
            "    if (dot(temp, temp) > 0.000001)\n"
            "        discard;\n"
            "    gl_FragColor = color;\n"
            "}\n";

        void SCSChannelManagerGLSLProgram::merge()
        {
            const int SCSIdOffset = 2;
            const char* programID = getVertexShader() + (isRectangularTexture() ? 1 : 0) + SCSIdOffset;
            GLuint glslProgram =
                isRectangularTexture()
                    ? OpenGL::getGLSLProgram(programID, getVertexShader(), mergeFragmentProgramRect)
                    : OpenGL::getGLSLProgram(programID, getVertexShader(), mergeFragmentProgram2D);

            GLint col = glGetUniformLocation(glslProgram, "color");
            GLint texSizeInv = -1;
            if (!isRectangularTexture())
                texSizeInv = glGetUniformLocation(glslProgram, "texSizeInv");

            glUseProgram(glslProgram);

            ProjTextureSetup setup = GLSLProgram;
            setupProjectiveTexture(setup, texSizeInv);

            glDisable(GL_ALPHA_TEST);
            glEnable(GL_CULL_FACE);
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LESS);
            glDepthMask(GL_TRUE);
            glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

            std::vector<Channel> channels;
            channels.push_back(AllChannels);
            for (std::vector<Channel>::const_iterator c = channels.begin(); c!=channels.end(); ++c) {

                scissor->recall(*c);
                scissor->enableScissor();

                const std::vector<Primitive*> primitives = getPrimitives(*c);
                for (std::vector<Primitive*>::const_iterator j = primitives.begin(); j != primitives.end(); ++j) {
                    glCullFace((*j)->getOperation() == Intersection ? GL_BACK : GL_FRONT);
                    RenderData* primitiveData = getRenderData(*j);
                    GLubyte * id = primitiveData->bufferId.vec();
                    glUniform4f(col, static_cast<float>(static_cast<double>(id[0]) / 255.0),
                                     static_cast<float>(static_cast<double>(id[1]) / 255.0),
                                     static_cast<float>(static_cast<double>(id[2]) / 255.0),
                                     static_cast<float>(static_cast<double>(id[3]) / 255.0));
                    (*j)->render();
                }
            }

            scissor->disableScissor();

            glDisable(GL_CULL_FACE);
            glDepthFunc(GL_LEQUAL);
            glUseProgram(0);

            resetProjectiveTexture(setup);

            clear();
        }


        ChannelManagerForBatches* getChannelManager() {

            if (GLAD_GL_VERSION_2_0)
            {
                bool useGLSL = getVertexShader() != 0;
                if (useGLSL)
                    return new SCSChannelManagerGLSLProgram;
            }

            // The ARB vertex program path has the following problem:
            // With Nouveau drivers on Linux and an NVidia GTX 710, the ARB_position_invariant
            // option appears to be buggy sometimes, and this causes z-buffer artifacts.
            // On the other hand, historical Intel drives failed to work for
            // fixed-function vertex setup together with ARB vertex programs.
            if (   OPENCSG_HAS_EXT(ARB_vertex_program)
                && OPENCSG_HAS_EXT(ARB_fragment_program)
            ) {
                std::string vendor;
                if (const char * v = (const char*)glGetString(GL_VENDOR))
                    vendor = v;
                bool isIntel = vendor.find("Intel") == 0;

                ProjTextureSetup setup = isIntel ? ARBShader : FixedFunction;
                return new SCSChannelManagerARBProgram(setup);
            }

            // The fallback path, using fixed function OpenGL, has its own problems:
            // Obviously, it does not support more than 255 primitives. More importantly,
            // due to the alpha test and the GL_EQUAL alpha function used here, due
            // to accuracy problems, the alpha test for some IDs may incorrectly fail.
            // Then, some primitives of the CSG share are completely missing.
            return new SCSChannelManagerAlphaOnly;
        }


        class IDGenerator {
        public:
            IDGenerator() : counter(0) {}
            IdBufferId newID() {
                ++counter;
                IdBufferId newId;
                newId.r =  (counter >> 24) & 0xff;
                newId.g =  (counter >> 16) & 0xff;
                newId.b =  (counter >>  8) & 0xff;
                newId.a =  (counter >>  0) & 0xff;
                return newId;
            }

        private:
            unsigned int counter;
        };

        ChannelManagerForBatches* channelMgr;

        void renderIntersectedFront(const std::vector<Primitive*>& primitives) {

            const std::size_t numberOfPrimitives = primitives.size();

            glDepthMask(GL_TRUE);

            // optimization for only one shape
            if (numberOfPrimitives == 1) {
                channelMgr->renderToChannel(true);
                glDepthFunc(GL_GREATER);
                glCullFace(GL_BACK);
                glEnable(GL_CULL_FACE);
                RenderData * primitiveData = getRenderData(primitives[0]);
                GLubyte * id = primitiveData->bufferId.vec();
                glColor4ubv(id);
                primitives[0]->render();
                glDisable(GL_CULL_FACE);
                glDepthFunc(GL_LESS);

                return;
            }

            // draw furthest front face
            channelMgr->renderToChannel(true);
            glStencilMask(OpenGL::stencilMask);
            glDepthFunc(GL_GREATER);
            glCullFace(GL_BACK);
            glEnable(GL_CULL_FACE);

            {
                for (std::vector<Primitive*>::const_iterator i = primitives.begin(); i != primitives.end(); ++i) {
                    RenderData * primitiveData = getRenderData(*i);
                    GLubyte * id = primitiveData->bufferId.vec();
                    glColor4ubv(id);
                    (*i)->render();
                }
            }

            // count back faces behind furthest front face
            channelMgr->renderToChannel(false);
            glStencilFunc(GL_ALWAYS, 0, OpenGL::stencilMask);
            glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
            glEnable(GL_STENCIL_TEST);
            glDepthMask(GL_FALSE);
            glCullFace(GL_FRONT);

            {
                for (std::vector<Primitive*>::const_iterator i = primitives.begin(); i != primitives.end(); ++i) {
                    (*i)->render();
                }
            }

            // where #back faces behind furthest front face != #intersected shapes
            // ->reset fragment
            channelMgr->renderToChannel(true);
            glStencilFunc(GL_NOTEQUAL, static_cast<GLint>(numberOfPrimitives), OpenGL::stencilMask);
            glDepthFunc(GL_ALWAYS);
            glDepthRange(0.0, 0.0);
            glDepthMask(GL_TRUE);
            glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
            glDisable(GL_CULL_FACE);
            glColor4ub(0, 0, 0, 0);

            OpenGL::drawQuad();

            glDepthRange(0.0, 1.0);
            glDepthFunc(GL_LESS);
            glDisable(GL_STENCIL_TEST);
        }

        void subtractPrimitives(const std::vector<Batch>& batches,
                                const unsigned int depthComplexity = 0) {

            int setting = getOption(CameraOutsideOptimization);
            bool cameraInsideModel = (setting == OptimizationOff);

            glStencilMask(OpenGL::stencilMask);
            glEnable(GL_STENCIL_TEST);
            glEnable(GL_CULL_FACE);

            size_t numberOfBatches = batches.size();
            BouncingSequencer bounce(numberOfBatches);
            SchoenfieldSequencer schoenfield(numberOfBatches);
            Sequencer * sequencer = 0;
            size_t numIterations;
            if (depthComplexity == 0)
            {
                sequencer = &schoenfield;
                numIterations = sequencer->size();
            }
            else
            {
                sequencer = &bounce;
                numIterations = sequencer->sizeForDepthComplexity(depthComplexity);
            }

            unsigned int stencilref = 0;
            for (size_t i = 0; i < numIterations; ++i)
            {
                const Batch& batch = batches[sequencer->index(i)];

                // create a distinct reference value
                ++stencilref;
                if (stencilref == OpenGL::stencilMax) {
                    glClear(GL_STENCIL_BUFFER_BIT);
                    stencilref = 1;
                }

                channelMgr->renderToChannel(false);

                glDepthMask(GL_FALSE );
                glStencilFunc(GL_ALWAYS, stencilref, OpenGL::stencilMask);
                glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

                if (cameraInsideModel)
                {
                    glDepthFunc(GL_GREATER);
                    glCullFace(GL_FRONT);

                    for (Batch::const_iterator j = batch.begin(); j != batch.end(); ++j) {
                        (*j)->render();
                    }

                    glStencilFunc(GL_EQUAL, stencilref, OpenGL::stencilMask);
                    glStencilOp(GL_ZERO, GL_ZERO, GL_KEEP);
                }

                glDepthFunc(GL_LESS);
                glCullFace(GL_BACK);

                {
                    for (Batch::const_iterator j = batch.begin(); j != batch.end(); ++j) {
                        (*j)->render();
                    }
                }

                // where front faces have been visible, render back faces
                channelMgr->renderToChannel(true);
                glDepthFunc(GL_GREATER);
                glDepthMask(GL_TRUE);
                glCullFace(GL_FRONT);
                glStencilFunc(GL_EQUAL, stencilref, OpenGL::stencilMask);
                glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);

                {
                    for (Batch::const_iterator j = batch.begin(); j != batch.end(); ++j) {
                        RenderData * primitiveData = getRenderData(*j);
                        GLubyte * id = primitiveData->bufferId.vec();
                        glColor4ubv(id);
                        (*j)->render();
                    }
                }
            }

            glDisable(GL_STENCIL_TEST);
        }

        bool subtractPrimitivesWithOcclusionQueries(const std::vector<Batch>& batches) {

            OpenGL::OcclusionQuery* occlusionTest = OpenGL::getOcclusionQuery(true);
            if (!occlusionTest) {
                return false;
            }

            const std::size_t numberOfBatches = batches.size();

            int setting = getOption(CameraOutsideOptimization);
            bool cameraInsideModel = (setting == OptimizationOff);

            glStencilMask(OpenGL::stencilMask);
            glEnable(GL_STENCIL_TEST);
            glEnable(GL_CULL_FACE);

            std::vector<unsigned int> fragmentcount(numberOfBatches, 0);
            unsigned int shapesWithoutUpdate = 0;

            SimpleSequencer sequencer(numberOfBatches);
            size_t numIterations = sequencer.size();

            unsigned int stencilref = 0;

            for (size_t i = 0; i < numIterations; ++i)
            {
                size_t idx = sequencer.index(i);
                const Batch& batch = batches[idx];

                // create a distinct reference value
                ++stencilref;
                if (stencilref == OpenGL::stencilMax) {
                    glClear(GL_STENCIL_BUFFER_BIT);
                    stencilref = 1;
                }

                channelMgr->renderToChannel(false);

                glDepthMask(GL_FALSE);
                glStencilFunc(GL_ALWAYS, stencilref, OpenGL::stencilMask);
                glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

                occlusionTest->beginQuery();
                if (cameraInsideModel)
                {
                    glDepthFunc(GL_GREATER);
                    glCullFace(GL_FRONT);

                    {
                        for (Batch::const_iterator j = batch.begin(); j != batch.end(); ++j) {
                            (*j)->render();
                        }
                    }

                    glStencilFunc(GL_EQUAL, stencilref, OpenGL::stencilMask);
                    glStencilOp(GL_ZERO, GL_ZERO, GL_KEEP);
                }

                glDepthFunc(GL_LESS);
                glCullFace(GL_BACK);

                {
                    for (Batch::const_iterator j = batch.begin(); j != batch.end(); ++j) {
                        (*j)->render();
                    }
                }
                occlusionTest->endQuery();
                // the fragment count query could occur here, but benches show that
                // the algorithm is faster if the query is delayed.
                // where front faces have been visible, render back faces
                channelMgr->renderToChannel(true);
                glDepthFunc(GL_GREATER);
                glDepthMask(GL_TRUE);
                glCullFace(GL_FRONT);
                glStencilFunc(GL_EQUAL, stencilref, OpenGL::stencilMask);
                glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);

                {
                    for (Batch::const_iterator j = batch.begin(); j != batch.end(); ++j) {
                        RenderData * primitiveData = getRenderData(*j);
                        GLubyte * id = primitiveData->bufferId.vec();
                        glColor4ubv(id);
                        (*j)->render();
                    }
                }

                unsigned int newFragmentCount = occlusionTest->getQueryResult();
                if (newFragmentCount != fragmentcount[idx]) {
                    fragmentcount[idx] = newFragmentCount;
                    shapesWithoutUpdate = 0;
                } else {
                    ++shapesWithoutUpdate;
                    if (shapesWithoutUpdate >= numberOfBatches)
                        break;
                }
            }

            delete occlusionTest;

            glDisable(GL_STENCIL_TEST);

            return true;
        }

        void renderIntersectedBack(const std::vector<Primitive*>& primitives) {
            // where a back face of intersected shape is in front of any subtracted shape
            // mask fragment as invisible. Updating depth values is not necessary, so when
            // having IDs, this is kind of simple.
            channelMgr->renderToChannel(true);
            glEnable(GL_CULL_FACE);
            glCullFace(GL_FRONT);
            glDepthMask(GL_FALSE);
            glDepthFunc(GL_LESS);
            glColor4ub(0, 0, 0, 0);

            for (std::vector<Primitive*>::const_iterator i = primitives.begin(); i != primitives.end(); ++i) {
                (*i)->render();
            }

            glDepthMask(GL_TRUE);
        }

    } // unnamed namespace

    void renderSCS(const std::vector<Primitive*>& primitives, DepthComplexityAlgorithm algorithm) {

        channelMgr = getChannelManager();
        if (!channelMgr->init())
        {
            delete channelMgr;
            return;
        }

        gRenderInfo.clear();

        scissor = new ScissorMemo;

        std::vector<Primitive*> intersected; intersected.reserve(primitives.size());
        std::vector<Primitive*> subtracted;  subtracted.reserve(primitives.size());

        {
            IDGenerator IDMaker;
            for (std::vector<Primitive*>::const_iterator itr = primitives.begin(); itr != primitives.end(); ++itr) {
                {
                    RenderData dta; 
                    dta.bufferId = IDMaker.newID();
                    gRenderInfo.insert(std::make_pair(*itr, dta));
                }
                Operation operation= (*itr)->getOperation();
                if (operation == Intersection) {
                    intersected.push_back(*itr);
                } else if (operation == Subtraction) {
                    subtracted.push_back(*itr);
                }
            }
        }

        Batcher subtractedBatches(subtracted);

        scissor->setIntersected(intersected);
        scissor->setCurrent(intersected);

        unsigned int depthComplexity = 0;
        if (algorithm == DepthComplexitySampling) {
            scissor->enableScissor();
            glClear(GL_STENCIL_BUFFER_BIT);
            depthComplexity =
                (std::min)(OpenGL::calcMaxDepthComplexity(subtracted, scissor->getCurrentArea()),
                           static_cast<unsigned int>(subtractedBatches.size()));
        }

        channelMgr->request();
        channelMgr->renderToChannel(true);

        scissor->enableScissor();
        scissor->store(channelMgr->current());

        glDepthMask(GL_TRUE);
        glStencilMask(OpenGL::stencilMask);
        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
        glClearDepth(0.0);      // near clipping plane! essential for algorithm!
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glClearDepth(1.0);

        renderIntersectedFront(intersected);
        if (!subtractedBatches.batches().empty())
        {
            scissor->enableDepthBounds();
            switch (algorithm) {
            case OcclusionQuery:
                if (subtractPrimitivesWithOcclusionQueries(subtractedBatches.batches()))
                    break; // success
                // Maybe we just should give up here?
                // fall through
            case NoDepthComplexitySampling:
                subtractPrimitives(subtractedBatches.batches());
                break;
            case DepthComplexitySampling:
                subtractPrimitives(subtractedBatches.batches(), depthComplexity);
                break;
            }
            scissor->disableDepthBounds();
        }
        renderIntersectedBack(intersected);

        scissor->disableScissor();

        channelMgr->store(channelMgr->current(), primitives, 0);
        channelMgr->free();

        delete scissor;
        delete channelMgr;
    }

} // namespace OpenCSG