Skip to main content
Search
Search This Blog
Leegoonz game Developer blog.
Visual strategy development lab Technical Art Director. Now working atAllegorithmic.
페이지
Home
About Leegoonz
LEEGOONZ.COM
ADVANCED SHADER DOT COM
ADVANCED SHADER FACEBOOK COMMUNITY
Substance user group in korea
More…
Share
Get link
Facebook
X
Pinterest
Email
Other Apps
Labels
programming
May 23, 2013
Texture Class
//this code is texture.h #ifndef TEXTURE_H #define TEXTURE_H #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #include
#endif //Platform Defined #if defined(__APPLE__) && defined(__MACH__) #include
#include
#else #include
#include
#endif #include
#include
#include
using std::string; using std::vector; using std::ifstream; struct TGA_Header { GLubyte ID_Length; GLubyte ColorMapType; GLubyte ImageType; GLubyte ColorMapSpecification[5]; GLshort xOrigin; GLshort yOrigin; GLshort ImageWidth; GLshort ImageHeight; GLubyte pixelDepth; }; class Texture { //Functions public: Texture(string filename , string name = ""); ~Texture(); //Variables public: unsigned char *imageData; unsigned int bpp; unsigned int width; unsigned int height; unsigned int texID; string name; static vector
textures; private: bool loadTGA(string filename); bool creatTexture(unsigned char *imageData , int width , int height, int type); }; #endif ========================================================================================= Below code is Texture.cpp ========================================================================================= #include "Texture.h" vector
Texture::textures; Texture::Texture(string in_filename , string in_name) { imageData = NULL; loadTGA(in_filename); name = in_name; textures.push_back(this); } Texture::~Texture() { for( vector
::iterator it = textures.begin(); it != textures.end(); it++) if((*it) == this) textures.erase(it); if(imageData) delete imageData; } bool Texture::loadTGA(string filename) { TGA_Header TGAheader; ifstream file( filename.data(), std::ios_base::binary); if( !file.is_open()) { return false; } if(!file.read( (char *)&TGAheader , sizeof(TGAheader))) { return false; } if ( TGAheader.ImageType !=2) { return false; } width = TGAheader.ImageWidth; height = TGAheader.ImageHeight; bpp = TGAheader.pixelDepth; if ( width <= 0 || height <= 0 || (bpp !=24 && bpp != 32)) { return false; } GLuint type = GL_RGBA; if ( bpp == 24) { type = GL_RGB; } GLuint bytesPerPixel = bpp / 8; GLuint imageSize = width * height * bytesPerPixel; imageData = new GLubyte[imageSize]; //이미지데이터에 아무것도 없으면 아무것도 반환 하지 않음 if (imageData == NULL ) { return false; } // if (!file.read (( char *)imageData, imageSize)) { delete imageData; return false; } //BGR 체널을 스왑 해서 RGB 로 변경 for ( GLuint i = 0; i < (int)imageSize; i += bytesPerPixel ) { GLuint temp = imageData[i]; imageData[i] = imageData[i + 2]; imageData[i + 2] = temp; } creatTexture(imageData , width , height , type); return true; } bool Texture::creatTexture(unsigned char *imageData , int width , int height, int type) { glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0 , type , width , height, 0 , type , GL_UNSIGNED_BYTE , imageData); return true; }
Game Developer Leegoon copyright all right reserved since 2010.
Comments
Popular Posts
December 14, 2013
AO with Gamma Control for Matcap bump Unlit shader
May 05, 2012
유니티 기본 스플레시 이미지 없애고 커스텀 스플레시 이미지 사용 하여 앱 인트로 제작 하기.
Comments
Post a Comment
덧글쓰기 기능 있는거 아시죠? ㅋㅋ