FixedFunctionTechnique::FixedFunctionTechnique(const std::vector<TextureLayer>& layers, const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize) { bool firstLayer = true; int i=0; for (std::vector<TextureLayer>::const_iterator it = layers.begin(); it != layers.end(); ++it) { osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet); if (!firstLayer) { stateset->setMode(GL_BLEND, osg::StateAttribute::ON); osg::ref_ptr<osg::Depth> depth (new osg::Depth); depth->setFunction(osg::Depth::EQUAL); stateset->setAttributeAndModes(depth, osg::StateAttribute::ON); } int texunit = 0; if(!firstLayer) { osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); // This is to map corner vertices directly to the center of a blendmap texel. osg::Matrixf texMat; float scale = (blendmapScale/(static_cast<float>(blendmapScale)+1.f)); texMat.preMultTranslate(osg::Vec3f(0.5f, 0.5f, 0.f)); texMat.preMultScale(osg::Vec3f(scale, scale, 1.f)); texMat.preMultTranslate(osg::Vec3f(-0.5f, -0.5f, 0.f)); stateset->setTextureAttributeAndModes(texunit, new osg::TexMat(texMat)); osg::ref_ptr<osg::TexEnvCombine> texEnvCombine (new osg::TexEnvCombine); texEnvCombine->setCombine_RGB(osg::TexEnvCombine::REPLACE); texEnvCombine->setSource0_RGB(osg::TexEnvCombine::PREVIOUS); stateset->setTextureAttributeAndModes(texunit, texEnvCombine, osg::StateAttribute::ON); ++texunit; } // Add the actual layer texture multiplied by the alpha map. osg::ref_ptr<osg::Texture2D> tex = it->mDiffuseMap; stateset->setTextureAttributeAndModes(texunit, tex.get()); osg::ref_ptr<osg::TexMat> texMat (new osg::TexMat); texMat->setMatrix(osg::Matrix::scale(osg::Vec3f(layerTileSize,layerTileSize,1.f))); stateset->setTextureAttributeAndModes(texunit, texMat, osg::StateAttribute::ON); firstLayer = false; addPass(stateset); } }
FixedFunctionTechnique::FixedFunctionTechnique(const std::vector<TextureLayer>& layers, const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize) { bool firstLayer = true; int i=0; for (std::vector<TextureLayer>::const_iterator it = layers.begin(); it != layers.end(); ++it) { osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet); if (!firstLayer) { stateset->setMode(GL_BLEND, osg::StateAttribute::ON); stateset->setAttributeAndModes(getEqualDepth(), osg::StateAttribute::ON); } int texunit = 0; if(!firstLayer) { osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); // This is to map corner vertices directly to the center of a blendmap texel. stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale)); static osg::ref_ptr<osg::TexEnvCombine> texEnvCombine; if (!texEnvCombine) { texEnvCombine = new osg::TexEnvCombine; texEnvCombine->setCombine_RGB(osg::TexEnvCombine::REPLACE); texEnvCombine->setSource0_RGB(osg::TexEnvCombine::PREVIOUS); } stateset->setTextureAttributeAndModes(texunit, texEnvCombine, osg::StateAttribute::ON); ++texunit; } // Add the actual layer texture multiplied by the alpha map. osg::ref_ptr<osg::Texture2D> tex = it->mDiffuseMap; stateset->setTextureAttributeAndModes(texunit, tex.get()); stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON); firstLayer = false; addPass(stateset); } }
std::vector<osg::ref_ptr<osg::StateSet> > createPasses(bool useShaders, bool forcePerPixelLighting, bool clampLighting, Shader::ShaderManager* shaderManager, const std::vector<TextureLayer> &layers, const std::vector<osg::ref_ptr<osg::Texture2D> > &blendmaps, int blendmapScale, float layerTileSize) { std::vector<osg::ref_ptr<osg::StateSet> > passes; bool firstLayer = true; unsigned int blendmapIndex = 0; unsigned int passIndex = 0; for (std::vector<TextureLayer>::const_iterator it = layers.begin(); it != layers.end(); ++it) { osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet); if (!firstLayer) { stateset->setMode(GL_BLEND, osg::StateAttribute::ON); stateset->setAttributeAndModes(getEqualDepth(), osg::StateAttribute::ON); } int texunit = 0; if (useShaders) { stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap); if (layerTileSize != 1.f) stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON); stateset->addUniform(new osg::Uniform("diffuseMap", texunit)); if(!firstLayer) { ++texunit; osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(blendmapIndex++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale)); stateset->addUniform(new osg::Uniform("blendMap", texunit)); } if (it->mNormalMap) { ++texunit; stateset->setTextureAttributeAndModes(texunit, it->mNormalMap); stateset->addUniform(new osg::Uniform("normalMap", texunit)); } Shader::ShaderManager::DefineMap defineMap; defineMap["forcePPL"] = forcePerPixelLighting ? "1" : "0"; defineMap["clamp"] = clampLighting ? "1" : "0"; defineMap["normalMap"] = (it->mNormalMap) ? "1" : "0"; defineMap["blendMap"] = !firstLayer ? "1" : "0"; defineMap["colorMode"] = "2"; defineMap["specularMap"] = it->mSpecular ? "1" : "0"; defineMap["parallax"] = (it->mNormalMap && it->mParallax) ? "1" : "0"; osg::ref_ptr<osg::Shader> vertexShader = shaderManager->getShader("terrain_vertex.glsl", defineMap, osg::Shader::VERTEX); osg::ref_ptr<osg::Shader> fragmentShader = shaderManager->getShader("terrain_fragment.glsl", defineMap, osg::Shader::FRAGMENT); if (!vertexShader || !fragmentShader) { // Try again without shader. Error already logged by above return createPasses(false, forcePerPixelLighting, clampLighting, shaderManager, layers, blendmaps, blendmapScale, layerTileSize); } stateset->setAttributeAndModes(shaderManager->getProgram(vertexShader, fragmentShader)); } else { if(!firstLayer) { osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(blendmapIndex++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); // This is to map corner vertices directly to the center of a blendmap texel. stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale)); static osg::ref_ptr<osg::TexEnvCombine> texEnvCombine; if (!texEnvCombine) { texEnvCombine = new osg::TexEnvCombine; texEnvCombine->setCombine_RGB(osg::TexEnvCombine::REPLACE); texEnvCombine->setSource0_RGB(osg::TexEnvCombine::PREVIOUS); } stateset->setTextureAttributeAndModes(texunit, texEnvCombine, osg::StateAttribute::ON); ++texunit; } // Add the actual layer texture multiplied by the alpha map. osg::ref_ptr<osg::Texture2D> tex = it->mDiffuseMap; stateset->setTextureAttributeAndModes(texunit, tex.get()); if (layerTileSize != 1.f) stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON); } firstLayer = false; stateset->setRenderBinDetails(passIndex++, "RenderBin"); passes.push_back(stateset); } return passes; }
ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* /*options*/) const { std::string ext( osgDB::getLowerCaseFileExtension(fileName) ); if( !acceptsExtension(ext) ) return ReadResult::FILE_NOT_HANDLED; osg::notify(osg::INFO) << "ReaderWriterGLOBE( \"" << fileName << "\" )" << std::endl; // strip the ".globe" pseudo-loader extension std::string tmpName( osgDB::getNameLessExtension(fileName) ); // get the next "extension", which actually contains the globe radius parameter std::string params( osgDB::getFileExtension(tmpName) ); if( params.empty() ) { osg::notify(osg::WARN) << "Missing parameters for " EXTENSION_NAME " pseudo-loader" << std::endl; return ReadResult::FILE_NOT_HANDLED; } // strip the "params extension", which must leave an image subfilename. std::string subFileName( osgDB::getNameLessExtension(tmpName) ); if( subFileName.empty() || subFileName == tmpName ) { osg::notify(osg::WARN) << "Missing image subfilename for " EXTENSION_NAME " pseudo-loader" << std::endl; return ReadResult::FILE_NOT_HANDLED; } osg::notify(osg::INFO) << EXTENSION_NAME " params = \"" << params << "\"" << std::endl; int radius; int count( sscanf( params.c_str(), "%d", &radius ) ); if( count != 1 ) { osg::notify(osg::WARN) << "Bad parameters for " EXTENSION_NAME " pseudo-loader: \"" << params << "\"" << std::endl; return ReadResult::FILE_NOT_HANDLED; } // recursively load the image subfile. osg::Image *image( osgDB::readImageFile(subFileName) ); if( !image ) { // propagate the read failure upwards osg::notify(osg::WARN) << "Image file \"" << subFileName << "\" could not be loaded" << std::endl; return ReadResult::FILE_NOT_HANDLED; } // create an osg::Sphere for the globe geometry osg::Geode* geode( new osg::Geode() ); geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0,0,0), radius))); // apply the image as a texture to the globe osg::Texture2D* tex2d( new osg::Texture2D ); tex2d->setImage( image ); tex2d->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT ); tex2d->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT ); osg::StateSet* stateset( geode->getOrCreateStateSet() ); stateset->setTextureAttributeAndModes( 0, tex2d, osg::StateAttribute::ON ); return geode; }
ShaderTechnique::ShaderTechnique(Shader::ShaderManager& shaderManager, bool forcePerPixelLighting, bool clampLighting, const std::vector<TextureLayer>& layers, const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize) { bool firstLayer = true; int i=0; for (std::vector<TextureLayer>::const_iterator it = layers.begin(); it != layers.end(); ++it) { osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet); if (!firstLayer) { stateset->setMode(GL_BLEND, osg::StateAttribute::ON); osg::ref_ptr<osg::Depth> depth (new osg::Depth); depth->setFunction(osg::Depth::EQUAL); stateset->setAttributeAndModes(depth, osg::StateAttribute::ON); } int texunit = 0; stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap); osg::ref_ptr<osg::TexMat> texMat (new osg::TexMat); texMat->setMatrix(osg::Matrix::scale(osg::Vec3f(layerTileSize,layerTileSize,1.f))); stateset->setTextureAttributeAndModes(texunit, texMat, osg::StateAttribute::ON); stateset->addUniform(new osg::Uniform("diffuseMap", texunit)); if(!firstLayer) { ++texunit; osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); // This is to map corner vertices directly to the center of a blendmap texel. osg::Matrixf texMat; float scale = (blendmapScale/(static_cast<float>(blendmapScale)+1.f)); texMat.preMultTranslate(osg::Vec3f(0.5f, 0.5f, 0.f)); texMat.preMultScale(osg::Vec3f(scale, scale, 1.f)); texMat.preMultTranslate(osg::Vec3f(-0.5f, -0.5f, 0.f)); stateset->setTextureAttributeAndModes(texunit, new osg::TexMat(texMat)); stateset->addUniform(new osg::Uniform("blendMap", texunit)); } if (it->mNormalMap) { ++texunit; stateset->setTextureAttributeAndModes(texunit, it->mNormalMap); stateset->addUniform(new osg::Uniform("normalMap", texunit)); } Shader::ShaderManager::DefineMap defineMap; defineMap["forcePPL"] = forcePerPixelLighting ? "1" : "0"; defineMap["clamp"] = clampLighting ? "1" : "0"; defineMap["normalMap"] = (it->mNormalMap) ? "1" : "0"; defineMap["blendMap"] = !firstLayer ? "1" : "0"; defineMap["colorMode"] = "2"; defineMap["specularMap"] = it->mSpecular ? "1" : "0"; osg::ref_ptr<osg::Shader> vertexShader = shaderManager.getShader("terrain_vertex.glsl", defineMap, osg::Shader::VERTEX); osg::ref_ptr<osg::Shader> fragmentShader = shaderManager.getShader("terrain_fragment.glsl", defineMap, osg::Shader::FRAGMENT); if (!vertexShader || !fragmentShader) throw std::runtime_error("Unable to create shader"); stateset->setAttributeAndModes(shaderManager.getProgram(vertexShader, fragmentShader)); firstLayer = false; addPass(stateset); } }
ShaderTechnique::ShaderTechnique(Shader::ShaderManager& shaderManager, bool forcePerPixelLighting, bool clampLighting, const std::vector<TextureLayer>& layers, const std::vector<osg::ref_ptr<osg::Texture2D> >& blendmaps, int blendmapScale, float layerTileSize) { bool firstLayer = true; int i=0; for (std::vector<TextureLayer>::const_iterator it = layers.begin(); it != layers.end(); ++it) { osg::ref_ptr<osg::StateSet> stateset (new osg::StateSet); if (!firstLayer) { stateset->setMode(GL_BLEND, osg::StateAttribute::ON); stateset->setAttributeAndModes(getEqualDepth(), osg::StateAttribute::ON); } int texunit = 0; stateset->setTextureAttributeAndModes(texunit, it->mDiffuseMap); stateset->setTextureAttributeAndModes(texunit, getLayerTexMat(layerTileSize), osg::StateAttribute::ON); stateset->addUniform(new osg::Uniform("diffuseMap", texunit)); if(!firstLayer) { ++texunit; osg::ref_ptr<osg::Texture2D> blendmap = blendmaps.at(i++); stateset->setTextureAttributeAndModes(texunit, blendmap.get()); stateset->setTextureAttributeAndModes(texunit, getBlendmapTexMat(blendmapScale)); stateset->addUniform(new osg::Uniform("blendMap", texunit)); } if (it->mNormalMap) { ++texunit; stateset->setTextureAttributeAndModes(texunit, it->mNormalMap); stateset->addUniform(new osg::Uniform("normalMap", texunit)); } Shader::ShaderManager::DefineMap defineMap; defineMap["forcePPL"] = forcePerPixelLighting ? "1" : "0"; defineMap["clamp"] = clampLighting ? "1" : "0"; defineMap["normalMap"] = (it->mNormalMap) ? "1" : "0"; defineMap["blendMap"] = !firstLayer ? "1" : "0"; defineMap["colorMode"] = "2"; defineMap["specularMap"] = it->mSpecular ? "1" : "0"; defineMap["parallax"] = (it->mNormalMap && it->mParallax) ? "1" : "0"; osg::ref_ptr<osg::Shader> vertexShader = shaderManager.getShader("terrain_vertex.glsl", defineMap, osg::Shader::VERTEX); osg::ref_ptr<osg::Shader> fragmentShader = shaderManager.getShader("terrain_fragment.glsl", defineMap, osg::Shader::FRAGMENT); if (!vertexShader || !fragmentShader) throw std::runtime_error("Unable to create shader"); stateset->setAttributeAndModes(shaderManager.getProgram(vertexShader, fragmentShader)); firstLayer = false; addPass(stateset); } }