Texture.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include "rendering/wrapper/Texture.h"
  2. #include "rendering/wrapper/GLWrapper.h"
  3. Texture::Texture() : texture(0) {
  4. glGenTextures(1, &texture);
  5. glBindTexture(GL_TEXTURE_2D, texture);
  6. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  7. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  8. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  9. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  10. }
  11. Texture::~Texture() {
  12. glDeleteTextures(1, &texture);
  13. }
  14. void Texture::setColors(int width, int height, const char* data, int channels) {
  15. glBindTexture(GL_TEXTURE_2D, texture);
  16. switch(channels) {
  17. case 1:
  18. glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);
  19. break;
  20. case 4:
  21. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  22. break;
  23. }
  24. }
  25. void Texture::setRGBFloatData(int width, int height, const float* data) {
  26. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, data);
  27. }
  28. void Texture::bind() const {
  29. glActiveTexture(GL_TEXTURE0);
  30. glBindTexture(GL_TEXTURE_2D, texture);
  31. }