[osg-users] GLSL Texture Animation Blinking

Ümit Uzun umituzun84 at gmail.com
Fri Sep 19 00:39:59 PDT 2008


Hi All,

I am trying to create GLSL texture animation on terrain surface. To
simplify, I am using only 4 image and one of them white image to mix using
alpha value. The others are caustics textures. When I create animation some
frame blinking with dark screen as you see from
VIDEO<http://www.fileden.com/files/2007/9/10/1423182/BlinkingTerrain.wmv>.
I am using OSG-2.7 on XP. What should I do to take smooth frames?

Another question, how should I send the images to the GLSL fragment shader?
I have to use 30 caustics texture in every LOD and I will create 4 LOD. So
how should I organize these 120 texture for making caustics animation? I
think using Uniforms for every texture isn't help me because of texture
units can't enough for all of them.

Best Regards.

Umit Uzun


-------------------------------Vertex-Shader-----------------------------------
    void main()
    {
        gl_TexCoord[0] = gl_MultiTexCoord0;
        gl_Position = ftransform();
    }
-------------------------------Vertex-Shader-----------------------------------
-------------------------------Fragment-Shader-----------------------------------
  uniform sampler2D Texture0;
  uniform sampler2D Texture1;
  uniform sampler2D Texture2;
  uniform sampler2D Texture3;
  uniform float timer;

  //-------------------
  void main()
  {
     vec4 texel0, texel1,texel2,texel3, resultColor;
     //-------------------
     texel0 = texture2D(Texture0, gl_TexCoord[0].st);
     texel1 = texture2D(Texture1, gl_TexCoord[0].st);
     texel2 = texture2D(Texture2, gl_TexCoord[0].st);
     texel3 = texture2D(Texture3, gl_TexCoord[0].st);
     //-------------------
     if( timer < 1.0f )
    {
        resultColor = mix(texel0,texel1,0.5f);
    }
    else if( timer <= 2.0f)
    {
        resultColor = mix(texel0,texel1,0.5f);
    }
    else if(timer <= 3.0f)
    {
        resultColor = mix(texel0,texel2,0.5f);
    }
    else if(timer <= 4.0f)
    {
        resultColor = mix(texel0,texel3,0.5f);
    }
     gl_FragColor = resultColor;
  }
-------------------------------Fragment-Shader-----------------------------------
---------------------------------OSG-Code-------------------------------------
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgDB/ReadFile>
#include <osg/Texture2D>
#include <osgGA/StateSetManipulator>
#include <osg/Program>
#include <osg/PolygonMode>
#include <math.h>

static float frameTime = 0.0f;
osg::Uniform* timer;

class UpdateStateCallback : public osg::NodeCallback
{
    public:
        UpdateStateCallback() {}

        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            if(frameTime >= 4.0f) frameTime = 0.0f;
            frameTime += 0.1f;
            timer->set(frameTime);
            traverse(node,nv);
        }
};

int main()
{
    osgViewer::Viewer viewer;
    osg::ref_ptr<osg::Group> root (new osg::Group);

    osg::ref_ptr<osg::Node> terrain = osgDB::readNodeFile("Terrain2.3ds");
    osg::ref_ptr<osg::StateSet> stateset = (terrain->getOrCreateStateSet());

    //Its shader objects
    osg::ref_ptr<osg::Program> terrainProg (new osg::Program);
    osg::ref_ptr<osg::Shader>
terrainvertexShader(osg::Shader::readShaderFile (osg::Shader::VERTEX,
"vertex.vert"));
    osg::ref_ptr<osg::Shader> terrainfragShader(osg::Shader::readShaderFile
(osg::Shader::FRAGMENT, "fragment.frag"));

    //Binding the box shaders to its program
    terrainProg->addShader(terrainvertexShader.get());
    terrainProg->addShader(terrainfragShader.get());

    //Attaching the shader program to the node
     stateset->setAttribute(terrainProg.get());

    osg::ref_ptr<osg::Image> image0 (osgDB::readImageFile("white.bmp"));
    osg::ref_ptr<osg::Texture2D> tex0 (new osg::Texture2D(image0.get()));

stateset->setTextureAttributeAndModes(0,tex0.get(),osg::StateAttribute::ON);
    stateset->addUniform(new osg::Uniform("Texture0",0));

    osg::ref_ptr<osg::Image> image1
(osgDB::readImageFile("caustics_deep_002.tga"));
    osg::ref_ptr<osg::Texture2D> tex1 (new osg::Texture2D(image1.get()));

stateset->setTextureAttributeAndModes(1,tex1.get(),osg::StateAttribute::ON
);
    stateset->addUniform(new osg::Uniform("Texture1",1));

    osg::ref_ptr<osg::Image> image2
(osgDB::readImageFile("caustics_deep_003.tga"));
    osg::ref_ptr<osg::Texture2D> tex2 (new osg::Texture2D(image2.get()));

stateset->setTextureAttributeAndModes(2,tex2.get(),osg::StateAttribute::ON
);
    stateset->addUniform(new osg::Uniform("Texture2",2));

    osg::ref_ptr<osg::Image> image3
(osgDB::readImageFile("caustics_deep_004.tga"));
    osg::ref_ptr<osg::Texture2D> tex3 (new osg::Texture2D(image3.get()));

stateset->setTextureAttributeAndModes(3,tex3.get(),osg::StateAttribute::ON
);
    stateset->addUniform(new osg::Uniform("Texture3",3));

    stateset->addUniform(new osg::Uniform("timer",frameTime));
    timer = stateset->getUniform("timer");

    //Adding all the shape to the root node for rendering
    root->addChild(terrain.get());
    root->setUpdateCallback(new UpdateStateCallback());

    // add the state manipulator to the viewer
    viewer.addEventHandler( new
osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );

    //Stats Event Handler s key
    viewer.addEventHandler(new osgViewer::StatsHandler);

    viewer.setSceneData( root.get() );

    return (viewer.run());
}
---------------------------------OSG-Code-------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20080919/dac06e1b/attachment-0002.htm>


More information about the osg-users mailing list