Canvas.cpp 13 KB

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