#include "osg/Texture" #include "osg/Input" #include "osg/Output" #include "osg/Registry" #include "osg/Image" #include "osg/Referenced" #include "osg/Notify" using namespace osg; Texture::Texture() { _handle = 0; _wrap_s = CLAMP; _wrap_t = CLAMP; _wrap_r = CLAMP; _min_filter = NEAREST_MIPMAP_LINEAR; _mag_filter = LINEAR; } Texture::~Texture() { if (_handle!=0) glDeleteTextures( 1, &_handle ); } Texture* Texture::instance() { static ref_ptr s_texture(new Texture); return s_texture.get(); } bool Texture::readLocalData(Input& fr) { bool iteratorAdvanced = false; if (fr[0].matchWord("file") && fr[1].isString()) { _image = fr.readImage(fr[1].getStr()); fr += 2; iteratorAdvanced = true; } WrapMode wrap; if (fr[0].matchWord("wrap_s") && matchWrapStr(fr[1].getStr(),wrap)) { _wrap_s = wrap; fr+=2; iteratorAdvanced = true; } if (fr[0].matchWord("wrap_t") && matchWrapStr(fr[1].getStr(),wrap)) { _wrap_t = wrap; fr+=2; iteratorAdvanced = true; } if (fr[0].matchWord("wrap_r") && matchWrapStr(fr[1].getStr(),wrap)) { _wrap_r = wrap; fr+=2; iteratorAdvanced = true; } FilterMode filter; if (fr[0].matchWord("min_filter") && matchFilterStr(fr[1].getStr(),filter)) { _min_filter = filter; fr+=2; iteratorAdvanced = true; } if (fr[0].matchWord("mag_filter") && matchFilterStr(fr[1].getStr(),filter)) { _mag_filter = filter; fr+=2; iteratorAdvanced = true; } return iteratorAdvanced; } bool Texture::writeLocalData(Output& fw) { if (_image.valid()) { fw.indent() << "file \""<<_image->getFileName()<<"\""<ensureDimensionsArePowerOfTwo(); glPixelStorei(GL_UNPACK_ALIGNMENT,_image->packing()); glGenTextures( 1, &_handle ); glBindTexture( GL_TEXTURE_2D, _handle ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _wrap_s ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _wrap_t ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _min_filter); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _mag_filter); if( _min_filter == LINEAR || _min_filter == NEAREST ) { glTexImage2D( GL_TEXTURE_2D, 0, _image->internalFormat(), _image->s(), _image->t(), 0, (GLenum)_image->pixelFormat(), (GLenum)_image->dataType(), _image->data() ); } else { gluBuild2DMipmaps( GL_TEXTURE_2D, _image->internalFormat(), _image->s(),_image->t(), (GLenum)_image->pixelFormat(), (GLenum)_image->dataType(), _image->data() ); } glBindTexture( GL_TEXTURE_2D, _handle ); } }