std::auto_ptr< Image > Graphics2D::loadImage(Material::Type type, RPG2kString const& name, bool trans) const { switch(type) { #define PP_case(type) case Material::type: PP_imageMaterial(PP_case) break; #undef PP_case default: rpg2k_assert(false); } for(int i = 0; i < Material::RES_END; i++) { for(int j = 0; j < IMG_END; j++) { SystemString filename = getOwner().getMaterial().getFileName( (Material::Resource)i, type, name, imageExt_[j] ); unsigned char* img = NULL; int width, height, channels; if( j == IMG_xyz ) { img = Image::xyz().load(filename, width, height, channels, trans); } else { SOIL_set_transparent(trans); img = SOIL_load_image(filename.c_str(), &width, &height, &channels, SOIL_LOAD_AUTO); } if(img == NULL) continue; GLuint texID = SOIL_internal_create_OGL_texture( img, width, height, channels, SOIL_CREATE_NEW_ID, SOIL_FLAG_TEXTURE_REPEATS, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_MAX_TEXTURE_SIZE ); if( j == IMG_xyz ) Image::xyz().free(img); else SOIL_free_image_data(img); cout << "Load Image success. NAME = " << name.toSystem() << ";" << endl; return std::auto_ptr< Image >( new Image(texID, width, height) ); } } throw std::runtime_error( std::string("Cannot load Image. Material = ").append( getOwner().getMaterial().getName(type) ) .append("; NAME = \"").append( name.toSystem() ).append("\";") ); }
void TextureRes::loadPNG(const char* path, bool reserveData, bool revertY){ _pImgData = SOIL_load_image(path, &_w, &_h, &_numChannels, SOIL_LOAD_AUTO); _numChannels == 4 ? GL_RGBA:GL_RGB; _glId = SOIL_internal_create_OGL_texture(_pImgData, _w, _h, _numChannels, SOIL_CREATE_NEW_ID, revertY ? SOIL_FLAG_INVERT_Y:0, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_MAX_TEXTURE_SIZE); if ( !reserveData ){ SOIL_free_image_data(_pImgData); _pImgData = NULL; } if ( _glId == 0 ){ lwerror("Failed to load texture: path=" << path); _glId = -1; } }
int TextureRes::loadAndCreateOgl(const unsigned char* buf, int buflen) { unsigned char* pImgData = SOIL_load_image_from_memory(buf, buflen, &w, &h, &numChannels, SOIL_LOAD_AUTO); assert(pImgData); glId = SOIL_internal_create_OGL_texture(pImgData, w, h, numChannels, SOIL_CREATE_NEW_ID, 0, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_MAX_TEXTURE_SIZE); SOIL_free_image_data(pImgData); if ( glId == 0 ){ glId = -1; return -1; } return 0; //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); }
TextureRes::TextureRes(int w, int h, const char* pData, bool hasAlpha, bool reserveData, bool revertY) : _glId(-1), _pImgData(NULL){ lwassert(pData); _w = w; _h = h; _numChannels = hasAlpha?4:3; int sz = w*h*_numChannels; _pImgData = new unsigned char[sz]; memcpy(_pImgData, pData, sz); _glId = SOIL_internal_create_OGL_texture(_pImgData, w, h, _numChannels, SOIL_CREATE_NEW_ID, revertY ? SOIL_FLAG_INVERT_Y:0, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_MAX_TEXTURE_SIZE); if ( !reserveData ){ delete [] _pImgData; _pImgData = NULL; } if ( _glId == 0 ){ lwerror("Failed to create texture"); _glId = -1; } }