Texture.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "wrapper/Texture.h"
  2. Texture::Texture(const TextureFormat& format) : format(format), texture(0) {
  3. glGenTextures(1, &texture);
  4. setNearestFilter();
  5. setRepeatWrap();
  6. }
  7. Texture::Texture() : Texture(TextureFormat::unknown()) {
  8. }
  9. Texture::~Texture() {
  10. glDeleteTextures(1, &texture);
  11. }
  12. void Texture::setFormat(const TextureFormat& tf) {
  13. format = tf;
  14. }
  15. void Texture::setFilter(GLint param) {
  16. bind();
  17. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, param);
  18. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, param);
  19. }
  20. void Texture::setNearestFilter() {
  21. setFilter(GL_NEAREST);
  22. }
  23. void Texture::setLinearFilter() {
  24. setFilter(GL_LINEAR);
  25. }
  26. void Texture::setWrap(GLint param) {
  27. bind();
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, param);
  29. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, param);
  30. }
  31. void Texture::setRepeatWrap() {
  32. setWrap(GL_REPEAT);
  33. }
  34. void Texture::setClampWrap() {
  35. setWrap(GL_CLAMP_TO_EDGE);
  36. }
  37. void Texture::setData(int width, int height, const void* data) {
  38. glBindTexture(GL_TEXTURE_2D, texture);
  39. glTexImage2D(GL_TEXTURE_2D, 0, format.internalformat, width, height, 0, format.format, format.type, data);
  40. }
  41. void Texture::bind() const {
  42. glBindTexture(GL_TEXTURE_2D, texture);
  43. }
  44. void Texture::bindTo(int index) const {
  45. glActiveTexture(GL_TEXTURE0 + index);
  46. bind();
  47. }