Guest User

Untitled

a guest
Oct 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1.  
  2. Texture(const unsigned int handle, const std::string& name, const std::string& path = "./") : Resource(handle,name,path)
  3. {
  4. glGenTextures(1,&iTexture);
  5. glBindTexture(GL_TEXTURE_2D,iTexture);
  6. glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  7. glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  8. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  9. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  10.  
  11.  
  12. std::ifstream fIn;
  13. fIn.open(GetName());
  14. if (fIn == NULL)
  15. {
  16. printf("Unable to load texture %s\n",name.c_str());
  17. fIn.close();
  18. glDeleteTextures(1,&iTexture);
  19. iTexture = 0;
  20. return;
  21. }
  22.  
  23.  
  24. //read tag header
  25. fIn.read((char*)&_header._magic,sizeof(int));
  26. fIn.read((char*)&_header._size,sizeof(int));
  27. if (_header._magic != 'mtib')
  28. {
  29. printf("File is not a bitm tag\n");
  30. fIn.close();
  31. glDeleteTextures(1,&iTexture);
  32. iTexture = 0;
  33. return;
  34. }
  35.  
  36. //Read bitm header
  37. fIn.read((char*)&_bitmheader._bpp,sizeof(char));
  38. fIn.read((char*)&_bitmheader._width,sizeof(int));
  39. fIn.read((char*)&_bitmheader._height,sizeof(int));
  40. fIn.read((char*)&_bitmheader._padding,sizeof(_bitmheader._padding));
  41.  
  42.  
  43.  
  44.  
  45. //read bitmap
  46.  
  47. char * _data;
  48.  
  49. _data = (char*)malloc((_bitmheader._width*_bitmheader._height*_bitmheader._bpp));
  50. if (_data == NULL)
  51. {
  52. printf("Unable to allocate bitmap memory.\n");
  53. fIn.close();
  54. glDeleteTextures(1,&iTexture);
  55. iTexture = 0;
  56. return;
  57. }
  58. fIn.read((char*)_data,(_bitmheader._width*_bitmheader._height*_bitmheader._bpp));//Read bitmap
  59.  
  60.  
  61. //glTexImage2D(GL_TEXTURE_2D,
  62. // 0,
  63. // _bitmheader._bpp,
  64. // _bitmheader._width,
  65. // _bitmheader._height,
  66. // 0,
  67. // _bitmheader._bpp == 3 ? GL_RGB : GL_RGBA,//If it is 3 bytes then its RGB, else RGBA. Find a better way to do this.
  68. // GL_UNSIGNED_BYTE,
  69. // _data);
  70.  
  71. gluBuild2DMipmaps(GL_TEXTURE_2D,
  72. _bitmheader._bpp,
  73. _bitmheader._width,
  74. _bitmheader._height,
  75. _bitmheader._bpp == 3 ? GL_RGB : GL_RGBA,
  76. GL_UNSIGNED_BYTE,
  77. _data);
  78.  
  79.  
  80.  
  81. //bind 0 so we don't use the texture we loaded.
  82. glBindTexture(GL_TEXTURE_2D,0);
  83.  
  84. free(_data);
  85. fIn.close();
  86. printf("Texture loaded: %s \n",this->GetName().c_str());
  87. }
Add Comment
Please, Sign In to add comment