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.

884 lines
32KB

  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. //==============================================================================
  20. void OpenGLHelpers::resetErrorState()
  21. {
  22. while (glGetError() != GL_NO_ERROR) {}
  23. }
  24. void OpenGLHelpers::clear (const Colour& colour)
  25. {
  26. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  27. colour.getFloatBlue(), colour.getFloatAlpha());
  28. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  29. }
  30. void OpenGLHelpers::setColour (const Colour& colour)
  31. {
  32. glColor4f (colour.getFloatRed(), colour.getFloatGreen(),
  33. colour.getFloatBlue(), colour.getFloatAlpha());
  34. }
  35. void OpenGLHelpers::prepareFor2D (const int width, const int height)
  36. {
  37. glMatrixMode (GL_PROJECTION);
  38. glLoadIdentity();
  39. #if JUCE_OPENGL_ES
  40. glOrthof (0.0f, (float) width, 0.0f, (float) height, 0.0f, 1.0f);
  41. #else
  42. glOrtho (0.0, width, 0.0, height, 0, 1);
  43. #endif
  44. glViewport (0, 0, width, height);
  45. }
  46. void OpenGLHelpers::setPerspective (double fovy, double aspect, double zNear, double zFar)
  47. {
  48. glLoadIdentity();
  49. #if JUCE_OPENGL_ES
  50. const float ymax = (float) (zNear * tan (fovy * double_Pi / 360.0));
  51. const float ymin = -ymax;
  52. glFrustumf (ymin * (float) aspect, ymax * (float) aspect, ymin, ymax, (float) zNear, (float) zFar);
  53. #else
  54. const double ymax = zNear * tan (fovy * double_Pi / 360.0);
  55. const double ymin = -ymax;
  56. glFrustum (ymin * aspect, ymax * aspect, ymin, ymax, zNear, zFar);
  57. #endif
  58. }
  59. void OpenGLHelpers::applyTransform (const AffineTransform& t)
  60. {
  61. const GLfloat m[] = { t.mat00, t.mat10, 0, 0,
  62. t.mat01, t.mat11, 0, 0,
  63. 0, 0, 1, 0,
  64. t.mat02, t.mat12, 0, 1 };
  65. glMultMatrixf (m);
  66. }
  67. void OpenGLHelpers::drawQuad2D (float x1, float y1,
  68. float x2, float y2,
  69. float x3, float y3,
  70. float x4, float y4,
  71. const Colour& colour)
  72. {
  73. const GLfloat vertices[] = { x1, y1, x2, y2, x4, y4, x3, y3 };
  74. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  75. setColour (colour);
  76. glEnableClientState (GL_VERTEX_ARRAY);
  77. glVertexPointer (2, GL_FLOAT, 0, vertices);
  78. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  79. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  80. glDisableClientState (GL_COLOR_ARRAY);
  81. glDisableClientState (GL_NORMAL_ARRAY);
  82. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  83. }
  84. void OpenGLHelpers::drawQuad3D (float x1, float y1, float z1,
  85. float x2, float y2, float z2,
  86. float x3, float y3, float z3,
  87. float x4, float y4, float z4,
  88. const Colour& colour)
  89. {
  90. const GLfloat vertices[] = { x1, y1, z1, x2, y2, z2, x4, y4, z4, x3, y3, z3 };
  91. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  92. setColour (colour);
  93. glEnableClientState (GL_VERTEX_ARRAY);
  94. glVertexPointer (3, GL_FLOAT, 0, vertices);
  95. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  96. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  97. glDisableClientState (GL_COLOR_ARRAY);
  98. glDisableClientState (GL_NORMAL_ARRAY);
  99. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  100. }
  101. //==============================================================================
  102. namespace OpenGLGradientHelpers
  103. {
  104. void drawTriangles (GLenum mode, const GLfloat* vertices, const GLfloat* textureCoords, const int numElements)
  105. {
  106. glEnable (GL_BLEND);
  107. glEnable (GL_TEXTURE_2D);
  108. glEnableClientState (GL_VERTEX_ARRAY);
  109. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  110. glDisableClientState (GL_COLOR_ARRAY);
  111. glDisableClientState (GL_NORMAL_ARRAY);
  112. glVertexPointer (2, GL_FLOAT, 0, vertices);
  113. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  114. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  115. glDrawArrays (mode, 0, numElements);
  116. }
  117. void fillWithLinearGradient (const Rectangle<int>& rect,
  118. const ColourGradient& grad,
  119. const AffineTransform& transform,
  120. const int textureSize)
  121. {
  122. const Point<float> p1 (grad.point1.transformedBy (transform));
  123. const Point<float> p2 (grad.point2.transformedBy (transform));
  124. const Point<float> p3 (Point<float> (grad.point1.getX() - (grad.point2.getY() - grad.point1.getY()) / textureSize,
  125. grad.point1.getY() + (grad.point2.getX() - grad.point1.getX()) / textureSize).transformedBy (transform));
  126. const AffineTransform textureTransform (AffineTransform::fromTargetPoints (p1.getX(), p1.getY(), 0.0f, 0.0f,
  127. p2.getX(), p2.getY(), 1.0f, 0.0f,
  128. p3.getX(), p3.getY(), 0.0f, 1.0f));
  129. const float l = (float) rect.getX();
  130. const float r = (float) rect.getRight();
  131. const float t = (float) rect.getY();
  132. const float b = (float) rect.getBottom();
  133. const GLfloat vertices[] = { l, t, r, t, l, b, r, b };
  134. GLfloat textureCoords[] = { l, t, r, t, l, b, r, b };
  135. textureTransform.transformPoints (textureCoords[0], textureCoords[1], textureCoords[2], textureCoords[3]);
  136. textureTransform.transformPoints (textureCoords[4], textureCoords[5], textureCoords[6], textureCoords[7]);
  137. drawTriangles (GL_TRIANGLE_STRIP, vertices, textureCoords, 4);
  138. }
  139. void fillWithRadialGradient (const Rectangle<int>& rect,
  140. const ColourGradient& grad,
  141. const AffineTransform& transform)
  142. {
  143. const Point<float> centre (grad.point1.transformedBy (transform));
  144. const float screenRadius = centre.getDistanceFrom (rect.getCentre().toFloat())
  145. + Point<int> (rect.getWidth() / 2,
  146. rect.getHeight() / 2).getDistanceFromOrigin()
  147. + 8.0f;
  148. const AffineTransform inverse (transform.inverted());
  149. const float sourceRadius = jmax (Point<float> (screenRadius, 0.0f).transformedBy (inverse).getDistanceFromOrigin(),
  150. Point<float> (0.0f, screenRadius).transformedBy (inverse).getDistanceFromOrigin());
  151. const int numDivisions = 90;
  152. GLfloat vertices [4 + numDivisions * 2];
  153. GLfloat textureCoords [4 + numDivisions * 2];
  154. {
  155. GLfloat* t = textureCoords;
  156. *t++ = 0.0f;
  157. *t++ = 0.0f;
  158. const float originalRadius = grad.point1.getDistanceFrom (grad.point2);
  159. const float texturePos = sourceRadius / originalRadius;
  160. for (int i = numDivisions + 1; --i >= 0;)
  161. {
  162. *t++ = texturePos;
  163. *t++ = 0.0f;
  164. }
  165. }
  166. {
  167. GLfloat* v = vertices;
  168. *v++ = centre.getX();
  169. *v++ = centre.getY();
  170. const Point<float> first (grad.point1.translated (0, -sourceRadius)
  171. .transformedBy (transform));
  172. *v++ = first.getX();
  173. *v++ = first.getY();
  174. for (int i = 1; i < numDivisions; ++i)
  175. {
  176. const float angle = i * (float_Pi * 2.0f / numDivisions);
  177. const Point<float> p (grad.point1.translated (std::sin (angle) * sourceRadius,
  178. std::cos (angle) * -sourceRadius)
  179. .transformedBy (transform));
  180. *v++ = p.getX();
  181. *v++ = p.getY();
  182. }
  183. *v++ = first.getX();
  184. *v++ = first.getY();
  185. }
  186. glEnable (GL_SCISSOR_TEST);
  187. glScissor (rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
  188. drawTriangles (GL_TRIANGLE_FAN, vertices, textureCoords, numDivisions + 2);
  189. glDisable (GL_SCISSOR_TEST);
  190. }
  191. }
  192. void OpenGLHelpers::fillRectWithColourGradient (const Rectangle<int>& rect,
  193. const ColourGradient& gradient,
  194. const AffineTransform& transform)
  195. {
  196. const int textureSize = 256;
  197. OpenGLTexture texture;
  198. HeapBlock<PixelARGB> lookup (textureSize);
  199. gradient.createLookupTable (lookup, textureSize);
  200. texture.load (lookup, textureSize, 1);
  201. texture.bind();
  202. if (gradient.point1 == gradient.point2)
  203. {
  204. fillRectWithColour (rect, gradient.getColourAtPosition (1.0));
  205. }
  206. else
  207. {
  208. if (gradient.isRadial)
  209. OpenGLGradientHelpers::fillWithRadialGradient (rect, gradient, transform);
  210. else
  211. OpenGLGradientHelpers::fillWithLinearGradient (rect, gradient, transform, textureSize);
  212. }
  213. }
  214. void OpenGLHelpers::fillRectWithColour (const Rectangle<int>& rect, const Colour& colour)
  215. {
  216. glEnableClientState (GL_VERTEX_ARRAY);
  217. glDisableClientState (GL_TEXTURE_COORD_ARRAY);
  218. glDisableClientState (GL_COLOR_ARRAY);
  219. glDisableClientState (GL_NORMAL_ARRAY);
  220. setColour (colour);
  221. fillRect (rect);
  222. }
  223. void OpenGLHelpers::fillRect (const Rectangle<int>& rect)
  224. {
  225. const GLfloat vertices[] = { (float) rect.getX(), (float) rect.getY(),
  226. (float) rect.getRight(), (float) rect.getY(),
  227. (float) rect.getX(), (float) rect.getBottom(),
  228. (float) rect.getRight(), (float) rect.getBottom() };
  229. glVertexPointer (2, GL_FLOAT, 0, vertices);
  230. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  231. }
  232. //==============================================================================
  233. // This breaks down a path into a series of horizontal strips of trapezoids..
  234. class TriangulatedPath::TrapezoidedPath
  235. {
  236. public:
  237. TrapezoidedPath (const Path& p, const AffineTransform& transform)
  238. : firstSlice (nullptr),
  239. windingMask (p.isUsingNonZeroWinding() ? -1 : 1)
  240. {
  241. for (PathFlatteningIterator iter (p, transform); iter.next();)
  242. addLine (floatToInt (iter.x1), floatToInt (iter.y1),
  243. floatToInt (iter.x2), floatToInt (iter.y2));
  244. }
  245. ~TrapezoidedPath()
  246. {
  247. for (HorizontalSlice* s = firstSlice; s != nullptr;)
  248. {
  249. const ScopedPointer<HorizontalSlice> deleter (s);
  250. s = s->next;
  251. }
  252. }
  253. template <class Consumer>
  254. void iterate (Consumer& consumer) const
  255. {
  256. for (HorizontalSlice* s = firstSlice; s != nullptr; s = s->next)
  257. s->iterate (consumer, windingMask);
  258. }
  259. private:
  260. void addLine (int x1, int y1, int x2, int y2)
  261. {
  262. int winding = 1;
  263. if (y2 < y1)
  264. {
  265. std::swap (x1, x2);
  266. std::swap (y1, y2);
  267. winding = -1;
  268. }
  269. HorizontalSlice* last = nullptr;
  270. HorizontalSlice* s = firstSlice;
  271. while (y2 > y1)
  272. {
  273. if (s == nullptr)
  274. {
  275. insert (last, new HorizontalSlice (nullptr, x1, y1, x2, y2, winding));
  276. break;
  277. }
  278. if (s->y2 > y1)
  279. {
  280. if (y1 < s->y1)
  281. {
  282. if (y2 <= s->y1)
  283. {
  284. insert (last, new HorizontalSlice (s, x1, y1, x2, y2, winding));
  285. break;
  286. }
  287. else
  288. {
  289. const int newX = x1 + (s->y1 - y1) * (x2 - x1) / (y2 - y1);
  290. HorizontalSlice* const newSlice = new HorizontalSlice (s, x1, y1, newX, s->y1, winding);
  291. insert (last, newSlice);
  292. last = newSlice;
  293. x1 = newX;
  294. y1 = s->y1;
  295. continue;
  296. }
  297. }
  298. else if (y1 > s->y1)
  299. {
  300. s->split (y1);
  301. s = s->next;
  302. jassert (s != nullptr);
  303. }
  304. jassert (y1 == s->y1);
  305. if (y2 > s->y2)
  306. {
  307. const int newY = s->y2;
  308. const int newX = x1 + (newY - y1) * (x2 - x1) / (y2 - y1);
  309. s->addLine (x1, newX, winding);
  310. x1 = newX;
  311. y1 = newY;
  312. }
  313. else
  314. {
  315. if (y2 < s->y2)
  316. s->split (y2);
  317. jassert (y2 == s->y2);
  318. s->addLine (x1, x2, winding);
  319. break;
  320. }
  321. }
  322. last = s;
  323. s = s->next;
  324. }
  325. }
  326. struct HorizontalSlice
  327. {
  328. HorizontalSlice (const HorizontalSlice& other, HorizontalSlice* const next_, int y1_, int y2_)
  329. : next (next_), y1 (y1_), y2 (y2_), segments (other.segments)
  330. {
  331. }
  332. HorizontalSlice (HorizontalSlice* const next_, int x1, int y1_, int x2, int y2_, int winding)
  333. : next (next_), y1 (y1_), y2 (y2_)
  334. {
  335. jassert (next != this);
  336. jassert (y2 > y1);
  337. segments.ensureStorageAllocated (32);
  338. segments.add (LineSegment (x1, x2, winding));
  339. }
  340. void addLine (const int x1, const int x2, int winding)
  341. {
  342. const int dy = y2 - y1;
  343. for (int i = 0; i < segments.size(); ++i)
  344. {
  345. const LineSegment& l = segments.getReference (i);
  346. const int diff1 = l.x1 - x1;
  347. const int diff2 = l.x2 - x2;
  348. if ((diff1 < 0) == (diff2 > 0))
  349. {
  350. const int dx1 = l.x2 - l.x1;
  351. const int dx2 = x2 - x1;
  352. const int dxDiff = dx2 - dx1;
  353. if (dxDiff != 0)
  354. {
  355. const int intersectionY = (dy * diff1) / dxDiff;
  356. if (intersectionY > 0 && intersectionY < dy)
  357. {
  358. const int intersectionX = x1 + (intersectionY * dx2) / dy;
  359. split (intersectionY + y1);
  360. next->addLine (intersectionX, x2, winding);
  361. addLine (x1, intersectionX, winding);
  362. return;
  363. }
  364. }
  365. }
  366. if (diff1 + diff2 > 0)
  367. {
  368. segments.insert (i, LineSegment (x1, x2, winding));
  369. return;
  370. }
  371. }
  372. segments.add (LineSegment (x1, x2, winding));
  373. }
  374. void split (const int newY)
  375. {
  376. jassert (newY > y1 && newY < y2);
  377. const int dy1 = newY - y1;
  378. const int dy2 = y2 - y1;
  379. next = new HorizontalSlice (*this, next, newY, y2);
  380. y2 = newY;
  381. LineSegment* const oldSegments = segments.getRawDataPointer();
  382. LineSegment* const newSegments = next->segments.getRawDataPointer();
  383. for (int i = 0; i < segments.size(); ++i)
  384. {
  385. LineSegment& l = oldSegments[i];
  386. const int newX = l.x1 + dy1 * (l.x2 - l.x1) / dy2;
  387. newSegments[i].x1 = newX;
  388. l.x2 = newX;
  389. }
  390. }
  391. template <class Consumer>
  392. void iterate (Consumer& consumer, const int windingMask)
  393. {
  394. jassert (segments.size() > 0);
  395. const float fy1 = intToFloat (y1);
  396. const float fy2 = intToFloat (y2);
  397. const LineSegment* s1 = segments.getRawDataPointer();
  398. const LineSegment* s2 = s1;
  399. int winding = s1->winding;
  400. for (int i = segments.size(); --i > 0;)
  401. {
  402. ++s2;
  403. winding += s2->winding;
  404. if ((winding & windingMask) == 0)
  405. {
  406. const float ax1 = intToFloat (s1->x1);
  407. const float ax2 = intToFloat (s1->x2);
  408. if (s1->x1 == s2->x1)
  409. consumer.addTriangle (ax1, fy1, ax2, fy2, intToFloat (s2->x2), fy2);
  410. else if (s1->x2 == s2->x2)
  411. consumer.addTriangle (ax1, fy1, intToFloat (s2->x1), fy1, ax2, fy2);
  412. else
  413. consumer.addTrapezoid (fy1, fy2, ax1, ax2, intToFloat (s2->x1), intToFloat (s2->x2));
  414. s1 = s2 + 1;
  415. }
  416. }
  417. }
  418. HorizontalSlice* next;
  419. int y1, y2;
  420. private:
  421. struct LineSegment
  422. {
  423. inline LineSegment (int x1_, int x2_, int winding_) noexcept
  424. : x1 (x1_), x2 (x2_), winding (winding_) {}
  425. int x1, x2;
  426. int winding;
  427. };
  428. Array<LineSegment> segments;
  429. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HorizontalSlice);
  430. };
  431. HorizontalSlice* firstSlice;
  432. const int windingMask;
  433. inline void insert (HorizontalSlice* const last, HorizontalSlice* const newOne) noexcept
  434. {
  435. if (last == nullptr)
  436. firstSlice = newOne;
  437. else
  438. last->next = newOne;
  439. }
  440. enum { factor = 128 };
  441. static inline int floatToInt (const float n) noexcept { return roundToInt (n * (float) factor); }
  442. static inline float intToFloat (const int n) noexcept { return n * (1.0f / (float) factor); }
  443. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TrapezoidedPath);
  444. };
  445. //==============================================================================
  446. struct TriangulatedPath::TriangleBlock
  447. {
  448. TriangleBlock() noexcept
  449. : numVertices (0),
  450. triangles (maxVerticesPerBlock)
  451. {}
  452. void draw() const
  453. {
  454. glVertexPointer (2, GL_FLOAT, 0, triangles);
  455. glDrawArrays (GL_TRIANGLES, 0, numVertices / 2);
  456. }
  457. inline GLfloat* getNextTriangle() noexcept { return triangles + numVertices; }
  458. void optimiseStorage() { triangles.realloc (numVertices); }
  459. // Some GL implementations can't take very large triangle lists, so store
  460. // the list as a series of blocks containing this max number of triangles.
  461. enum { maxVerticesPerBlock = 256 * 6 };
  462. unsigned int numVertices;
  463. HeapBlock<GLfloat> triangles;
  464. };
  465. TriangulatedPath::TriangulatedPath (const Path& path, const AffineTransform& transform)
  466. {
  467. startNewBlock();
  468. TrapezoidedPath (path, transform).iterate (*this);
  469. }
  470. void TriangulatedPath::draw (const int oversamplingLevel) const
  471. {
  472. glColor4f (1.0f, 1.0f, 1.0f, 1.0f / (oversamplingLevel * oversamplingLevel));
  473. glTranslatef (-0.5f, -0.5f, 0.0f);
  474. const float inc = 1.0f / oversamplingLevel;
  475. for (int y = oversamplingLevel; --y >= 0;)
  476. {
  477. for (int x = oversamplingLevel; --x >= 0;)
  478. {
  479. glTranslatef (inc, 0.0f, 0.0f);
  480. for (int i = 0; i < blocks.size(); ++i)
  481. blocks.getUnchecked(i)->draw();
  482. }
  483. glTranslatef (-1.0f, inc, 0.0f);
  484. }
  485. }
  486. void TriangulatedPath::optimiseStorage()
  487. {
  488. currentBlock->optimiseStorage();
  489. }
  490. void TriangulatedPath::startNewBlock()
  491. {
  492. currentBlock = new TriangleBlock();
  493. blocks.add (currentBlock);
  494. }
  495. void TriangulatedPath::addTriangle (GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3)
  496. {
  497. if (currentBlock->numVertices >= TriangleBlock::maxVerticesPerBlock)
  498. startNewBlock();
  499. GLfloat* t = currentBlock->getNextTriangle();
  500. *t++ = x1; *t++ = y1; *t++ = x2; *t++ = y2; *t++ = x3; *t++ = y3;
  501. currentBlock->numVertices += 6;
  502. }
  503. void TriangulatedPath::addTrapezoid (GLfloat y1, GLfloat y2, GLfloat x1, GLfloat x2, GLfloat x3, GLfloat x4)
  504. {
  505. if (currentBlock->numVertices >= TriangleBlock::maxVerticesPerBlock - 6)
  506. startNewBlock();
  507. GLfloat* t = currentBlock->getNextTriangle();
  508. *t++ = x1; *t++ = y1; *t++ = x2; *t++ = y2; *t++ = x3; *t++ = y1;
  509. *t++ = x4; *t++ = y2; *t++ = x2; *t++ = y2; *t++ = x3; *t++ = y1;
  510. currentBlock->numVertices += 12;
  511. }
  512. //==============================================================================
  513. OpenGLTextureFromImage::OpenGLTextureFromImage (const Image& image)
  514. : width (image.getWidth()),
  515. height (image.getHeight())
  516. {
  517. OpenGLFrameBufferImage* glImage = dynamic_cast <OpenGLFrameBufferImage*> (image.getSharedImage());
  518. if (glImage != nullptr)
  519. {
  520. textureID = glImage->frameBuffer.getTextureID();
  521. }
  522. else
  523. {
  524. if (OpenGLTexture::isValidSize (width, height))
  525. {
  526. texture = new OpenGLTexture();
  527. texture->load (image);
  528. textureID = texture->getTextureID();
  529. }
  530. else
  531. {
  532. frameBuffer = new OpenGLFrameBuffer();
  533. frameBuffer->initialise (image);
  534. textureID = frameBuffer->getTextureID();
  535. }
  536. }
  537. }
  538. OpenGLTextureFromImage::~OpenGLTextureFromImage() {}
  539. //==============================================================================
  540. OpenGLRenderingTarget::OpenGLRenderingTarget() {}
  541. OpenGLRenderingTarget::~OpenGLRenderingTarget() {}
  542. void OpenGLRenderingTarget::prepareFor2D()
  543. {
  544. OpenGLHelpers::prepareFor2D (getRenderingTargetWidth(),
  545. getRenderingTargetHeight());
  546. }
  547. namespace GLPathRendering
  548. {
  549. void clipToPath (OpenGLRenderingTarget& target,
  550. const Path& path, const AffineTransform& transform)
  551. {
  552. const int w = target.getRenderingTargetWidth();
  553. const int h = target.getRenderingTargetHeight();
  554. OpenGLFrameBuffer fb;
  555. fb.initialise (w, h);
  556. fb.makeCurrentAndClear();
  557. fb.createAlphaChannelFromPath (path, transform);
  558. target.makeCurrentRenderingTarget();
  559. target.prepareFor2D();
  560. glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
  561. glBlendFunc (GL_DST_ALPHA, GL_ZERO);
  562. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  563. fb.drawAt (0, 0);
  564. }
  565. void fillPathWithColour (OpenGLRenderingTarget& target,
  566. const Rectangle<int>& clip, const Path& path,
  567. const AffineTransform& pathTransform,
  568. const Colour& colour)
  569. {
  570. OpenGLFrameBuffer f;
  571. f.initialise (clip.getWidth(), clip.getHeight());
  572. f.makeCurrentAndClear();
  573. f.createAlphaChannelFromPath (path, pathTransform.translated ((float) -clip.getX(), (float) -clip.getY())
  574. .followedBy (AffineTransform::verticalFlip ((float) clip.getHeight())));
  575. f.releaseAsRenderingTarget();
  576. target.makeCurrentRenderingTarget();
  577. glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  578. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  579. OpenGLHelpers::setColour (colour);
  580. target.prepareFor2D();
  581. f.drawAt ((float) clip.getX(), (float) (target.getRenderingTargetHeight() - clip.getBottom()));
  582. }
  583. void fillPathWithGradient (OpenGLRenderingTarget& target,
  584. const Rectangle<int>& clip, const Path& path,
  585. const AffineTransform& pathTransform,
  586. const ColourGradient& grad,
  587. const AffineTransform& gradientTransform,
  588. const GLfloat alpha)
  589. {
  590. const int targetHeight = target.getRenderingTargetHeight();
  591. OpenGLFrameBuffer f;
  592. f.initialise (clip.getWidth(), clip.getHeight());
  593. f.makeCurrentAndClear();
  594. const AffineTransform correction (AffineTransform::translation ((float) -clip.getX(), (float) -clip.getY())
  595. .followedBy (AffineTransform::verticalFlip ((float) clip.getHeight())));
  596. f.createAlphaChannelFromPath (path, pathTransform.followedBy (correction));
  597. f.makeCurrentRenderingTarget();
  598. f.prepareFor2D();
  599. glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  600. glBlendFunc (GL_DST_ALPHA, GL_ZERO);
  601. OpenGLHelpers::fillRectWithColourGradient (Rectangle<int> (0, 0, clip.getWidth(), clip.getHeight()),
  602. grad, gradientTransform.followedBy (correction));
  603. f.releaseAsRenderingTarget();
  604. target.makeCurrentRenderingTarget();
  605. glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  606. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  607. glColor4f (alpha, alpha, alpha, alpha);
  608. target.prepareFor2D();
  609. f.drawAt ((float) clip.getX(), (float) (targetHeight - clip.getBottom()));
  610. }
  611. void fillPathWithImage (OpenGLRenderingTarget& target,
  612. const Rectangle<int>& clip, const Path& path,
  613. const AffineTransform& transform,
  614. GLuint textureID, GLfloat textureWidth, GLfloat textureHeight,
  615. const AffineTransform& textureTransform,
  616. const bool tiled,
  617. const GLfloat alpha)
  618. {
  619. const int targetHeight = target.getRenderingTargetHeight();
  620. OpenGLFrameBuffer f;
  621. f.initialise (clip.getWidth(), clip.getHeight());
  622. f.makeCurrentRenderingTarget();
  623. f.prepareFor2D();
  624. glDisable (GL_BLEND);
  625. glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  626. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  627. const GLfloat clipX = (GLfloat) clip.getX();
  628. const GLfloat clipY = (GLfloat) clip.getY();
  629. const GLfloat clipH = (GLfloat) clip.getHeight();
  630. const GLfloat clipB = (GLfloat) clip.getBottom();
  631. const AffineTransform correction (AffineTransform::translation (-clipX, -clipY)
  632. .followedBy (AffineTransform::verticalFlip (clipH)));
  633. glBindTexture (GL_TEXTURE_2D, textureID);
  634. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  635. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  636. glEnableClientState (GL_VERTEX_ARRAY);
  637. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  638. glDisableClientState (GL_COLOR_ARRAY);
  639. glDisableClientState (GL_NORMAL_ARRAY);
  640. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  641. if (tiled)
  642. {
  643. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  644. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  645. const GLfloat clipW = (GLfloat) clip.getWidth();
  646. const GLfloat clipR = (GLfloat) clip.getRight();
  647. const GLfloat vertices[] = { 0, clipH, clipW, clipH, 0, 0, clipW, 0 };
  648. GLfloat textureCoords[] = { clipX, clipY, clipR, clipY, clipX, clipB, clipR, clipB };
  649. {
  650. const AffineTransform t (textureTransform.inverted().scaled (1.0f / textureWidth,
  651. 1.0f / textureHeight));
  652. t.transformPoints (textureCoords[0], textureCoords[1], textureCoords[2], textureCoords[3]);
  653. t.transformPoints (textureCoords[4], textureCoords[5], textureCoords[6], textureCoords[7]);
  654. }
  655. glVertexPointer (2, GL_FLOAT, 0, vertices);
  656. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  657. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  658. }
  659. else
  660. {
  661. glClearColor (0, 0, 0, 0);
  662. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  663. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  664. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  665. GLfloat vertices[] = { 0, 0, textureWidth, 0, 0, textureHeight, textureWidth, textureHeight };
  666. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  667. {
  668. const AffineTransform t (textureTransform.followedBy (correction));
  669. t.transformPoints (vertices[0], vertices[1], vertices[2], vertices[3]);
  670. t.transformPoints (vertices[4], vertices[5], vertices[6], vertices[7]);
  671. }
  672. glVertexPointer (2, GL_FLOAT, 0, vertices);
  673. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  674. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  675. }
  676. glBindTexture (GL_TEXTURE_2D, 0);
  677. clipToPath (f, path, transform.followedBy (correction));
  678. f.releaseAsRenderingTarget();
  679. target.makeCurrentRenderingTarget();
  680. glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  681. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  682. glColor4f (1.0f, 1.0f, 1.0f, alpha);
  683. target.prepareFor2D();
  684. f.drawAt (clipX, targetHeight - clipB);
  685. }
  686. }
  687. void OpenGLRenderingTarget::fillPath (const Rectangle<int>& clip,
  688. const Path& path, const AffineTransform& transform,
  689. const FillType& fill)
  690. {
  691. if (! fill.isInvisible())
  692. {
  693. if (fill.isColour())
  694. {
  695. GLPathRendering::fillPathWithColour (*this, clip, path, transform, fill.colour);
  696. }
  697. else if (fill.isGradient())
  698. {
  699. GLPathRendering::fillPathWithGradient (*this, clip, path, transform,
  700. *(fill.gradient), fill.transform,
  701. fill.colour.getFloatAlpha());
  702. }
  703. else if (fill.isTiledImage())
  704. {
  705. OpenGLTextureFromImage t (fill.image);
  706. GLPathRendering::fillPathWithImage (*this, clip, path, transform,
  707. t.textureID, (GLfloat) t.width, (GLfloat) t.height,
  708. fill.transform, true,
  709. fill.colour.getFloatAlpha());
  710. }
  711. }
  712. }
  713. END_JUCE_NAMESPACE