Texture.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <png.h>
  2. #include "client/rendering/Texture.h"
  3. Texture::Texture(const char* path) : data(nullptr), texture(0) {
  4. load(path);
  5. }
  6. Texture::~Texture() {
  7. if(data != nullptr) {
  8. delete[] data;
  9. }
  10. glDeleteTextures(1, &texture);
  11. }
  12. void Texture::load(const char* path) {
  13. FILE* file = fopen(path, "r");
  14. if(file == nullptr) {
  15. std::cout << "texture '" << path << "' does not exist\n";
  16. return;
  17. }
  18. bool b = load(path, file);
  19. fclose(file);
  20. if(b) {
  21. initGL();
  22. }
  23. }
  24. bool Texture::load(const char* path, FILE* file) {
  25. // check signature of png
  26. unsigned char buffer[8];
  27. if(fread(buffer, sizeof (char), 8, file) != 8) {
  28. std::cout << "cannot read signature of texture '" << path << "'\n";
  29. return false;
  30. }
  31. if(png_sig_cmp(buffer, 0, 8)) {
  32. std::cout << "file '" << path << "' is not a texture\n";
  33. return false;
  34. }
  35. // create structures for data
  36. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
  37. if(png == nullptr) {
  38. std::cout << "cannot create texture data structure\n";
  39. return false;
  40. }
  41. png_infop info = png_create_info_struct(png);
  42. if(info == nullptr) {
  43. std::cout << "cannot create image info structure\n";
  44. return false;
  45. }
  46. u32** rowPointers = nullptr;
  47. // set callback for errors
  48. if(setjmp(png_jmpbuf(png))) {
  49. if(rowPointers != nullptr) {
  50. png_free(png, rowPointers);
  51. }
  52. png_destroy_read_struct(&png, &info, nullptr);
  53. std::cout << "texture '" << path << "' has used error callback\n";
  54. return false;
  55. }
  56. // set reading function
  57. png_init_io(png, file);
  58. // notify about already used signature bytes
  59. png_set_sig_bytes(png, 8);
  60. // read info data
  61. png_read_info(png, info);
  62. width = png_get_image_width(png, info);
  63. height = png_get_image_height(png, info);
  64. // read image data
  65. data = new u32[width * height];
  66. // allocate and set row pointer to correct places in block
  67. rowPointers = (u32**) png_malloc(png, height * (sizeof (u32*)));
  68. for(unsigned int i = 0; i < height; i++) {
  69. rowPointers[i] = (data + i * width);
  70. }
  71. png_set_rows(png, info, (png_bytepp) rowPointers);
  72. png_read_image(png, (png_bytepp) rowPointers);
  73. png_free(png, rowPointers);
  74. png_destroy_read_struct(&png, &info, NULL);
  75. return true;
  76. }
  77. void Texture::initGL() {
  78. glGenTextures(1, &texture);
  79. glBindTexture(GL_TEXTURE_2D, texture);
  80. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  81. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  82. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  83. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  84. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
  85. delete[] data;
  86. data = nullptr;
  87. }
  88. void Texture::bind(uint index) const {
  89. glActiveTexture(GL_TEXTURE0 + index);
  90. glBindTexture(GL_TEXTURE_2D, texture);
  91. }