[osg-users] Creating a glowing sun in scene

Sebastian Messerschmidt sebastian.messerschmidt at gmx.de
Wed Feb 22 03:41:10 PST 2017


Hi Suraj,


> Hi All,
>
> As i had posted previously, I could obtain the following results attached in the screen short in my attempt to create a glowing sun in the scene.
>
> I am also attaching the source code.
>
> As I am taking the baby-steps in doing shaders programming in OSG, i am having a really bad time. My problem is :
>
> 1)
> I am not able to display the scene with both the glowing sun and cessna.osg in it. In the present form of the code it only displays me the blurred sphere( suppose to be the glowing sun).  How to get bnoth the blurred sphere and cessna displayed together?
>
> 2) After referring to online resources as previously suggested replies i could not implement a Bloom pass using shader? Any code snippet to do this?
> Basically i am not able to understand how to use the output of one fragment shader "combineFragSource"into another fragment shader that would make the HDR effect...

Basically you want postprocessing on rendered scene. Since you already 
have a RTT-Pass: You need another pass that binds the RTT-pass input 
texture and use it inside your second pass.
It sounds as like to have the blur on the sun only, so I guess you have 
to do the blur first and the rest of the scene on-top of that.
Take a look at osgPPU, it is great for learning how to do multipass 
rendering with shaders and FBO.
In general it boils down to have RTT-passes that either draw from the 
main-camera's perspective (like multiple scene passes) or to have a 
fullscreen-pass that renders on FBO-attached textures from other passes.

Cheers
Sebastian


