Texture.cpp 3.1 KB

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