osg::Node* addRefractStateSet(osg::Node* node) { osg::StateSet* stateset = new osg::StateSet(); osg::TextureCubeMap* reflectmap = readCubeMap(); stateset->setTextureAttributeAndModes( 0, reflectmap, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); stateset->setTextureAttributeAndModes( 1, reflectmap, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); osg::TexMat* texMat = new osg::TexMat; stateset->setTextureAttribute(0, texMat); // --------------------------------------------------- // Vertex Program // --------------------------------------------------- osg::VertexProgram* vp = new osg::VertexProgram(); vp->setVertexProgram( vpstr ); vp->setProgramLocalParameter( 0, osg::Vec4( fresnel, fresnel, fresnel, 1.0f ) ); vp->setProgramLocalParameter( 1, osg::Vec4( refract, refract*refract, 0.0f, 0.0f ) ); stateset->setAttributeAndModes( vp, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); // --------------------------------------------------- // fragment = refraction*(1-fresnel) + reflection*fresnel // T0 = texture unit 0, refraction map // T1 = texture unit 1, reflection map // C.rgb = primary color, water color // C.a = primary color, fresnel factor // Cp = result from previous texture environment // --------------------------------------------------- // REPLACE function: Arg0 // = T0 osg::TexEnvCombine *te0 = new osg::TexEnvCombine; te0->setCombine_RGB(osg::TexEnvCombine::REPLACE); te0->setSource0_RGB(osg::TexEnvCombine::TEXTURE0); te0->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); // INTERPOLATE function: Arg0 * (Arg2) + Arg1 * (1-Arg2) // = T1 * C0.a + Cp * (1-C0.a) osg::TexEnvCombine *te1 = new osg::TexEnvCombine; // rgb = Cp + Ct te1->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE); te1->setSource0_RGB(osg::TexEnvCombine::TEXTURE1); te1->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); te1->setSource1_RGB(osg::TexEnvCombine::PREVIOUS); te1->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); te1->setSource2_RGB(osg::TexEnvCombine::PRIMARY_COLOR); te1->setOperand2_RGB(osg::TexEnvCombine::SRC_COLOR); stateset->setTextureAttributeAndModes(0, te0, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); stateset->setTextureAttributeAndModes(1, te1, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); osg::Group* group = new osg::Group; group->addChild(node); group->setCullCallback(new TexMatCallback(*texMat)); group->setStateSet( stateset ); return group; }
void add_skybox_to_node(osg::ref_ptr<osg::ClearNode> mynode, std::string posx_fname, std::string negx_fname, std::string posy_fname, std::string negy_fname, std::string posz_fname, std::string negz_fname ) { osg::StateSet* stateset = new osg::StateSet(); osg::TexEnv* te = new osg::TexEnv; te->setMode(osg::TexEnv::REPLACE); stateset->setTextureAttributeAndModes(0, te, osg::StateAttribute::ON); osg::TexGen *tg = new osg::TexGen; tg->setMode(osg::TexGen::NORMAL_MAP); stateset->setTextureAttributeAndModes(0, tg, osg::StateAttribute::ON); osg::TexMat *tm = new osg::TexMat; stateset->setTextureAttribute(0, tm); osg::TextureCubeMap* skymap = readCubeMap(posx_fname, negx_fname, posy_fname, negy_fname, posz_fname, negz_fname); stateset->setTextureAttributeAndModes(0, skymap, osg::StateAttribute::ON); stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); stateset->setMode( GL_CULL_FACE, osg::StateAttribute::OFF ); // clear the depth to the far plane. osg::Depth* depth = new osg::Depth; depth->setFunction(osg::Depth::ALWAYS); depth->setRange(1.0,1.0); stateset->setAttributeAndModes(depth, osg::StateAttribute::ON ); stateset->setRenderBinDetails(-1,"RenderBin"); osg::ref_ptr<osg::Drawable> drawable = new osg::ShapeDrawable( new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),500)); osg::ref_ptr<osg::Geode> geode = new osg::Geode(); geode->setCullingActive(false); geode->setStateSet( stateset ); geode->addDrawable(drawable.get()); osg::ref_ptr<osg::Transform> transform = new MoveEarthySkyWithEyePointTransform(); transform->setCullingActive(false); transform->addChild(geode.get()); // mynode.setRequiresClear(false); mynode->setCullCallback(new TexMatCallback(*tm)); mynode->addChild(transform.get()); }
void SkyBoxComponent::Finished() { BaseClass::Finished(); osg::StateSet* stateset = mSkyboxGeode->getOrCreateStateSet(); osg::TextureCubeMap* skymap = readCubeMap(this); if(skymap) { stateset->setTextureAttributeAndModes(0, skymap, osg::StateAttribute::ON); } }
osg::Node* SkyBox::createSkyBox(std::string name) { _name = name; osg::StateSet* stateset = new osg::StateSet(); osg::TexEnv* te = new osg::TexEnv; te->setMode(osg::TexEnv::REPLACE); stateset->setTextureAttributeAndModes(0, te, osg::StateAttribute::ON); osg::TexGen *tg = new osg::TexGen; tg->setMode(osg::TexGen::NORMAL_MAP); stateset->setTextureAttributeAndModes(0, tg, osg::StateAttribute::ON); osg::TexMat *tm = new osg::TexMat; stateset->setTextureAttribute(0, tm); osg::TextureCubeMap* skymap = readCubeMap(); stateset->setTextureAttributeAndModes(0, skymap, osg::StateAttribute::ON); stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); stateset->setMode( GL_CULL_FACE, osg::StateAttribute::OFF ); // clear the depth to the far plane. osg::Depth* depth = new osg::Depth; depth->setFunction(osg::Depth::ALWAYS); depth->setRange(1.0,1.0); stateset->setAttributeAndModes(depth, osg::StateAttribute::ON ); stateset->setRenderBinDetails(6,"RenderBin"); osg::Drawable* drawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),5)); osg::Geode* geode = new osg::Geode; geode->setCullingActive(false); geode->setStateSet( stateset ); geode->addDrawable(drawable); osg::Transform* transform = new MoveEarthySkyWithEyePointTransform; transform->setCullingActive(false); transform->addChild(geode); osg::ClearNode* clearNode = new osg::ClearNode; // clearNode->setRequiresClear(false); clearNode->setCullCallback(new TexMatCallback(*tm)); clearNode->addChild(transform); return clearNode; }
osg::Node* SkyBox::createSkyBox(const std::string &strImagePath) { osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet(); osg::ref_ptr<osg::TexEnv> te = new osg::TexEnv; te->setMode(osg::TexEnv::REPLACE); stateset->setTextureAttributeAndModes(0, te, osg::StateAttribute::ON); osg::ref_ptr<osg::TexGen> tg = new osg::TexGen; tg->setMode(osg::TexGen::NORMAL_MAP); stateset->setTextureAttributeAndModes(0, tg, osg::StateAttribute::ON); osg::ref_ptr<osg::TexMat> tm = new osg::TexMat; stateset->setTextureAttribute(0, tm); osg::TextureCubeMap* skymap = readCubeMap(strImagePath); stateset->setTextureAttributeAndModes(0, skymap, osg::StateAttribute::ON); stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); stateset->setMode( GL_CULL_FACE, osg::StateAttribute::OFF ); // clear the depth to the far plane. osg::ref_ptr<osg::Depth> depth = new osg::Depth; depth->setFunction(osg::Depth::ALWAYS); depth->setRange(1.0,1.0); stateset->setAttributeAndModes(depth, osg::StateAttribute::ON ); stateset->setRenderBinDetails(-1,"RenderBin"); osg::ref_ptr<osg::Drawable> drawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),100)); osg::ref_ptr<osg::Geode> geode = new osg::Geode; geode->setCullingActive(false); geode->setStateSet( stateset ); geode->addDrawable(drawable); osg::ref_ptr<osg::Transform>transform = new MoveEarthySkyWithEyePointTransform; transform->setCullingActive(false); transform->addChild(geode); osg::ref_ptr<osg::ClearNode> clearNode = new osg::ClearNode; // clearNode->setRequiresClear(false); clearNode->setCullCallback(new TexMatCallback(*tm)); clearNode->addChild(transform); return clearNode.release(); }
SkyBoxComponent::SkyBoxComponent() : BaseClass(new osg::ClearNode()) , mDrawables(new osg::Group()) { Register(TextureUpId, &mTextureUp); Register(TextureDownId, &mTextureDown); Register(TextureNorthId, &mTextureNorth); Register(TextureSouthId, &mTextureSouth); Register(TextureEastId, &mTextureEast); Register(TextureWestId, &mTextureWest); osg::StateSet* stateset = new osg::StateSet(); osg::TexEnv* te = new osg::TexEnv; te->setMode(osg::TexEnv::REPLACE); stateset->setTextureAttributeAndModes(0, te, osg::StateAttribute::ON); osg::TexGen *tg = new osg::TexGen; tg->setMode(osg::TexGen::NORMAL_MAP); stateset->setTextureAttributeAndModes(0, tg, osg::StateAttribute::ON); osg::TexMat *tm = new osg::TexMat; stateset->setTextureAttribute(0, tm); osg::TextureCubeMap* skymap = readCubeMap(this); if(skymap) { stateset->setTextureAttributeAndModes(0, skymap, osg::StateAttribute::ON); } stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); stateset->setMode(GL_FOG,osg::StateAttribute::OFF); stateset->setMode( GL_CULL_FACE, osg::StateAttribute::OFF ); /*osg::Shader* vShader = new osg::Shader(osg::Shader::VERTEX); osg::Shader* fShader = new osg::Shader(osg::Shader::FRAGMENT); const char vpstr[] = "varying vec3 vTexCoord;\n" "void main(void) {\n" "gl_Position = ftransform(); vTexCoord = gl_Vertex.xyz;" "}"; vShader->setShaderSource(vpstr); const char fpstr[] = "uniform samplerCube skyBox;\n" "varying vec3 vTexCoord;\n" "void main(void)\n" "{\n" " vec3 tex = vec3(vTexCoord.x, vTexCoord.y, -vTexCoord.z);\n" " gl_FragColor = textureCube( skyBox, tex.xzy );\n" " gl_FragColor.a = 0.0;\n" "}"; fShader->setShaderSource(fpstr); osg::Program* program = new osg::Program(); program->setName("SkyboxShader"); program->addShader(vShader); program->addShader(fShader); */ // clear the depth to the far plane. osg::Depth* depth = new osg::Depth; depth->setFunction(osg::Depth::ALWAYS); depth->setRange(1.0,1.0); stateset->setAttributeAndModes(depth, osg::StateAttribute::ON ); stateset->setRenderBinDetails(-10000,"RenderBin"); osg::Drawable* drawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),10000)); mSkyboxGeode = new osg::Geode; mSkyboxGeode->setNodeMask(dtEntity::NodeMasks::VISIBLE); mSkyboxGeode->setCullingActive(false); mSkyboxGeode->setStateSet( stateset ); mSkyboxGeode->addDrawable(drawable); osg::Transform* transform = new MoveEarthySkyWithEyePointTransform(); transform->setCullingActive(false); transform->addChild(mSkyboxGeode); osg::ClearNode* clearNode = static_cast<osg::ClearNode*>(GetNode()); clearNode->setRequiresClear(false); clearNode->setCullCallback(new TexMatCallback(*tm)); clearNode->setNodeMask(0xFFFFFFFF); clearNode->addChild(transform); clearNode->addChild(mDrawables); }