atmospheric scattering problem

Started by
5 comments, last by Charly7 16 years, 1 month ago
Hi all, I'm triyng to use atmospheric scattering shader (GLSL) in my OpenSG project but I've some problems. To test the shader in a simple application, I've created a small OpenSG project where I have only a sphere. I want try to use of Sean O'Neil shader (only GroundFromSpaceFrag.glsl and GroundFromSpaceVert.glsl for now) and I've followed all the calculations of the parameters from his project. But when I run my small program I can see only a white sphere. I think I'm doing some errors in parameters calculation. This is my OpenSG code where I calculate the values to pass to my shader: // Headers #include <OpenSG/OSGGLUT.h> #include <OpenSG/OSGConfig.h> #include <OpenSG/OSGSimpleGeometry.h> #include <OpenSG/OSGGLUTWindow.h> #include <OpenSG/OSGSimpleSceneManager.h> #include <OpenSG/OSGBaseFunctions.h> #include <OpenSG/OSGTransform.h> #include <OpenSG/OSGGroup.h> #include <OpenSG/OSGPointLight.h> #include <OpenSG/OSGRenderAction.h> #include <OpenSG/OSGSceneFileHandler.h> #include <OpenSG/OSGSimpleMaterial.h> #include <OpenSG/OSGImage.h> #include <OpenSG/OSGTextureChunk.h> #include <OpenSG/OSGGradientBackground.h> #include <OpenSG/OSGSHLChunk.h> #include <OpenSG/OSGShadowMapViewport.h> #include <OpenSG/OSGBaseFunctions.h> #include <OpenSG/OSGTransform.h> #define PI 3.14159f // Activate the OpenSG namespace OSG_USING_NAMESPACE SHLChunkPtr _shl = NullFC; //variables for shaders Vec3f vCamera; Vec3f m_vLightDirection; float m_fWavelength[3]; float m_fWavelength4[3]; float m_fInnerRadius; float m_fOuterRadius; float m_fScale; float m_Kr, m_Kr4PI; float m_Km, m_Km4PI; float m_ESun; float m_g; float m_fRayleighScaleDepth; float m_fMieScaleDepth; int m_nSamples; // The SimpleSceneManager to manage simple applications SimpleSceneManager *mgr; // forward declaration so we can have the interesting stuff upfront int setupGLUT( int *argc, char *argv[] ); // redraw the window void display( void ) { // create the matrix Matrix m; Real32 t = glutGet(GLUT_ELAPSED_TIME ); mgr->redraw(); } void update(void) { glutPostRedisplay(); } // Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { FILE *f; // OSG init osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // the connection between GLUT and OpenSG GLUTWindowPtr gwin= GLUTWindow::create(); gwin->setId(winid); gwin->init(); // create the scene NodePtr sphere = makeSphere(4, 10); // create the transformation node // scenegraph nodes are split into 2 parts: the node and its core // 1. create the Node NodePtr scene = makeCoredNode<Group>(); //read the shaders from files f=fopen("GroundFromSpaceFrag.glsl","r"); char fp[2048]; std::string fp_p; fp_p.clear(); do { fgets(fp,2048,f); fp_p.append(fp); } while (!feof(f)); fclose(f); f=fopen("GroundFromSpaceVert.glsl","r"); char vp[2048]; std::string vp_p; vp_p.clear(); do { fgets(vp,2048,f); vp_p.append(vp); } while (!feof(f)); fclose(f); //Values for shader //calculate camera position Navigator * nav = mgr->getNavigator(); Matrix posMat = nav->getMatrix(); Vec4f vCamera = posMat[3]; float magnitudeCam = sqrt(vCamera[0]*vCamera[0] + vCamera[1]*vCamera[1] + vCamera[2]*vCamera[2]); float magnitudeCam2 = magnitudeCam * magnitudeCam; Vec3f m_vLight = Vec3f(1000.0,1000.0,1000.0); float magnitude = sqrt(m_vLight[0]*m_vLight[0] + m_vLight[1]*m_vLight[1] + m_vLight[2]*m_vLight[2]); m_vLightDirection = Vec3f(m_vLight[0] / magnitude, m_vLight[1] / magnitude, m_vLight[2] / magnitude); m_fWavelength[0] = 0.650f; // 650 nm for red m_fWavelength[1] = 0.570f; // 570 nm for green m_fWavelength[2] = 0.475f; // 475 nm for blue m_fWavelength4[0] = powf(m_fWavelength[0], 4.0); m_fWavelength4[1] = powf(m_fWavelength[1], 4.0); m_fWavelength4[2] = powf(m_fWavelength[2], 4.0); m_fInnerRadius = 10.0f; m_fOuterRadius = 10.25f; m_fScale = 1 / (m_fOuterRadius - m_fInnerRadius); m_fRayleighScaleDepth = 0.25f; m_fMieScaleDepth = 0.1f; m_nSamples = 2; m_Kr = 0.0025f; m_Kr = 0.0025f; // Rayleigh scattering constant m_Kr4PI = m_Kr*4.0f*PI; m_Km = 0.0015f; // Mie scattering constant m_Km4PI = m_Km*4.0f*PI; m_ESun = 15.0f; // Sun brightness constant m_g = -0.95f; // The Mie phase asymmetry factor //create the shader material ChunkMaterialPtr cmat = ChunkMaterial::create(); _shl = SHLChunk::create(); beginEditCP(_shl); _shl->setVertexProgram(vp_p); _shl->setFragmentProgram(fp_p); //set uniform variables of the shaders _shl->setUniformParameter("v3CameraPos",vCamera); _shl->setUniformParameter("v3LightPos", m_vLightDirection); _shl->setUniformParameter("v3InvWavelength", Vec3f(1/m_fWavelength4[0], 1/m_fWavelength4[1], 1/m_fWavelength4[2])); //_shl->setUniformParameter("fCameraHeight", magnitudeCam); //_shl->setUniformParameter("fCameraHeight2",magnitudeCam2); _shl->setUniformParameter("fInnerRadius", m_fInnerRadius); //_shl->setUniformParameter("fInnerRadius2", m_fInnerRadius*m_fInnerRadius); //_shl->setUniformParameter("fOuterRadius", m_fOuterRadius); //_shl->setUniformParameter("fOuterRadius2", m_fOuterRadius*m_fOuterRadius); _shl->setUniformParameter("fKrESun", m_Kr*m_ESun); _shl->setUniformParameter("fKmESun", m_Km*m_ESun); _shl->setUniformParameter("fKr4PI", m_Kr4PI); _shl->setUniformParameter("fKm4PI", m_Km4PI); _shl->setUniformParameter("fScale", 1.0f / (m_fOuterRadius - m_fInnerRadius)); _shl->setUniformParameter("fScaleDepth", m_fRayleighScaleDepth); _shl->setUniformParameter("fScaleOverScaleDepth", (1.0f / (m_fOuterRadius - m_fInnerRadius)) / m_fRayleighScaleDepth); //_shl->setUniformParameter("g", m_g); //_shl->setUniformParameter("g2", m_g*m_g); _shl->setUniformParameter("nSamples", m_nSamples); _shl->setUniformParameter("fSamples", m_nSamples); endEditCP(_shl); MaterialChunkPtr matLight = MaterialChunk::create(); beginEditCP(matLight); matLight->setAmbient(Color4f(0.3,0.3,0.3,1.0)); matLight->setDiffuse(Color4f(1.0,1.0,1.0,1.0)); matLight->setSpecular(Color4f(0.0,0.0,0.0,1.0)); endEditCP(matLight); beginEditCP(cmat); //cmat->addChunk(matLight); cmat->addChunk(_shl); endEditCP(cmat); GeometryPtr sphereGeo = GeometryPtr::dcast(sphere->getCore()); beginEditCP(sphereGeo, Geometry::MaterialFieldMask); sphereGeo->setMaterial(cmat); endEditCP(sphereGeo, Geometry::MaterialFieldMask); beginEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask); { scene->addChild(sphere); } endEditCP (scene, Node::CoreFieldMask | Node::ChildrenFieldMask); // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); // show the whole scene mgr->showAll(); // GLUT main loop glutMainLoop(); return 0; } // // GLUT callback functions // // react to size changes void reshape(int w, int h) { mgr->resize(w, h); glutPostRedisplay(); } // react to mouse button presses void mouse(int button, int state, int x, int y) { if (state) mgr->mouseButtonRelease(button, x, y); else mgr->mouseButtonPress(button, x, y); glutPostRedisplay(); } // react to mouse motions with pressed buttons void motion(int x, int y) { mgr->mouseMove(x, y); glutPostRedisplay(); } // react to keys void keyboard(unsigned char k, int x, int y) { switch(k) { case 27: { OSG::osgExit(); exit(0); } break; } } // setup the GLUT library which handles the windows for us int setupGLUT(int *argc, char *argv[]) { glutInit(argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); int winid = glutCreateWindow("OpenSG"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); // call the redraw function whenever there's nothing else to do glutIdleFunc(update); return winid; } [\code] Can somebody tell me what I wrong? Why my sphere is only white? I'm going crazy...please help me. I'll wait for your essential helps. Thx. Bye Charly
Advertisement
Try to use 100.0 and 102.5 as radii.
Also you might try to modify the shader's output to see what component is messing up (perhaps there is one computation that is too big or too small).

