Texture.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "rendering/Texture.h"
  2. #include "io/ImageReader.h"
  3. Texture::Format::Format(const GL::TextureFormat& tf, bool linear_, bool depth_)
  4. : format(tf), linear(linear_), depth(depth_) {
  5. }
  6. Texture::Format Texture::Format::color8(int channels, bool linear) {
  7. return Format(GL::TextureFormat::color8(channels), linear);
  8. }
  9. Texture::Format Texture::Format::float16(int channels, bool linear) {
  10. return Format(GL::TextureFormat::float16(channels), linear);
  11. }
  12. Texture::Format Texture::Format::float32(int channels, bool linear) {
  13. return Format(GL::TextureFormat::float32(channels), linear);
  14. }
  15. Texture::Format Texture::Format::depth16(bool linear) {
  16. return Format(GL::TextureFormat::depth16(), linear, true);
  17. }
  18. Texture::Format Texture::Format::depth32(bool linear) {
  19. return Format(GL::TextureFormat::depth32(), linear, true);
  20. }
  21. Texture::Format Texture::Format::unknown() {
  22. return Format(GL::TextureFormat::unknown(), false, false);
  23. }
  24. Texture::Texture() : format(Format::unknown()), texture(0), maxMipMaps(0) {
  25. }
  26. Texture::~Texture() {
  27. GL::deleteTexture(texture);
  28. }
  29. void Texture::init(const Format& format_, int maxMipMaps_) {
  30. if(texture != 0) {
  31. return;
  32. }
  33. format = format_;
  34. maxMipMaps = maxMipMaps_;
  35. texture = GL::genTexture();
  36. setNearestFilter();
  37. setRepeatWrap();
  38. }
  39. void Texture::setNearestFilter() {
  40. bind();
  41. if(maxMipMaps > 0) {
  42. GL::setMipMapNearFilter2D();
  43. } else {
  44. GL::setNearFilter2D();
  45. }
  46. }
  47. void Texture::setLinearFilter() {
  48. bind();
  49. if(maxMipMaps > 0) {
  50. GL::setMipMapLinearFilter2D();
  51. } else {
  52. GL::setLinearFilter2D();
  53. }
  54. }
  55. void Texture::setRepeatWrap() {
  56. bind();
  57. GL::setRepeatWrap2D();
  58. }
  59. void Texture::setClampWrap() {
  60. bind();
  61. GL::setClampWrap2D();
  62. }
  63. void Texture::setData(int width, int height, const void* data) {
  64. bind();
  65. GL::texImage2D(format.format, width, height, data);
  66. if(maxMipMaps > 0) {
  67. GL::generateMipmap2D(maxMipMaps);
  68. }
  69. }
  70. void Texture::bind() const {
  71. GL::bindTexture2D(texture);
  72. }
  73. void Texture::bindTo(int index) const {
  74. GL::activeTexture(index);
  75. bind();
  76. }
  77. Error Texture::load(const char* path, int maxMipMaps_) {
  78. ImageReader::Image image;
  79. Error error = ImageReader::load(image, path);
  80. if(error.has()) {
  81. return error;
  82. }
  83. if(image.channels < 1 || image.channels > 4) {
  84. error = {"'"};
  85. error.message.append(path)
  86. .append("' has unsupported number of channels: ")
  87. .append(image.channels);
  88. return error;
  89. } else if(image.bitdepth != 8) {
  90. error = {"bit depth of '"};
  91. error.message.append(path).append("' is not 8");
  92. return error;
  93. }
  94. init(Format::color8(static_cast<int>(image.channels)), maxMipMaps_);
  95. setData(static_cast<int>(image.width), static_cast<int>(image.height),
  96. image.data);
  97. return {};
  98. }