OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Creating a glowing sun in scene


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
suraj (Suraj Paul)
User


Joined: 26 Aug 2016
Posts: 24

PostPosted: Sat Sep 17, 2016 8:02 am    Post subject:
Creating a glowing sun in scene
Reply with quote

Hi,

I am trying to render a scene from outer space where i need to create a sun glowing at a certain position in space. I could create the sphere but unable to create a glowing effect--a bright sphere with emitting light beyond its boundary. Its something similar to the attached image.

I went through old forum topics like the one:
http://forum.openscenegraph.org/viewtopic.php?t=15409

But i could get only the highlight but the glowing effect is missing. I feel its something to do with material/texture. Any help or guidance with this?
I recently learned OSG and using it first time.

...

Thank you!

Cheers,
Suraj[b][/b]
Back to top
View user's profile Send private message Send e-mail
d_a_heitbrink (David Heitbrink)
User


Joined: 23 Sep 2013
Posts: 37

PostPosted: Fri Sep 23, 2016 8:06 pm    Post subject:
Reply with quote

There are a few ways of doing this. What you most likely want to do is to look up information on high dynamic range rendering. This involves multi-pass rendering where you use some kind of blur filter to make the brighest parts of your scene bleed out.
This is a resonable example:
http://learnopengl.com/#!Advanced-Lighting/Bloom

Basically this would involve using shaders, and attaching a color buffer to your camera.

Another way, although cheep and crappy, is to just add textured quad, with transparency set on it. And just overlay it on your scene.
Back to top
View user's profile Send private message Send e-mail
suraj (Suraj Paul)
User


Joined: 26 Aug 2016
Posts: 24

PostPosted: Sat Sep 24, 2016 4:04 am    Post subject:
Reply with quote

Hi David,

I have already attempted textured quad, with transparency on it. But it gives, as u said, a cheap look.

Being new to multi-pass rendering with blur filter, I read the link you shared.
Any getting started example on creating Light Bloom in OSG?
...

Thank you!

Cheers,
Suraj
Back to top
View user's profile Send private message Send e-mail
d_a_heitbrink (David Heitbrink)
User


Joined: 23 Sep 2013
Posts: 37

PostPosted: Mon Sep 26, 2016 2:06 pm    Post subject:
Reply with quote

I think looking at the osgblenddrawbuffers or osgmultiplerendertargets might be a good start. Neither of these are really doing what you are aiming for, but shows you how to add the color buffers and shaders.

You might also look at the OSGPPU project. It I think it has some more relevant examples, although the OSG PPU project is not being actively maintained at this time.
Back to top
View user's profile Send private message Send e-mail
suraj (Suraj Paul)
User


Joined: 26 Aug 2016
Posts: 24

PostPosted: Wed Feb 22, 2017 10:53 am    Post subject:
Reply with quote

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...

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:ShockedN|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
Back to top
View user's profile Send private message Send e-mail
SMesserschmidt (Sebastian Messerschmidt)
Forum Moderator


Joined: 10 Sep 2013
Posts: 720

PostPosted: Wed Feb 22, 2017 11:43 am    Post subject:
Creating a glowing sun in scene
Reply with quote

Hi Suraj,


Quote:
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


Quote:

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:ShockedN|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






------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts Recommended way to render a scene fro... Hannes Naude General 6 Tue Apr 11, 2017 11:12 am View latest post
No new posts Covert and use 3ds Max scene to OpenS... lucia General 0 Mon Mar 06, 2017 9:19 am View latest post
No new posts How to handle cameras in the scene gr... wernerM General 7 Fri Feb 24, 2017 4:47 pm View latest post
No new posts Visualizing a light cone in scene suraj General 7 Tue Jan 10, 2017 10:44 am View latest post
No new posts Scene is transparent when loaded with... matheusberger General 0 Thu Nov 03, 2016 2:18 pm View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP