Canvas.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /**
  2. * Copyright (c) 2006-2012 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 "Graphics.h"
  22. #include "common/Matrix.h"
  23. #include <cstring> // For memcpy
  24. namespace love
  25. {
  26. namespace graphics
  27. {
  28. namespace opengl
  29. {
  30. // strategy for fbo creation, interchangable at runtime:
  31. // none, opengl >= 3.0, extensions
  32. struct FramebufferStrategy
  33. {
  34. /// create a new framebuffer, depth_stencil and texture
  35. /**
  36. * @param[out] framebuffer Framebuffer name
  37. * @param[out] depth_stencil Name for packed depth and stencil buffer
  38. * @param[out] img Texture name
  39. * @param[in] width Width of framebuffer
  40. * @param[in] height Height of framebuffer
  41. * @param[in] texture_type Type of the canvas texture.
  42. * @return Creation status
  43. */
  44. virtual GLenum createFBO(GLuint &, GLuint &, GLuint &, int, int, Canvas::TextureType)
  45. {
  46. return GL_FRAMEBUFFER_UNSUPPORTED;
  47. }
  48. /// remove objects
  49. /**
  50. * @param[in] framebuffer Framebuffer name
  51. * @param[in] depth_stencil Name for packed depth and stencil buffer
  52. * @param[in] img Texture name
  53. */
  54. virtual void deleteFBO(GLuint, GLuint, GLuint) {}
  55. virtual void bindFBO(GLuint) {}
  56. };
  57. struct FramebufferStrategyGL3 : public FramebufferStrategy
  58. {
  59. virtual GLenum createFBO(GLuint &framebuffer, GLuint &depth_stencil, GLuint &img, int width, int height, Canvas::TextureType texture_type)
  60. {
  61. // get currently bound fbo to reset to it later
  62. GLint current_fbo;
  63. glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &current_fbo);
  64. // create framebuffer
  65. glGenFramebuffers(1, &framebuffer);
  66. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  67. // create stencil buffer
  68. glGenRenderbuffers(1, &depth_stencil);
  69. glBindRenderbuffer(GL_RENDERBUFFER, depth_stencil);
  70. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_STENCIL, width, height);
  71. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
  72. GL_RENDERBUFFER, depth_stencil);
  73. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  74. GL_RENDERBUFFER, depth_stencil);
  75. // generate texture save target
  76. GLint internalFormat;
  77. GLenum format;
  78. switch (texture_type)
  79. {
  80. case Canvas::TYPE_HDR:
  81. internalFormat = GL_RGBA16F;
  82. format = GL_FLOAT;
  83. break;
  84. case Canvas::TYPE_NORMAL:
  85. default:
  86. internalFormat = GL_RGBA8;
  87. format = GL_UNSIGNED_BYTE;
  88. }
  89. glGenTextures(1, &img);
  90. bindTexture(img);
  91. setTextureFilter(Image::getDefaultFilter());
  92. glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height,
  93. 0, GL_RGBA, format, NULL);
  94. bindTexture(0);
  95. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  96. GL_TEXTURE_2D, img, 0);
  97. // check status
  98. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  99. // unbind framebuffer
  100. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  101. glBindFramebuffer(GL_FRAMEBUFFER, (GLuint) current_fbo);
  102. return status;
  103. }
  104. virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil, GLuint img)
  105. {
  106. deleteTexture(img);
  107. glDeleteRenderbuffers(1, &depth_stencil);
  108. glDeleteFramebuffers(1, &framebuffer);
  109. }
  110. virtual void bindFBO(GLuint framebuffer)
  111. {
  112. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  113. }
  114. };
  115. struct FramebufferStrategyPackedEXT : public FramebufferStrategy
  116. {
  117. virtual GLenum createFBO(GLuint &framebuffer, GLuint &depth_stencil, GLuint &img, int width, int height, Canvas::TextureType texture_type)
  118. {
  119. GLint current_fbo;
  120. glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &current_fbo);
  121. // create framebuffer
  122. glGenFramebuffersEXT(1, &framebuffer);
  123. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
  124. // create stencil buffer
  125. glGenRenderbuffersEXT(1, &depth_stencil);
  126. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_stencil);
  127. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT,
  128. width, height);
  129. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  130. GL_RENDERBUFFER_EXT, depth_stencil);
  131. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  132. GL_RENDERBUFFER_EXT, depth_stencil);
  133. // generate texture save target
  134. GLint internalFormat;
  135. GLenum format;
  136. switch (texture_type)
  137. {
  138. case Canvas::TYPE_HDR:
  139. internalFormat = GL_RGBA16F;
  140. format = GL_FLOAT;
  141. break;
  142. case Canvas::TYPE_NORMAL:
  143. default:
  144. internalFormat = GL_RGBA8;
  145. format = GL_UNSIGNED_BYTE;
  146. }
  147. glGenTextures(1, &img);
  148. bindTexture(img);
  149. setTextureFilter(Image::getDefaultFilter());
  150. glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height,
  151. 0, GL_RGBA, format, NULL);
  152. bindTexture(0);
  153. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  154. GL_TEXTURE_2D, img, 0);
  155. // check status
  156. GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  157. // unbind framebuffer
  158. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  159. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (GLuint) current_fbo);
  160. return status;
  161. }
  162. virtual void deleteFBO(GLuint framebuffer, GLuint depth_stencil, GLuint img)
  163. {
  164. deleteTexture(img);
  165. glDeleteRenderbuffersEXT(1, &depth_stencil);
  166. glDeleteFramebuffersEXT(1, &framebuffer);
  167. }
  168. virtual void bindFBO(GLuint framebuffer)
  169. {
  170. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
  171. }
  172. };
  173. struct FramebufferStrategyEXT : public FramebufferStrategyPackedEXT
  174. {
  175. virtual GLenum createFBO(GLuint &framebuffer, GLuint &stencil, GLuint &img, int width, int height, Canvas::TextureType texture_type)
  176. {
  177. GLint current_fbo;
  178. glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &current_fbo);
  179. // create framebuffer
  180. glGenFramebuffersEXT(1, &framebuffer);
  181. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
  182. // create stencil buffer
  183. glGenRenderbuffersEXT(1, &stencil);
  184. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencil);
  185. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX,
  186. width, height);
  187. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  188. GL_RENDERBUFFER_EXT, stencil);
  189. // generate texture save target
  190. GLint internalFormat;
  191. GLenum format;
  192. switch (texture_type)
  193. {
  194. case Canvas::TYPE_HDR:
  195. internalFormat = GL_RGBA16F;
  196. format = GL_FLOAT;
  197. break;
  198. case Canvas::TYPE_NORMAL:
  199. default:
  200. internalFormat = GL_RGBA8;
  201. format = GL_UNSIGNED_BYTE;
  202. }
  203. glGenTextures(1, &img);
  204. bindTexture(img);
  205. setTextureFilter(Image::getDefaultFilter());
  206. glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height,
  207. 0, GL_RGBA, format, NULL);
  208. bindTexture(0);
  209. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  210. GL_TEXTURE_2D, img, 0);
  211. // check status
  212. GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  213. // unbind framebuffer
  214. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  215. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (GLuint) current_fbo);
  216. return status;
  217. }
  218. bool isSupported()
  219. {
  220. GLuint fb, stencil, img;
  221. GLenum status = createFBO(fb, stencil, img, 2, 2, Canvas::TYPE_NORMAL);
  222. deleteFBO(fb, stencil, img);
  223. return status == GL_FRAMEBUFFER_COMPLETE;
  224. }
  225. };
  226. FramebufferStrategy *strategy = NULL;
  227. FramebufferStrategy strategyNone;
  228. FramebufferStrategyGL3 strategyGL3;
  229. FramebufferStrategyPackedEXT strategyPackedEXT;
  230. FramebufferStrategyEXT strategyEXT;
  231. Canvas *Canvas::current = NULL;
  232. static void getStrategy()
  233. {
  234. if (!strategy)
  235. {
  236. if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_object)
  237. strategy = &strategyGL3;
  238. else if (GLEE_EXT_framebuffer_object && GLEE_EXT_packed_depth_stencil)
  239. strategy = &strategyPackedEXT;
  240. else if (GLEE_EXT_framebuffer_object && strategyEXT.isSupported())
  241. strategy = &strategyEXT;
  242. else
  243. strategy = &strategyNone;
  244. }
  245. }
  246. Canvas::Canvas(int width, int height, TextureType texture_type)
  247. : width(width)
  248. , height(height)
  249. , texture_type(texture_type)
  250. {
  251. float w = static_cast<float>(width);
  252. float h = static_cast<float>(height);
  253. // world coordinates
  254. vertices[0].x = 0;
  255. vertices[0].y = h;
  256. vertices[1].x = 0;
  257. vertices[1].y = 0;
  258. vertices[2].x = w;
  259. vertices[2].y = 0;
  260. vertices[3].x = w;
  261. vertices[3].y = h;
  262. // texture coordinates
  263. vertices[0].s = 0;
  264. vertices[0].t = 0;
  265. vertices[1].s = 0;
  266. vertices[1].t = 1;
  267. vertices[2].s = 1;
  268. vertices[2].t = 1;
  269. vertices[3].s = 1;
  270. vertices[3].t = 0;
  271. settings.filter = Image::getDefaultFilter();
  272. getStrategy();
  273. loadVolatile();
  274. }
  275. Canvas::~Canvas()
  276. {
  277. // reset framebuffer if still using this one
  278. if (current == this)
  279. stopGrab();
  280. unloadVolatile();
  281. }
  282. bool Canvas::isSupported()
  283. {
  284. getStrategy();
  285. return (strategy != &strategyNone);
  286. }
  287. bool Canvas::isHdrSupported()
  288. {
  289. return GLEE_VERSION_3_0 || GLEE_ARB_texture_float;
  290. }
  291. void Canvas::bindDefaultCanvas()
  292. {
  293. if (current != NULL)
  294. current->stopGrab();
  295. }
  296. void Canvas::startGrab()
  297. {
  298. // already grabbing
  299. if (current == this)
  300. return;
  301. // cleanup after previous fbo
  302. if (current != NULL)
  303. current->stopGrab();
  304. // bind buffer and clear screen
  305. glPushAttrib(GL_VIEWPORT_BIT | GL_TRANSFORM_BIT);
  306. strategy->bindFBO(fbo);
  307. glViewport(0, 0, width, height);
  308. // Reset the projection matrix
  309. glMatrixMode(GL_PROJECTION);
  310. glPushMatrix();
  311. glLoadIdentity();
  312. // Set up orthographic view (no depth)
  313. glOrtho(0.0, width, height, 0.0, -1.0, 1.0);
  314. // Switch back to modelview matrix
  315. glMatrixMode(GL_MODELVIEW);
  316. // indicate we are using this fbo
  317. current = this;
  318. }
  319. void Canvas::stopGrab()
  320. {
  321. // i am not grabbing. leave me alone
  322. if (current != this)
  323. return;
  324. // bind default
  325. strategy->bindFBO(0);
  326. glMatrixMode(GL_PROJECTION);
  327. glPopMatrix();
  328. glPopAttrib();
  329. current = NULL;
  330. }
  331. void Canvas::clear(const Color &c)
  332. {
  333. GLuint previous = 0;
  334. if (current != NULL)
  335. previous = current->fbo;
  336. strategy->bindFBO(fbo);
  337. glPushAttrib(GL_COLOR_BUFFER_BIT);
  338. glClearColor((float)c.r/255.0f, (float)c.g/255.0f, (float)c.b/255.0f, (float)c.a/255.0f);
  339. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  340. glPopAttrib();
  341. strategy->bindFBO(previous);
  342. }
  343. void Canvas::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
  344. {
  345. static Matrix t;
  346. t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
  347. drawv(t, vertices);
  348. }
  349. void Canvas::drawq(love::graphics::Quad *quad, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
  350. {
  351. const vertex *v = quad->getVertices();
  352. // mirror quad on x axis
  353. vertex w[4];
  354. memcpy(w, v, sizeof(vertex)*4);
  355. for (size_t i = 0; i < 4; ++i)
  356. w[i].t = 1. - w[i].t;
  357. static Matrix t;
  358. t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
  359. drawv(t, w);
  360. }
  361. love::image::ImageData *Canvas::getImageData(love::image::Image *image)
  362. {
  363. int row = 4 * width;
  364. int size = row * height;
  365. GLubyte *pixels = new GLubyte[size];
  366. GLubyte *flipped = new GLubyte[size];
  367. strategy->bindFBO(fbo);
  368. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  369. if (current)
  370. strategy->bindFBO(current->fbo);
  371. else
  372. strategy->bindFBO(0);
  373. GLubyte *src = pixels, *dst = flipped + size - row;
  374. for (int i = 0; i < height; ++i, dst -= row, src += row)
  375. {
  376. memcpy(dst, src, row);
  377. }
  378. love::image::ImageData *img = image->newImageData(width, height, (void *)flipped);
  379. delete[] pixels;
  380. return img;
  381. }
  382. void Canvas::setFilter(const Image::Filter &f)
  383. {
  384. bindTexture(img);
  385. setTextureFilter(f);
  386. }
  387. Image::Filter Canvas::getFilter() const
  388. {
  389. bindTexture(img);
  390. return getTextureFilter();
  391. }
  392. void Canvas::setWrap(const Image::Wrap &w)
  393. {
  394. bindTexture(img);
  395. setTextureWrap(w);
  396. }
  397. Image::Wrap Canvas::getWrap() const
  398. {
  399. bindTexture(img);
  400. return getTextureWrap();
  401. }
  402. bool Canvas::loadVolatile()
  403. {
  404. status = strategy->createFBO(fbo, depth_stencil, img, width, height, texture_type);
  405. if (status != GL_FRAMEBUFFER_COMPLETE)
  406. return false;
  407. setFilter(settings.filter);
  408. setWrap(settings.wrap);
  409. Color c;
  410. c.r = c.g = c.b = c.a = 0;
  411. clear(c);
  412. return true;
  413. }
  414. void Canvas::unloadVolatile()
  415. {
  416. settings.filter = getFilter();
  417. settings.wrap = getWrap();
  418. strategy->deleteFBO(fbo, depth_stencil, img);
  419. }
  420. int Canvas::getWidth()
  421. {
  422. return width;
  423. }
  424. int Canvas::getHeight()
  425. {
  426. return height;
  427. }
  428. void Canvas::drawv(const Matrix &t, const vertex *v) const
  429. {
  430. glPushMatrix();
  431. glMultMatrixf((const GLfloat *)t.getElements());
  432. bindTexture(img);
  433. glEnableClientState(GL_VERTEX_ARRAY);
  434. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  435. glVertexPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&v[0].x);
  436. glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&v[0].s);
  437. glDrawArrays(GL_QUADS, 0, 4);
  438. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  439. glDisableClientState(GL_VERTEX_ARRAY);
  440. glPopMatrix();
  441. }
  442. bool Canvas::getConstant(const char *in, Canvas::TextureType &out)
  443. {
  444. return textureTypes.find(in, out);
  445. }
  446. bool Canvas::getConstant(Canvas::TextureType in, const char *&out)
  447. {
  448. return textureTypes.find(in, out);
  449. }
  450. StringMap<Canvas::TextureType, Canvas::TYPE_MAX_ENUM>::Entry Canvas::textureTypeEntries[] =
  451. {
  452. {"normal", Canvas::TYPE_NORMAL},
  453. {"hdr", Canvas::TYPE_HDR},
  454. };
  455. StringMap<Canvas::TextureType, Canvas::TYPE_MAX_ENUM> Canvas::textureTypes(Canvas::textureTypeEntries, sizeof(Canvas::textureTypeEntries));
  456. } // opengl
  457. } // graphics
  458. } // love