Small OpenGL based c++ rendering library
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.

gltexture.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* libsegl - Sebas Extended GL Library
  2. * Collection of Opengl/3D-Math helpers
  3. *
  4. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  5. * http://www.seba-geek.de
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #include "gltexture.h"
  23. namespace segl {
  24. GLTexture::GLTexture() {
  25. init();
  26. alltextures.push_back(this);
  27. }
  28. GLTexture::GLTexture(std::string fname, GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
  29. init();
  30. setParameter(_minfilter, _magfilter, _wraps, _wrapt);
  31. loadImage(fname);
  32. filename = fname;
  33. alltextures.push_back(this);
  34. }
  35. GLTexture::GLTexture(SDL_Surface *srfc) {
  36. init();
  37. loadSurface(srfc);
  38. }
  39. void GLTexture::init() {
  40. loaded = false;
  41. texconverted = false;
  42. keepsurface = false;
  43. donotreload = false;
  44. minfilter = GL_LINEAR;
  45. magfilter = GL_LINEAR;
  46. wraps = GL_REPEAT;
  47. wrapt = GL_REPEAT;
  48. tex = 0;
  49. width = height = 0;
  50. }
  51. bool GLTexture::loadLocalSurface() {
  52. if(!texconverted)
  53. convertLocalSurface();
  54. width = tex->w;
  55. height = tex->h;
  56. glGenTextures(1, &texint);
  57. glBindTexture(GL_TEXTURE_2D, texint);
  58. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
  59. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
  60. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wraps);
  61. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapt);
  62. gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_BGRA, GL_UNSIGNED_BYTE, tex->pixels);
  63. loaded = true;
  64. return true;
  65. }
  66. void GLTexture::convertLocalSurface() {
  67. tex = SDL_DisplayFormatAlpha(tex);
  68. mirrorSurfaceMiddleX(tex);
  69. texconverted = true;
  70. }
  71. bool GLTexture::loadImage(std::string fname) {
  72. if(isLoaded())
  73. unloadTexture();
  74. filename = fname;
  75. tex = IMG_Load(filename.c_str());
  76. if(!tex) {
  77. std::cerr << "Textur konnte nicht geladen werden: " << filename << std::endl;
  78. return false;
  79. }
  80. bool ret = loadLocalSurface();
  81. keepsurface = false;
  82. SDL_FreeSurface(tex);
  83. tex = 0;
  84. return ret;
  85. }
  86. bool GLTexture::loadSurface(SDL_Surface *srfc, bool noreloading) {
  87. if(isLoaded())
  88. unloadTexture();
  89. if(filename!="")
  90. filename = "";
  91. tex = srfc;
  92. keepsurface = true;
  93. donotreload = noreloading;
  94. return loadLocalSurface();
  95. }
  96. bool GLTexture::selectTexture() {
  97. if(!loaded) {
  98. std::cerr << "Textur wurde nicht geladen: " << filename << std::endl;
  99. return false;
  100. }
  101. glBindTexture(GL_TEXTURE_2D, texint);
  102. return true;
  103. }
  104. void GLTexture::unloadTexture() {
  105. glDeleteTextures(1, &texint);
  106. loaded = false;
  107. // Entweder tex == image, wurde schon gel�scht oder wir m�ssen es nich l�schen
  108. }
  109. bool GLTexture::setParameter(GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
  110. if(_minfilter!=0)
  111. minfilter = _minfilter;
  112. if(_magfilter!=0)
  113. magfilter = _magfilter;
  114. if(_wraps!=0)
  115. wraps = _wraps;
  116. if(_wrapt!=0)
  117. wrapt = _wrapt;
  118. if(!loaded)
  119. return true;
  120. loaded = false;
  121. texconverted = false;
  122. if(keepsurface&&tex!=0) {
  123. unloadTexture();
  124. return loadLocalSurface();
  125. } else if(filename!="") {
  126. //std::cout << "Filename: " << filename << std::endl;
  127. return loadImage(filename);
  128. } else {
  129. // keine datei, kein surface
  130. unloadTexture();
  131. std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl;
  132. return false;
  133. }
  134. //std::cout << "Texture reloaded" << std::endl;
  135. }
  136. int GLTexture::getW() {
  137. return width;
  138. }
  139. int GLTexture::getH() {
  140. return height;
  141. }
  142. bool GLTexture::isLoaded() {
  143. return loaded;
  144. }
  145. void GLTexture::reloadAll() {
  146. for(unsigned int i=0; i<alltextures.size(); i++) {
  147. alltextures[i]->setParameter();
  148. }
  149. }
  150. GLTexture::~GLTexture() {
  151. if(isLoaded())
  152. unloadTexture();
  153. for(unsigned int i=0; i<alltextures.size(); i++) {
  154. if(alltextures[i]==this) {
  155. alltextures.erase(alltextures.begin()+i);
  156. }
  157. }
  158. }
  159. std::vector<GLTexture*> GLTexture::alltextures;
  160. } // namespace segl