DISTRHO Plugin Framework
OpenGL.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2021 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 // -----------------------------------------------------------------------
24 // Fix OpenGL includes for Windows, based on glfw code (part 1)
25 
26 #undef DGL_CALLBACK_DEFINED
27 #undef DGL_WINGDIAPI_DEFINED
28 
29 #ifdef DISTRHO_OS_WINDOWS
30 
31 #ifndef APIENTRY
32 # define APIENTRY __stdcall
33 #endif // APIENTRY
34 
35 /* We need WINGDIAPI defined */
36 #ifndef WINGDIAPI
37 # if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__POCC__)
38 # define WINGDIAPI __declspec(dllimport)
39 # elif defined(__LCC__)
40 # define WINGDIAPI __stdcall
41 # else
42 # define WINGDIAPI extern
43 # endif
44 # define DGL_WINGDIAPI_DEFINED
45 #endif // WINGDIAPI
46 
47 /* Some <GL/glu.h> files also need CALLBACK defined */
48 #ifndef CALLBACK
49 # if defined(_MSC_VER)
50 # if (defined(_M_MRX000) || defined(_M_IX86) || defined(_M_ALPHA) || defined(_M_PPC)) && !defined(MIDL_PASS)
51 # define CALLBACK __stdcall
52 # else
53 # define CALLBACK
54 # endif
55 # else
56 # define CALLBACK __stdcall
57 # endif
58 # define DGL_CALLBACK_DEFINED
59 #endif // CALLBACK
60 
61 /* Most GL/glu.h variants on Windows need wchar_t */
62 #include <cstddef>
63 
64 #endif // DISTRHO_OS_WINDOWS
65 
66 // -----------------------------------------------------------------------
67 // OpenGL includes
68 
69 #ifdef DISTRHO_OS_MAC
70 # ifdef DGL_USE_OPENGL3
71 # include <OpenGL/gl3.h>
72 # include <OpenGL/gl3ext.h>
73 # else
74 # include <OpenGL/gl.h>
75 # endif
76 #else
77 # ifndef DISTRHO_OS_WINDOWS
78 # define GL_GLEXT_PROTOTYPES
79 # endif
80 # ifndef __GLEW_H__
81 # include <GL/gl.h>
82 # include <GL/glext.h>
83 # endif
84 #endif
85 
86 // -----------------------------------------------------------------------
87 // Missing OpenGL defines
88 
89 #if defined(GL_BGR_EXT) && !defined(GL_BGR)
90 # define GL_BGR GL_BGR_EXT
91 #endif
92 
93 #if defined(GL_BGRA_EXT) && !defined(GL_BGRA)
94 # define GL_BGRA GL_BGRA_EXT
95 #endif
96 
97 #ifndef GL_CLAMP_TO_BORDER
98 # define GL_CLAMP_TO_BORDER 0x812D
99 #endif
100 
101 // -----------------------------------------------------------------------
102 // Fix OpenGL includes for Windows, based on glfw code (part 2)
103 
104 #ifdef DGL_CALLBACK_DEFINED
105 # undef CALLBACK
106 # undef DGL_CALLBACK_DEFINED
107 #endif
108 
109 #ifdef DGL_WINGDIAPI_DEFINED
110 # undef WINGDIAPI
111 # undef DGL_WINGDIAPI_DEFINED
112 #endif
113 
114 START_NAMESPACE_DGL
115 
116 // -----------------------------------------------------------------------
117 
118 /**
119  OpenGL Graphics context.
120  */
122 {
123 };
124 
125 // -----------------------------------------------------------------------
126 
127 static inline
128 ImageFormat asDISTRHOImageFormat(const GLenum format)
129 {
130  switch (format)
131  {
132  case GL_LUMINANCE:
133  return kImageFormatGrayscale;
134  case GL_BGR:
135  return kImageFormatBGR;
136  case GL_BGRA:
137  return kImageFormatBGRA;
138  case GL_RGB:
139  return kImageFormatRGB;
140  case GL_RGBA:
141  return kImageFormatRGBA;
142  }
143 
144  return kImageFormatNull;
145 }
146 
147 static inline
148 GLenum asOpenGLImageFormat(const ImageFormat format)
149 {
150  switch (format)
151  {
152  case kImageFormatNull:
153  break;
154  case kImageFormatGrayscale:
155  return GL_LUMINANCE;
156  case kImageFormatBGR:
157  return GL_BGR;
158  case kImageFormatBGRA:
159  return GL_BGRA;
160  case kImageFormatRGB:
161  return GL_RGB;
162  case kImageFormatRGBA:
163  return GL_RGBA;
164  }
165 
166  return 0x0;
167 }
168 
169 // -----------------------------------------------------------------------
170 
171 /**
172  OpenGL Image class.
173 
174  This is an Image class that handles raw image data in pixels.
175  You can init the image data on the contructor or later on by calling loadFromMemory().
176 
177  To generate raw data useful for this class see the utils/png2rgba.py script.
178  Be careful when using a PNG without alpha channel, for those the format is 'GL_BGR'
179  instead of the default 'GL_BGRA'.
180 
181  Images are drawn on screen via 2D textures.
182  */
183 class OpenGLImage : public ImageBase
184 {
185 public:
186  /**
187  Constructor for a null Image.
188  */
189  OpenGLImage();
190 
191  /**
192  Constructor using raw image data.
193  @note @a rawData must remain valid for the lifetime of this Image.
194  */
195  OpenGLImage(const char* rawData, uint width, uint height, ImageFormat format = kImageFormatBGRA);
196 
197  /**
198  Constructor using raw image data.
199  @note @a rawData must remain valid for the lifetime of this Image.
200  */
201  OpenGLImage(const char* rawData, const Size<uint>& size, ImageFormat format = kImageFormatBGRA);
202 
203  /**
204  Constructor using another image data.
205  */
206  OpenGLImage(const OpenGLImage& image);
207 
208  /**
209  Destructor.
210  */
211  ~OpenGLImage() override;
212 
213  /**
214  Load image data from memory.
215  @note @a rawData must remain valid for the lifetime of this Image.
216  */
217  void loadFromMemory(const char* rawData,
218  const Size<uint>& size,
219  ImageFormat format = kImageFormatBGRA) noexcept override;
220 
221  /**
222  Draw this image at position @a pos using the graphics context @a context.
223  */
224  void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
225 
226  /**
227  TODO document this.
228  */
229  OpenGLImage& operator=(const OpenGLImage& image) noexcept;
230 
231  // FIXME this should not be needed
232  inline void loadFromMemory(const char* rdata, uint w, uint h, ImageFormat fmt = kImageFormatBGRA)
233  { loadFromMemory(rdata, Size<uint>(w, h), fmt); };
234  inline void draw(const GraphicsContext& context)
235  { drawAt(context, Point<int>(0, 0)); };
236  inline void drawAt(const GraphicsContext& context, int x, int y)
237  { drawAt(context, Point<int>(x, y)); };
238 
239  /**
240  Constructor using raw image data, specifying an OpenGL image format.
241  @note @a rawData must remain valid for the lifetime of this Image.
242  DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one.
243  */
244  DISTRHO_DEPRECATED_BY("OpenGLImage(const char*, uint, uint, ImageFormat)")
245  explicit OpenGLImage(const char* rawData, uint width, uint height, GLenum glFormat);
246 
247  /**
248  Constructor using raw image data, specifying an OpenGL image format.
249  @note @a rawData must remain valid for the lifetime of this Image.
250  DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one.
251  */
252  DISTRHO_DEPRECATED_BY("OpenGLImage(const char*, const Size<uint>&, ImageFormat)")
253  explicit OpenGLImage(const char* rawData, const Size<uint>& size, GLenum glFormat);
254 
255  /**
256  Draw this image at (0, 0) point using the current OpenGL context.
257  DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
258  */
259  DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)")
260  void draw();
261 
262  /**
263  Draw this image at (x, y) point using the current OpenGL context.
264  DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
265  */
266  DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, int, int)")
267  void drawAt(int x, int y);
268 
269  /**
270  Draw this image at position @a pos using the current OpenGL context.
271  DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
272  */
273  DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, const Point<int>&)")
274  void drawAt(const Point<int>& pos);
275 
276  /**
277  Get the image type.
278  DEPRECATED Type is always assumed to be GL_UNSIGNED_BYTE.
279  */
280  DISTRHO_DEPRECATED
281  GLenum getType() const noexcept { return GL_UNSIGNED_BYTE; }
282 
283 private:
284  GLuint textureId;
285  bool setupCalled;
286 };
287 
288 // -----------------------------------------------------------------------
289 
295 
296 // -----------------------------------------------------------------------
297 
298 END_NAMESPACE_DGL
299 
300 #endif
ImageBaseButton
Definition: ImageBaseWidgets.hpp:84
ImageBaseKnob
Definition: ImageBaseWidgets.hpp:130
GraphicsContext
Definition: Base.hpp:154
ImageBaseSlider
Definition: ImageBaseWidgets.hpp:171
OpenGLImage::operator=
OpenGLImage & operator=(const OpenGLImage &image) noexcept
OpenGLImage::loadFromMemory
void loadFromMemory(const char *rawData, const Size< uint > &size, ImageFormat format=kImageFormatBGRA) noexcept override
Size< uint >
OpenGLImage::~OpenGLImage
~OpenGLImage() override
OpenGLImage::OpenGLImage
OpenGLImage()
ImageBaseAboutWindow
Definition: ImageBaseWidgets.hpp:39
ImageBase
Definition: ImageBase.hpp:44
OpenGLGraphicsContext
Definition: OpenGL.hpp:121
OpenGLImage::draw
void draw()
Point< int >
OpenGLImage::drawAt
void drawAt(const GraphicsContext &context, const Point< int > &pos) override
OpenGLImage::getType
DISTRHO_DEPRECATED GLenum getType() const noexcept
Definition: OpenGL.hpp:281
ImageBaseSwitch
Definition: ImageBaseWidgets.hpp:222
OpenGLImage
Definition: OpenGL.hpp:183