Texture.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 2:
  21. glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, width, height, 0, GL_RG, GL_UNSIGNED_BYTE, data);
  22. break;
  23. case 3:
  24. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  25. break;
  26. case 4:
  27. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  28. break;
  29. }
  30. }
  31. void Texture::bind() const {
  32. glActiveTexture(GL_TEXTURE0);
  33. glBindTexture(GL_TEXTURE_2D, texture);
  34. }