forked from cinder/Cinder
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscoped.h
399 lines (328 loc) · 13.1 KB
/
scoped.h
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
/*
Copyright (c) 2015, The Cinder Project, All rights reserved.
This code is intended for use with the Cinder C++ library: http://libcinder.org
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.
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.
*/
#pragma once
#include "cinder/gl/platform.h"
#include "cinder/gl/wrapper.h"
#include "cinder/gl/Texture.h"
#include "cinder/Noncopyable.h"
namespace cinder { namespace gl {
class Context;
typedef std::shared_ptr<class Vao> VaoRef;
typedef std::shared_ptr<class BufferObj> BufferObjRef;
typedef std::shared_ptr<class Fbo> FboRef;
typedef std::shared_ptr<class Renderbuffer> RenderbufferRef;
#if defined( CINDER_GL_HAS_SAMPLERS )
class Sampler;
typedef std::shared_ptr<Sampler> SamplerRef;
#endif
struct CI_API ScopedVao : private Noncopyable {
ScopedVao( Vao *vao );
ScopedVao( VaoRef &vao );
~ScopedVao();
private:
Context *mCtx;
};
struct CI_API ScopedBuffer : public Noncopyable {
ScopedBuffer( const BufferObjRef &bufferObj );
ScopedBuffer( GLenum target, GLuint id );
~ScopedBuffer();
private:
Context *mCtx;
GLenum mTarget;
};
struct CI_API ScopedState : private Noncopyable {
ScopedState( GLenum cap, GLboolean value );
~ScopedState();
private:
Context *mCtx;
GLenum mCap;
};
struct CI_API ScopedColor : private Noncopyable {
ScopedColor();
ScopedColor( const ColorAf &color );
ScopedColor( float red, float green, float blue, float alpha = 1 );
~ScopedColor();
private:
Context *mCtx;
ColorAf mColor;
};
//! Controls the current blend mode for the current scope.
struct CI_API ScopedBlend : private Noncopyable {
//! Enables or disables blending (`GL_BLEND`) state.
ScopedBlend( GLboolean enable );
//! Parallels glBlendFunc(), and implicitly enables blending
ScopedBlend( GLenum sfactor, GLenum dfactor );
//! Parallels glBlendFuncSeparate(), and implicitly enables blending
ScopedBlend( GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha );
~ScopedBlend();
private:
Context *mCtx;
bool mSaveFactors; // whether we should also set the blend factors rather than just the blend state
};
//! Enables blending state for the current scope and sets the blending function for standard alpha blending (`sfactor = GL_SRC_ALPHA, dfactor = GL_ONE_MINUS_SRC_ALPHA`).
struct CI_API ScopedBlendAlpha : private ScopedBlend {
ScopedBlendAlpha()
: ScopedBlend( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )
{}
};
//! Enables blending state for the current scope and sets the blending function for premultiplied alpha blending (`sfactor = GL_ONE, dfactor = GL_ONE_MINUS_SRC_ALPHA`).
struct CI_API ScopedBlendPremult : private ScopedBlend {
ScopedBlendPremult()
: ScopedBlend( GL_ONE, GL_ONE_MINUS_SRC_ALPHA )
{}
};
//! Enables blending state for the current scope and sets the blending function additive blending (`sfactor = GL_SRC_ALPHA, dfactor = GL_ONE`).
struct CI_API ScopedBlendAdditive : public ScopedBlend
{
ScopedBlendAdditive()
: ScopedBlend( GL_SRC_ALPHA, GL_ONE )
{}
};
struct CI_API ScopedGlslProg : private Noncopyable {
ScopedGlslProg( GlslProgRef &prog );
ScopedGlslProg( const std::shared_ptr<const GlslProg> &prog );
ScopedGlslProg( const GlslProg *prog );
~ScopedGlslProg();
private:
Context *mCtx;
};
struct CI_API ScopedFramebuffer : private Noncopyable {
ScopedFramebuffer( const FboRef &fbo, GLenum target = GL_FRAMEBUFFER );
//! Prefer the FboRef variant when possible. This does not allow gl::Fbo to mark itself as needing multisample resolution.
ScopedFramebuffer( GLenum target, GLuint framebufferId );
~ScopedFramebuffer();
private:
Context *mCtx;
GLenum mTarget;
};
struct CI_API ScopedActiveTexture : private Noncopyable {
//! Sets the currently active texture through glActiveTexture. Expects values relative to \c 0, \em not GL_TEXTURE0
ScopedActiveTexture( uint8_t textureUnit );
~ScopedActiveTexture();
private:
Context *mCtx;
};
struct CI_API ScopedTextureBind : private Noncopyable {
ScopedTextureBind( GLenum target, GLuint textureId );
ScopedTextureBind( GLenum target, GLuint textureId, uint8_t textureUnit );
ScopedTextureBind( const TextureBaseRef &texture );
ScopedTextureBind( const TextureBaseRef &texture, uint8_t textureUnit );
//! \cond
// These overloads are to alleviate a VS2013 bug where it cannot deduce
// the correct constructor when a TextureBaseRef subclass is passed in
ScopedTextureBind( const Texture2dRef &texture, uint8_t textureUnit );
#if ! defined( CINDER_GL_ES )
ScopedTextureBind( const Texture1dRef &texture, uint8_t textureUnit );
#endif // ! defined( CINDER_GL_ES )
#if ! defined( CINDER_GL_ES_2 )
ScopedTextureBind( const Texture3dRef &texture, uint8_t textureUnit );
#endif // ! defined( CINDER_GL_ES_2 )
ScopedTextureBind( const TextureCubeMapRef &texture, uint8_t textureUnit );
//! \endcond
~ScopedTextureBind();
private:
Context *mCtx;
GLenum mTarget;
uint8_t mTextureUnit;
};
#if defined( CINDER_GL_HAS_SAMPLERS )
struct CI_API ScopedSamplerBind : private Noncopyable {
ScopedSamplerBind( GLuint samplerId, uint8_t textureUnit = 0 );
ScopedSamplerBind( const Sampler &sampler, uint8_t textureUnit = 0 );
ScopedSamplerBind( const SamplerRef &sampler, uint8_t textureUnit = 0 );
~ScopedSamplerBind();
private:
Context *mCtx;
uint8_t mTextureUnit;
};
#endif // defined( CINDER_GL_HAS_SAMPLERS )
struct CI_API ScopedScissor : private Noncopyable {
//! Implicitly enables scissor test
ScopedScissor( const ivec2 &lowerLeftPosition, const ivec2 &dimension );
//! Implicitly enables scissor test
ScopedScissor( int lowerLeftX, int lowerLeftY, int width, int height );
~ScopedScissor();
private:
Context *mCtx;
};
struct CI_API ScopedViewport : private Noncopyable {
ScopedViewport( const ivec2 &lowerLeftPosition, const ivec2 &dimension );
ScopedViewport( const ivec2 &size );
ScopedViewport( int lowerLeftX, int lowerLeftY, int width, int height );
~ScopedViewport();
private:
Context *mCtx;
};
struct CI_API ScopedModelMatrix : private Noncopyable {
ScopedModelMatrix() { gl::pushModelMatrix(); }
explicit ScopedModelMatrix( const mat4 &m )
{
gl::pushModelMatrix();
gl::setModelMatrix( m );
}
~ScopedModelMatrix() { gl::popModelMatrix(); }
};
struct CI_API ScopedViewMatrix : private Noncopyable {
ScopedViewMatrix() { gl::pushViewMatrix(); }
explicit ScopedViewMatrix( const mat4 &m )
{
gl::pushViewMatrix();
gl::setViewMatrix( m );
}
~ScopedViewMatrix() { gl::popViewMatrix(); }
};
struct CI_API ScopedProjectionMatrix : private Noncopyable {
ScopedProjectionMatrix() { gl::pushProjectionMatrix(); }
explicit ScopedProjectionMatrix( const mat4 &m )
{
gl::pushProjectionMatrix();
gl::setProjectionMatrix( m );
}
~ScopedProjectionMatrix() { gl::popProjectionMatrix(); }
};
//! Preserves all matrices
struct CI_API ScopedMatrices : private Noncopyable {
ScopedMatrices() { gl::pushMatrices(); }
explicit ScopedMatrices( const Camera &cam )
{
gl::pushMatrices();
gl::setMatrices( cam );
}
~ScopedMatrices() { gl::popMatrices(); }
};
//! Scopes state of face culling.
struct CI_API ScopedFaceCulling : private Noncopyable {
//! Enables or disables polygon culling based on \a cull
ScopedFaceCulling( bool cull );
//! Enables or disables polygon culling based on \a cull and specifies a mode, either \c GL_BACK or GL_FRONT
ScopedFaceCulling( bool cull, GLenum cullFace );
~ScopedFaceCulling();
private:
Context *mCtx;
bool mSaveFace;
};
#if ! defined( CINDER_GL_ES )
//! Scopes state of logic op.
struct CI_API ScopedLogicOp : private Noncopyable {
//! Enables or disables logical operation based on \a enable
ScopedLogicOp( bool enable );
//! Enables or disables logical operation based on \a enable and specifies a mode, \c GL_CLEAR, \c GL_SET, \c GL_COPY, \c GL_COPY_INVERTED, \c GL_NOOP, \c GL_INVERT, \c GL_AND, \c GL_NAND, \c GL_OR, \c GL_NOR, \c GL_XOR, \c GL_EQUIV, \c GL_AND_REVERSE, \c GL_AND_INVERTED, \c GL_OR_REVERSE, or \c GL_OR_INVERTED.
ScopedLogicOp( bool enable, GLenum mode );
~ScopedLogicOp();
private:
Context *mCtx;
bool mSaveMode;
};
#endif
//! Scopes state of depth testing and writing
struct CI_API ScopedDepth : private Noncopyable {
//! Enables or disables both depth testing and writing to the depth buffer
ScopedDepth( bool enableReadAndWrite );
//! Enables or disables depth comparisons and writing to the depth buffer, and specifies a depth comparison function, either \c GL_NEVER, \c GL_LESS, \c GL_EQUAL, \c GL_LEQUAL, \c GL_GREATER, \c GL_NOTEQUAL, \c GL_GEQUAL and \c GL_ALWAYS.
ScopedDepth( bool enableReadAndWrite, GLenum depthFunc );
~ScopedDepth();
private:
Context *mCtx;
bool mSaveMask;
bool mSaveFunc;
};
//! Scopes state to control the depth testing / reading operation. See information on c\ GL_DEPTH_TEST.
struct CI_API ScopedDepthTest : private Noncopyable {
//! Enables or disables the depth testing / reading operation for the scope of this object.
ScopedDepthTest( bool enableTest );
//! Enables or disables the depth test operation, which controls reading and writing to the depth buffer, for the scope of this object. Also specifies a depth comparison function, either \c GL_NEVER, \c GL_LESS, \c GL_EQUAL, \c GL_LEQUAL, \c GL_GREATER, \c GL_NOTEQUAL, \c GL_GEQUAL and \c GL_ALWAYS (see info on `glDepthFunc`).
ScopedDepthTest( bool enableTest, GLenum depthFunc );
//! Destructor returns state to how it was before this object was constructed.
~ScopedDepthTest();
private:
Context *mCtx;
bool mSaveFunc;
};
//! Scopes state to control whether successful depth tests write to the depth buffer. See information on `glDepthMask()`. \note You must enable depth testing / reading (`GL_DEPTH_TEST`) for this to take place.
struct CI_API ScopedDepthWrite : private Noncopyable {
//! Enables or disables writing to the depth buffer for the scope of this object.
ScopedDepthWrite( bool enableWrite );
//! Destructor returns state to how it was before this object was constructed.
~ScopedDepthWrite();
private:
Context *mCtx;
};
//! Scopes state of Renderbuffer binding
struct CI_API ScopedRenderbuffer : private Noncopyable {
ScopedRenderbuffer( const RenderbufferRef &renderBuffer );
ScopedRenderbuffer( GLenum target, GLuint id );
~ScopedRenderbuffer();
private:
Context *mCtx;
};
//! Scopes state of line width
struct CI_API ScopedLineWidth : private Noncopyable {
ScopedLineWidth( float width );
~ScopedLineWidth();
private:
Context *mCtx;
};
#if ! defined( CINDER_GL_ES )
//! Scopes polygon rasterization mode for \c GL_FRONT_AND_BACK
struct CI_API ScopedPolygonMode : private Noncopyable {
//! Values for \a mode may be \c GL_POINT, \c GL_LINE or \c GL_FILL.
ScopedPolygonMode( GLenum mode );
~ScopedPolygonMode();
private:
Context *mCtx;
};
#endif // ! defined( CINDER_GL_ES )
//! Scopes winding order defining front-facing polygons
struct CI_API ScopedFrontFace : private Noncopyable {
//! Values for \a mode may be \c GL_CW or \c GL_CCW
ScopedFrontFace( GLenum mode );
~ScopedFrontFace();
private:
Context *mCtx;
};
//! Scopes writing of frame buffer color components
class ScopedColorMask : private Noncopyable {
public:
//! Values for \a red, \a green, \a blue and \a alpha may be \c GL_TRUE or \c GL_FALSE
ScopedColorMask( GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha );
~ScopedColorMask();
private:
Context *mCtx;
};
//! Scopes the front and back writing of individual bits in the stencil planes (front and back)
class ScopedStencilMask : private Noncopyable {
public:
//! Values for \a mask may be between \c 0x00 and \c 0xFF
ScopedStencilMask( GLuint mask );
//! Values for \a front and \a back may be between \c 0x00 and \c 0xFF
ScopedStencilMask( GLuint front, GLuint back );
~ScopedStencilMask();
private:
Context *mCtx;
};
#if defined( CINDER_GL_HAS_KHR_DEBUG )
//! Scopes debug group message
struct CI_API ScopedDebugGroup : private Noncopyable {
ScopedDebugGroup( const std::string &message );
ScopedDebugGroup( GLuint id, const std::string &message );
ScopedDebugGroup( GLenum source, GLuint id, const std::string &message );
~ScopedDebugGroup();
};
#endif
} } // namespace cinder::gl