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.

251 lines
6.4KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2019 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. // -----------------------------------------------------------------------
  20. // Fix OpenGL includes for Windows, based on glfw code (part 1)
  21. #undef DGL_CALLBACK_DEFINED
  22. #undef DGL_WINGDIAPI_DEFINED
  23. #ifdef DISTRHO_OS_WINDOWS
  24. #ifndef APIENTRY
  25. # define APIENTRY __stdcall
  26. #endif // APIENTRY
  27. /* We need WINGDIAPI defined */
  28. #ifndef WINGDIAPI
  29. # if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__POCC__)
  30. # define WINGDIAPI __declspec(dllimport)
  31. # elif defined(__LCC__)
  32. # define WINGDIAPI __stdcall
  33. # else
  34. # define WINGDIAPI extern
  35. # endif
  36. # define DGL_WINGDIAPI_DEFINED
  37. #endif // WINGDIAPI
  38. /* Some <GL/glu.h> files also need CALLBACK defined */
  39. #ifndef CALLBACK
  40. # if defined(_MSC_VER)
  41. # if (defined(_M_MRX000) || defined(_M_IX86) || defined(_M_ALPHA) || defined(_M_PPC)) && !defined(MIDL_PASS)
  42. # define CALLBACK __stdcall
  43. # else
  44. # define CALLBACK
  45. # endif
  46. # else
  47. # define CALLBACK __stdcall
  48. # endif
  49. # define DGL_CALLBACK_DEFINED
  50. #endif // CALLBACK
  51. /* Most GL/glu.h variants on Windows need wchar_t */
  52. #include <cstddef>
  53. #endif // DISTRHO_OS_WINDOWS
  54. // -----------------------------------------------------------------------
  55. // OpenGL includes
  56. #ifdef DISTRHO_OS_MAC
  57. # include <OpenGL/gl.h>
  58. #else
  59. # ifndef DISTRHO_OS_WINDOWS
  60. # define GL_GLEXT_PROTOTYPES
  61. # endif
  62. # include <GL/gl.h>
  63. # include <GL/glext.h>
  64. #endif
  65. // -----------------------------------------------------------------------
  66. // Missing OpenGL defines
  67. #if defined(GL_BGR_EXT) && !defined(GL_BGR)
  68. # define GL_BGR GL_BGR_EXT
  69. #endif
  70. #if defined(GL_BGRA_EXT) && !defined(GL_BGRA)
  71. # define GL_BGRA GL_BGRA_EXT
  72. #endif
  73. #ifndef GL_CLAMP_TO_BORDER
  74. # define GL_CLAMP_TO_BORDER 0x812D
  75. #endif
  76. // -----------------------------------------------------------------------
  77. // Fix OpenGL includes for Windows, based on glfw code (part 2)
  78. #ifdef DGL_CALLBACK_DEFINED
  79. # undef CALLBACK
  80. # undef DGL_CALLBACK_DEFINED
  81. #endif
  82. #ifdef DGL_WINGDIAPI_DEFINED
  83. # undef WINGDIAPI
  84. # undef DGL_WINGDIAPI_DEFINED
  85. #endif
  86. START_NAMESPACE_DGL
  87. // -----------------------------------------------------------------------
  88. /**
  89. OpenGL Graphics context.
  90. */
  91. struct OpenGLGraphicsContext : GraphicsContext
  92. {
  93. };
  94. // -----------------------------------------------------------------------
  95. /**
  96. OpenGL Image class.
  97. This is an Image class that handles raw image data in pixels.
  98. You can init the image data on the contructor or later on by calling loadFromMemory().
  99. To generate raw data useful for this class see the utils/png2rgba.py script.
  100. Be careful when using a PNG without alpha channel, for those the format is 'GL_BGR'
  101. instead of the default 'GL_BGRA'.
  102. Images are drawn on screen via 2D textures.
  103. */
  104. class OpenGLImage : public ImageBase
  105. {
  106. public:
  107. /**
  108. Constructor for a null Image.
  109. */
  110. OpenGLImage();
  111. /**
  112. Constructor using raw image data.
  113. @note @a rawData must remain valid for the lifetime of this Image.
  114. */
  115. OpenGLImage(const char* const rawData,
  116. const uint width,
  117. const uint height,
  118. const GLenum format = GL_BGRA,
  119. const GLenum type = GL_UNSIGNED_BYTE);
  120. /**
  121. Constructor using raw image data.
  122. @note @a rawData must remain valid for the lifetime of this Image.
  123. */
  124. OpenGLImage(const char* const rawData,
  125. const Size<uint>& size,
  126. const GLenum format = GL_BGRA,
  127. const GLenum type = GL_UNSIGNED_BYTE);
  128. /**
  129. Constructor using another image data.
  130. */
  131. OpenGLImage(const OpenGLImage& image);
  132. /**
  133. Destructor.
  134. */
  135. ~OpenGLImage() override;
  136. /**
  137. Load image data from memory.
  138. @note @a rawData must remain valid for the lifetime of this Image.
  139. */
  140. void loadFromMemory(const char* const rawData,
  141. const uint width,
  142. const uint height,
  143. const GLenum format = GL_BGRA,
  144. const GLenum type = GL_UNSIGNED_BYTE) noexcept;
  145. /**
  146. Load image data from memory.
  147. @note @a rawData must remain valid for the lifetime of this Image.
  148. */
  149. void loadFromMemory(const char* const rawData,
  150. const Size<uint>& size,
  151. const GLenum format = GL_BGRA,
  152. const GLenum type = GL_UNSIGNED_BYTE) noexcept;
  153. /**
  154. TODO document this.
  155. */
  156. void setup();
  157. /**
  158. TODO document this.
  159. */
  160. void cleanup();
  161. /**
  162. Get the image format.
  163. */
  164. GLenum getFormat() const noexcept;
  165. /**
  166. Get the image type.
  167. */
  168. GLenum getType() const noexcept;
  169. /**
  170. Draw this image at position @a pos using the graphics context @a context.
  171. */
  172. void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
  173. /**
  174. TODO document this.
  175. */
  176. OpenGLImage& operator=(const OpenGLImage& image) noexcept;
  177. /**
  178. Draw this image at (0, 0) point using the current OpenGL context.
  179. */
  180. // TODO mark as deprecated
  181. void draw();
  182. /**
  183. Draw this image at (x, y) point using the current OpenGL context.
  184. */
  185. // TODO mark as deprecated
  186. void drawAt(const int x, const int y);
  187. /**
  188. Draw this image at position @a pos using the current OpenGL context.
  189. */
  190. // TODO mark as deprecated
  191. void drawAt(const Point<int>& pos);
  192. protected:
  193. /** @internal */
  194. // void _drawAt(const Point<int>& pos) override;
  195. private:
  196. GLenum fFormat;
  197. GLenum fType;
  198. GLuint fTextureId;
  199. bool setupCalled;
  200. };
  201. // -----------------------------------------------------------------------
  202. END_NAMESPACE_DGL
  203. #endif