>
> I know, its a novice question to ask but after putting efforts from my end  i am really stalled! Please help, community.
>
>
>
> ...
>
> Thank you!
>
> Cheers,
> Suraj
>
> /* -*-c++-*- OpenSceneGraph Cookbook
>   * Chapter 6 Recipe 6
>   *
> */
>
> #include <osg/Texture2D>
> #include <osg/Group>
> #include <osgDB/ReadFile>
> #include <osgViewer/Viewer>
> #include <osg/ShapeDrawable>
> #include "CommonFunctions"
> #include <osg/MatrixTransform>
> #include <iostream>
> #include <osg/Node>
> #define texsize 512
> //uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);
>
>
> static const char* vertSource = {
>      "void main(void)\n"
>      "{\n"
>      "   gl_Position = ftransform();\n"
>      "   gl_TexCoord[0] = gl_MultiTexCoord0;\n"
>      "}\n"
> };
>
> static const char* blurFragSource = {
>      "uniform sampler2D inputTex;\n"
>      "uniform vec2 blurDir;\n"
>      "void main(void)\n"
>      "{\n"
>      "   vec2 uv = gl_TexCoord[0].st;\n"
>      "   vec2 offset = vec2(0.02*blurDir.x, 0.02*blurDir.y);\n"
>      "   vec4 color = texture2D(inputTex, uv) * 0.3;\n"
>      "   color += texture2D(inputTex, uv - offset*3.0) * 0.15;\n"
>      "   color += texture2D(inputTex, uv - offset*2.0) * 0.15;\n"
>      "   color += texture2D(inputTex, uv - offset) * 0.25;\n"
>      "   color += texture2D(inputTex, uv + offset) * 0.25;\n"
>      "   color += texture2D(inputTex, uv + offset*2.0) * 0.1;\n"
>      "   color += texture2D(inputTex, uv + offset*3.0) * 0.15;\n"
>      "   gl_FragColor = color;\n"
>      "}\n"
> };
>
>
> static const char* combineFragSource = {
>      "uniform sampler2D sceneTex;\n"
>      "uniform sampler2D blurTex;\n"
>     
>      "void main(void)\n"
>      "{\n"
>      "   vec2 uv = gl_TexCoord[0].st;\n"
>      "   vec4 fullColor = texture2D(sceneTex, uv);\n"
>      "   vec4 blurColor = texture2D(blurTex, uv);\n"
>      "   float mod_color = fullColor + 1.5* (blurColor + fullColor);\n"
>      "   gl_FragColor = mod_color;\n"
>      "}\n"
> };
>
>
> typedef std::pair<osg::Camera*, osg::Texture*> RTTPair;
>
> RTTPair createColorInput( osg::Node* scene )
> {
>      osg::ref_ptr<osg::Texture2D> tex2D = new osg::Texture2D;
>      tex2D->setTextureSize( texsize, texsize );
>      tex2D->setInternalFormat( GL_RGBA );
>      
>      osg::ref_ptr<osg::Camera> camera = osgCookBook::createRTTCamera(osg::Camera::COLOR_BUFFER, tex2D.get());
>      camera->addChild( scene );
>      return RTTPair(camera.release(), tex2D.get());
> }
>
> RTTPair createDepthInput( osg::Node* scene )
> {
>      osg::ref_ptr<osg::Texture2D> tex2D = new osg::Texture2D;
>      tex2D->setTextureSize( texsize, texsize );
>      tex2D->setInternalFormat( GL_DEPTH_COMPONENT24 );
>      tex2D->setSourceFormat( GL_DEPTH_COMPONENT );
>      tex2D->setSourceType( GL_FLOAT );
>      
>      osg::ref_ptr<osg::Camera> camera = osgCookBook::createRTTCamera(osg::Camera::DEPTH_BUFFER, tex2D.get());
>      camera->addChild( scene );
>      return RTTPair(camera.release(), tex2D.get());
> }
>
> RTTPair createBlurPass( osg::Texture* inputTex, const osg::Vec2& dir )
> {
>      osg::ref_ptr<osg::Texture2D> tex2D = new osg::Texture2D;
>      tex2D->setTextureSize( texsize, texsize );
>      tex2D->setInternalFormat( GL_RGBA );
>      osg::ref_ptr<osg::Camera> camera = osgCookBook::createRTTCamera( osg::Camera::COLOR_BUFFER, tex2D.get(), true);
>      
>      osg::ref_ptr<osg::Program> blurProg = new osg::Program;
>      blurProg->addShader( new osg::Shader(osg::Shader::VERTEX, vertSource) );
>      blurProg->addShader( new osg::Shader(osg::Shader::FRAGMENT, blurFragSource) );
>      
>      osg::StateSet* ss = camera->getOrCreateStateSet();
>      ss->setTextureAttributeAndModes( 0, inputTex );
>      ss->setAttributeAndModes( blurProg.get(), osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE );
>      ss->addUniform( new osg::Uniform("sceneTex", 0) );
>      ss->addUniform( new osg::Uniform("blurDir", dir) );
>      return RTTPair(camera.release(), tex2D.get());
> }
>
>
> int main( int argc, char** argv )
> {
>      osg::ArgumentParser arguments( &argc, argv );
>      //osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles( arguments );
>      osg::Node* scene = osgDB::readNodeFiles( arguments );
>      osg::Group* mygroup = new osg::Group;
> 	osg::Node* cessna = osgDB::readNodeFile("/home/suraj/osgex/ex_cam/cessna.osg");
>      osg::MatrixTransform * mt_cessna = new osg::MatrixTransform;
>      mt_cessna->setMatrix(osg::Matrix::translate(0,0, 2));
>      mt_cessna->addChild(cessna);
>     
>      if ( !scene )
>       {//scene = osgDB::readNodeFile("lz.osg");
>
> //Model drawing
> osg::ref_ptr<osg::Geode> geode = new osg::Geode();
> osg::ref_ptr<osg::ShapeDrawable> sh1 = new osg::ShapeDrawable;
> sh1->setShape(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),10.0f ) );
> sh1->setColor(osg::Vec4(1.0,1.0,1.0,0.5));
> geode->addDrawable(sh1.get() );
> mygroup->addChild(geode);
> scene=dynamic_cast <osg::Node*>(mygroup);
>      }
>      // The first pass: color
>      RTTPair pass0_color = createColorInput( scene );
>      
>      // The first pass: depth
>      RTTPair pass0_depth = createDepthInput( scene );
>      
>      // The horizonal blur pass
>      RTTPair pass1 = createBlurPass( pass0_color.second, osg::Vec2(1, 0) );
>      
>      // The vertical blur pass
>      RTTPair pass2 = createBlurPass( pass1.second, osg::Vec2(0, 1) );
>
>      // The final pass
>      osg::ref_ptr<osg::Camera> hudCamera = osgCookBook::createHUDCamera(0.0, 1.0, 0.0, 1.0);
>       hudCamera->addChild( osgCookBook::createScreenQuad(1.0f, 1.0f, 1.) );
>      // hudCamera->addChild( mt_cessna );
>      
>      osg::ref_ptr<osg::Program> finalProg = new osg::Program;
>      finalProg->addShader( new osg::Shader(osg::Shader::VERTEX, vertSource) );
>      finalProg->addShader( new osg::Shader(osg::Shader::FRAGMENT, combineFragSource) );
>      
>       
>      osg::StateSet* stateset = hudCamera->getOrCreateStateSet();
>      stateset->setTextureAttributeAndModes( 0, pass0_color.second );
>      stateset->setTextureAttributeAndModes( 1, pass2.second );
>      stateset->setTextureAttributeAndModes( 2, pass0_depth.second );
>      //stateset->setTextureAttributeAndModes( 3, pass3.second );
>      stateset->setAttributeAndModes( finalProg.get() );
>      stateset->addUniform( new osg::Uniform("sceneTex", 0) );
>      stateset->addUniform( new osg::Uniform("blurTex", 1) );
>         // Build the scene graph
>      osg::ref_ptr<osg::Group> root = new osg::Group;
>      root->addChild(mt_cessna);
>      root->addChild( pass0_color.first );
>      //root->addChild( pass0_depth.first );
>      root->addChild( pass1.first );
>      root->addChild( pass2.first );
>      root->addChild( hudCamera.get() );
>      
>      osgViewer::Viewer viewer;
>      viewer.getCamera()->setComputeNearFarMode( osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR );
>      viewer.setSceneData( root.get() );
>      return viewer.run();
> }
> ...
>
> Thank you!
>
> Cheers,
> Suraj
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=70310#70310
>
>
>
>
> Attachments:
> http://forum.openscenegraph.org//files/screenshot_1_514.png
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




More information about the osg-users mailing list