From 5a974ab09192ceb58e5748b2e8556d664f6f6fd0 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 1 Apr 2003 20:32:57 +0000 Subject: [PATCH] Fixes to osg::Texture's handling of anisotropy updating and hardware generate mip mapping. Added switching off generate mip map from osgText::Font.cpp. Changed savedmodel.osg to saved_model.osg in ViewerEventHandler. --- examples/osgtexture2D/osgtexture2D.cpp | 174 +++++++++++++++++++++++-- src/osg/Texture.cpp | 30 +++-- src/osgProducer/ViewerEventHandler.cpp | 2 +- src/osgText/Font.cpp | 46 +++++-- 4 files changed, 219 insertions(+), 33 deletions(-) diff --git a/examples/osgtexture2D/osgtexture2D.cpp b/examples/osgtexture2D/osgtexture2D.cpp index 6dd370e34..db659a27f 100644 --- a/examples/osgtexture2D/osgtexture2D.cpp +++ b/examples/osgtexture2D/osgtexture2D.cpp @@ -30,6 +30,11 @@ public: _currPos(0), _prevTime(0.0) { + // start with a mip mapped mode to ensure that + _minFilterList.push_back(osg::Texture2D::LINEAR_MIPMAP_LINEAR); + _magFilterList.push_back(osg::Texture2D::LINEAR); + _textList.push_back("Tri-linear mip mapping (default filtering)\nsetFilter(MIN_FILTER,LINEAR_MIP_LINEAR)\nsetFilter(MAG_FILTER,LINEAR)"); + _minFilterList.push_back(osg::Texture2D::NEAREST); _magFilterList.push_back(osg::Texture2D::NEAREST); _textList.push_back("Nearest filtering\nsetFilter(MIN_FILTER,NEAREST)\nsetFilter(MAG_FILTER,NEAREST)"); @@ -50,9 +55,6 @@ public: _magFilterList.push_back(osg::Texture2D::LINEAR); _textList.push_back("bi-linear mip mapping\nsetFilter(MIN_FILTER,NEAREST_MIPMAP_LINEAR)\nsetFilter(MAG_FILTER,LINEAR)"); - _minFilterList.push_back(osg::Texture2D::LINEAR_MIPMAP_LINEAR); - _magFilterList.push_back(osg::Texture2D::LINEAR); - _textList.push_back("Tri-linear mip mapping (default filtering)\nsetFilter(MIN_FILTER,LINEAR_MIP_LINEAR)\nsetFilter(MAG_FILTER,LINEAR)"); setValues(); } @@ -146,6 +148,7 @@ osg::Node* createFilterWall(osg::BoundingBox& bb,const std::string& filename) // set up the texture state. osg::Texture2D* texture = new osg::Texture2D; + texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state. texture->setImage(osgDB::readImageFile(filename)); osg::StateSet* stateset = geom->getOrCreateStateSet(); @@ -297,6 +300,7 @@ osg::Node* createAnisotripicWall(osg::BoundingBox& bb,const std::string& filenam // set up the texture state. osg::Texture2D* texture = new osg::Texture2D; + texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state. texture->setImage(osgDB::readImageFile(filename)); osg::StateSet* stateset = geom->getOrCreateStateSet(); @@ -328,7 +332,7 @@ osg::Node* createAnisotripicWall(osg::BoundingBox& bb,const std::string& filenam /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // -// filter wall and animation callback. +// wrap wall and animation callback. // class WrapCallback : public osg::NodeCallback { @@ -352,7 +356,7 @@ public: _textList.push_back("Clamp to border color extension\nsetWrap(WRAP_S,CLAMP_TO_BORDER)"); _wrapList.push_back(osg::Texture2D::REPEAT); - _textList.push_back("Repear wrap\nsetWrap(WRAP_S,REPEAT)"); + _textList.push_back("Repeat wrap\nsetWrap(WRAP_S,REPEAT)"); _wrapList.push_back(osg::Texture2D::MIRROR); _textList.push_back("Mirror wrap extension\nsetWrap(WRAP_S,MIRROR)"); @@ -435,7 +439,7 @@ osg::Node* createWrapWall(osg::BoundingBox& bb,const std::string& filename) geom->setTexCoordArray(0,texcoords); osg::Vec3Array* normals = new osg::Vec3Array(1); - (*normals)[0].set(0.0f,-1.0f,0.0f); + (*normals)[0].set(-1.0f,0.0f,0.0f); geom->setNormalArray(normals); geom->setNormalBinding(osg::Geometry::BIND_OVERALL); @@ -448,6 +452,7 @@ osg::Node* createWrapWall(osg::BoundingBox& bb,const std::string& filename) // set up the texture state. osg::Texture2D* texture = new osg::Texture2D; + texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state. texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); // only used when wrap is set to CLAMP_TO_BORDER texture->setImage(osgDB::readImageFile(filename)); @@ -482,12 +487,156 @@ osg::Node* createWrapWall(osg::BoundingBox& bb,const std::string& filename) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // -// filter wall and animation callback. +// sublooad wall and animation callback. // -osg::Node* createSubloadWall(osg::BoundingBox& bb,const std::string& filename) +class ImageUpdateCallback : public osg::NodeCallback { - return 0; +public: + + ImageUpdateCallback(osg::Texture2D* texture,osgText::Text* text,double delay=1.0): + _texture(texture), + _text(text), + _delay(delay), + _currPos(0), + _prevTime(0.0) + { + + + _imageList.push_back(osgDB::readImageFile("dog_left_eye.jpg")); + _textList.push_back("Subloaded Image 1 - dog_left_eye.jpg"); + + _imageList.push_back(osgDB::readImageFile("dog_right_eye.jpg")); + _textList.push_back("Subloaded Image 2 - dog_right_eye.jpg"); + + setValues(); + } + + virtual void operator()(osg::Node*, osg::NodeVisitor* nv) + { + if (nv->getFrameStamp()) + { + double currTime = nv->getFrameStamp()->getReferenceTime(); + if (currTime-_prevTime>_delay) + { + // update filter modes and text. + setValues(); + + // advance the current positon, wrap round if required. + _currPos++; + if (_currPos>=_imageList.size()) _currPos=0; + + // record time + _prevTime = currTime; + } + } + } + + void setValues() + { + // Note, as long as the images are the same dimensions subloading will be used + // to update the textures. If dimensions change then the texture objects have + // to be deleted and re-recreated. + // + // The load/subload happens during the draw traversal so doesn't happen on + // the setImage which just updates internal pointers and modifed flags. + + _texture->setImage(_imageList[_currPos].get()); + + //_texture->dirtyTextureObject(); + + _text->setText(_textList[_currPos]); + } + +protected: + + typedef std::vector< osg::ref_ptr > ImageList; + typedef std::vector TextList; + + osg::ref_ptr _texture; + osg::ref_ptr _text; + double _delay; + + ImageList _imageList; + TextList _textList; + + unsigned int _currPos; + double _prevTime; + +}; + +osg::Node* createSubloadWall(osg::BoundingBox& bb) +{ + osg::Group* group = new osg::Group; + + // left hand side of bounding box. + osg::Vec3 top_left(bb.xMin(),bb.yMax(),bb.zMax()); + osg::Vec3 bottom_left(bb.xMin(),bb.yMax(),bb.zMin()); + osg::Vec3 bottom_right(bb.xMax(),bb.yMax(),bb.zMin()); + osg::Vec3 top_right(bb.xMax(),bb.yMax(),bb.zMax()); + osg::Vec3 center((bb.xMax()+bb.xMin())*0.5f,bb.yMax(),(bb.zMin()+bb.zMax())*0.5f); + float height = bb.zMax()-bb.zMin(); + + // create the geometry for the wall. + osg::Geometry* geom = new osg::Geometry; + + osg::Vec3Array* vertices = new osg::Vec3Array(4); + (*vertices)[0] = top_left; + (*vertices)[1] = bottom_left; + (*vertices)[2] = bottom_right; + (*vertices)[3] = top_right; + geom->setVertexArray(vertices); + + osg::Vec2Array* texcoords = new osg::Vec2Array(4); + (*texcoords)[0].set(0.0f,1.0f); + (*texcoords)[1].set(0.0f,0.0f); + (*texcoords)[2].set(1.0f,0.0f); + (*texcoords)[3].set(1.0f,1.0f); + geom->setTexCoordArray(0,texcoords); + + osg::Vec3Array* normals = new osg::Vec3Array(1); + (*normals)[0].set(0.0f,-1.0f,0.0f); + geom->setNormalArray(normals); + geom->setNormalBinding(osg::Geometry::BIND_OVERALL); + + geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4)); + + osg::Geode* geom_geode = new osg::Geode; + geom_geode->addDrawable(geom); + group->addChild(geom_geode); + + + // set up the texture state. + osg::Texture2D* texture = new osg::Texture2D; + texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state. + texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); + texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); + + osg::StateSet* stateset = geom->getOrCreateStateSet(); + stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); + + // create the text label. + + osgText::Text* text = new osgText::Text; + text->setFont("fonts/arial.ttf"); + text->setPosition(center); + text->setCharacterSize(height*0.03f); + text->setAlignment(osgText::Text::CENTER_CENTER); + text->setAxisAlignment(osgText::Text::XZ_PLANE); + + osg::Geode* text_geode = new osg::Geode; + text_geode->addDrawable(text); + + osg::StateSet* text_stateset = text_geode->getOrCreateStateSet(); + text_stateset->setAttributeAndModes(new osg::PolygonOffset(-1.0f,-1.0f),osg::StateAttribute::ON); + + group->addChild(text_geode); + + // set the update callback to cycle through the various min and mag filter modes. + group->setUpdateCallback(new ImageUpdateCallback(texture,text)); + + return group; + } @@ -496,13 +645,16 @@ osg::Node* createModel() // create the root node which will hold the model. osg::Group* root = new osg::Group(); + + // turn off lighting + root->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); osg::BoundingBox bb(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f); root->addChild(createFilterWall(bb,"Images/lz.rgb")); - root->addChild(createAnisotripicWall(bb,"Images/tank.rgb")); + root->addChild(createAnisotripicWall(bb,"Images/primitives.gif")); root->addChild(createWrapWall(bb,"Images/tree0.rgba")); - root->addChild(createSubloadWall(bb,"Images/tree0.rgba")); + root->addChild(createSubloadWall(bb)); return root; } diff --git a/src/osg/Texture.cpp b/src/osg/Texture.cpp index 1328b687f..1fcf17944 100644 --- a/src/osg/Texture.cpp +++ b/src/osg/Texture.cpp @@ -350,7 +350,7 @@ void Texture::applyTexParameters(GLenum target, State& state) const glTexParameteri( target, GL_TEXTURE_MIN_FILTER, _min_filter); glTexParameteri( target, GL_TEXTURE_MAG_FILTER, _mag_filter); - if (_maxAnisotropy>1.0f && extensions->isTextureFilterAnisotropicSupported()) + if (extensions->isTextureFilterAnisotropicSupported()) { // note, GL_TEXTURE_MAX_ANISOTROPY_EXT will either be defined // by gl.h (or via glext.h) or by include/osg/Texture. @@ -442,7 +442,12 @@ void Texture::applyTexImage2D_load(GLenum target, const Image* image, State& sta if( _min_filter == LINEAR || _min_filter == NEAREST || useHardwareMipMapGeneration) { - if (useHardwareMipMapGeneration) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE); + bool hardwareMipMapOn = false; + if (_min_filter != LINEAR && _min_filter != NEAREST) + { + if (useHardwareMipMapGeneration) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE); + hardwareMipMapOn = true; + } if ( !compressed_image) { @@ -468,7 +473,7 @@ void Texture::applyTexImage2D_load(GLenum target, const Image* image, State& sta data); } - if (useHardwareMipMapGeneration) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE); + if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE); } else { @@ -572,12 +577,13 @@ void Texture::applyTexImage2D_subload(GLenum target, const Image* image, State& // image size has changed so we have to re-load the image from scratch. if (image->s()!=inwidth || image->t()!=inheight) { - applyTexImage2D_subload(target, image, state, inwidth, inheight,numMimpmapLevels); + + applyTexImage2D_load(target, image, state, inwidth, inheight,numMimpmapLevels); return; } // else image size the same as when loaded so we can go ahead and subload - + // get the contextID (user defined ID of 0 upwards) for the // current OpenGL context. @@ -649,7 +655,14 @@ void Texture::applyTexImage2D_subload(GLenum target, const Image* image, State& if( _min_filter == LINEAR || _min_filter == NEAREST || useHardwareMipMapGeneration) { - if (useHardwareMipMapGeneration) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE); + + bool hardwareMipMapOn = false; + if (_min_filter != LINEAR && _min_filter != NEAREST) + { + if (useHardwareMipMapGeneration) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE); + hardwareMipMapOn = true; + } + if (!compressed_image) { @@ -671,7 +684,7 @@ void Texture::applyTexImage2D_subload(GLenum target, const Image* image, State& data ); } - if (useHardwareMipMapGeneration) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE); + if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE); } else { @@ -732,7 +745,8 @@ void Texture::applyTexImage2D_subload(GLenum target, const Image* image, State& { if (!compressed_image) { - numMimpmapLevels = 0; + + numMimpmapLevels = 0; int width = s_powerOfTwo; int height = t_powerOfTwo; diff --git a/src/osgProducer/ViewerEventHandler.cpp b/src/osgProducer/ViewerEventHandler.cpp index 1e64a9843..edd02c1b5 100644 --- a/src/osgProducer/ViewerEventHandler.cpp +++ b/src/osgProducer/ViewerEventHandler.cpp @@ -672,7 +672,7 @@ void DrawCallback::createStatsText() ViewerEventHandler::ViewerEventHandler(OsgCameraGroup* cg): _cg(cg), - _writeNodeFileName("savedmodel.osg"), + _writeNodeFileName("saved_model.osg"), _displayHelp(false), _frameStatsMode(NO_STATS) { diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index ccb027c0a..b8e235674 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -388,6 +388,8 @@ void Font::GlyphTexture::apply(osg::State& state) const // get the globj for the current contextID. GLuint& handle = getTextureObject(contextID); + bool generateMipMapOn = false; + if (handle == 0) { // being bound for the first time, need to allocate the texture @@ -395,6 +397,7 @@ void Font::GlyphTexture::apply(osg::State& state) const glBindTexture( GL_TEXTURE_2D, handle ); applyTexParameters(GL_TEXTURE_2D,state); + // need to look at generate mip map extension if mip mapping required. switch(_min_filter) @@ -403,8 +406,10 @@ void Font::GlyphTexture::apply(osg::State& state) const case NEAREST_MIPMAP_LINEAR: case LINEAR_MIPMAP_NEAREST: case LINEAR_MIPMAP_LINEAR: - if (generateMipMapSupported) { + if (generateMipMapSupported) + { glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE); + generateMipMapOn = true; } else glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, LINEAR); break; @@ -419,6 +424,7 @@ void Font::GlyphTexture::apply(osg::State& state) const GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 0 ); + } else @@ -428,21 +434,26 @@ void Font::GlyphTexture::apply(osg::State& state) const if (getTextureParameterDirty(contextID)) { applyTexParameters(GL_TEXTURE_2D,state); + } + bool generateMipMapOn = false; - // need to look at generate mip map extension if mip mapping required. - switch(_min_filter) + // need to look at generate mip map extension if mip mapping required. + switch(_min_filter) + { + case NEAREST_MIPMAP_NEAREST: + case NEAREST_MIPMAP_LINEAR: + case LINEAR_MIPMAP_NEAREST: + case LINEAR_MIPMAP_LINEAR: + if (generateMipMapSupported) { - case NEAREST_MIPMAP_NEAREST: - case NEAREST_MIPMAP_LINEAR: - case LINEAR_MIPMAP_NEAREST: - case LINEAR_MIPMAP_LINEAR: - if (generateMipMapSupported) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE); - else glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, LINEAR); - break; - default: - // not mip mapping so no problems. - break; + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE); + generateMipMapOn = true; } + else glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, LINEAR); + break; + default: + // not mip mapping so no problems. + break; } } @@ -468,6 +479,15 @@ void Font::GlyphTexture::apply(osg::State& state) const { //std::cout << "no need to subload "<