DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
10KB

  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. #ifndef DGL_OPENGL_HPP_INCLUDED
  17. #define DGL_OPENGL_HPP_INCLUDED
  18. #include "ImageBase.hpp"
  19. #include "ImageBaseWidgets.hpp"
  20. #include "OpenGL-include.hpp"
  21. START_NAMESPACE_DGL
  22. // --------------------------------------------------------------------------------------------------------------------
  23. #ifdef DGL_USE_OPENGL3
  24. /**
  25. OpenGL3 Graphics context.
  26. This provides access to the program, shaders and uniforms used by the underlying DPF implementation.
  27. */
  28. struct OpenGL3GraphicsContext : GraphicsContext
  29. {
  30. /**
  31. The OpenGL3 program used for this context.
  32. It is activated automatically before any widget onDisplay() is called.
  33. If changing the current OpenGL program make sure to revert back to this one at the end of your pipeline.
  34. @code
  35. // use custom program
  36. glUseProgram(context.program);
  37. // custom stuff here
  38. // revert back
  39. glUseProgram(context.program);
  40. @endcode
  41. */
  42. GLuint program;
  43. /**
  44. A vec4 uniform used to set the next drawing color.
  45. @code
  46. const GLfloat color[4] = { red, green, blue, alpha };
  47. glUniform4fv(context.color, 1, color);
  48. @endcode
  49. */
  50. GLuint color;
  51. /**
  52. A vertex shader attribute directly linked to gl_Position.
  53. Use this to set the bounds for drawing, normalized as -1.0 to +1.0.
  54. The @a width and @a height provide the total window size for convenience.
  55. @code
  56. const GLfloat triangle[] = { x1, y1, x2, y2, x3, y3 };
  57. glEnableVertexAttribArray(context.bounds);
  58. glVertexAttribPointer(context.bounds, 2, GL_FLOAT, GL_FALSE, 0, triangle);
  59. @endcode
  60. */
  61. GLuint bounds;
  62. /**
  63. A vertex shader attribute directly linked to GL_TEXTURE0 map.
  64. // TODO find the correct wording, map??.
  65. @code
  66. const GLfloat map[] = { 0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f, 0.f };
  67. glEnableVertexAttribArray(context.textureMap);
  68. glVertexAttribPointer(context.textureMap, 2, GL_FLOAT, GL_FALSE, 0, map);
  69. @endcode
  70. */
  71. GLuint textureMap;
  72. /**
  73. A boolean uniform used to indicate if next drawing should @a texture or @a color.
  74. Set to 0 for color mode, 1 for texture.
  75. Default mode is color, if changed make sure to revert to color mode at the end of your pipeline.
  76. @code
  77. // setup for drawing based on texture
  78. glUniform1i(context.usingTexture, 1);
  79. // bind texture
  80. glBindTexture(GL_TEXTURE_2D, myTextureId);
  81. // etc..
  82. // glDrawElements or similar
  83. // unbind texture
  84. glBindTexture(GL_TEXTURE_2D, 0);
  85. // etc..
  86. // revert to color mode
  87. glUniform1i(context.usingTexture, 0);
  88. @endcode
  89. */
  90. GLuint usingTexture;
  91. /**
  92. Set of buffers created with glGenBuffers.
  93. Used internally in DPF to draw generic shapes, can be reused in custom code.
  94. Unbound by default, make sure to leave them unbound at the end of your pipeline.
  95. */
  96. GLuint buffers[2];
  97. /**
  98. Total width of the window used for this context.
  99. */
  100. uint width;
  101. /**
  102. Total height of the window used for this context.
  103. */
  104. uint height;
  105. };
  106. #endif
  107. // --------------------------------------------------------------------------------------------------------------------
  108. static inline
  109. ImageFormat asDISTRHOImageFormat(const GLenum format)
  110. {
  111. switch (format)
  112. {
  113. #if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
  114. case GL_RED:
  115. #else
  116. case GL_LUMINANCE:
  117. #endif
  118. return kImageFormatGrayscale;
  119. #ifndef DGL_USE_GLES
  120. case GL_BGR:
  121. return kImageFormatBGR;
  122. case GL_BGRA:
  123. return kImageFormatBGRA;
  124. #endif
  125. case GL_RGB:
  126. return kImageFormatRGB;
  127. case GL_RGBA:
  128. return kImageFormatRGBA;
  129. }
  130. return kImageFormatNull;
  131. }
  132. static inline
  133. GLenum asOpenGLImageFormat(const ImageFormat format)
  134. {
  135. switch (format)
  136. {
  137. case kImageFormatNull:
  138. break;
  139. case kImageFormatGrayscale:
  140. #if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
  141. return GL_RED;
  142. #else
  143. return GL_LUMINANCE;
  144. #endif
  145. case kImageFormatBGR:
  146. #ifndef DGL_USE_GLES
  147. return GL_BGR;
  148. #else
  149. return 0;
  150. #endif
  151. case kImageFormatBGRA:
  152. #ifndef DGL_USE_GLES
  153. return GL_BGRA;
  154. #else
  155. return 0;
  156. #endif
  157. case kImageFormatRGB:
  158. return GL_RGB;
  159. case kImageFormatRGBA:
  160. return GL_RGBA;
  161. }
  162. return 0;
  163. }
  164. // --------------------------------------------------------------------------------------------------------------------
  165. /**
  166. OpenGL Image class.
  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. To generate raw data useful for this class see the utils/png2rgba.py script.
  170. Be careful when using a PNG without alpha channel, for those the format is 'GL_BGR'
  171. instead of the default 'GL_BGRA'.
  172. Images are drawn on screen via 2D textures.
  173. */
  174. class OpenGLImage : public ImageBase
  175. {
  176. public:
  177. /**
  178. Constructor for a null Image.
  179. */
  180. OpenGLImage();
  181. /**
  182. Constructor using raw image data.
  183. @note @a rawData must remain valid for the lifetime of this Image.
  184. */
  185. OpenGLImage(const char* rawData, uint width, uint height, ImageFormat format = kImageFormatBGRA);
  186. /**
  187. Constructor using raw image data.
  188. @note @a rawData must remain valid for the lifetime of this Image.
  189. */
  190. OpenGLImage(const char* rawData, const Size<uint>& size, ImageFormat format = kImageFormatBGRA);
  191. /**
  192. Constructor using another image data.
  193. */
  194. OpenGLImage(const OpenGLImage& image);
  195. /**
  196. Destructor.
  197. */
  198. ~OpenGLImage() override;
  199. /**
  200. Load image data from memory.
  201. @note @a rawData must remain valid for the lifetime of this Image.
  202. */
  203. void loadFromMemory(const char* rawData,
  204. const Size<uint>& size,
  205. ImageFormat format = kImageFormatBGRA) noexcept override;
  206. /**
  207. Draw this image at position @a pos using the graphics context @a context.
  208. */
  209. void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
  210. #ifdef DGL_USE_GLES
  211. /**
  212. Get the image format.
  213. */
  214. ImageFormat getFormat() const noexcept;
  215. /**
  216. Get the raw image data.
  217. */
  218. const char* getRawData() const noexcept;
  219. #endif
  220. /**
  221. TODO document this.
  222. */
  223. OpenGLImage& operator=(const OpenGLImage& image) noexcept;
  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. #if DGL_ALLOW_DEPRECATED_METHODS
  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. 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*, const Size<uint>&, ImageFormat)")
  245. explicit OpenGLImage(const char* rawData, const Size<uint>& size, GLenum glFormat);
  246. /**
  247. Draw this image at (0, 0) point using the current OpenGL context.
  248. DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
  249. */
  250. DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)")
  251. void draw();
  252. /**
  253. Draw this image at (x, y) point using the current OpenGL context.
  254. DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
  255. */
  256. DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, int, int)")
  257. void drawAt(int x, int y);
  258. /**
  259. Draw this image at position @a pos using the current OpenGL context.
  260. DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL.
  261. */
  262. DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&, const Point<int>&)")
  263. void drawAt(const Point<int>& pos);
  264. /**
  265. Get the image type.
  266. DEPRECATED Type is always assumed to be GL_UNSIGNED_BYTE.
  267. */
  268. DISTRHO_DEPRECATED
  269. GLenum getType() const noexcept { return GL_UNSIGNED_BYTE; }
  270. #endif // DGL_ALLOW_DEPRECATED_METHODS
  271. private:
  272. bool setupCalled;
  273. bool textureInit;
  274. GLuint textureId;
  275. #ifdef DGL_USE_GLES
  276. mutable char* convertedData;
  277. mutable const char* rawDataLast;
  278. #endif
  279. };
  280. // --------------------------------------------------------------------------------------------------------------------
  281. typedef ImageBaseAboutWindow<OpenGLImage> OpenGLImageAboutWindow;
  282. typedef ImageBaseButton<OpenGLImage> OpenGLImageButton;
  283. typedef ImageBaseKnob<OpenGLImage> OpenGLImageKnob;
  284. typedef ImageBaseSlider<OpenGLImage> OpenGLImageSlider;
  285. typedef ImageBaseSwitch<OpenGLImage> OpenGLImageSwitch;
  286. // --------------------------------------------------------------------------------------------------------------------
  287. END_NAMESPACE_DGL
  288. #endif // DGL_OPENGL_HPP_INCLUDED