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.

809 lines
28KB

  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_WINDOWS
  20. enum
  21. {
  22. GL_FRAMEBUFFER_EXT = 0x8D40,
  23. GL_RENDERBUFFER_EXT = 0x8D41,
  24. GL_COLOR_ATTACHMENT0_EXT = 0x8CE0,
  25. GL_DEPTH_ATTACHMENT_EXT = 0x8D00,
  26. GL_STENCIL_ATTACHMENT_EXT = 0x8D20,
  27. GL_FRAMEBUFFER_COMPLETE_EXT = 0x8CD5,
  28. GL_DEPTH24_STENCIL8_EXT = 0x88F0,
  29. GL_RENDERBUFFER_DEPTH_SIZE_EXT = 0x8D54
  30. };
  31. #endif
  32. #if JUCE_WINDOWS || JUCE_LINUX
  33. #define FRAMEBUFFER_FUNCTION_LIST(USE_FUNCTION) \
  34. USE_FUNCTION (glIsRenderbufferEXT, GLboolean, (GLuint renderbuffer))\
  35. USE_FUNCTION (glBindRenderbufferEXT, void, (GLenum target, GLuint renderbuffer))\
  36. USE_FUNCTION (glDeleteRenderbuffersEXT, void, (GLsizei n, const GLuint *renderbuffers))\
  37. USE_FUNCTION (glGenRenderbuffersEXT, void, (GLsizei n, GLuint *renderbuffers))\
  38. USE_FUNCTION (glRenderbufferStorageEXT, void, (GLenum target, GLenum internalformat, GLsizei width, GLsizei height))\
  39. USE_FUNCTION (glGetRenderbufferParameterivEXT, void, (GLenum target, GLenum pname, GLint* params))\
  40. USE_FUNCTION (glIsFramebufferEXT, GLboolean, (GLuint framebuffer))\
  41. USE_FUNCTION (glBindFramebufferEXT, void, (GLenum target, GLuint framebuffer))\
  42. USE_FUNCTION (glDeleteFramebuffersEXT, void, (GLsizei n, const GLuint *framebuffers))\
  43. USE_FUNCTION (glGenFramebuffersEXT, void, (GLsizei n, GLuint *framebuffers))\
  44. USE_FUNCTION (glCheckFramebufferStatusEXT, GLenum, (GLenum target))\
  45. USE_FUNCTION (glFramebufferTexture1DEXT, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level))\
  46. USE_FUNCTION (glFramebufferTexture2DEXT, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level))\
  47. USE_FUNCTION (glFramebufferTexture3DEXT, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset))\
  48. USE_FUNCTION (glFramebufferRenderbufferEXT, void, (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer))\
  49. USE_FUNCTION (glGetFramebufferAttachmentParameterivEXT, void, (GLenum target, GLenum attachment, GLenum pname, GLint *params))\
  50. USE_FUNCTION (glGenerateMipmapEXT, void, (GLenum target))\
  51. #if JUCE_WINDOWS
  52. #define APICALLTYPE __stdcall
  53. #else
  54. #define APICALLTYPE
  55. #endif
  56. #define DECLARE_FUNCTION(name, returnType, params) \
  57. typedef returnType (APICALLTYPE * type_ ## name) params; static type_ ## name name;
  58. FRAMEBUFFER_FUNCTION_LIST (DECLARE_FUNCTION)
  59. #undef DECLARE_FUNCTION
  60. static bool framebufferFunctionsInitialised = false;
  61. static void initialiseFrameBufferFunctions()
  62. {
  63. if (! framebufferFunctionsInitialised)
  64. {
  65. framebufferFunctionsInitialised = true;
  66. #if JUCE_LINUX
  67. #define JUCE_LOOKUP_FUNCTION(name) glXGetProcAddress ((const GLubyte*) name)
  68. #else
  69. #define JUCE_LOOKUP_FUNCTION(name) wglGetProcAddress (name)
  70. #endif
  71. #define FIND_FUNCTION(name, returnType, params) name = (type_ ## name) JUCE_LOOKUP_FUNCTION (#name);
  72. FRAMEBUFFER_FUNCTION_LIST (FIND_FUNCTION)
  73. #undef FIND_FUNCTION
  74. #undef JUCE_LOOKUP_FUNCTION
  75. }
  76. }
  77. #undef FRAMEBUFFER_FUNCTION_LIST
  78. //==============================================================================
  79. #elif JUCE_OPENGL_ES
  80. #define glIsRenderbufferEXT glIsRenderbufferOES
  81. #define glBindRenderbufferEXT glBindRenderbufferOES
  82. #define glDeleteRenderbuffersEXT glDeleteRenderbuffersOES
  83. #define glGenRenderbuffersEXT glGenRenderbuffersOES
  84. #define glRenderbufferStorageEXT glRenderbufferStorageOES
  85. #define glGetRenderbufferParameterivEXT glGetRenderbufferParameterivOES
  86. #define glIsFramebufferEXT glIsFramebufferOES
  87. #define glBindFramebufferEXT glBindFramebufferOES
  88. #define glDeleteFramebuffersEXT glDeleteFramebuffersOES
  89. #define glGenFramebuffersEXT glGenFramebuffersOES
  90. #define glCheckFramebufferStatusEXT glCheckFramebufferStatusOES
  91. #define glFramebufferTexture1DEXT glFramebufferTexture1DOES
  92. #define glFramebufferTexture2DEXT glFramebufferTexture2DOES
  93. #define glFramebufferTexture3DEXT glFramebufferTexture3DOES
  94. #define glFramebufferRenderbufferEXT glFramebufferRenderbufferOES
  95. #define glGetFramebufferAttachmentParameterivEXT glGetFramebufferAttachmentParameterivOES
  96. #define glGenerateMipmapEXT glGenerateMipmapOES
  97. #define GL_FRAMEBUFFER_EXT GL_FRAMEBUFFER_OES
  98. #define GL_RGBA8 GL_RGBA
  99. #define GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT0_OES
  100. #define GL_RENDERBUFFER_EXT GL_RENDERBUFFER_OES
  101. #define GL_DEPTH24_STENCIL8_EXT GL_DEPTH24_STENCIL8_OES
  102. #define GL_RENDERBUFFER_DEPTH_SIZE_EXT GL_RENDERBUFFER_DEPTH_SIZE_OES
  103. #define GL_DEPTH_ATTACHMENT_EXT GL_DEPTH_ATTACHMENT_OES
  104. #define GL_STENCIL_ATTACHMENT_EXT GL_STENCIL_ATTACHMENT_OES
  105. #define GL_FRAMEBUFFER_COMPLETE_EXT GL_FRAMEBUFFER_COMPLETE_OES
  106. #define GL_FRAMEBUFFER_UNSUPPORTED_EXT GL_FRAMEBUFFER_UNSUPPORTED_OES
  107. #define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_OES
  108. #endif
  109. //==============================================================================
  110. class OpenGLFrameBuffer::Pimpl
  111. {
  112. public:
  113. Pimpl (const int width_, const int height_,
  114. const bool wantsDepthBuffer, const bool wantsStencilBuffer,
  115. const GLenum textureType = GL_TEXTURE_2D)
  116. : width (width_),
  117. height (height_),
  118. textureID (0),
  119. frameBufferHandle (0),
  120. depthOrStencilBuffer (0),
  121. hasDepthBuffer (false),
  122. hasStencilBuffer (false),
  123. ok (false)
  124. {
  125. #if JUCE_WINDOWS || JUCE_LINUX
  126. initialiseFrameBufferFunctions();
  127. if (glGenFramebuffersEXT == nullptr)
  128. return;
  129. #endif
  130. OpenGLHelpers::resetErrorState();
  131. glGenFramebuffersEXT (1, &frameBufferHandle);
  132. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBufferHandle);
  133. glGenTextures (1, &textureID);
  134. glBindTexture (textureType, textureID);
  135. glTexImage2D (textureType, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  136. glTexParameterf (textureType, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  137. glTexParameterf (textureType, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  138. glTexParameterf (textureType, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  139. glTexParameterf (textureType, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  140. glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, textureType, textureID, 0);
  141. if (wantsDepthBuffer || wantsStencilBuffer)
  142. {
  143. glGenRenderbuffersEXT (1, &depthOrStencilBuffer);
  144. glBindRenderbufferEXT (GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  145. jassert (glIsRenderbufferEXT (depthOrStencilBuffer));
  146. if (wantsDepthBuffer && wantsStencilBuffer)
  147. {
  148. glRenderbufferStorageEXT (GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT, width, height);
  149. GLint params = 0;
  150. glGetRenderbufferParameterivEXT (GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &params);
  151. glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  152. glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  153. hasDepthBuffer = true;
  154. hasStencilBuffer = true;
  155. }
  156. else
  157. {
  158. #if JUCE_OPENGL_ES
  159. glRenderbufferStorageEXT (GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT16, width, height);
  160. #else
  161. glRenderbufferStorageEXT (GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
  162. #endif
  163. GLint params = 0;
  164. glGetRenderbufferParameterivEXT (GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &params);
  165. glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  166. hasDepthBuffer = true;
  167. }
  168. }
  169. ok = checkStatus();
  170. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
  171. }
  172. ~Pimpl()
  173. {
  174. if (textureID != 0)
  175. glDeleteTextures (1, &textureID);
  176. if (depthOrStencilBuffer != 0)
  177. glDeleteRenderbuffersEXT (1, &depthOrStencilBuffer);
  178. if (frameBufferHandle != 0)
  179. glDeleteFramebuffersEXT (1, &frameBufferHandle);
  180. }
  181. bool bind() { return bind (frameBufferHandle); }
  182. bool unbind() { return bind (0); }
  183. const int width, height;
  184. GLuint textureID, frameBufferHandle, depthOrStencilBuffer;
  185. bool hasDepthBuffer, hasStencilBuffer, ok;
  186. private:
  187. bool bind (GLuint buffer)
  188. {
  189. if (ok)
  190. {
  191. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, buffer);
  192. ok = checkStatus();
  193. }
  194. return ok;
  195. }
  196. static bool checkStatus() noexcept
  197. {
  198. const GLenum status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
  199. return status == GL_NO_ERROR
  200. || status == GL_FRAMEBUFFER_COMPLETE_EXT;
  201. }
  202. JUCE_DECLARE_NON_COPYABLE (Pimpl);
  203. };
  204. //==============================================================================
  205. class OpenGLFrameBuffer::SavedState
  206. {
  207. public:
  208. SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
  209. : width (w), height (h),
  210. data (w * h)
  211. {
  212. buffer.readPixels (data, Rectangle<int> (0, 0, w, h));
  213. }
  214. bool restore (OpenGLFrameBuffer& buffer)
  215. {
  216. if (buffer.initialise (width, height))
  217. {
  218. buffer.writePixels (data, Rectangle<int> (0, 0, width, height));
  219. return true;
  220. }
  221. return false;
  222. }
  223. private:
  224. const int width, height;
  225. HeapBlock <int32> data;
  226. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState);
  227. };
  228. //==============================================================================
  229. OpenGLFrameBuffer::OpenGLFrameBuffer() {}
  230. OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
  231. bool OpenGLFrameBuffer::initialise (int width, int height)
  232. {
  233. pimpl = nullptr;
  234. pimpl = new Pimpl (width, height, true, false);
  235. if (! pimpl->ok)
  236. pimpl = nullptr;
  237. return pimpl != nullptr;
  238. }
  239. void OpenGLFrameBuffer::release()
  240. {
  241. pimpl = nullptr;
  242. savedState = nullptr;
  243. }
  244. void OpenGLFrameBuffer::saveAndRelease()
  245. {
  246. if (pimpl != nullptr)
  247. {
  248. savedState = new SavedState (*this, pimpl->width, pimpl->height);
  249. pimpl = nullptr;
  250. }
  251. }
  252. bool OpenGLFrameBuffer::reloadSavedCopy()
  253. {
  254. if (savedState != nullptr
  255. && savedState->restore (*this))
  256. {
  257. savedState = nullptr;
  258. return true;
  259. }
  260. return false;
  261. }
  262. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  263. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  264. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  265. bool OpenGLFrameBuffer::makeCurrentTarget()
  266. {
  267. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  268. // reloadSavedCopy() to put it back into GPU memory before using it..
  269. jassert (savedState == nullptr);
  270. return pimpl != nullptr && pimpl->bind();
  271. }
  272. void OpenGLFrameBuffer::releaseCurrentTarget()
  273. {
  274. if (pimpl != nullptr)
  275. pimpl->unbind();
  276. }
  277. void OpenGLFrameBuffer::clear (const Colour& colour)
  278. {
  279. if (makeCurrentTarget())
  280. {
  281. OpenGLHelpers::clear (colour);
  282. releaseCurrentTarget();
  283. }
  284. }
  285. bool OpenGLFrameBuffer::readPixels (void* target, const Rectangle<int>& area)
  286. {
  287. if (! makeCurrentTarget())
  288. return false;
  289. OpenGLHelpers::prepareFor2D (pimpl->width, pimpl->height);
  290. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  291. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, target);
  292. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
  293. return true;
  294. }
  295. bool OpenGLFrameBuffer::writePixels (const void* data, const Rectangle<int>& area)
  296. {
  297. if (! makeCurrentTarget())
  298. return false;
  299. OpenGLHelpers::prepareFor2D (pimpl->width, pimpl->height);
  300. glDisable (GL_DEPTH_TEST);
  301. glDisable (GL_BLEND);
  302. #if JUCE_OPENGL_ES
  303. // GLES has no glDrawPixels function, so we have to create a texture and draw it..
  304. GLuint temporaryTexture = 0;
  305. glGenTextures (1, &temporaryTexture);
  306. jassert (temporaryTexture != 0); // can't create a texture!
  307. if (temporaryTexture != 0)
  308. {
  309. glEnable (GL_TEXTURE_2D);
  310. glBindTexture (GL_TEXTURE_2D, temporaryTexture);
  311. glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
  312. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, area.getWidth(), area.getHeight(), 0,
  313. GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
  314. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  315. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  316. const int cropRect[4] = { 0, 0, area.getWidth(), area.getHeight() };
  317. glTexParameteriv (GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
  318. glDrawTexiOES (area.getX(), area.getY(), 1, area.getWidth(), area.getHeight());
  319. glBindTexture (GL_TEXTURE_2D, 0);
  320. glDeleteTextures (1, &temporaryTexture);
  321. }
  322. #else
  323. glRasterPos2i (area.getX(), area.getY());
  324. glBindTexture (GL_TEXTURE_2D, 0);
  325. glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
  326. glDrawPixels (area.getWidth(), area.getHeight(), GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
  327. #endif
  328. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
  329. return true;
  330. }
  331. void OpenGLFrameBuffer::draw2D (float x1, float y1,
  332. float x2, float y2,
  333. float x3, float y3,
  334. float x4, float y4,
  335. const Colour& colour) const
  336. {
  337. if (pimpl != nullptr)
  338. {
  339. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  340. OpenGLHelpers::drawQuad2D (x1, y1, x2, y2, x3, y3, x4, y4, colour);
  341. glBindTexture (GL_TEXTURE_2D, 0);
  342. }
  343. }
  344. void OpenGLFrameBuffer::draw3D (float x1, float y1, float z1,
  345. float x2, float y2, float z2,
  346. float x3, float y3, float z3,
  347. float x4, float y4, float z4,
  348. const Colour& colour) const
  349. {
  350. if (pimpl != nullptr)
  351. {
  352. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  353. OpenGLHelpers::drawQuad3D (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, colour);
  354. glBindTexture (GL_TEXTURE_2D, 0);
  355. }
  356. }
  357. //==============================================================================
  358. // This breaks down a path into a series of horizontal strips of trapezoids..
  359. class TrapezoidedPath
  360. {
  361. public:
  362. TrapezoidedPath (const Path& p)
  363. : firstSlice (nullptr),
  364. windingMask (p.isUsingNonZeroWinding() ? -1 : 1)
  365. {
  366. for (PathFlatteningIterator iter (p); iter.next();)
  367. addLine (floatToInt (iter.x1), floatToInt (iter.y1),
  368. floatToInt (iter.x2), floatToInt (iter.y2));
  369. }
  370. ~TrapezoidedPath()
  371. {
  372. for (HorizontalSlice* s = firstSlice; s != nullptr;)
  373. {
  374. const ScopedPointer<HorizontalSlice> deleter (s);
  375. s = s->next;
  376. }
  377. }
  378. template <class Consumer>
  379. void iterate (Consumer& consumer) const
  380. {
  381. for (HorizontalSlice* s = firstSlice; s != nullptr; s = s->next)
  382. s->iterate (consumer, windingMask);
  383. }
  384. private:
  385. void addLine (int x1, int y1, int x2, int y2)
  386. {
  387. int winding = 1;
  388. if (y2 < y1)
  389. {
  390. std::swap (x1, x2);
  391. std::swap (y1, y2);
  392. winding = -1;
  393. }
  394. HorizontalSlice* last = nullptr;
  395. HorizontalSlice* s = firstSlice;
  396. while (y2 > y1)
  397. {
  398. if (s == nullptr)
  399. {
  400. insert (last, new HorizontalSlice (nullptr, x1, y1, x2, y2, winding));
  401. break;
  402. }
  403. if (s->y2 > y1)
  404. {
  405. if (y1 < s->y1)
  406. {
  407. if (y2 <= s->y1)
  408. {
  409. insert (last, new HorizontalSlice (s, x1, y1, x2, y2, winding));
  410. break;
  411. }
  412. else
  413. {
  414. const int newX = x1 + (s->y1 - y1) * (x2 - x1) / (y2 - y1);
  415. HorizontalSlice* const newSlice = new HorizontalSlice (s, x1, y1, newX, s->y1, winding);
  416. insert (last, newSlice);
  417. last = newSlice;
  418. x1 = newX;
  419. y1 = s->y1;
  420. continue;
  421. }
  422. }
  423. else if (y1 > s->y1)
  424. {
  425. s->split (y1);
  426. s = s->next;
  427. jassert (s != nullptr);
  428. }
  429. jassert (y1 == s->y1);
  430. if (y2 > s->y2)
  431. {
  432. const int newY = s->y2;
  433. const int newX = x1 + (newY - y1) * (x2 - x1) / (y2 - y1);
  434. s->addLine (x1, newX, winding);
  435. x1 = newX;
  436. y1 = newY;
  437. }
  438. else
  439. {
  440. if (y2 < s->y2)
  441. s->split (y2);
  442. jassert (y2 == s->y2);
  443. s->addLine (x1, x2, winding);
  444. break;
  445. }
  446. }
  447. last = s;
  448. s = s->next;
  449. }
  450. }
  451. struct HorizontalSlice
  452. {
  453. HorizontalSlice (const HorizontalSlice& other, HorizontalSlice* const next_, int y1_, int y2_)
  454. : next (next_), y1 (y1_), y2 (y2_), segments (other.segments)
  455. {
  456. }
  457. HorizontalSlice (HorizontalSlice* const next_, int x1, int y1_, int x2, int y2_, int winding)
  458. : next (next_), y1 (y1_), y2 (y2_)
  459. {
  460. jassert (next != this);
  461. jassert (y2 > y1);
  462. segments.ensureStorageAllocated (32);
  463. segments.add (LineSegment (x1, x2, winding));
  464. }
  465. void addLine (const int x1, const int x2, int winding)
  466. {
  467. const int dy = y2 - y1;
  468. for (int i = 0; i < segments.size(); ++i)
  469. {
  470. const LineSegment& l = segments.getReference (i);
  471. const int diff1 = l.x1 - x1;
  472. const int diff2 = l.x2 - x2;
  473. if ((diff1 < 0) == (diff2 > 0))
  474. {
  475. const int dx1 = l.x2 - l.x1;
  476. const int dx2 = x2 - x1;
  477. const int dxDiff = dx2 - dx1;
  478. if (dxDiff != 0)
  479. {
  480. const int intersectionY = (dy * diff1) / dxDiff;
  481. if (intersectionY > 0 && intersectionY < dy)
  482. {
  483. const int intersectionX = x1 + (intersectionY * dx2) / dy;
  484. split (intersectionY + y1);
  485. next->addLine (intersectionX, x2, winding);
  486. addLine (x1, intersectionX, winding);
  487. return;
  488. }
  489. }
  490. }
  491. if (diff1 + diff2 > 0)
  492. {
  493. segments.insert (i, LineSegment (x1, x2, winding));
  494. return;
  495. }
  496. }
  497. segments.add (LineSegment (x1, x2, winding));
  498. }
  499. void split (const int newY)
  500. {
  501. jassert (newY > y1 && newY < y2);
  502. const int dy1 = newY - y1;
  503. const int dy2 = y2 - y1;
  504. next = new HorizontalSlice (*this, next, newY, y2);
  505. y2 = newY;
  506. LineSegment* const oldSegments = segments.getRawDataPointer();
  507. LineSegment* const newSegments = next->segments.getRawDataPointer();
  508. for (int i = 0; i < segments.size(); ++i)
  509. {
  510. LineSegment& l = oldSegments[i];
  511. const int newX = l.x1 + dy1 * (l.x2 - l.x1) / dy2;
  512. newSegments[i].x1 = newX;
  513. l.x2 = newX;
  514. }
  515. }
  516. template <class Consumer>
  517. void iterate (Consumer& consumer, const int windingMask)
  518. {
  519. jassert (segments.size() > 0);
  520. const float fy1 = intToFloat (y1);
  521. const float fy2 = intToFloat (y2);
  522. const LineSegment* s1 = segments.getRawDataPointer();
  523. const LineSegment* s2 = s1;
  524. int winding = s1->winding;
  525. for (int i = segments.size(); --i > 0;)
  526. {
  527. ++s2;
  528. winding += s2->winding;
  529. if ((winding & windingMask) == 0)
  530. {
  531. const float ax1 = intToFloat (s1->x1);
  532. const float ax2 = intToFloat (s1->x2);
  533. if (s1->x1 == s2->x1)
  534. consumer.addTriangle (ax1, fy1, ax2, fy2, intToFloat (s2->x2), fy2);
  535. else if (s1->x2 == s2->x2)
  536. consumer.addTriangle (ax1, fy1, intToFloat (s2->x1), fy1, ax2, fy2);
  537. else
  538. consumer.addTrapezoid (fy1, fy2, ax1, ax2, intToFloat (s2->x1), intToFloat (s2->x2));
  539. s1 = s2 + 1;
  540. }
  541. }
  542. }
  543. HorizontalSlice* next;
  544. int y1, y2;
  545. private:
  546. struct LineSegment
  547. {
  548. inline LineSegment (int x1_, int x2_, int winding_) noexcept
  549. : x1 (x1_), x2 (x2_), winding (winding_) {}
  550. int x1, x2;
  551. int winding;
  552. };
  553. Array<LineSegment> segments;
  554. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HorizontalSlice);
  555. };
  556. HorizontalSlice* firstSlice;
  557. const int windingMask;
  558. inline void insert (HorizontalSlice* const last, HorizontalSlice* const newOne) noexcept
  559. {
  560. if (last == nullptr)
  561. firstSlice = newOne;
  562. else
  563. last->next = newOne;
  564. }
  565. enum { factor = 128 };
  566. static inline int floatToInt (const float n) noexcept { return roundToInt (n * (float) factor); }
  567. static inline float intToFloat (const int n) noexcept { return n * (1.0f / (float) factor); }
  568. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TrapezoidedPath);
  569. };
  570. //==============================================================================
  571. // Breaks a path into a set of openGL triangles..
  572. class TriangulatedPath
  573. {
  574. public:
  575. TriangulatedPath (const Path& path)
  576. {
  577. startNewBlock();
  578. TrapezoidedPath (path).iterate (*this);
  579. }
  580. void draw (const int oversamplingLevel) const
  581. {
  582. glColor4f (1.0f, 1.0f, 1.0f, 1.0f / (oversamplingLevel * oversamplingLevel));
  583. glTranslatef (-0.5f, -0.5f, 0.0f);
  584. const float inc = 1.0f / oversamplingLevel;
  585. for (int y = oversamplingLevel; --y >= 0;)
  586. {
  587. for (int x = oversamplingLevel; --x >= 0;)
  588. {
  589. glTranslatef (inc, 0.0f, 0.0f);
  590. for (int i = 0; i < blocks.size(); ++i)
  591. blocks.getUnchecked(i)->draw();
  592. }
  593. glTranslatef (-1.0f, inc, 0.0f);
  594. }
  595. }
  596. void addTriangle (GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3)
  597. {
  598. if (currentBlock->numDone >= trianglesPerBlock)
  599. startNewBlock();
  600. GLfloat* t = currentBlock->getNextTriangle();
  601. *t++ = x1; *t++ = y1; *t++ = x2; *t++ = y2; *t++ = x3; *t++ = y3;
  602. currentBlock->numDone++;
  603. }
  604. void addTrapezoid (GLfloat y1, GLfloat y2, GLfloat x1, GLfloat x2, GLfloat x3, GLfloat x4)
  605. {
  606. if (currentBlock->numDone >= trianglesPerBlock - 1)
  607. startNewBlock();
  608. GLfloat* t = currentBlock->getNextTriangle();
  609. *t++ = x1; *t++ = y1; *t++ = x2; *t++ = y2; *t++ = x3; *t++ = y1;
  610. *t++ = x4; *t++ = y2; *t++ = x2; *t++ = y2; *t++ = x3; *t++ = y1;
  611. currentBlock->numDone += 2;
  612. }
  613. private:
  614. // Some GL implementations can't take very large triangle lists, so store
  615. // the list as a series of blocks containing this max number of triangles.
  616. enum { trianglesPerBlock = 256 };
  617. struct TriangleBlock
  618. {
  619. TriangleBlock() noexcept : numDone (0) {}
  620. void draw() const
  621. {
  622. glVertexPointer (2, GL_FLOAT, 0, triangles);
  623. glDrawArrays (GL_TRIANGLES, 0, numDone * 3);
  624. }
  625. inline GLfloat* getNextTriangle() noexcept { return triangles + numDone * 6; }
  626. int numDone;
  627. GLfloat triangles [trianglesPerBlock * 6];
  628. };
  629. void startNewBlock()
  630. {
  631. currentBlock = new TriangleBlock();
  632. blocks.add (currentBlock);
  633. }
  634. OwnedArray<TriangleBlock> blocks;
  635. TriangleBlock* currentBlock;
  636. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TriangulatedPath);
  637. };
  638. //==============================================================================
  639. void OpenGLFrameBuffer::createAlphaChannelFromPath (const Path& path, const int oversamplingLevel)
  640. {
  641. makeCurrentTarget();
  642. glEnableClientState (GL_VERTEX_ARRAY);
  643. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  644. glDisableClientState (GL_COLOR_ARRAY);
  645. glDisableClientState (GL_NORMAL_ARRAY);
  646. glDisable (GL_TEXTURE_2D);
  647. glDisable (GL_DEPTH_TEST);
  648. glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
  649. glEnable (GL_BLEND);
  650. glBlendFunc (GL_ONE, GL_ONE);
  651. OpenGLHelpers::prepareFor2D (getWidth(), getHeight());
  652. TriangulatedPath (path).draw (oversamplingLevel);
  653. }
  654. END_JUCE_NAMESPACE