Using 100 and 102.5 as radii I obtain always the same result. I don't think this is the problem because all scattering paper (like the Sean O'Neill one) say that the ratio OuterRadius / InnerRadius must be 1.25 and it's what I have using 10 e 12.5 too.
What I can modify on my shader output? Any other suggestions?
Thx.

Charly
First the result computed in the fragment is made from two components, ... check each of them to see which one is wrong.

Then move to the shader program and check the components of the wrong value.

To check a component just output it's value and see if it's what should be :)
Quote:Original post by Charly7
Using 100 and 102.5 as radii I obtain always the same result. I don't think this is the problem because all scattering paper (like the Sean O'Neill one) say that the ratio OuterRadius / InnerRadius must be 1.25 and it's what I have using 10 e 12.5 too.
What I can modify on my shader output? Any other suggestions?
Thx.

Charly


Wasn't the radius 1.025, not 1.25?

Formski
Ops sorry...I did a mistake writing. Anyway I use values which ratio is 1.025. In each case I obtain only a white sphere!!!

Help!!!

Charly
Quote:
First the result computed in the fragment is made from two components, ... check each of them to see which one is wrong. Then move to the shader program and check the components of the wrong value. To check a component just output it's value and see if it's what should be :)


I check each components of frag shader and I saw that is the first one that doesn't work well. The gl_Color that should take the value from gl_FrontColor(in vert shader) returns a white color. I check also all the parameters that I use to calculate the gl_FrontColor in vert shader but they seem to be right. The value that I pass from my OpenSG application to my shader are the same of Sean O'Neil app but I can't understand what happen in my vert shader.
I'm really messe up cuz I have only 2 weeks to finish my project. Please can somebody help me? Can somebody tell me what is wrong in my application? If you want to know more about my application I can send you a zip of it.

Please help me. I'll wait for oyur essential answers.
Thanks in advance.

Charly

This topic is closed to new replies.

Advertisement