File: stencilManager.cpp

package info (click to toggle)
opencsg 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,980 kB
  • sloc: ansic: 54,939; cpp: 5,252; sh: 1,531; perl: 876; makefile: 595; python: 135
file content (198 lines) | stat: -rw-r--r-- 5,940 bytes parent folder | download
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
// OpenCSG - library for image-based CSG rendering for OpenGL
// Copyright (C) 2002-2022, 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.

//
// stencilManager.cpp
//

#include "opencsgConfig.h"
#include <GL/glew.h>
#ifdef _WIN32
#include <GL/wglew.h>
#elif !defined(__APPLE__)
#include <GL/glxew.h>
#endif
#include "area.h"
#include "openglHelper.h"
#include "stencilManager.h"
#include <vector>

namespace OpenCSG {

    namespace OpenGL {

        StencilManager::StencilManager(const PCArea& area) : mArea(area), mSaved(false) {}
            // Do not save the stencil buffer at all. 
            // This is, currently, used always.

        StencilManager::~StencilManager() { }

        const PCArea& StencilManager::getArea() const {
            return mArea;
        }

        void StencilManager::clear() {
            if (!mSaved) {
                save();
                mSaved = true;
            }
            glStencilMask(OpenGL::stencilMask);
            glClear(GL_STENCIL_BUFFER_BIT);
        }

        bool StencilManager::alreadySaved() const {
            return mSaved;
        }

        void StencilManager::save() {
            // implemented empty. will not save the stencil buffer
        }

        void StencilManager::restore() {
            // implemented empty. will not restore the stencil buffer
        }





        class StencilManagerGL10 : public StencilManager {
        public:
            StencilManagerGL10(const PCArea& area) : StencilManager(area) {};
                // Saves and restores the stencil buffer by copying it from
                // graphics memory to main memory and back.

                // Warning: There appears to be a bug in this implementation.
                //          Saving and restoring does not work at all.

            virtual void save();
            virtual void restore();
        };

        namespace {
            std::vector<GLubyte>* buf = 0;
            int dx, dy;
        }

        void StencilManagerGL10::save() {
            
            const PCArea& area = getArea();
            dx = area.maxx - area.minx;
            dy = area.maxy - area.miny;

            unsigned int size = (8+dx)*dy; // 8 needed due to possible alignment (?)
            if (!buf) {
                buf = new std::vector<GLubyte>(size);
            } else if (buf->size() < size) {
                buf->resize(size);
            }

            glReadPixels(area.minx, area.miny, dx, dy, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &((*buf)[0]));
        }

        void StencilManagerGL10::restore() {

            if (!alreadySaved()) return; // stencil buffer was never changed, restore nothing

            glMatrixMode(GL_PROJECTION);
            glPushMatrix();
            glLoadIdentity();
            glMatrixMode(GL_MODELVIEW);
            glPushMatrix();
            glLoadIdentity();

            glRasterPos2i(-1, -1);
            glDrawPixels(dx, dy, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &((*buf)[0]));

            glMatrixMode(GL_PROJECTION);
            glPopMatrix();
            glMatrixMode(GL_MODELVIEW);
            glPopMatrix();
        }




#ifdef _WIN32
        class StencilManagerARBBufferRegionW32 : public StencilManager {
        public:
            StencilManagerARBBufferRegionW32(const PCArea& area) : StencilManager(area) {};
                // Uses the ARB_buffer_region extension for saving and restoring the 
                // stencil buffer. If this extension is available, this works well
                // and is reasonably fast.

            virtual void save();
            virtual void restore();
        private:
            HDC hdc; 
            HANDLE handle;
        };

        void StencilManagerARBBufferRegionW32::save() {

            const PCArea& area = getArea();
            dx = area.maxx - area.minx;
            dy = area.maxy - area.miny;

            hdc = wglGetCurrentDC();
            handle = wglCreateBufferRegionARB(hdc, 1, WGL_STENCIL_BUFFER_BIT_ARB);
            if (handle == 0) {
                return;
            }
            int result = wglSaveBufferRegionARB(handle, area.minx, area.miny, dx, dy);

        }

        void StencilManagerARBBufferRegionW32::restore() {

            if (!alreadySaved()) return; // stencil buffer was never changed, restore nothing

            const PCArea& area = getArea();
            dx = area.maxx - area.minx;
            dy = area.maxy - area.miny;

            wglRestoreBufferRegionARB(handle, area.minx, area.miny, dx, dy, 0, 0);
            wglDeleteBufferRegionARB(handle);
        }

#endif // _WIN32



        StencilManager* getStencilManager(const PCArea& area) {
#ifdef _WIN32
            /*
            // uncomment for possibility of saving the stencil buffer under windows
            if (WGLEW_ARB_buffer_region) {
                return new StencilManagerARBBufferRegionW32(area);
            }
            */
#endif // _WIN32

            /*
            // this option has a bug unfortunately and is not usable
            return new StencilManagerGL10(area);
            */
            return new StencilManager(area);
        }

    } // namespace OpenGL

} // namespace OpenCSG