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.

183 lines
5.9KB

  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. BEGIN_JUCE_NAMESPACE
  19. OpenGLTexture::OpenGLTexture()
  20. : textureID (0), width (0), height (0)
  21. {
  22. }
  23. OpenGLTexture::~OpenGLTexture()
  24. {
  25. release();
  26. }
  27. bool OpenGLTexture::isValidSize (int width, int height)
  28. {
  29. return isPowerOfTwo (width) && isPowerOfTwo (height);
  30. }
  31. void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type)
  32. {
  33. // Texture objects can only be created when the current thread has an active OpenGL
  34. // context. You'll need to make an OpenGLComponent active before calling this.
  35. jassert (OpenGLHelpers::isContextActive());
  36. jassert (isValidSize (w, h)); // Perhaps these dimensions must be a power-of-two?
  37. width = w;
  38. height = h;
  39. if (textureID == 0)
  40. {
  41. glGenTextures (1, &textureID);
  42. glBindTexture (GL_TEXTURE_2D, textureID);
  43. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  44. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  45. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  46. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  47. }
  48. else
  49. {
  50. glBindTexture (GL_TEXTURE_2D, textureID);
  51. }
  52. glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  53. glTexImage2D (GL_TEXTURE_2D, 0, type == GL_ALPHA ? GL_ALPHA : GL_RGBA,
  54. w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
  55. }
  56. template <class PixelType>
  57. struct Flipper
  58. {
  59. static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
  60. const int w, const int h, const int textureW, const int textureH)
  61. {
  62. dataCopy.malloc (textureW * textureH);
  63. for (int y = 0; y < h; ++y)
  64. {
  65. const PixelType* src = (const PixelType*) srcData;
  66. PixelARGB* const dst = (PixelARGB*) (dataCopy + textureW * (textureH - 1 - y));
  67. for (int x = 0; x < w; ++x)
  68. dst[x].set (src[x]);
  69. srcData += lineStride;
  70. }
  71. }
  72. };
  73. void OpenGLTexture::loadImage (const Image& image)
  74. {
  75. const int imageW = image.getWidth();
  76. const int imageH = image.getHeight();
  77. const int textureW = nextPowerOfTwo (imageW);
  78. const int textureH = nextPowerOfTwo (imageH);
  79. HeapBlock<PixelARGB> dataCopy;
  80. Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  81. switch (srcData.pixelFormat)
  82. {
  83. case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH, textureW, textureH); break;
  84. case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH, textureW, textureH); break;
  85. case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH, textureW, textureH); break;
  86. default: break;
  87. }
  88. create (textureW, textureH, dataCopy, GL_BGRA_EXT);
  89. }
  90. void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
  91. {
  92. jassert (isValidSize (w, h));
  93. create (w, h, pixels, GL_BGRA_EXT);
  94. }
  95. void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
  96. {
  97. jassert (isValidSize (w, h));
  98. create (w, h, pixels, GL_ALPHA);
  99. }
  100. void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
  101. {
  102. const int textureW = nextPowerOfTwo (w);
  103. const int textureH = nextPowerOfTwo (h);
  104. HeapBlock<PixelARGB> flippedCopy;
  105. Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h, textureW, textureH);
  106. loadARGB (flippedCopy, textureW, textureH);
  107. }
  108. void OpenGLTexture::release()
  109. {
  110. if (textureID != 0)
  111. {
  112. glDeleteTextures (1, &textureID);
  113. textureID = 0;
  114. width = 0;
  115. height = 0;
  116. }
  117. }
  118. void OpenGLTexture::bind() const
  119. {
  120. glBindTexture (GL_TEXTURE_2D, textureID);
  121. }
  122. void OpenGLTexture::unbind() const
  123. {
  124. glBindTexture (GL_TEXTURE_2D, 0);
  125. }
  126. void OpenGLTexture::draw2D (float x1, float y1,
  127. float x2, float y2,
  128. float x3, float y3,
  129. float x4, float y4,
  130. const Colour& colour) const
  131. {
  132. bind();
  133. OpenGLHelpers::drawQuad2D (x1, y1, x2, y2, x3, y3, x4, y4, colour);
  134. unbind();
  135. }
  136. void OpenGLTexture::draw3D (float x1, float y1, float z1,
  137. float x2, float y2, float z2,
  138. float x3, float y3, float z3,
  139. float x4, float y4, float z4,
  140. const Colour& colour) const
  141. {
  142. bind();
  143. OpenGLHelpers::drawQuad3D (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, colour);
  144. unbind();
  145. }
  146. END_JUCE_NAMESPACE