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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "gltexture.h"
  2. namespace segl {
  3. GLTexture::GLTexture() {
  4. init();
  5. alltextures.push_back(this);
  6. }
  7. GLTexture::GLTexture(std::string fname, GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
  8. init();
  9. setParameter(_minfilter, _magfilter, _wraps, _wrapt);
  10. loadImage(fname);
  11. filename = fname;
  12. alltextures.push_back(this);
  13. }
  14. GLTexture::GLTexture(SDL_Surface *srfc) {
  15. init();
  16. loadSurface(srfc);
  17. }
  18. void GLTexture::init() {
  19. loaded = false;
  20. texconverted = false;
  21. keepsurface = false;
  22. donotreload = false;
  23. minfilter = GL_LINEAR;
  24. magfilter = GL_LINEAR;
  25. wraps = GL_REPEAT;
  26. wrapt = GL_REPEAT;
  27. tex = 0;
  28. width = height = 0;
  29. }
  30. bool GLTexture::loadLocalSurface() {
  31. if(!texconverted)
  32. convertLocalSurface();
  33. width = tex->w;
  34. height = tex->h;
  35. glGenTextures(1, &texint);
  36. glBindTexture(GL_TEXTURE_2D, texint);
  37. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
  38. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
  39. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wraps);
  40. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapt);
  41. gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_BGRA, GL_UNSIGNED_BYTE, tex->pixels);
  42. loaded = true;
  43. return true;
  44. }
  45. void GLTexture::convertLocalSurface() {
  46. tex = SDL_DisplayFormatAlpha(tex);
  47. mirrorSurfaceMiddleX(tex);
  48. texconverted = true;
  49. }
  50. bool GLTexture::loadImage(std::string fname) {
  51. if(isLoaded())
  52. unloadTexture();
  53. filename = fname;
  54. tex = IMG_Load(filename.c_str());
  55. if(!tex) {
  56. std::cerr << "Textur konnte nicht geladen werden: " << filename << std::endl;
  57. return false;
  58. }
  59. bool ret = loadLocalSurface();
  60. keepsurface = false;
  61. SDL_FreeSurface(tex);
  62. tex = 0;
  63. return ret;
  64. }
  65. bool GLTexture::loadSurface(SDL_Surface *srfc, bool noreloading) {
  66. if(isLoaded())
  67. unloadTexture();
  68. if(filename!="")
  69. filename = "";
  70. tex = srfc;
  71. keepsurface = true;
  72. donotreload = noreloading;
  73. return loadLocalSurface();
  74. }
  75. bool GLTexture::selectTexture() {
  76. if(!loaded) {
  77. std::cerr << "Textur wurde nicht geladen: " << filename << std::endl;
  78. return false;
  79. }
  80. glBindTexture(GL_TEXTURE_2D, texint);
  81. return true;
  82. }
  83. void GLTexture::unloadTexture() {
  84. glDeleteTextures(1, &texint);
  85. loaded = false;
  86. // Entweder tex == image, wurde schon gel�scht oder wir m�ssen es nich l�schen
  87. }
  88. bool GLTexture::setParameter(GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
  89. if(_minfilter!=0)
  90. minfilter = _minfilter;
  91. if(_magfilter!=0)
  92. magfilter = _magfilter;
  93. if(_wraps!=0)
  94. wraps = _wraps;
  95. if(_wrapt!=0)
  96. wrapt = _wrapt;
  97. if(!loaded)
  98. return true;
  99. loaded = false;
  100. texconverted = false;
  101. if(keepsurface&&tex!=0) {
  102. unloadTexture();
  103. return loadLocalSurface();
  104. } else if(filename!="") {
  105. //std::cout << "Filename: " << filename << std::endl;
  106. return loadImage(filename);
  107. } else {
  108. // keine datei, kein surface
  109. unloadTexture();
  110. std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl;
  111. return false;
  112. }
  113. //std::cout << "Texture reloaded" << std::endl;
  114. }
  115. int GLTexture::getW() {
  116. return width;
  117. }
  118. int GLTexture::getH() {
  119. return height;
  120. }
  121. bool GLTexture::isLoaded() {
  122. return loaded;
  123. }
  124. void GLTexture::reloadAll() {
  125. for(unsigned int i=0; i<alltextures.size(); i++) {
  126. alltextures[i]->setParameter();
  127. }
  128. }
  129. GLTexture::~GLTexture() {
  130. if(isLoaded())
  131. unloadTexture();
  132. for(unsigned int i=0; i<alltextures.size(); i++) {
  133. if(alltextures[i]==this) {
  134. alltextures.erase(alltextures.begin()+i);
  135. }
  136. }
  137. }
  138. std::vector<GLTexture*> GLTexture::alltextures;
  139. } // namespace segl