Show
Ignore:
Timestamp:
09/04/06 14:46:49 (8 years ago)
Author:
robert
Message:

Added osg::TexMat::s/getScaleByTextureRectangleSize feature that allows
one to use the osg::TexMat? to automatically scale non dimensional tex coordinats to
the size of the last texture rectangle applied.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgtexturerectangle/osgtexturerectangle.cpp

    r5381 r5522  
    2020#include <osg/Geometry> 
    2121#include <osg/Geode> 
     22#include <osg/TexMat> 
    2223 
    2324#include <osg/Group> 
     
    4142{ 
    4243public: 
    43     TexturePanCallback(osg::Geometry* geom, osg::Image* img, 
     44    TexturePanCallback(osg::TexMat* texmat, 
    4445                       double delay = 0.05) : 
    45         _geom(geom), 
    46         _img(img), 
     46        _texmat(texmat), 
    4747        _phaseS(35.0f), 
    4848        _phaseT(18.0f), 
     
    5555    virtual void operator()(osg::Node*, osg::NodeVisitor* nv) 
    5656    { 
    57         if (!_geom || !_img) 
     57        if (!_texmat) 
    5858            return; 
    5959 
     
    7070                // calculate new texture coordinates 
    7171                float s, t; 
    72                 s = ((sin(rad * _phaseS) + 1) * 0.5f) * (_img->s() * scaleR); 
    73                 t = ((sin(rad * _phaseT) + 1) * 0.5f) * (_img->t() * scaleR); 
    74  
    75                 // set new texture coordinate array 
    76                 osg::Vec2Array* texcoords = (osg::Vec2Array*) _geom->getTexCoordArray(0); 
    77                 float w = _img->s() * scale, h = _img->t() * scale; 
    78                 (*texcoords)[0].set(s, t+h); 
    79                 (*texcoords)[1].set(s, t); 
    80                 (*texcoords)[2].set(s+w, t); 
    81                 (*texcoords)[3].set(s+w, t+h); 
     72                s = ((sin(rad * _phaseS) + 1) * 0.5f) * (scaleR); 
     73                t = ((sin(rad * _phaseT) + 1) * 0.5f) * (scaleR); 
     74 
     75 
     76                _texmat->setMatrix(osg::Matrix::translate(s,t,1.0)*osg::Matrix::scale(scale,scale,1.0)); 
    8277 
    8378                // record time 
     
    8883 
    8984private: 
    90     osg::Geometry* _geom; 
    91     osg::Image* _img; 
     85    osg::TexMat* _texmat; 
    9286 
    9387    float _phaseS, _phaseT, _phaseScale; 
     
    149143    state->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); 
    150144 
     145    // setup state 
     146    osg::TexMat* texmat = new osg::TexMat; 
     147    texmat->setScaleByTextureRectangleSize(true); 
     148    state->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON); 
     149 
    151150    // turn off lighting  
    152151    state->setMode(GL_LIGHTING, osg::StateAttribute::OFF); 
     
    155154    osg::Geode* geode = new osg::Geode; 
    156155    geode->addDrawable(geom); 
    157     geode->setUpdateCallback(new TexturePanCallback(geom, img)); 
     156    geode->setUpdateCallback(new TexturePanCallback(texmat)); 
    158157     
    159158    return geode; 
     
    195194        "TextureRectangle Mini-HOWTO", 
    196195        "- essentially behaves like Texture2D, *except* that:", 
    197         "- tex coords must be non-normalized (0..pixel) instead of (0..1)", 
     196        "- tex coords must be non-normalized (0..pixel) instead of (0..1),\nalternatively you can use osg::TexMat to scale normal non dimensional texcoords.", 
    198197        "- wrap modes must be CLAMP, CLAMP_TO_EDGE, or CLAMP_TO_BORDER\n  repeating wrap modes are not supported", 
    199198        "- filter modes must be NEAREST or LINEAR since\n  mipmaps are not supported",