Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

TextureRenderData.cpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "graphics/TextureRenderData.h"
  19. #include "graphics/Renderer.h"
  20. #include "resource/ImgColor.h"
  21. #include "Core.h"
  22. TextureRenderData::TextureRenderData(const ImgColor& imgColor)
  23. {
  24. const Image& image = imgColor.image;
  25. glGenTextures(1, &handle_);
  26. glBindTexture(GL_TEXTURE_2D, handle_);
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Core::get().renderer().textureMinFilter());
  28. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, Core::get().renderer().textureMaxAnisotropy());
  29. switch(image.format())
  30. {
  31. case PixelFormat::kA8R8G8B8:
  32. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.width(), image.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, image.data());
  33. break;
  34. case PixelFormat::kR8G8B8:
  35. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.width(), image.height(), 0, GL_BGR, GL_UNSIGNED_BYTE, image.data());
  36. break;
  37. case PixelFormat::kDXT1:
  38. glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, image.width(), image.height(), 0, (GLsizei)image.size(), image.data());
  39. break;
  40. case PixelFormat::kDXT3:
  41. glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, image.width(), image.height(), 0, (GLsizei)image.size(), image.data());
  42. break;
  43. case PixelFormat::kDXT5:
  44. glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, image.width(), image.height(), 0, (GLsizei)image.size(), image.data());
  45. break;
  46. default:
  47. throw runtime_error("Unsupported image format");
  48. }
  49. glGenerateMipmap(GL_TEXTURE_2D);
  50. }
  51. TextureRenderData::~TextureRenderData()
  52. {
  53. glDeleteTextures(1, &handle_);
  54. }
  55. void TextureRenderData::bind()
  56. {
  57. glBindTexture(GL_TEXTURE_2D, handle_);
  58. }