Texture.cpp 3.8 KB

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