Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Texture(const unsigned int handle, const std::string& name, const std::string& path = "./") : Resource(handle,name,path)
- {
- glGenTextures(1,&iTexture);
- glBindTexture(GL_TEXTURE_2D,iTexture);
- glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
- glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST);
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
- std::ifstream fIn;
- fIn.open(GetName());
- if (fIn == NULL)
- {
- printf("Unable to load texture %s\n",name.c_str());
- fIn.close();
- glDeleteTextures(1,&iTexture);
- iTexture = 0;
- return;
- }
- //read tag header
- fIn.read((char*)&_header._magic,sizeof(int));
- fIn.read((char*)&_header._size,sizeof(int));
- if (_header._magic != 'mtib')
- {
- printf("File is not a bitm tag\n");
- fIn.close();
- glDeleteTextures(1,&iTexture);
- iTexture = 0;
- return;
- }
- //Read bitm header
- fIn.read((char*)&_bitmheader._bpp,sizeof(char));
- fIn.read((char*)&_bitmheader._width,sizeof(int));
- fIn.read((char*)&_bitmheader._height,sizeof(int));
- fIn.read((char*)&_bitmheader._padding,sizeof(_bitmheader._padding));
- //read bitmap
- char * _data;
- _data = (char*)malloc((_bitmheader._width*_bitmheader._height*_bitmheader._bpp));
- if (_data == NULL)
- {
- printf("Unable to allocate bitmap memory.\n");
- fIn.close();
- glDeleteTextures(1,&iTexture);
- iTexture = 0;
- return;
- }
- fIn.read((char*)_data,(_bitmheader._width*_bitmheader._height*_bitmheader._bpp));//Read bitmap
- //glTexImage2D(GL_TEXTURE_2D,
- // 0,
- // _bitmheader._bpp,
- // _bitmheader._width,
- // _bitmheader._height,
- // 0,
- // _bitmheader._bpp == 3 ? GL_RGB : GL_RGBA,//If it is 3 bytes then its RGB, else RGBA. Find a better way to do this.
- // GL_UNSIGNED_BYTE,
- // _data);
- gluBuild2DMipmaps(GL_TEXTURE_2D,
- _bitmheader._bpp,
- _bitmheader._width,
- _bitmheader._height,
- _bitmheader._bpp == 3 ? GL_RGB : GL_RGBA,
- GL_UNSIGNED_BYTE,
- _data);
- //bind 0 so we don't use the texture we loaded.
- glBindTexture(GL_TEXTURE_2D,0);
- free(_data);
- fIn.close();
- printf("Texture loaded: %s \n",this->GetName().c_str());
- }
Add Comment
Please, Sign In to add comment