Hi All,

I want to create sky night effect on simple quad.3ds model with using GLSL
like RenderMonkey-> Examples-> GL2->ScreenSpace Effects->NightSky. I opened
up render monkey and analyzed the given sample and it's look like
THAT<http://www.fileden.com/files/2007/9/10/1423182/SkyNightShaderRenderMonkey.wmv>.
Then I have tried to port this sample to OSG but when I port fairly just as
RenderMonkey sample I couldn't get expected result which looks like
THAT<http://www.fileden.com/files/2007/9/10/1423182/NightSkyShaderOSG.wmv>.


What do you think about two given video? Is there anyone who can realize the
bug or drawback in my ported OSG code?

Best Regards.

Umit Uzun

Note : Given codes may have useless pattern because of haven't optimized.

/---------------------------OSG-CODE------------------------------/
static float frameTime = 0.0f;
static float addition = 0.001f;
osg::Uniform* timer;

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

        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            if(frameTime > 118.0f) frameTime = 0.0f;//addition = -0.1f;
            else if(frameTime <= 0.0f) addition = 0.001f;
            frameTime += addition;
            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("ScreenAlignedQuad.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,
"shaders/sualtiefekt.vert"));
    osg::ref_ptr<osg::Shader> terrainfragShader(osg::Shader::readShaderFile
(osg::Shader::FRAGMENT, "shaders/sualtiefekt.frag"));

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

    //Attaching the shader program to the node

stateset->setAttributeAndModes(terrainProg.get(),osg::StateAttribute::OFF);

    osg::ref_ptr<osg::Image> image0
(osgDB::readImageFile("NoiseVolume.dds"));
    osg::ref_ptr<osg::Texture3D> texture3D (new osg::Texture3D());
    texture3D->setImage(image0.get());


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

    stateset->addUniform(new
osg::Uniform("NorthernLightColor",osg::Vec4(0.3373,0.4253,0.7852,1.0000)));

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

    //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.setCameraManipulator(new osgGA::TrackballManipulator());

    // add the window size toggle handler
    viewer.addEventHandler(new osgViewer::WindowSizeHandler);

    osgUtil::Optimizer optimizer;
    optimizer.optimize(root.get());

    viewer.setSceneData( root.get() );

    return (viewer.run());
}
/---------------------------OSG-CODE------------------------------/

/---------------------------VERTEX-SHADER------------------------------/
varying vec2 vPosition;

void main(void)
{

   // Clean up inaccuracies
   vec2 Position;
   Position.xy = sign(gl_Vertex.xy);

   gl_Position = gl_ModelViewProjectionMatrix * vec4(Position.xy, 0.0, 1.0);
   vPosition = Position.xy * 0.5;
}
/---------------------------VERTEX-SHADER------------------------------/

/---------------------------FRAGMENT-SHADER------------------------------/
uniform sampler3D Noise;
uniform float time_0_X;
uniform vec4 NorthernLightColor;
varying vec2 vPosition;

void main(void)
{
   // Get some noise to offset the grid
   vec3 coord = 0.3 * vec3(vPosition.x + 0.5 * vPosition.y, vPosition.y, 0);
   vec2 noisy = vec2(
                        texture3D(Noise, coord).x,
                        texture3D(Noise, coord + 0.5).x
                     );

   // Create the point-grid pattern
   vec2 fp = fract(6.0 * vPosition + 8.0 * noisy);
   fp *= (1.0 - fp);
   float glitter = clamp(1.0 - 40.0 * (fp.x + fp.y), 0.0, 1.0);

   // Other noise to shadow some stars
   float shadow = texture3D(Noise, vec3(vPosition * 2.0, time_0_X * 0.1)).x;

   // Northern light
   float f = clamp(1.0 - 5.0 * vPosition.y * vPosition.y, 0.0, 1.0);
   float northernLight = f * (texture3D(Noise, vec3(vPosition.x * 0.6,
vPosition.y * 0.2, time_0_X * 0.05)).x - 0.2);
   // Combine and output
   float cmpResult = (glitter * float(shadow < 0.42));

   gl_FragColor = mix( vec4(cmpResult), NorthernLightColor, northernLight);
}
/---------------------------FRAGMENT-SHADER------------------------------/
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to