Texture.cpp 3.6 KB

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