Texture.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "rendering/Texture.h"
  2. Texture::Texture()
  3. : format(TextureFormat::unknown()), texture(0), maxMipMaps(0) {
  4. }
  5. Texture::~Texture() {
  6. GL::deleteTexture(texture);
  7. }
  8. void Texture::init(const TextureFormat& format, int maxMipMaps) {
  9. if(texture != 0) {
  10. return;
  11. }
  12. Texture::format = format;
  13. Texture::maxMipMaps = maxMipMaps;
  14. texture = GL::genTexture();
  15. setNearestFilter();
  16. setRepeatWrap();
  17. }
  18. void Texture::setNearestFilter() {
  19. bind();
  20. if(maxMipMaps > 0) {
  21. GL::setMipMapNearFilter2D();
  22. } else {
  23. GL::setNearFilter2D();
  24. }
  25. }
  26. void Texture::setLinearFilter() {
  27. bind();
  28. if(maxMipMaps > 0) {
  29. GL::setMipMapLinearFilter2D();
  30. } else {
  31. GL::setLinearFilter2D();
  32. }
  33. }
  34. void Texture::setRepeatWrap() {
  35. bind();
  36. GL::setRepeatWrap2D();
  37. }
  38. void Texture::setClampWrap() {
  39. bind();
  40. GL::setClampWrap2D();
  41. }
  42. void Texture::setData(int width, int height, const void* data) {
  43. bind();
  44. GL::texImage2D(format.format, width, height, data);
  45. if(maxMipMaps > 0) {
  46. GL::generateMipmap2D(maxMipMaps);
  47. }
  48. }
  49. void Texture::bind() const {
  50. GL::bindTexture2D(texture);
  51. }
  52. void Texture::bindTo(int index) const {
  53. GL::activeTexture(index);
  54. bind();
  55. }