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.6KB

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