VVISF & VVGL
GLScene.hpp
1 #ifndef VVGL_GLScene_hpp
2 #define VVGL_GLScene_hpp
3 
4 #include "VVGL_Defines.hpp"
5 
6 #include <functional>
7 #include <mutex>
8 #include <map>
9 #include "GLBufferPool.hpp"
10 #include "GLCachedProperty.hpp"
11 
12 #if defined(VVGL_SDK_MAC)
13 #import <TargetConditionals.h>
14 #endif
15 
16 
17 
18 
19 namespace VVGL {
20 
21 
22 using namespace std;
23 
24 
25 
26 
28 
33 class VVGL_EXPORT GLScene {
34  // objects/structs/data types
35  public:
40  using RenderCallback = std::function<void(const GLScene & inScene)>;
47  using RenderPrepCallback = std::function<void(const GLScene & inScene, const bool & inSceneReshaped, const bool & inPgmChanged)>;
48 
50  struct RenderTarget {
52  GLBufferRef fbo = nullptr;
54  GLBufferRef color = nullptr;
56  GLBufferRef depth = nullptr;
57  public:
58  RenderTarget(){};
60  RenderTarget(const GLBufferRef &f, const GLBufferRef &c, const GLBufferRef &d) { fbo=f;color=c;depth=d; };
61 
63  inline uint32_t fboName() const { return (fbo==nullptr) ? 0 : fbo->name; };
65  inline uint32_t colorName() const { return (color==nullptr) ? 0 : color->name; };
67  inline uint32_t colorTarget() const { return (color==nullptr) ? GL_TEXTURE_2D : color->desc.target; };
69  inline uint32_t depthName() const { return (depth==nullptr) ? 0 : depth->name; };
71  inline uint32_t depthTarget() const { return (depth==nullptr) ? GL_TEXTURE_2D : depth->desc.target; };
72  };
73 
74 
75  // instance variables
76  protected:
77  recursive_mutex _renderLock;
78  GLContextRef _context = nullptr;
79 
80  bool _deleted = false;
81  bool _initialized = false;
82  bool _needsReshape = true;
83  bool _alwaysNeedsReshape = false;
84 
85  // every time the scene is doing its render prep, this lambda is executed. drawing setup code goes here.
86  RenderPrepCallback _renderPrepCallback = nullptr;
87  // this callback gets hit immediately before the program is linked (after the shaders have been compiled & attached to the program, but before the program was linked).
88  RenderCallback _renderPreLinkCallback = nullptr;
89  // every time the scene renders, this lambda is executed. drawing code goes here.
90  RenderCallback _renderCallback = nullptr;
91  RenderCallback _renderCleanupCallback = nullptr;
92  // the render target contains the GL framebuffer and relevant attachments (render to texture/buffer/depth buffer/etc)
93  RenderTarget _renderTarget;
94 
95  // these vars pertain to optional default orthogonal sizing, which i use a lot for compositing/drawing 2D UIs with GL- if 'orthoUniId' is >= 0 then the vertex shader will create an orthogonal projection matrix of size 'orthoSize' and incorporating 'orthoFlipped'
96  Size _orthoSize = { 0., 0. };
97  bool _orthoFlipped = false;
98  GLCachedUni _orthoUni = GLCachedUni("vvglOrthoProj");
99 
100  // these vars pertain to whether or not the scene clears the attached framebuffer before drawing (and what color is used to clear)
101  bool _performClear = true;
102  GLColor _clearColor = GLColor(0., 0., 0., 0.);
103  bool _clearColorUpdated = false;
104 
105  // these vars pertain to the program being used by the GL context
106  string *_vsString = nullptr;
107  string *_gsString = nullptr;
108  string *_fsString = nullptr;
109  bool _vsStringUpdated = false;
110  bool _gsStringUpdated = false;
111  bool _fsStringUpdated = false;
112  uint32_t _program = 0; // 0, or the compiled program
113  uint32_t _vs = 0;
114  uint32_t _gs = 0;
115  uint32_t _fs = 0;
116  mutex _errLock;
117  mutex _errDictLock;
118  map<string,string> _errDict;
119 
120  // this class- and subclasses of it- often need to create GPU resources. by default the global buffer pool (GetGlobalBufferPool()) will be used- unless this var is non-null...
121  GLBufferPoolRef _privatePool = nullptr; // by default this is null and the scene will try to use the global buffer pool to create interim resources (temp/persistent buffers). if non-null, the scene will use this pool to create interim resources.
122  GLTexToTexCopierRef _privateCopier = nullptr; // by default this is null and the scene will try to use the global buffer copier to create interim resources. if non-null, the scene will use this copier to create interim resources.
123 
124 
125  // functions
126  public:
128  GLScene();
130  GLScene(const GLContextRef & inCtx);
131 
132  virtual ~GLScene();
133 
134  virtual void prepareToBeDeleted();
135 
140 
143  virtual GLBufferRef createAndRenderABuffer(const Size & inSize=Size(640.,480.), const GLBufferPoolRef & inPool=nullptr);
145  virtual void renderToBuffer(const GLBufferRef & inBuffer);
147  virtual void render(const RenderTarget & inRenderTarget=RenderTarget());
149  virtual void renderBlackFrame(const RenderTarget & inRenderTarget=RenderTarget());
151  virtual void renderOpaqueBlackFrame(const RenderTarget & inRenderTarget=RenderTarget());
153  virtual void renderRedFrame(const RenderTarget & inRenderTarget=RenderTarget());
154 
156 
157 
159  inline GLContextRef context() const { return _context; }
160 
161 
166 
169  void setRenderPreLinkCallback(const RenderCallback & n);
171  void setRenderPrepCallback(const RenderPrepCallback & n);
173  void setRenderCallback(const RenderCallback & n);
175  void setRenderCleanupCallback(const RenderCallback & n);
176 
178 
179 
184 
187  void setAlwaysNeedsReshape(const bool & n);
189  virtual void setOrthoSize(const Size & n);
191  Size orthoSize() const;
193  void setOrthoFlipped(const bool & n);
195  bool orthoFlipped() const;
197 
198 
203 
206  void setClearColor(const GLColor & n);
208  void setClearColor(const float & r, const float & g, const float & b, const float & a);
210  void setClearColor(float * n);
212  void setPerformClear(const bool & n);
213 
215 
216 
217  // getters/setters for program stuff
222 
225  virtual void setVertexShaderString(const string & n);
227  virtual string vertexShaderString();
229  virtual void setGeometryShaderString(const string & n);
231  virtual string geometryShaderString();
233  virtual void setFragmentShaderString(const string & n);
235  virtual string fragmentShaderString();
237  inline uint32_t program() const { return _program; }
238 
240 
241 
243  inline GLVersion glVersion() const { if (_context==nullptr) return GLVersion_Unknown; return _context->version; }
244 
246  void setPrivatePool(const GLBufferPoolRef & n) { _privatePool=n; }
248  GLBufferPoolRef privatePool() const { return _privatePool; }
250  void setPrivateCopier(const GLTexToTexCopierRef & n) { _privateCopier=n; }
252  GLTexToTexCopierRef privateCopier() const { return _privateCopier; }
253 
254  protected:
255  // assumed that _renderLock was obtained before calling. assumed that context is non-null and has been set as current GL context before calling.
256  virtual void _renderPrep();
257  // assumed that _renderLock was obtained before calling. assumed that context is non-null and has been set as current GL context before calling.
258  virtual void _initialize();
259  // assumed that _renderLock was obtained before calling. assumed that context is non-null and has been set as current GL context before calling.
260  virtual void _reshape();
261  // assumed that _renderLock was obtained before calling. assumed that context is non-null and has been set as current GL context before calling.
262  virtual void _renderCleanup();
263 };
264 
265 
266 
267 
272 inline GLSceneRef CreateGLSceneRef() { return make_shared<GLScene>(); }
277 inline GLSceneRef CreateGLSceneRefUsing(const GLContextRef & inCtx) { return make_shared<GLScene>(inCtx); }
278 
279 
280 
281 
282 }
283 
284 
285 #endif /* VVGL_GLScene_hpp */
This struct describes an RGBA color.
Definition: VVGL_Base.hpp:116
shared_ptr< GLScene > GLSceneRef
A GLSceneRef is a shared pointer around a GLScene.
Definition: VVGL_Base.hpp:65
Definition: GLBuffer.hpp:13
GLContextRef context() const
Returns the context used by this scene.
Definition: GLScene.hpp:159
GLSceneRef CreateGLSceneRefUsing(const GLContextRef &inCtx)
Creates and returns a GLScene. The scene uses the passed GL context to do its drawing.
Definition: GLScene.hpp:277
STL namespace.
Basic struct for 2D size.
Definition: VVGL_Geom.hpp:46
std::function< void(const GLScene &inScene, const bool &inSceneReshaped, const bool &inPgmChanged)> RenderPrepCallback
This defines the interface for the render prep callback, which is executed prior to the render callba...
Definition: GLScene.hpp:47
GLSceneRef CreateGLSceneRef()
Creates and returns a GLScene. The scene makes a new GL context which shares the context of the globa...
Definition: GLScene.hpp:272
std::function< void(const GLScene &inScene)> RenderCallback
This defines the interface for a function/lambda that is used for encapsulating user-provided drawing...
Definition: GLScene.hpp:40
void setPrivateCopier(const GLTexToTexCopierRef &n)
Sets the receiver&#39;s private buffer copier (which should default to null). If non-null, this copier will be used to copy any resources that need to be copied- like setPrivatePool(), handy if you have a variety of GL contexts that aren&#39;t shared and you have to switch between them rapidly on a per-frame basis.
Definition: GLScene.hpp:250
Manages drawing in a GLContext, provides a simple interface for orthographic rendering, render-to-texture operations, and loading vert/geo/frag shaders.
Definition: GLScene.hpp:33
RenderTarget(const GLBufferRef &f, const GLBufferRef &c, const GLBufferRef &d)
Preferred constructor, populates all three attachments at once.
Definition: GLScene.hpp:60
uint32_t colorName() const
Returns the name of the buffer/texture to be used as the color attachment (returns 0 if there&#39;s no co...
Definition: GLScene.hpp:65
uint32_t program() const
Gets the program ID.
Definition: GLScene.hpp:237
uint32_t fboName() const
Returns the name of the FBO (or 0 if there&#39;s no FBO)
Definition: GLScene.hpp:63
GLTexToTexCopierRef privateCopier() const
Gets the receiver&#39;s private buffer copier- null by default, only non-null if something called setPriv...
Definition: GLScene.hpp:252
RenderTarget member vars are used to provide attachments for the GL framebuffer. These buffers need t...
Definition: GLScene.hpp:50
GLVersion
This enum is used to describe the GL environment of a GL context, which is determined at runtime...
Definition: VVGL_Base.hpp:94
void setPrivatePool(const GLBufferPoolRef &n)
Sets the receiver&#39;s private buffer pool (default is null). If non-null, this buffer pool will be used...
Definition: GLScene.hpp:246
GLBufferPoolRef privatePool() const
Gets the receiver&#39;s private buffer pool- null by default, only non-null if something called setPrivat...
Definition: GLScene.hpp:248
uint32_t depthTarget() const
Returns the texture target of the buffer/texture to be used as the depth attachment.
Definition: GLScene.hpp:71
GLVersion glVersion() const
Returns the version of OpenGL currently being used by this scene&#39;s GL context.
Definition: GLScene.hpp:243
uint32_t depthName() const
Returns the name of the buffer/texture to be used as the depth attachment (returns 0 if there&#39;s no de...
Definition: GLScene.hpp:69
uint32_t colorTarget() const
Returns the texture target of the buffer/texture to be used as the color attachment.
Definition: GLScene.hpp:67