Texture.cpp 1.2 KB

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