Texture.cpp 3.6 KB

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