Texture.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "wrapper/Texture.h"
  2. Texture::Texture(Mode mode) : texture(0) {
  3. glGenTextures(1, &texture);
  4. glBindTexture(GL_TEXTURE_2D, texture);
  5. switch(mode) {
  6. case NEAREST:
  7. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  8. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  9. break;
  10. case LINEAR:
  11. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  12. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  13. break;
  14. }
  15. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  16. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  17. }
  18. Texture::~Texture() {
  19. glDeleteTextures(1, &texture);
  20. }
  21. void Texture::setColorData(int width, int height, const Color4* data) {
  22. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  23. }
  24. void Texture::setColorData(int width, int height, const Color3* data) {
  25. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  26. }
  27. void Texture::setColorData(int width, int height, const Color2* data) {
  28. glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, width, height, 0, GL_RG, GL_UNSIGNED_BYTE, data);
  29. }
  30. void Texture::setColorData(int width, int height, const Color1* data) {
  31. glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);
  32. }
  33. void Texture::setRGBFloatData(int width, int height, const float* data) {
  34. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, data);
  35. }
  36. void Texture::bind(int index) const {
  37. glActiveTexture(GL_TEXTURE0 + index);
  38. glBindTexture(GL_TEXTURE_2D, texture);
  39. }