#include #include #include #include typedef void (APIENTRY * MyCompressedTexImage2DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data); using namespace osg; Texture2D::Texture2D(): _textureWidth(0), _textureHeight(0), _numMimpmapLevels(0) { } Texture2D::Texture2D(const Texture2D& text,const CopyOp& copyop): Texture(text,copyop), _image(copyop(text._image.get())), _textureWidth(text._textureWidth), _textureHeight(text._textureHeight), _numMimpmapLevels(text._numMimpmapLevels), _subloadCallback(text._subloadCallback) { } Texture2D::~Texture2D() { } int Texture2D::compare(const StateAttribute& sa) const { // check the types are equal and then create the rhs variable // used by the COMPARE_StateAttribute_Paramter macro's below. COMPARE_StateAttribute_Types(Texture2D,sa) if (_image!=rhs._image) // smart pointer comparison. { if (_image.valid()) { if (rhs._image.valid()) { if (_image->getFileName()getFileName()) return -1; else if (_image->getFileName()>rhs._image->getFileName()) return 1;; } else { return 1; // valid lhs._image is greater than null. } } else if (rhs._image.valid()) { return -1; // valid rhs._image is greater than null. } } int result = compareTexture(rhs); if (result!=0) return result; // compare each paramter in turn against the rhs. COMPARE_StateAttribute_Parameter(_textureWidth) COMPARE_StateAttribute_Parameter(_textureHeight) COMPARE_StateAttribute_Parameter(_subloadCallback) return 0; // passed all the above comparison macro's, must be equal. } void Texture2D::setImage(Image* image) { // delete old texture objects. for(TextureNameList::iterator itr=_handleList.begin(); itr!=_handleList.end(); ++itr) { if (*itr != 0) { // contact global texture object handler to delete texture objects // in appropriate context. // glDeleteTextures( 1L, (const GLuint *)itr ); *itr = 0; } } _image = image; } void Texture2D::apply(State& state) const { // get the contextID (user defined ID of 0 upwards) for the // current OpenGL context. const uint contextID = state.getContextID(); // get the globj for the current contextID. GLuint& handle = getTextureObject(contextID); if (handle != 0) { glBindTexture( GL_TEXTURE_2D, handle ); if (getTextureParameterDity(state.getContextID())) applyTexParameters(GL_TEXTURE_2D,state); if (_subloadCallback.valid()) { _subloadCallback->subload(*this,state); } } else if (_subloadCallback.valid()) { glGenTextures( 1L, (GLuint *)&handle ); glBindTexture( GL_TEXTURE_2D, handle ); applyTexParameters(GL_TEXTURE_2D,state); _subloadCallback->load(*this,state); // in theory the following line is redundent, but in practice // have found that the first frame drawn doesn't apply the textures // unless a second bind is called?!! // perhaps it is the first glBind which is not required... glBindTexture( GL_TEXTURE_2D, handle ); } else if (_image.valid() && _image->data()) { glGenTextures( 1L, (GLuint *)&handle ); glBindTexture( GL_TEXTURE_2D, handle ); applyTexParameters(GL_TEXTURE_2D,state); applyTexImage2D(GL_TEXTURE_2D,_image.get(),state, _textureWidth, _textureHeight, _numMimpmapLevels); // in theory the following line is redundent, but in practice // have found that the first frame drawn doesn't apply the textures // unless a second bind is called?!! // perhaps it is the first glBind which is not required... glBindTexture( GL_TEXTURE_2D, handle ); } else { glBindTexture( GL_TEXTURE_2D, 0 ); } } void Texture2D::computeInternalFormat() const { if (_image.valid()) computeInternalFormatWithImage(*_image); } void Texture2D::copyTexImage2D(State& state, int x, int y, int width, int height ) { const uint contextID = state.getContextID(); // get the globj for the current contextID. GLuint& handle = getTextureObject(contextID); if (handle) { if (width==(int)_textureWidth && height==(int)_textureHeight) { // we have a valid texture object which is the right size // so lets play clever and use copyTexSubImage2D instead. // this allows use to reuse the texture object and avoid // expensive memory allocations. copyTexSubImage2D(state,0 ,0, x, y, width, height); return; } // the relevent texture object is not of the right size so // needs to been deleted // remove previously bound textures. dirtyTextureObject(); // note, dirtyTextureObject() dirties all the texture objects for // this texture, is this right? Perhaps we should dirty just the // one for this context. Note sure yet will leave till later. // RO July 2001. } // remove any previously assigned images as these are nolonger valid. _image = NULL; // switch off mip-mapping. _min_filter = LINEAR; _mag_filter = LINEAR; // Get a new 2d texture handle. glGenTextures( 1, &handle ); glBindTexture( GL_TEXTURE_2D, handle ); applyTexParameters(GL_TEXTURE_2D,state); glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, x, y, width, height, 0 ); _textureWidth = width; _textureHeight = height; // inform state that this texture is the current one bound. state.haveAppliedAttribute(this); } void Texture2D::copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height ) { const uint contextID = state.getContextID(); // get the globj for the current contextID. GLuint& handle = getTextureObject(contextID); if (handle) { // we have a valid image glBindTexture( GL_TEXTURE_2D, handle ); applyTexParameters(GL_TEXTURE_2D,state); glCopyTexSubImage2D( GL_TEXTURE_2D, 0, xoffset,yoffset, x, y, width, height); /* Redundant, delete later */ glBindTexture( GL_TEXTURE_2D, handle ); // inform state that this texture is the current one bound. state.haveAppliedAttribute(this); } else { // no texture object already exsits for this context so need to // create it upfront - simply call copyTexImage2D. copyTexImage2D(state,x,y,width,height); } }