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