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.

743 lines
24KB

  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::getExtensionFunction (const char* functionName)
  25. {
  26. #if JUCE_WINDOWS
  27. return (void*) wglGetProcAddress (functionName);
  28. #elif JUCE_LINUX
  29. return (void*) glXGetProcAddress ((const GLubyte*) functionName);
  30. #else
  31. static void* handle = dlopen (nullptr, RTLD_LAZY);
  32. return dlsym (handle, functionName);
  33. #endif
  34. }
  35. #if ! JUCE_OPENGL_ES
  36. namespace
  37. {
  38. bool isExtensionSupportedV3 (const char* extensionName)
  39. {
  40. #ifndef GL_NUM_EXTENSIONS
  41. enum { GL_NUM_EXTENSIONS = 0x821d };
  42. #endif
  43. JUCE_DECLARE_GL_EXTENSION_FUNCTION (glGetStringi, const GLubyte*, (GLenum, GLuint))
  44. if (glGetStringi == nullptr)
  45. glGetStringi = (type_glGetStringi) OpenGLHelpers::getExtensionFunction ("glGetStringi");
  46. if (glGetStringi != nullptr)
  47. {
  48. GLint numExtensions = 0;
  49. glGetIntegerv (GL_NUM_EXTENSIONS, &numExtensions);
  50. for (int i = 0; i < numExtensions; ++i)
  51. if (strcmp (extensionName, (const char*) glGetStringi (GL_EXTENSIONS, i)) == 0)
  52. return true;
  53. }
  54. return false;
  55. }
  56. }
  57. #endif
  58. bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
  59. {
  60. jassert (extensionName != nullptr); // you must supply a genuine string for this.
  61. jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
  62. #if ! JUCE_OPENGL_ES
  63. const GLubyte* version = glGetString (GL_VERSION);
  64. if (version != nullptr && version[0] >= '3')
  65. {
  66. return isExtensionSupportedV3 (extensionName);
  67. }
  68. else
  69. #endif
  70. {
  71. const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
  72. jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
  73. for (;;)
  74. {
  75. const char* found = strstr (extensions, extensionName);
  76. if (found == nullptr)
  77. break;
  78. extensions = found + strlen (extensionName);
  79. if (extensions[0] == ' ' || extensions[0] == 0)
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. void OpenGLHelpers::clear (const Colour& colour)
  86. {
  87. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  88. colour.getFloatBlue(), colour.getFloatAlpha());
  89. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  90. }
  91. void OpenGLHelpers::setColour (const Colour& colour)
  92. {
  93. glColor4f (colour.getFloatRed(), colour.getFloatGreen(),
  94. colour.getFloatBlue(), colour.getFloatAlpha());
  95. }
  96. void OpenGLHelpers::prepareFor2D (const int width, const int height)
  97. {
  98. glMatrixMode (GL_PROJECTION);
  99. glLoadIdentity();
  100. #if JUCE_OPENGL_ES
  101. glOrthof (0.0f, (GLfloat) width, 0.0f, (GLfloat) height, 0.0f, 1.0f);
  102. #else
  103. glOrtho (0.0, width, 0.0, height, 0, 1);
  104. #endif
  105. glViewport (0, 0, width, height);
  106. }
  107. void OpenGLHelpers::setPerspective (double fovy, double aspect, double zNear, double zFar)
  108. {
  109. glLoadIdentity();
  110. #if JUCE_OPENGL_ES
  111. const GLfloat ymax = (GLfloat) (zNear * tan (fovy * double_Pi / 360.0));
  112. const GLfloat ymin = -ymax;
  113. glFrustumf (ymin * (GLfloat) aspect, ymax * (GLfloat) aspect, ymin, ymax, (GLfloat) zNear, (GLfloat) zFar);
  114. #else
  115. const double ymax = zNear * tan (fovy * double_Pi / 360.0);
  116. const double ymin = -ymax;
  117. glFrustum (ymin * aspect, ymax * aspect, ymin, ymax, zNear, zFar);
  118. #endif
  119. }
  120. void OpenGLHelpers::applyTransform (const AffineTransform& t)
  121. {
  122. const GLfloat m[] = { t.mat00, t.mat10, 0, 0,
  123. t.mat01, t.mat11, 0, 0,
  124. 0, 0, 1, 0,
  125. t.mat02, t.mat12, 0, 1 };
  126. glMultMatrixf (m);
  127. }
  128. void OpenGLHelpers::enableScissorTest (const Rectangle<int>& clip)
  129. {
  130. glEnable (GL_SCISSOR_TEST);
  131. glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
  132. }
  133. void OpenGLHelpers::drawQuad2D (float x1, float y1,
  134. float x2, float y2,
  135. float x3, float y3,
  136. float x4, float y4,
  137. const Colour& colour)
  138. {
  139. const GLfloat vertices[] = { x1, y1, x2, y2, x4, y4, x3, y3 };
  140. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  141. setColour (colour);
  142. glEnableClientState (GL_VERTEX_ARRAY);
  143. glVertexPointer (2, GL_FLOAT, 0, vertices);
  144. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  145. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  146. glDisableClientState (GL_COLOR_ARRAY);
  147. glDisableClientState (GL_NORMAL_ARRAY);
  148. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  149. }
  150. void OpenGLHelpers::drawQuad3D (float x1, float y1, float z1,
  151. float x2, float y2, float z2,
  152. float x3, float y3, float z3,
  153. float x4, float y4, float z4,
  154. const Colour& colour)
  155. {
  156. const GLfloat vertices[] = { x1, y1, z1, x2, y2, z2, x4, y4, z4, x3, y3, z3 };
  157. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  158. setColour (colour);
  159. glEnableClientState (GL_VERTEX_ARRAY);
  160. glVertexPointer (3, GL_FLOAT, 0, vertices);
  161. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  162. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  163. glDisableClientState (GL_COLOR_ARRAY);
  164. glDisableClientState (GL_NORMAL_ARRAY);
  165. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  166. }
  167. void OpenGLHelpers::drawTriangleStrip (const GLfloat* const vertices, const GLfloat* const textureCoords, const int numVertices) noexcept
  168. {
  169. glEnable (GL_TEXTURE_2D);
  170. glDisableClientState (GL_COLOR_ARRAY);
  171. glDisableClientState (GL_NORMAL_ARRAY);
  172. glEnableClientState (GL_VERTEX_ARRAY);
  173. glVertexPointer (2, GL_FLOAT, 0, vertices);
  174. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  175. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  176. glDrawArrays (GL_TRIANGLE_STRIP, 0, numVertices);
  177. }
  178. void OpenGLHelpers::drawTriangleStrip (const GLfloat* const vertices, const GLfloat* const textureCoords,
  179. const int numVertices, const GLuint textureID) noexcept
  180. {
  181. jassert (textureID != 0);
  182. glBindTexture (GL_TEXTURE_2D, textureID);
  183. drawTriangleStrip (vertices, textureCoords, numVertices);
  184. glBindTexture (GL_TEXTURE_2D, 0);
  185. }
  186. void OpenGLHelpers::drawTextureQuad (GLuint textureID, int x, int y, int w, int h)
  187. {
  188. const GLfloat l = (GLfloat) x;
  189. const GLfloat t = (GLfloat) y;
  190. const GLfloat r = (GLfloat) (x + w);
  191. const GLfloat b = (GLfloat) (y + h);
  192. const GLfloat vertices[] = { l, t, r, t, l, b, r, b };
  193. const GLfloat textureCoords[] = { 0, 1.0f, 1.0f, 1.0f, 0, 0, 1.0f, 0 };
  194. drawTriangleStrip (vertices, textureCoords, 4, textureID);
  195. }
  196. void OpenGLHelpers::fillRectWithTexture (const Rectangle<int>& rect, GLuint textureID, const float alpha)
  197. {
  198. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  199. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  200. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  201. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  202. glColor4f (alpha, alpha, alpha, alpha);
  203. drawTextureQuad (textureID, rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
  204. }
  205. //==============================================================================
  206. void OpenGLHelpers::fillRectWithColour (const Rectangle<int>& rect, const Colour& colour)
  207. {
  208. glEnableClientState (GL_VERTEX_ARRAY);
  209. glDisableClientState (GL_TEXTURE_COORD_ARRAY);
  210. glDisableClientState (GL_COLOR_ARRAY);
  211. glDisableClientState (GL_NORMAL_ARRAY);
  212. setColour (colour);
  213. fillRect (rect);
  214. }
  215. void OpenGLHelpers::fillRect (const Rectangle<int>& rect)
  216. {
  217. const GLfloat vertices[] = { (GLfloat) rect.getX(), (GLfloat) rect.getY(),
  218. (GLfloat) rect.getRight(), (GLfloat) rect.getY(),
  219. (GLfloat) rect.getX(), (GLfloat) rect.getBottom(),
  220. (GLfloat) rect.getRight(), (GLfloat) rect.getBottom() };
  221. glVertexPointer (2, GL_FLOAT, 0, vertices);
  222. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  223. }
  224. //==============================================================================
  225. struct OpenGLEdgeTableRenderer
  226. {
  227. OpenGLEdgeTableRenderer() noexcept
  228. : lastAlpha (-1)
  229. {
  230. }
  231. void draw (const EdgeTable& et)
  232. {
  233. glDisableClientState (GL_TEXTURE_COORD_ARRAY);
  234. glEnableClientState (GL_VERTEX_ARRAY);
  235. glVertexPointer (2, GL_FLOAT, 0, vertices);
  236. et.iterate (*this);
  237. }
  238. void setEdgeTableYPos (const int y) noexcept
  239. {
  240. vertices[1] = vertices[5] = (GLfloat) y;
  241. vertices[3] = vertices[7] = (GLfloat) (y + 1);
  242. }
  243. void handleEdgeTablePixel (const int x, const int alphaLevel) noexcept
  244. {
  245. drawHorizontal (x, 1, alphaLevel);
  246. }
  247. void handleEdgeTablePixelFull (const int x) noexcept
  248. {
  249. drawHorizontal (x, 1, 255);
  250. }
  251. void handleEdgeTableLine (const int x, const int width, const int alphaLevel) noexcept
  252. {
  253. drawHorizontal (x, width, alphaLevel);
  254. }
  255. void handleEdgeTableLineFull (const int x, const int width) noexcept
  256. {
  257. drawHorizontal (x, width, 255);
  258. }
  259. private:
  260. GLfloat vertices[8];
  261. int lastAlpha;
  262. void drawHorizontal (int x, const int w, const int alphaLevel) noexcept
  263. {
  264. vertices[0] = vertices[2] = (GLfloat) x;
  265. vertices[4] = vertices[6] = (GLfloat) (x + w);
  266. if (lastAlpha != alphaLevel)
  267. {
  268. lastAlpha = alphaLevel;
  269. const float a = alphaLevel / 255.0f;
  270. glColor4f (a, a, a, a);
  271. }
  272. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  273. }
  274. JUCE_DECLARE_NON_COPYABLE (OpenGLEdgeTableRenderer);
  275. };
  276. void OpenGLHelpers::fillEdgeTable (const EdgeTable& edgeTable)
  277. {
  278. OpenGLEdgeTableRenderer etr;
  279. etr.draw (edgeTable);
  280. }
  281. //==============================================================================
  282. // This breaks down a path into a series of horizontal strips of trapezoids..
  283. class TriangulatedPath::TrapezoidedPath
  284. {
  285. public:
  286. TrapezoidedPath (const Path& p, const AffineTransform& transform)
  287. : firstSlice (nullptr),
  288. windingMask (p.isUsingNonZeroWinding() ? -1 : 1)
  289. {
  290. for (PathFlatteningIterator iter (p, transform); iter.next();)
  291. addLine (floatToInt (iter.x1), floatToInt (iter.y1),
  292. floatToInt (iter.x2), floatToInt (iter.y2));
  293. }
  294. ~TrapezoidedPath()
  295. {
  296. for (HorizontalSlice* s = firstSlice; s != nullptr;)
  297. {
  298. const ScopedPointer<HorizontalSlice> deleter (s);
  299. s = s->next;
  300. }
  301. }
  302. template <class Consumer>
  303. void iterate (Consumer& consumer) const
  304. {
  305. for (HorizontalSlice* s = firstSlice; s != nullptr; s = s->next)
  306. s->iterate (consumer, windingMask);
  307. }
  308. private:
  309. void addLine (int x1, int y1, int x2, int y2)
  310. {
  311. int winding = 1;
  312. if (y2 < y1)
  313. {
  314. std::swap (x1, x2);
  315. std::swap (y1, y2);
  316. winding = -1;
  317. }
  318. HorizontalSlice* last = nullptr;
  319. HorizontalSlice* s = firstSlice;
  320. while (y2 > y1)
  321. {
  322. if (s == nullptr)
  323. {
  324. insert (last, new HorizontalSlice (nullptr, x1, y1, x2, y2, winding));
  325. break;
  326. }
  327. if (s->y2 > y1)
  328. {
  329. if (y1 < s->y1)
  330. {
  331. if (y2 <= s->y1)
  332. {
  333. insert (last, new HorizontalSlice (s, x1, y1, x2, y2, winding));
  334. break;
  335. }
  336. else
  337. {
  338. const int newX = x1 + (int) ((s->y1 - y1) * (int64) (x2 - x1) / (y2 - y1));
  339. HorizontalSlice* const newSlice = new HorizontalSlice (s, x1, y1, newX, s->y1, winding);
  340. insert (last, newSlice);
  341. last = newSlice;
  342. x1 = newX;
  343. y1 = s->y1;
  344. continue;
  345. }
  346. }
  347. else if (y1 > s->y1)
  348. {
  349. s->split (y1);
  350. s = s->next;
  351. jassert (s != nullptr);
  352. }
  353. jassert (y1 == s->y1);
  354. if (y2 > s->y2)
  355. {
  356. const int newY = s->y2;
  357. const int newX = x1 + (int) ((newY - y1) * (int64) (x2 - x1) / (y2 - y1));
  358. s->addLine (x1, newX, winding);
  359. x1 = newX;
  360. y1 = newY;
  361. }
  362. else
  363. {
  364. if (y2 < s->y2)
  365. s->split (y2);
  366. jassert (y2 == s->y2);
  367. s->addLine (x1, x2, winding);
  368. break;
  369. }
  370. }
  371. last = s;
  372. s = s->next;
  373. }
  374. }
  375. struct HorizontalSlice
  376. {
  377. HorizontalSlice (const HorizontalSlice& other, HorizontalSlice* const next_, int y1_, int y2_)
  378. : next (next_), y1 (y1_), y2 (y2_), segments (other.segments)
  379. {
  380. }
  381. HorizontalSlice (HorizontalSlice* const next_, int x1, int y1_, int x2, int y2_, int winding)
  382. : next (next_), y1 (y1_), y2 (y2_)
  383. {
  384. jassert (next != this);
  385. jassert (y2 > y1);
  386. segments.ensureStorageAllocated (32);
  387. segments.add (LineSegment (x1, x2, winding));
  388. }
  389. void addLine (const int x1, const int x2, int winding)
  390. {
  391. const int dy = y2 - y1;
  392. for (int i = 0; i < segments.size(); ++i)
  393. {
  394. const LineSegment& l = segments.getReference (i);
  395. const int diff1 = l.x1 - x1;
  396. const int diff2 = l.x2 - x2;
  397. if ((diff1 < 0) == (diff2 > 0))
  398. {
  399. const int dx1 = l.x2 - l.x1;
  400. const int dx2 = x2 - x1;
  401. const int dxDiff = dx2 - dx1;
  402. if (dxDiff != 0)
  403. {
  404. const int intersectionY = (int) ((dy * (int64) diff1) / dxDiff);
  405. if (intersectionY > 0 && intersectionY < dy)
  406. {
  407. const int intersectionX = x1 + (intersectionY * dx2) / dy;
  408. split (intersectionY + y1);
  409. next->addLine (intersectionX, x2, winding);
  410. addLine (x1, intersectionX, winding);
  411. return;
  412. }
  413. }
  414. }
  415. if (diff1 + diff2 > 0)
  416. {
  417. segments.insert (i, LineSegment (x1, x2, winding));
  418. return;
  419. }
  420. }
  421. segments.add (LineSegment (x1, x2, winding));
  422. }
  423. void split (const int newY)
  424. {
  425. jassert (newY > y1 && newY < y2);
  426. const int dy1 = newY - y1;
  427. const int dy2 = y2 - y1;
  428. next = new HorizontalSlice (*this, next, newY, y2);
  429. y2 = newY;
  430. LineSegment* const oldSegments = segments.getRawDataPointer();
  431. LineSegment* const newSegments = next->segments.getRawDataPointer();
  432. for (int i = 0; i < segments.size(); ++i)
  433. {
  434. LineSegment& l = oldSegments[i];
  435. const int newX = l.x1 + (int) (dy1 * (int64) (l.x2 - l.x1) / dy2);
  436. newSegments[i].x1 = newX;
  437. l.x2 = newX;
  438. }
  439. }
  440. template <class Consumer>
  441. void iterate (Consumer& consumer, const int windingMask)
  442. {
  443. jassert (segments.size() > 0);
  444. const float fy1 = intToFloat (y1);
  445. const float fy2 = intToFloat (y2);
  446. const LineSegment* s1 = segments.getRawDataPointer();
  447. const LineSegment* s2 = s1;
  448. int winding = s1->winding;
  449. for (int i = segments.size(); --i > 0;)
  450. {
  451. ++s2;
  452. winding += s2->winding;
  453. if ((winding & windingMask) == 0)
  454. {
  455. const float ax1 = intToFloat (s1->x1);
  456. const float ax2 = intToFloat (s1->x2);
  457. if (s1->x1 == s2->x1)
  458. consumer.addTriangle (ax1, fy1, ax2, fy2, intToFloat (s2->x2), fy2);
  459. else if (s1->x2 == s2->x2)
  460. consumer.addTriangle (ax1, fy1, intToFloat (s2->x1), fy1, ax2, fy2);
  461. else
  462. consumer.addTrapezoid (fy1, fy2, ax1, ax2, intToFloat (s2->x1), intToFloat (s2->x2));
  463. s1 = s2 + 1;
  464. }
  465. }
  466. }
  467. HorizontalSlice* next;
  468. int y1, y2;
  469. private:
  470. struct LineSegment
  471. {
  472. inline LineSegment (int x1_, int x2_, int winding_) noexcept
  473. : x1 (x1_), x2 (x2_), winding (winding_) {}
  474. int x1, x2;
  475. int winding;
  476. };
  477. Array<LineSegment> segments;
  478. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HorizontalSlice);
  479. };
  480. HorizontalSlice* firstSlice;
  481. const int windingMask;
  482. inline void insert (HorizontalSlice* const last, HorizontalSlice* const newOne) noexcept
  483. {
  484. if (last == nullptr)
  485. firstSlice = newOne;
  486. else
  487. last->next = newOne;
  488. }
  489. enum { factor = 128 };
  490. static inline int floatToInt (const float n) noexcept { return roundToInt (n * (float) factor); }
  491. static inline float intToFloat (const int n) noexcept { return n * (1.0f / (float) factor); }
  492. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TrapezoidedPath);
  493. };
  494. //==============================================================================
  495. struct TriangulatedPath::TriangleBlock
  496. {
  497. TriangleBlock() noexcept
  498. : numVertices (0),
  499. triangles (maxVerticesPerBlock)
  500. {}
  501. void draw() const
  502. {
  503. glVertexPointer (2, GL_FLOAT, 0, triangles);
  504. glDrawArrays (GL_TRIANGLES, 0, numVertices / 2);
  505. }
  506. inline GLfloat* getNextTriangle() noexcept { return triangles + numVertices; }
  507. void optimiseStorage() { triangles.realloc (numVertices); }
  508. // Some GL implementations can't take very large triangle lists, so store
  509. // the list as a series of blocks containing this max number of triangles.
  510. enum { maxVerticesPerBlock = 256 * 6 };
  511. unsigned int numVertices;
  512. HeapBlock<GLfloat> triangles;
  513. };
  514. TriangulatedPath::TriangulatedPath (const Path& path, const AffineTransform& transform)
  515. {
  516. startNewBlock();
  517. TrapezoidedPath (path, transform).iterate (*this);
  518. }
  519. TriangulatedPath::~TriangulatedPath() {}
  520. void TriangulatedPath::draw (const int oversamplingLevel) const
  521. {
  522. const float a = 1.0f / (oversamplingLevel * oversamplingLevel);
  523. glColor4f (a, a, a, a);
  524. glPushMatrix();
  525. glTranslatef (-0.5f, -0.5f, 0.0f);
  526. const float inc = 1.0f / oversamplingLevel;
  527. for (int y = oversamplingLevel; --y >= 0;)
  528. {
  529. for (int x = oversamplingLevel; --x >= 0;)
  530. {
  531. glTranslatef (inc, 0.0f, 0.0f);
  532. for (int i = 0; i < blocks.size(); ++i)
  533. blocks.getUnchecked(i)->draw();
  534. }
  535. glTranslatef (-1.0f, inc, 0.0f);
  536. }
  537. glPopMatrix();
  538. }
  539. void TriangulatedPath::optimiseStorage()
  540. {
  541. currentBlock->optimiseStorage();
  542. }
  543. void TriangulatedPath::startNewBlock()
  544. {
  545. currentBlock = new TriangleBlock();
  546. blocks.add (currentBlock);
  547. }
  548. void TriangulatedPath::addTriangle (GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3)
  549. {
  550. if (currentBlock->numVertices >= TriangleBlock::maxVerticesPerBlock)
  551. startNewBlock();
  552. GLfloat* t = currentBlock->getNextTriangle();
  553. *t++ = x1; *t++ = y1; *t++ = x2; *t++ = y2; *t++ = x3; *t++ = y3;
  554. currentBlock->numVertices += 6;
  555. }
  556. void TriangulatedPath::addTrapezoid (GLfloat y1, GLfloat y2, GLfloat x1, GLfloat x2, GLfloat x3, GLfloat x4)
  557. {
  558. if (currentBlock->numVertices >= TriangleBlock::maxVerticesPerBlock - 6)
  559. startNewBlock();
  560. GLfloat* t = currentBlock->getNextTriangle();
  561. *t++ = x1; *t++ = y1; *t++ = x2; *t++ = y2; *t++ = x3; *t++ = y1;
  562. *t++ = x4; *t++ = y2; *t++ = x2; *t++ = y2; *t++ = x3; *t++ = y1;
  563. currentBlock->numVertices += 12;
  564. }
  565. //==============================================================================
  566. OpenGLTextureFromImage::OpenGLTextureFromImage (const Image& image)
  567. : width (image.getWidth()),
  568. height (image.getHeight())
  569. {
  570. OpenGLFrameBufferImage* glImage = dynamic_cast <OpenGLFrameBufferImage*> (image.getSharedImage());
  571. if (glImage != nullptr)
  572. {
  573. textureID = glImage->frameBuffer.getTextureID();
  574. }
  575. else
  576. {
  577. if (OpenGLTexture::isValidSize (width, height))
  578. {
  579. texture = new OpenGLTexture();
  580. texture->load (image);
  581. textureID = texture->getTextureID();
  582. }
  583. else
  584. {
  585. frameBuffer = new OpenGLFrameBuffer();
  586. frameBuffer->initialise (image);
  587. textureID = frameBuffer->getTextureID();
  588. }
  589. }
  590. }
  591. OpenGLTextureFromImage::~OpenGLTextureFromImage() {}
  592. //==============================================================================
  593. OpenGLRenderingTarget::OpenGLRenderingTarget() {}
  594. OpenGLRenderingTarget::~OpenGLRenderingTarget() {}
  595. void OpenGLRenderingTarget::prepareFor2D()
  596. {
  597. OpenGLHelpers::prepareFor2D (getRenderingTargetWidth(),
  598. getRenderingTargetHeight());
  599. }
  600. END_JUCE_NAMESPACE