Texture.cpp 3.1 KB

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