Canvas.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /**
  2. * Copyright (c) 2006-2013 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Canvas.h"
  21. #include "Image.h"
  22. #include "Graphics.h"
  23. #include "common/Matrix.h"
  24. #include "common/config.h"
  25. #include <cstring> // For memcpy
  26. #include <limits>
  27. namespace love
  28. {
  29. namespace graphics
  30. {
  31. namespace opengl
  32. {
  33. // strategy for fbo creation, interchangable at runtime:
  34. // none, opengl >= 3.0, extensions
  35. struct FramebufferStrategy
  36. {
  37. virtual ~FramebufferStrategy() {}
  38. /// create a new framebuffer and texture
  39. /**
  40. * @param[out] framebuffer Framebuffer name
  41. * @param[out] img Texture name
  42. * @param[in] width Width of framebuffer
  43. * @param[in] height Height of framebuffer
  44. * @param[in] texture_type Type of the canvas texture.
  45. * @return Creation status
  46. */
  47. virtual GLenum createFBO(GLuint &, GLuint &, int, int, Canvas::TextureType)
  48. {
  49. return GL_FRAMEBUFFER_UNSUPPORTED;
  50. }
  51. /// Create a stencil buffer and attach it to the active framebuffer object
  52. /**
  53. * @param[in] width Width of the stencil buffer
  54. * @param[in] height Height of the stencil buffer
  55. * @param[out] stencil Name for stencil buffer
  56. * @return Whether the stencil buffer was successfully created
  57. **/
  58. virtual bool createStencil(int, int, GLuint &)
  59. {
  60. return false;
  61. }
  62. /// remove objects
  63. /**
  64. * @param[in] framebuffer Framebuffer name
  65. * @param[in] depth_stencil Name for packed depth and stencil buffer
  66. * @param[in] img Texture name
  67. */
  68. virtual void deleteFBO(GLuint, GLuint, GLuint) {}
  69. virtual void bindFBO(GLuint) {}
  70. /// attach additional canvases to the active framebuffer for rendering
  71. /**
  72. * @param[in] canvases List of canvases to attach
  73. **/
  74. virtual void setAttachments(const std::vector<Canvas *> &) {}
  75. /// stop using all additional attached canvases
  76. virtual void setAttachments() {}
  77. };
  78. struct FramebufferStrategyGL3 : public FramebufferStrategy
  79. {
  80. virtual GLenum createFBO(GLuint &framebuffer, GLuint &img, int width, int height, Canvas::TextureType texture_type)
  81. {
  82. // get currently bound fbo to reset to it later
  83. GLint current_fbo;
  84. glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &current_fbo);
  85. // create framebuffer
  86. glGenFramebuffers(1, &framebuffer);
  87. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  88. // generate texture save target
  89. GLint internalFormat;
  90. GLenum format;
  91. switch (texture_type)
  92. {
  93. case Canvas::TYPE_HDR:
  94. internalFormat = GL_RGBA16F;
  95. format = GL_FLOAT;
  96. break;
  97. case Canvas::TYPE_NORMAL:
  98. default:
  99. internalFormat = GL_RGBA8;
  100. format = GL_UNSIGNED_BYTE;
  101. }
  102. glGenTextures(1, &img);
  103. gl.bindTexture(img);
  104. gl.setTextureFilter(Image::getDefaultFilter());
  105. glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height,
  106. 0, GL_RGBA, format, NULL);
  107. gl.bindTexture(0);
  108. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  109. GL_TEXTURE_2D, img, 0);
  110. // check status
  111. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  112. // unbind framebuffer
  113. glBindFramebuffer(GL_FRAMEBUFFER, (GLuint) current_fbo);
  114. return status;
  115. }
  116. virtual bool createStencil(int width, int height, GLuint &stencil)
  117. {
  118. // create combined depth/stencil buffer
  119. glDeleteRenderbuffers(1, &stencil);
  120. glGenRenderbuffers(1, &stencil);
  121. glBindRenderbuffer(GL_RENDERBUFFER, stencil);
  122. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_STENCIL, width, height);
  123. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
  124. GL_RENDERBUFFER, stencil);
  125. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  126. GL_RENDERBUFFER, stencil);
  127. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  128. // check status
  129. return glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
  130. }
  131. virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil, GLuint img)
  132. {
  133. gl.deleteTexture(img);
  134. glDeleteRenderbuffers(1, &depth_stencil);
  135. glDeleteFramebuffers(1, &framebuffer);
  136. }
  137. virtual void bindFBO(GLuint framebuffer)
  138. {
  139. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  140. }
  141. virtual void setAttachments()
  142. {
  143. // set a single render target
  144. glDrawBuffer(GL_COLOR_ATTACHMENT0);
  145. }
  146. virtual void setAttachments(const std::vector<Canvas *> &canvases)
  147. {
  148. if (canvases.size() == 0)
  149. {
  150. setAttachments();
  151. return;
  152. }
  153. std::vector<GLenum> drawbuffers;
  154. drawbuffers.push_back(GL_COLOR_ATTACHMENT0);
  155. // attach the canvas framebuffer textures to the currently bound framebuffer
  156. for (size_t i = 0; i < canvases.size(); i++)
  157. {
  158. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 + i,
  159. GL_TEXTURE_2D, canvases[i]->getTextureName(), 0);
  160. drawbuffers.push_back(GL_COLOR_ATTACHMENT1 + i);
  161. }
  162. // set up multiple render targets
  163. if (GLEE_VERSION_2_0)
  164. glDrawBuffers(drawbuffers.size(), &drawbuffers[0]);
  165. else if (GLEE_ARB_draw_buffers)
  166. glDrawBuffersARB(drawbuffers.size(), &drawbuffers[0]);
  167. else if (GLEE_ATI_draw_buffers)
  168. glDrawBuffersATI(drawbuffers.size(), &drawbuffers[0]);
  169. }
  170. };
  171. struct FramebufferStrategyPackedEXT : public FramebufferStrategy
  172. {
  173. virtual GLenum createFBO(GLuint &framebuffer, GLuint &img, int width, int height, Canvas::TextureType texture_type)
  174. {
  175. GLint current_fbo;
  176. glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &current_fbo);
  177. // create framebuffer
  178. glGenFramebuffersEXT(1, &framebuffer);
  179. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
  180. // generate texture save target
  181. GLint internalFormat;
  182. GLenum format;
  183. switch (texture_type)
  184. {
  185. case Canvas::TYPE_HDR:
  186. internalFormat = GL_RGBA16F;
  187. format = GL_FLOAT;
  188. break;
  189. case Canvas::TYPE_NORMAL:
  190. default:
  191. internalFormat = GL_RGBA8;
  192. format = GL_UNSIGNED_BYTE;
  193. }
  194. glGenTextures(1, &img);
  195. gl.bindTexture(img);
  196. gl.setTextureFilter(Image::getDefaultFilter());
  197. glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height,
  198. 0, GL_RGBA, format, NULL);
  199. gl.bindTexture(0);
  200. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  201. GL_TEXTURE_2D, img, 0);
  202. // check status
  203. GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  204. // unbind framebuffer
  205. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (GLuint) current_fbo);
  206. return status;
  207. }
  208. virtual bool createStencil(int width, int height, GLuint &stencil)
  209. {
  210. // create combined depth/stencil buffer
  211. glDeleteRenderbuffers(1, &stencil);
  212. glGenRenderbuffersEXT(1, &stencil);
  213. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencil);
  214. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT,
  215. width, height);
  216. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  217. GL_RENDERBUFFER_EXT, stencil);
  218. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  219. GL_RENDERBUFFER_EXT, stencil);
  220. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  221. // check status
  222. return glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT;
  223. }
  224. virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil, GLuint img)
  225. {
  226. gl.deleteTexture(img);
  227. glDeleteRenderbuffersEXT(1, &depth_stencil);
  228. glDeleteFramebuffersEXT(1, &framebuffer);
  229. }
  230. virtual void bindFBO(GLuint framebuffer)
  231. {
  232. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
  233. }
  234. virtual void setAttachments()
  235. {
  236. // set a single render target
  237. glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
  238. }
  239. virtual void setAttachments(const std::vector<Canvas *> &canvases)
  240. {
  241. if (canvases.size() == 0)
  242. {
  243. setAttachments();
  244. return;
  245. }
  246. std::vector<GLenum> drawbuffers;
  247. drawbuffers.push_back(GL_COLOR_ATTACHMENT0_EXT);
  248. // attach the canvas framebuffer textures to the currently bound framebuffer
  249. for (size_t i = 0; i < canvases.size(); i++)
  250. {
  251. glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1_EXT + i,
  252. GL_TEXTURE_2D, canvases[i]->getTextureName(), 0);
  253. drawbuffers.push_back(GL_COLOR_ATTACHMENT1_EXT + i);
  254. }
  255. // set up multiple render targets
  256. if (GLEE_VERSION_2_0)
  257. glDrawBuffers(drawbuffers.size(), &drawbuffers[0]);
  258. else if (GLEE_ARB_draw_buffers)
  259. glDrawBuffersARB(drawbuffers.size(), &drawbuffers[0]);
  260. else if (GLEE_ATI_draw_buffers)
  261. glDrawBuffersATI(drawbuffers.size(), &drawbuffers[0]);
  262. }
  263. };
  264. struct FramebufferStrategyEXT : public FramebufferStrategyPackedEXT
  265. {
  266. virtual bool createStencil(int width, int height, GLuint &stencil)
  267. {
  268. // create stencil buffer
  269. glDeleteRenderbuffers(1, &stencil);
  270. glGenRenderbuffersEXT(1, &stencil);
  271. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencil);
  272. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX,
  273. width, height);
  274. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  275. GL_RENDERBUFFER_EXT, stencil);
  276. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  277. // check status
  278. return glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT;
  279. }
  280. bool isSupported()
  281. {
  282. GLuint fb = 0, stencil = 0, img = 0;
  283. GLenum status = createFBO(fb, img, 2, 2, Canvas::TYPE_NORMAL);
  284. deleteFBO(fb, stencil, img);
  285. return status == GL_FRAMEBUFFER_COMPLETE;
  286. }
  287. };
  288. FramebufferStrategy *strategy = NULL;
  289. FramebufferStrategy strategyNone;
  290. FramebufferStrategyGL3 strategyGL3;
  291. FramebufferStrategyPackedEXT strategyPackedEXT;
  292. FramebufferStrategyEXT strategyEXT;
  293. Canvas *Canvas::current = NULL;
  294. static void getStrategy()
  295. {
  296. if (!strategy)
  297. {
  298. if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_object)
  299. strategy = &strategyGL3;
  300. else if (GLEE_EXT_framebuffer_object && GLEE_EXT_packed_depth_stencil)
  301. strategy = &strategyPackedEXT;
  302. else if (GLEE_EXT_framebuffer_object && strategyEXT.isSupported())
  303. strategy = &strategyEXT;
  304. else
  305. strategy = &strategyNone;
  306. }
  307. }
  308. static int maxFBOColorAttachments = 0;
  309. static int maxDrawBuffers = 0;
  310. Canvas::Canvas(int width, int height, TextureType texture_type)
  311. : width(width)
  312. , height(height)
  313. , texture_type(texture_type)
  314. {
  315. float w = static_cast<float>(width);
  316. float h = static_cast<float>(height);
  317. // world coordinates
  318. vertices[0].x = 0;
  319. vertices[0].y = h;
  320. vertices[1].x = 0;
  321. vertices[1].y = 0;
  322. vertices[2].x = w;
  323. vertices[2].y = 0;
  324. vertices[3].x = w;
  325. vertices[3].y = h;
  326. // texture coordinates
  327. vertices[0].s = 0;
  328. vertices[0].t = 0;
  329. vertices[1].s = 0;
  330. vertices[1].t = 1;
  331. vertices[2].s = 1;
  332. vertices[2].t = 1;
  333. vertices[3].s = 1;
  334. vertices[3].t = 0;
  335. settings.filter = Image::getDefaultFilter();
  336. getStrategy();
  337. loadVolatile();
  338. }
  339. Canvas::~Canvas()
  340. {
  341. // reset framebuffer if still using this one
  342. if (current == this)
  343. stopGrab();
  344. unloadVolatile();
  345. }
  346. bool Canvas::isSupported()
  347. {
  348. getStrategy();
  349. return (strategy != &strategyNone);
  350. }
  351. bool Canvas::isHDRSupported()
  352. {
  353. return GLEE_VERSION_3_0 || (isSupported() && GLEE_ARB_texture_float);
  354. }
  355. bool Canvas::isMultiCanvasSupported()
  356. {
  357. if (!(isSupported() && (GLEE_VERSION_2_0 || GLEE_ARB_draw_buffers || GLEE_ATI_draw_buffers)))
  358. return false;
  359. if (maxFBOColorAttachments == 0 || maxDrawBuffers == 0)
  360. {
  361. glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxFBOColorAttachments);
  362. glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
  363. }
  364. // system must support at least 4 simultanious active canvases
  365. return maxFBOColorAttachments >= 4 && maxDrawBuffers >= 4;
  366. }
  367. void Canvas::bindDefaultCanvas()
  368. {
  369. if (current != NULL)
  370. current->stopGrab();
  371. }
  372. void Canvas::setupGrab()
  373. {
  374. // already grabbing
  375. if (current == this)
  376. return;
  377. // cleanup after previous fbo
  378. if (current != NULL)
  379. current->stopGrab();
  380. // bind buffer and clear screen
  381. glPushAttrib(GL_VIEWPORT_BIT | GL_TRANSFORM_BIT);
  382. strategy->bindFBO(fbo);
  383. gl.setViewport(OpenGL::Viewport(0, 0, width, height));
  384. // Reset the projection matrix
  385. glMatrixMode(GL_PROJECTION);
  386. glPushMatrix();
  387. glLoadIdentity();
  388. // Set up orthographic view (no depth)
  389. glOrtho(0.0, width, height, 0.0, -1.0, 1.0);
  390. // Switch back to modelview matrix
  391. glMatrixMode(GL_MODELVIEW);
  392. // indicate we are using this fbo
  393. current = this;
  394. }
  395. void Canvas::startGrab(const std::vector<Canvas *> &canvases)
  396. {
  397. if (canvases.size() > 0)
  398. {
  399. if (!isMultiCanvasSupported())
  400. throw love::Exception("Multi-canvas rendering is not supported on this system.");
  401. if (canvases.size()+1 > size_t(maxDrawBuffers) || canvases.size()+1 > size_t(maxFBOColorAttachments))
  402. throw love::Exception("This system can't simultaniously render to %d canvases.", canvases.size()+1);
  403. }
  404. for (size_t i = 0; i < canvases.size(); i++)
  405. {
  406. if (canvases[i]->getWidth() != width || canvases[i]->getHeight() != height)
  407. throw love::Exception("All canvas arguments must have the same dimensions.");
  408. if (canvases[i]->getTextureType() != texture_type)
  409. throw love::Exception("All canvas arguments must have the same texture type.");
  410. }
  411. setupGrab();
  412. // don't attach anything if there's nothing to attach/detach
  413. if (canvases.size() == 0 && attachedCanvases.size() == 0)
  414. return;
  415. // attach the canvas textures to the active FBO and set up multiple render targets
  416. strategy->setAttachments(canvases);
  417. // retain newly attached canvases
  418. for (size_t i = 0; i < canvases.size(); i++)
  419. canvases[i]->retain();
  420. // release any old canvases
  421. for (size_t i = 0; i < attachedCanvases.size(); i++)
  422. attachedCanvases[i]->release();
  423. attachedCanvases = canvases;
  424. }
  425. void Canvas::startGrab()
  426. {
  427. setupGrab();
  428. if (attachedCanvases.size() == 0)
  429. return;
  430. // make sure the FBO is only using a single canvas
  431. strategy->setAttachments();
  432. // release any previously attached canvases
  433. for (size_t i = 0; i < attachedCanvases.size(); i++)
  434. attachedCanvases[i]->release();
  435. attachedCanvases.clear();
  436. }
  437. void Canvas::stopGrab()
  438. {
  439. // i am not grabbing. leave me alone
  440. if (current != this)
  441. return;
  442. // bind default
  443. strategy->bindFBO(0);
  444. glMatrixMode(GL_PROJECTION);
  445. glPopMatrix();
  446. glPopAttrib();
  447. current = NULL;
  448. }
  449. void Canvas::clear(const Color &c)
  450. {
  451. GLuint previous = 0;
  452. if (current != this)
  453. {
  454. if (current != NULL)
  455. previous = current->fbo;
  456. strategy->bindFBO(fbo);
  457. }
  458. // Make sure only this canvas is cleared when multi-canvas rendering is set.
  459. if (attachedCanvases.size() > 0)
  460. strategy->setAttachments();
  461. // Don't use the state-shadowed gl.setClearColor because we want to save the
  462. // previous clear color.
  463. glClearColor((float)c.r/255.0f, (float)c.g/255.0f, (float)c.b/255.0f, (float)c.a/255.0f);
  464. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  465. if (attachedCanvases.size() > 0)
  466. strategy->setAttachments(attachedCanvases);
  467. if (current != this)
  468. strategy->bindFBO(previous);
  469. // Restore the previous clear color.
  470. gl.setClearColor(gl.getClearColor());
  471. }
  472. void Canvas::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
  473. {
  474. static Matrix t;
  475. t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
  476. drawv(t, vertices);
  477. }
  478. void Canvas::drawg(love::graphics::Geometry *geom, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
  479. {
  480. static Matrix t;
  481. t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
  482. // flip texture coordinates vertically
  483. size_t vcount = geom->getVertexCount();
  484. const vertex *w = geom->getVertexArray();
  485. vertex *v = new vertex[vcount];
  486. for (size_t i = 0; i < vcount; ++i)
  487. {
  488. v[i] = w[i];
  489. v[i].t = 1. - v[i].t;
  490. }
  491. // use colors stored in geometry (horrible, horrible hack)
  492. if (geom->hasVertexColors())
  493. {
  494. glEnableClientState(GL_COLOR_ARRAY);
  495. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(vertex), (GLvoid *)&v->r);
  496. }
  497. drawv(t, v, vcount, GL_TRIANGLE_FAN);
  498. if (geom->hasVertexColors())
  499. {
  500. glDisableClientState(GL_COLOR_ARRAY);
  501. gl.setColor(gl.getColor());
  502. }
  503. delete[] v;
  504. }
  505. bool Canvas::checkCreateStencil()
  506. {
  507. // Do nothing if we've already created the stencil buffer.
  508. if (depth_stencil)
  509. return true;
  510. if (current != this)
  511. strategy->bindFBO(fbo);
  512. bool success = strategy->createStencil(width, height, depth_stencil);
  513. if (current && current != this)
  514. strategy->bindFBO(current->fbo);
  515. else if (!current)
  516. strategy->bindFBO(0);
  517. return success;
  518. }
  519. love::image::ImageData *Canvas::getImageData(love::image::Image *image)
  520. {
  521. int row = 4 * width;
  522. int size = row * height;
  523. GLubyte *pixels = new GLubyte[size];
  524. GLubyte *flipped = new GLubyte[size];
  525. strategy->bindFBO(fbo);
  526. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  527. if (current)
  528. strategy->bindFBO(current->fbo);
  529. else
  530. strategy->bindFBO(0);
  531. GLubyte *src = pixels, *dst = flipped + size - row;
  532. for (int i = 0; i < height; ++i, dst -= row, src += row)
  533. memcpy(dst, src, row);
  534. love::image::ImageData *img = image->newImageData(width, height, (void *)flipped);
  535. delete[] pixels;
  536. delete[] flipped;
  537. return img;
  538. }
  539. void Canvas::getPixel(unsigned char* pixel_rgba, int x, int y)
  540. {
  541. if (current != this)
  542. strategy->bindFBO(fbo);
  543. glReadPixels(x, height - y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel_rgba);
  544. if (current && current != this)
  545. strategy->bindFBO(current->fbo);
  546. else if (!current)
  547. strategy->bindFBO(0);
  548. }
  549. const std::vector<Canvas *> &Canvas::getAttachedCanvases() const
  550. {
  551. return attachedCanvases;
  552. }
  553. void Canvas::setFilter(const Image::Filter &f)
  554. {
  555. gl.bindTexture(img);
  556. gl.setTextureFilter(f);
  557. }
  558. Image::Filter Canvas::getFilter() const
  559. {
  560. gl.bindTexture(img);
  561. return gl.getTextureFilter();
  562. }
  563. void Canvas::setWrap(const Image::Wrap &w)
  564. {
  565. gl.bindTexture(img);
  566. gl.setTextureWrap(w);
  567. }
  568. Image::Wrap Canvas::getWrap() const
  569. {
  570. gl.bindTexture(img);
  571. return gl.getTextureWrap();
  572. }
  573. bool Canvas::loadVolatile()
  574. {
  575. // glTexImage2D is guaranteed to error in this case.
  576. if (width > gl.getMaxTextureSize() || height > gl.getMaxTextureSize())
  577. return false;
  578. status = strategy->createFBO(fbo, img, width, height, texture_type);
  579. if (status != GL_FRAMEBUFFER_COMPLETE)
  580. return false;
  581. setFilter(settings.filter);
  582. setWrap(settings.wrap);
  583. Color c(0, 0, 0, 0);
  584. clear(c);
  585. return true;
  586. }
  587. void Canvas::unloadVolatile()
  588. {
  589. settings.filter = getFilter();
  590. settings.wrap = getWrap();
  591. strategy->deleteFBO(fbo, depth_stencil, img);
  592. for (size_t i = 0; i < attachedCanvases.size(); i++)
  593. attachedCanvases[i]->release();
  594. attachedCanvases.clear();
  595. }
  596. int Canvas::getWidth()
  597. {
  598. return width;
  599. }
  600. int Canvas::getHeight()
  601. {
  602. return height;
  603. }
  604. void Canvas::drawv(const Matrix &t, const vertex *v, GLsizei count, GLenum mode) const
  605. {
  606. glPushMatrix();
  607. glMultMatrixf((const GLfloat *)t.getElements());
  608. gl.bindTexture(img);
  609. glEnableClientState(GL_VERTEX_ARRAY);
  610. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  611. // XXX: drawg() enables/disables GL_COLOR_ARRAY in order to use the color
  612. // defined in the geometry to draw itself.
  613. // if the drawing method below is changed to use something other than
  614. // glDrawArrays(), drawg() needs to be updated accordingly!
  615. glVertexPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&v[0].x);
  616. glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&v[0].s);
  617. glDrawArrays(mode, 0, count);
  618. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  619. glDisableClientState(GL_VERTEX_ARRAY);
  620. glPopMatrix();
  621. }
  622. bool Canvas::getConstant(const char *in, Canvas::TextureType &out)
  623. {
  624. return textureTypes.find(in, out);
  625. }
  626. bool Canvas::getConstant(Canvas::TextureType in, const char *&out)
  627. {
  628. return textureTypes.find(in, out);
  629. }
  630. StringMap<Canvas::TextureType, Canvas::TYPE_MAX_ENUM>::Entry Canvas::textureTypeEntries[] =
  631. {
  632. {"normal", Canvas::TYPE_NORMAL},
  633. {"hdr", Canvas::TYPE_HDR},
  634. };
  635. StringMap<Canvas::TextureType, Canvas::TYPE_MAX_ENUM> Canvas::textureTypes(Canvas::textureTypeEntries, sizeof(Canvas::textureTypeEntries));
  636. } // opengl
  637. } // graphics
  638. } // love