The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

195 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. OpenGLTexture::OpenGLTexture()
  19. : textureID (0), width (0), height (0), ownerContext (nullptr)
  20. {
  21. }
  22. OpenGLTexture::~OpenGLTexture()
  23. {
  24. release();
  25. }
  26. bool OpenGLTexture::isValidSize (int width, int height)
  27. {
  28. return isPowerOfTwo (width) && isPowerOfTwo (height);
  29. }
  30. void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
  31. {
  32. ownerContext = OpenGLContext::getCurrentContext();
  33. // Texture objects can only be created when the current thread has an active OpenGL
  34. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  35. jassert (ownerContext != nullptr);
  36. if (textureID == 0)
  37. {
  38. JUCE_CHECK_OPENGL_ERROR
  39. glGenTextures (1, &textureID);
  40. glBindTexture (GL_TEXTURE_2D, textureID);
  41. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  42. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  43. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  44. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  45. JUCE_CHECK_OPENGL_ERROR
  46. }
  47. else
  48. {
  49. glBindTexture (GL_TEXTURE_2D, textureID);
  50. JUCE_CHECK_OPENGL_ERROR;
  51. }
  52. glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  53. JUCE_CHECK_OPENGL_ERROR
  54. width = nextPowerOfTwo (w);
  55. height = nextPowerOfTwo (h);
  56. const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
  57. if (width != w || height != h)
  58. {
  59. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  60. width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
  61. glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
  62. type, GL_UNSIGNED_BYTE, pixels);
  63. }
  64. else
  65. {
  66. glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
  67. w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
  68. }
  69. JUCE_CHECK_OPENGL_ERROR
  70. }
  71. template <class PixelType>
  72. struct Flipper
  73. {
  74. static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
  75. const int w, const int h)
  76. {
  77. dataCopy.malloc ((size_t) (w * h));
  78. for (int y = 0; y < h; ++y)
  79. {
  80. const PixelType* src = (const PixelType*) srcData;
  81. PixelARGB* const dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
  82. for (int x = 0; x < w; ++x)
  83. dst[x].set (src[x]);
  84. srcData += lineStride;
  85. }
  86. }
  87. };
  88. void OpenGLTexture::loadImage (const Image& image)
  89. {
  90. const int imageW = image.getWidth();
  91. const int imageH = image.getHeight();
  92. HeapBlock<PixelARGB> dataCopy;
  93. Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  94. switch (srcData.pixelFormat)
  95. {
  96. case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  97. case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  98. case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
  99. default: break;
  100. }
  101. create (imageW, imageH, dataCopy, JUCE_RGBA_FORMAT, true);
  102. }
  103. void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
  104. {
  105. create (w, h, pixels, JUCE_RGBA_FORMAT, false);
  106. }
  107. void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
  108. {
  109. create (w, h, pixels, GL_ALPHA, false);
  110. }
  111. void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
  112. {
  113. HeapBlock<PixelARGB> flippedCopy;
  114. Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h);
  115. create (w, h, flippedCopy, JUCE_RGBA_FORMAT, true);
  116. }
  117. void OpenGLTexture::release()
  118. {
  119. if (textureID != 0
  120. && ownerContext == OpenGLContext::getCurrentContext())
  121. {
  122. glDeleteTextures (1, &textureID);
  123. textureID = 0;
  124. width = 0;
  125. height = 0;
  126. }
  127. }
  128. void OpenGLTexture::bind() const
  129. {
  130. glBindTexture (GL_TEXTURE_2D, textureID);
  131. }
  132. void OpenGLTexture::unbind() const
  133. {
  134. glBindTexture (GL_TEXTURE_2D, 0);
  135. }
  136. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  137. void OpenGLTexture::draw2D (float x1, float y1,
  138. float x2, float y2,
  139. float x3, float y3,
  140. float x4, float y4,
  141. const Colour& colour) const
  142. {
  143. bind();
  144. OpenGLHelpers::drawQuad2D (x1, y1, x2, y2, x3, y3, x4, y4, colour);
  145. unbind();
  146. }
  147. void OpenGLTexture::draw3D (float x1, float y1, float z1,
  148. float x2, float y2, float z2,
  149. float x3, float y3, float z3,
  150. float x4, float y4, float z4,
  151. const Colour& colour) const
  152. {
  153. bind();
  154. OpenGLHelpers::drawQuad3D (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, colour);
  155. unbind();
  156. }
  157. #endif