Dusk Audio Framework
Loading...
Searching...
No Matches
OpenGL.hpp
1/*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any purpose with
6 * or without fee is hereby granted, provided that the above copyright notice and this
7 * permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef DGL_OPENGL_HPP_INCLUDED
18#define DGL_OPENGL_HPP_INCLUDED
19
20#include "ImageBase.hpp"
21#include "ImageBaseWidgets.hpp"
22
23#include "OpenGL-include.hpp"
24
25START_NAMESPACE_DGL
26
27// --------------------------------------------------------------------------------------------------------------------
28
29#ifdef DGL_USE_OPENGL3
30/**
31 OpenGL3 Graphics context.
32
33 This provides access to the program, shaders and uniforms used by the underlying DAF implementation.
34 */
35struct OpenGL3GraphicsContext : GraphicsContext
36{
37 /**
38 The OpenGL3 program used for this context.
39 It is activated automatically before any widget onDisplay() is called.
40 If changing the current OpenGL program make sure to revert back to this one at the end of your pipeline.
41
42 @code
43 // use custom program
44 glUseProgram(context.program);
45
46 // custom stuff here
47
48 // revert back
49 glUseProgram(context.program);
50 @endcode
51 */
52 GLuint program;
53
54 /**
55 A vec4 uniform used to set the next drawing color.
56
57 @code
58 const GLfloat color[4] = { red, green, blue, alpha };
59 glUniform4fv(context.color, 1, color);
60 @endcode
61 */
62 GLuint color;
63
64 /**
65 A vertex shader attribute directly linked to gl_Position.
66 Use this to set the bounds for drawing, normalized as -1.0 to +1.0.
67 The @a width and @a height provide the total window size for convenience.
68
69 @code
70 const GLfloat triangle[] = { x1, y1, x2, y2, x3, y3 };
71 glEnableVertexAttribArray(context.bounds);
72 glVertexAttribPointer(context.bounds, 2, GL_FLOAT, GL_FALSE, 0, triangle);
73 @endcode
74 */
75 GLuint bounds;
76
77 /**
78 A vertex shader attribute directly linked to GL_TEXTURE0 map.
79 // TODO find the correct wording, map??.
80
81 @code
82 const GLfloat map[] = { 0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f, 0.f };
83 glEnableVertexAttribArray(context.textureMap);
84 glVertexAttribPointer(context.textureMap, 2, GL_FLOAT, GL_FALSE, 0, map);
85 @endcode
86 */
87 GLuint textureMap;
88
89 /**
90 A boolean uniform used to indicate if next drawing should @a texture or @a color.
91 Set to 0 for color mode, 1 for texture.
92 Default mode is color, if changed make sure to revert to color mode at the end of your pipeline.
93
94 @code
95 // setup for drawing based on texture
96 glUniform1i(context.usingTexture, 1);
97
98 // bind texture
99 glBindTexture(GL_TEXTURE_2D, myTextureId);
100 // etc..
101
102 // glDrawElements or similar
103
104 // unbind texture
105 glBindTexture(GL_TEXTURE_2D, 0);
106 // etc..
107
108 // revert to color mode
109 glUniform1i(context.usingTexture, 0);
110 @endcode
111 */
112 GLuint usingTexture;
113
114 /**
115 Set of buffers created with glGenBuffers.
116 Used internally in DAF to draw generic shapes, can be reused in custom code.
117 Unbound by default, make sure to leave them unbound at the end of your pipeline.
118 */
119 GLuint buffers[2];
120
121 /**
122 Vertex array object created with glGenVertexArrays, bound for the duration of onDisplay().
123 A core profile has no default vertex array object, so every glVertexAttribPointer and draw call needs a
124 real one bound or it fails with GL_INVALID_OPERATION and the draw is silently dropped. DAF binds this one
125 before calling onDisplay() and rebinds it around its own drawing, because NanoVG binds its own vertex
126 array object and leaves zero bound when it is done.
127 If changing the current vertex array object make sure to revert back to this one at the end of your
128 pipeline.
129 Always 0 when building for GLESv2, which has no vertex array objects.
130
131 @code
132 // use custom vertex array object
133 glBindVertexArray(myVertexArray);
134
135 // custom stuff here
136
137 // revert back
138 glBindVertexArray(context.vao);
139 @endcode
140 */
141 GLuint vao;
142
143 /**
144 Total width of the window used for this context.
145 */
146 uint width;
147
148 /**
149 Total height of the window used for this context.
150 */
151 uint height;
152};
153#endif
154
155// --------------------------------------------------------------------------------------------------------------------
156
157static inline
158ImageFormat asDISTRHOImageFormat(const GLenum format)
159{
160 switch (format)
161 {
162 #if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
163 case GL_RED:
164 #else
165 case GL_LUMINANCE:
166 #endif
167 return kImageFormatGrayscale;
168 #ifndef DGL_USE_GLES
169 case GL_BGR:
170 return kImageFormatBGR;
171 case GL_BGRA:
172 return kImageFormatBGRA;
173 #endif
174 case GL_RGB:
175 return kImageFormatRGB;
176 case GL_RGBA:
177 return kImageFormatRGBA;
178 }
179
180 return kImageFormatNull;
181}
182
183static inline
184GLenum asOpenGLImageFormat(const ImageFormat format)
185{
186 switch (format)
187 {
188 case kImageFormatNull:
189 break;
190 case kImageFormatGrayscale:
191 #if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
192 return GL_RED;
193 #else
194 return GL_LUMINANCE;
195 #endif
196 case kImageFormatBGR:
197 #ifndef DGL_USE_GLES
198 return GL_BGR;
199 #else
200 return 0;
201 #endif
202 case kImageFormatBGRA:
203 #ifndef DGL_USE_GLES
204 return GL_BGRA;
205 #else
206 return 0;
207 #endif
208 case kImageFormatRGB:
209 return GL_RGB;
210 case kImageFormatRGBA:
211 return GL_RGBA;
212 }
213
214 return 0;
215}
216
217// --------------------------------------------------------------------------------------------------------------------
218
219/**
220 OpenGL Image class.
221
222 This is an Image class that handles raw image data in pixels.
223 You can init the image data on the constructor or later on by calling loadFromMemory().
224
225 To generate raw data useful for this class see the utils/png2rgba.py script.
226 Be careful when using a PNG without alpha channel, for those the format is 'GL_BGR'
227 instead of the default 'GL_BGRA'.
228
229 Images are drawn on screen via 2D textures.
230 */
231class OpenGLImage : public ImageBase
232{
233public:
234 /**
235 Constructor for a null Image.
236 */
238
239 /**
240 Constructor using raw image data.
241 @note @a rawData must remain valid for the lifetime of this Image.
242 */
243 OpenGLImage(const char* rawData, uint width, uint height, ImageFormat format = kImageFormatBGRA);
244
245 /**
246 Constructor using raw image data.
247 @note @a rawData must remain valid for the lifetime of this Image.
248 */
249 OpenGLImage(const char* rawData, const Size<uint>& size, ImageFormat format = kImageFormatBGRA);
250
251 /**
252 Constructor using another image data.
253 */
255
256 /**
257 Destructor.
258 */
259 ~OpenGLImage() override;
260
261 /**
262 Load image data from memory.
263 @note @a rawData must remain valid for the lifetime of this Image.
264 */
265 void loadFromMemory(const char* rawData,
266 const Size<uint>& size,
267 ImageFormat format = kImageFormatBGRA) noexcept override;
268
269 /**
270 Draw this image at position @a pos using the graphics context @a context.
271 */
272 void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
273
274 #ifdef DGL_USE_GLES
275 /**
276 Get the image format.
277 */
278 ImageFormat getFormat() const noexcept;
279
280 /**
281 Get the raw image data.
282 */
283 const char* getRawData() const noexcept;
284 #endif
285
286 /**
287 TODO document this.
288 */
289 OpenGLImage& operator=(const OpenGLImage& image) noexcept;
290
291 // FIXME this should not be needed
292 inline void loadFromMemory(const char* rdata, uint w, uint h, ImageFormat fmt = kImageFormatBGRA)
293 { loadFromMemory(rdata, Size<uint>(w, h), fmt); }
294 inline void draw(const GraphicsContext& context)
295 { drawAt(context, Point<int>(0, 0)); }
296 inline void drawAt(const GraphicsContext& context, int x, int y)
297 { drawAt(context, Point<int>(x, y)); }
298
299 #ifdef DGL_ALLOW_DEPRECATED_METHODS
300 /**
301 Constructor using raw image data, specifying an OpenGL image format.
302 @note @a rawData must remain valid for the lifetime of this Image.
303 DEPRECATED This constructor uses OpenGL image format instead of DAF one.
304 */
305 DAF_DEPRECATED_BY("OpenGLImage(const char*, uint, uint, ImageFormat)")
306 explicit OpenGLImage(const char* rawData, uint width, uint height, GLenum glFormat);
307
308 /**
309 Constructor using raw image data, specifying an OpenGL image format.
310 @note @a rawData must remain valid for the lifetime of this Image.
311 DEPRECATED This constructor uses OpenGL image format instead of DAF one.
312 */
313 DAF_DEPRECATED_BY("OpenGLImage(const char*, const Size<uint>&, ImageFormat)")
314 explicit OpenGLImage(const char* rawData, const Size<uint>& size, GLenum glFormat);
315
316 /**
317 Draw this image at (0, 0) point using the current OpenGL context.
318 DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
319 */
320 DAF_DEPRECATED_BY("draw(const GraphicsContext&)")
321 void draw();
322
323 /**
324 Draw this image at (x, y) point using the current OpenGL context.
325 DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
326 */
327 DAF_DEPRECATED_BY("drawAt(const GraphicsContext&, int, int)")
328 void drawAt(int x, int y);
329
330 /**
331 Draw this image at position @a pos using the current OpenGL context.
332 DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
333 */
334 DAF_DEPRECATED_BY("drawAt(const GraphicsContext&, const Point<int>&)")
335 void drawAt(const Point<int>& pos);
336
337 /**
338 Get the image type.
339 DEPRECATED Type is always assumed to be GL_UNSIGNED_BYTE.
340 */
341 DAF_DEPRECATED
342 GLenum getType() const noexcept { return GL_UNSIGNED_BYTE; }
343 #endif // DGL_ALLOW_DEPRECATED_METHODS
344
345private:
346 bool setupCalled;
347 bool textureInit;
348 GLuint textureId;
349 #ifdef DGL_USE_GLES
350 mutable char* convertedData;
351 mutable const char* rawDataLast;
352 #endif
353};
354
355// --------------------------------------------------------------------------------------------------------------------
356
357typedef ImageBaseAboutWindow<OpenGLImage> OpenGLImageAboutWindow;
358typedef ImageBaseButton<OpenGLImage> OpenGLImageButton;
359typedef ImageBaseKnob<OpenGLImage> OpenGLImageKnob;
360typedef ImageBaseSlider<OpenGLImage> OpenGLImageSlider;
361typedef ImageBaseSwitch<OpenGLImage> OpenGLImageSwitch;
362
363// --------------------------------------------------------------------------------------------------------------------
364
365END_NAMESPACE_DGL
366
367#endif // DGL_OPENGL_HPP_INCLUDED
Definition ImageBaseWidgets.hpp:40
Definition ImageBaseWidgets.hpp:86
Definition ImageBaseWidgets.hpp:132
Definition ImageBaseWidgets.hpp:173
Definition ImageBaseWidgets.hpp:225
ImageFormat getFormat() const noexcept
const char * getRawData() const noexcept
DAF_DEPRECATED GLenum getType() const noexcept
Definition OpenGL.hpp:342
void loadFromMemory(const char *rawData, const Size< uint > &size, ImageFormat format=kImageFormatBGRA) noexcept override
OpenGLImage & operator=(const OpenGLImage &image) noexcept
~OpenGLImage() override
OpenGLImage(const OpenGLImage &image)
OpenGLImage(const char *rawData, uint width, uint height, ImageFormat format=kImageFormatBGRA)
void drawAt(const GraphicsContext &context, const Point< int > &pos) override
OpenGLImage(const char *rawData, const Size< uint > &size, ImageFormat format=kImageFormatBGRA)
Definition Geometry.hpp:41
Definition Geometry.hpp:133
Definition Base.hpp:287