Image.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 "Image.h"
  21. // STD
  22. #include <cstring> // For memcpy
  23. #include <algorithm> // for min/max
  24. #include <iostream>
  25. namespace love
  26. {
  27. namespace graphics
  28. {
  29. namespace opengl
  30. {
  31. Image::Image(love::image::ImageData *data)
  32. : width((float)(data->getWidth()))
  33. , height((float)(data->getHeight()))
  34. , texture(0)
  35. , mipmapsharpness(0.0f)
  36. {
  37. data->retain();
  38. this->data = data;
  39. memset(vertices, 255, sizeof(vertex)*4);
  40. vertices[0].x = 0;
  41. vertices[0].y = 0;
  42. vertices[1].x = 0;
  43. vertices[1].y = height;
  44. vertices[2].x = width;
  45. vertices[2].y = height;
  46. vertices[3].x = width;
  47. vertices[3].y = 0;
  48. vertices[0].s = 0;
  49. vertices[0].t = 0;
  50. vertices[1].s = 0;
  51. vertices[1].t = 1;
  52. vertices[2].s = 1;
  53. vertices[2].t = 1;
  54. vertices[3].s = 1;
  55. vertices[3].t = 0;
  56. filter = getDefaultFilter();
  57. }
  58. Image::~Image()
  59. {
  60. if (data != 0)
  61. data->release();
  62. unload();
  63. }
  64. float Image::getWidth() const
  65. {
  66. return width;
  67. }
  68. float Image::getHeight() const
  69. {
  70. return height;
  71. }
  72. const vertex *Image::getVertices() const
  73. {
  74. return vertices;
  75. }
  76. love::image::ImageData *Image::getData() const
  77. {
  78. return data;
  79. }
  80. void Image::getRectangleVertices(int x, int y, int w, int h, vertex *vertices) const
  81. {
  82. // Check upper.
  83. x = (x+w > (int)width) ? (int)width-w : x;
  84. y = (y+h > (int)height) ? (int)height-h : y;
  85. // Check lower.
  86. x = (x < 0) ? 0 : x;
  87. y = (y < 0) ? 0 : y;
  88. vertices[0].x = 0;
  89. vertices[0].y = 0;
  90. vertices[1].x = 0;
  91. vertices[1].y = (float)h;
  92. vertices[2].x = (float)w;
  93. vertices[2].y = (float)h;
  94. vertices[3].x = (float)w;
  95. vertices[3].y = 0;
  96. float tx = (float)x/width;
  97. float ty = (float)y/height;
  98. float tw = (float)w/width;
  99. float th = (float)h/height;
  100. vertices[0].s = tx;
  101. vertices[0].t = ty;
  102. vertices[1].s = tx;
  103. vertices[1].t = ty+th;
  104. vertices[2].s = tx+tw;
  105. vertices[2].t = ty+th;
  106. vertices[3].s = tx+tw;
  107. vertices[3].t = ty;
  108. }
  109. void Image::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
  110. {
  111. static Matrix t;
  112. t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
  113. drawv(t, vertices);
  114. }
  115. void Image::drawq(love::graphics::Quad *quad, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
  116. {
  117. static Matrix t;
  118. const vertex *v = quad->getVertices();
  119. t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
  120. drawv(t, v);
  121. }
  122. void Image::checkMipmapsCreated() const
  123. {
  124. if (filter.mipmap != FILTER_NEAREST && filter.mipmap != FILTER_LINEAR)
  125. return;
  126. if (!hasMipmapSupport())
  127. throw love::Exception("Mipmap filtering is not supported on this system!");
  128. // some old GPUs/systems claim support for NPOT textures, but fail when generating mipmaps
  129. // we can't detect which systems will do this, so we fail gracefully for all NPOT images
  130. int w = int(width), h = int(height);
  131. if (w != next_p2(w) || h != next_p2(h))
  132. throw love::Exception("Could not generate mipmaps: image does not have power of two dimensions!");
  133. bind();
  134. GLint mipmapscreated;
  135. glGetTexParameteriv(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, &mipmapscreated);
  136. // generate mipmaps for this image if we haven't already
  137. if (!mipmapscreated)
  138. {
  139. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  140. if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_object)
  141. glGenerateMipmap(GL_TEXTURE_2D);
  142. else if (GLEE_EXT_framebuffer_object)
  143. glGenerateMipmapEXT(GL_TEXTURE_2D);
  144. else
  145. // modify single texel to trigger mipmap chain generation
  146. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data->getData());
  147. }
  148. }
  149. void Image::setFilter(const Image::Filter &f)
  150. {
  151. filter = f;
  152. bind();
  153. checkMipmapsCreated();
  154. setTextureFilter(f);
  155. }
  156. const Image::Filter &Image::getFilter() const
  157. {
  158. return filter;
  159. }
  160. void Image::setWrap(const Image::Wrap &w)
  161. {
  162. wrap = w;
  163. bind();
  164. setTextureWrap(w);
  165. }
  166. const Image::Wrap &Image::getWrap() const
  167. {
  168. return wrap;
  169. }
  170. void Image::setMipmapSharpness(float sharpness)
  171. {
  172. if (!hasMipmapSharpnessSupport())
  173. return;
  174. // LOD bias has the range (-maxbias, maxbias)
  175. mipmapsharpness = std::min(std::max(sharpness, -maxmipmapsharpness + 0.01f), maxmipmapsharpness - 0.01f);
  176. bind();
  177. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -mipmapsharpness); // negative bias is sharper
  178. }
  179. float Image::getMipmapSharpness() const
  180. {
  181. return mipmapsharpness;
  182. }
  183. void Image::bind() const
  184. {
  185. if (texture == 0)
  186. return;
  187. bindTexture(texture);
  188. }
  189. bool Image::load()
  190. {
  191. return loadVolatile();
  192. }
  193. void Image::unload()
  194. {
  195. return unloadVolatile();
  196. }
  197. bool Image::loadVolatile()
  198. {
  199. if (hasMipmapSharpnessSupport())
  200. glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS, &maxmipmapsharpness);
  201. if (hasNpot())
  202. return loadVolatileNPOT();
  203. else
  204. return loadVolatilePOT();
  205. }
  206. bool Image::loadVolatilePOT()
  207. {
  208. glGenTextures(1,(GLuint *)&texture);
  209. bindTexture(texture);
  210. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  211. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  212. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  213. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  214. float p2width = next_p2(width);
  215. float p2height = next_p2(height);
  216. float s = width/p2width;
  217. float t = height/p2height;
  218. vertices[1].t = t;
  219. vertices[2].t = t;
  220. vertices[2].s = s;
  221. vertices[3].s = s;
  222. glTexImage2D(GL_TEXTURE_2D,
  223. 0,
  224. GL_RGBA8,
  225. (GLsizei)p2width,
  226. (GLsizei)p2height,
  227. 0,
  228. GL_RGBA,
  229. GL_UNSIGNED_BYTE,
  230. 0);
  231. glTexSubImage2D(GL_TEXTURE_2D,
  232. 0,
  233. 0,
  234. 0,
  235. (GLsizei)width,
  236. (GLsizei)height,
  237. GL_RGBA,
  238. GL_UNSIGNED_BYTE,
  239. data->getData());
  240. setMipmapSharpness(mipmapsharpness);
  241. setFilter(filter);
  242. setWrap(wrap);
  243. return true;
  244. }
  245. bool Image::loadVolatileNPOT()
  246. {
  247. glGenTextures(1,(GLuint *)&texture);
  248. bindTexture(texture);
  249. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  250. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  251. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  252. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  253. glTexImage2D(GL_TEXTURE_2D,
  254. 0,
  255. GL_RGBA8,
  256. (GLsizei)width,
  257. (GLsizei)height,
  258. 0,
  259. GL_RGBA,
  260. GL_UNSIGNED_BYTE,
  261. data->getData());
  262. setMipmapSharpness(mipmapsharpness);
  263. setFilter(filter);
  264. setWrap(wrap);
  265. return true;
  266. }
  267. void Image::unloadVolatile()
  268. {
  269. // Delete the hardware texture.
  270. if (texture != 0)
  271. {
  272. deleteTexture(texture);
  273. texture = 0;
  274. }
  275. }
  276. void Image::drawv(const Matrix &t, const vertex *v) const
  277. {
  278. bind();
  279. glPushMatrix();
  280. glMultMatrixf((const GLfloat *)t.getElements());
  281. glEnableClientState(GL_VERTEX_ARRAY);
  282. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  283. glVertexPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&v[0].x);
  284. glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&v[0].s);
  285. glDrawArrays(GL_QUADS, 0, 4);
  286. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  287. glDisableClientState(GL_VERTEX_ARRAY);
  288. glPopMatrix();
  289. }
  290. bool Image::hasNpot()
  291. {
  292. return GLEE_VERSION_2_0 || GLEE_ARB_texture_non_power_of_two;
  293. }
  294. bool Image::hasMipmapSupport()
  295. {
  296. return (GLEE_VERSION_1_4 || GLEE_SGIS_generate_mipmap) != 0;
  297. }
  298. bool Image::hasMipmapSharpnessSupport()
  299. {
  300. return (GLEE_VERSION_1_4 || GLEE_EXT_texture_lod_bias) != 0;
  301. }
  302. } // opengl
  303. } // graphics
  304. } // love