NoiseTexture.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <png.h>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include "client/rendering/NoiseTexture.h"
  6. NoiseTexture::NoiseTexture(u32 width, u32 height)
  7. {
  8. glGenTextures(1, &texture);
  9. glBindTexture(GL_TEXTURE_2D, texture);
  10. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  11. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  12. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  13. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  14. std::vector<float> data;
  15. for(u32 x = 0; x < width; x++)
  16. {
  17. for(u32 y = 0; y < height; y++)
  18. {
  19. data.push_back(getRandom());
  20. data.push_back(getRandom());
  21. data.push_back(getRandom());
  22. }
  23. }
  24. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, data.data());
  25. }
  26. NoiseTexture::~NoiseTexture()
  27. {
  28. glDeleteTextures(1, &texture);
  29. }
  30. void NoiseTexture::bind(unsigned int index) const
  31. {
  32. glActiveTexture(GL_TEXTURE0 + index);
  33. glBindTexture(GL_TEXTURE_2D, texture);
  34. }
  35. float NoiseTexture::getRandom() const
  36. {
  37. float r = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
  38. return r * 2.0f - 1.0f;
  39. }