diff --git a/examples/osgcubemap/osgcubemap.cpp b/examples/osgcubemap/osgcubemap.cpp index 843186485..164d65e05 100644 --- a/examples/osgcubemap/osgcubemap.cpp +++ b/examples/osgcubemap/osgcubemap.cpp @@ -40,33 +40,101 @@ #include #include -void create_specular_highlights(osg::Node *node) +int main(int argc, char *argv[]) { - osg::StateSet *ss = node->getOrCreateStateSet(); + // use an ArgumentParser object to manage the program arguments. + osg::ArgumentParser arguments(&argc,argv); + + // construct the viewer. + osgViewer::Viewer viewer; + + osg::ref_ptr tcm = new osg::TextureCubeMap; - // create and setup the texture object - osg::TextureCubeMap *tcm = new osg::TextureCubeMap; tcm->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP); tcm->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP); tcm->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP); - tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); - tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); - // generate the six highlight map images (light direction = [1, 1, -1]) - osgUtil::HighlightMapGenerator *mapgen = new osgUtil::HighlightMapGenerator( - osg::Vec3(1, 1, -1), // light direction - osg::Vec4(1, 0.9f, 0.8f, 1), // light color - 8); // specular exponent + if (arguments.read("--no-mip-map")) + { + tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); + tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); + } + else + { + tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); + tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); + } - mapgen->generateMap(); + if (arguments.read("--hardware-mip-map") || arguments.read("--hmp")) + { + OSG_NOTICE<<"tcm->setUseHardwareMipMapGeneration(true)"<setUseHardwareMipMapGeneration(true); + } - // assign the six images to the texture object - tcm->setImage(osg::TextureCubeMap::POSITIVE_X, mapgen->getImage(osg::TextureCubeMap::POSITIVE_X)); - tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_X)); - tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Y)); - tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Y)); - tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Z)); - tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Z)); + std::string filename; + if (arguments.read("--posx", filename)) tcm->setImage(osg::TextureCubeMap::POSITIVE_X, osgDB::readImageFile(filename)); + if (arguments.read("--negx", filename)) tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, osgDB::readImageFile(filename)); + if (arguments.read("--posy", filename)) tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, osgDB::readImageFile(filename)); + if (arguments.read("--negy", filename)) tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, osgDB::readImageFile(filename)); + if (arguments.read("--posz", filename)) tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, osgDB::readImageFile(filename)); + if (arguments.read("--negz", filename)) tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, osgDB::readImageFile(filename)); + + + int numValidImages = 0; + if (tcm->getImage(osg::TextureCubeMap::POSITIVE_X)) ++numValidImages; + if (tcm->getImage(osg::TextureCubeMap::NEGATIVE_X)) ++numValidImages; + if (tcm->getImage(osg::TextureCubeMap::POSITIVE_Y)) ++numValidImages; + if (tcm->getImage(osg::TextureCubeMap::NEGATIVE_Y)) ++numValidImages; + if (tcm->getImage(osg::TextureCubeMap::POSITIVE_Z)) ++numValidImages; + if (tcm->getImage(osg::TextureCubeMap::NEGATIVE_Z)) ++numValidImages; + + if (numValidImages!=6) + { + // generate the six highlight map images (light direction = [1, 1, -1]) + osgUtil::HighlightMapGenerator *mapgen = new osgUtil::HighlightMapGenerator( + osg::Vec3(1, 1, -1), // light direction + osg::Vec4(1, 0.9f, 0.8f, 1), // light color + 8); // specular exponent + + mapgen->generateMap(); + + // assign the six images to the texture object + if (!tcm->getImage(osg::TextureCubeMap::POSITIVE_X)) tcm->setImage(osg::TextureCubeMap::POSITIVE_X, mapgen->getImage(osg::TextureCubeMap::POSITIVE_X)); + if (!tcm->getImage(osg::TextureCubeMap::NEGATIVE_X)) tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_X)); + if (!tcm->getImage(osg::TextureCubeMap::POSITIVE_Y)) tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Y)); + if (!tcm->getImage(osg::TextureCubeMap::NEGATIVE_Y)) tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Y)); + if (!tcm->getImage(osg::TextureCubeMap::POSITIVE_Z)) tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Z)); + if (!tcm->getImage(osg::TextureCubeMap::NEGATIVE_Z)) tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Z)); + } + + float LODBias; + if (arguments.read("--lod",LODBias)) + { + tcm->setLODBias(LODBias); + } + + osg::ref_ptr program = new osg::Program; + std::string shaderFilename; + while (arguments.read("-s", shaderFilename)) + { + osg::ref_ptr shader = osgDB::readRefShaderFile(shaderFilename); + if (shader) program->addShader(shader); + } + + + // load the nodes from the commandline arguments. + osg::ref_ptr rootnode = osgDB::readRefNodeFiles(arguments); + + // if not loaded assume no arguments passed in, try use default mode instead. + if (!rootnode) rootnode = osgDB::readRefNodeFile("cessna.osgt"); + + if (!rootnode) + { + osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<getOrCreateStateSet(); // enable texturing, replacing any textures in the subgraphs ss->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); @@ -84,31 +152,13 @@ void create_specular_highlights(osg::Node *node) te->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR); te->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); -} -int main(int argc, char *argv[]) -{ - // use an ArgumentParser object to manage the program arguments. - osg::ArgumentParser arguments(&argc,argv); - - // construct the viewer. - osgViewer::Viewer viewer; - - // load the nodes from the commandline arguments. - osg::ref_ptr rootnode = osgDB::readRefNodeFiles(arguments); - - // if not loaded assume no arguments passed in, try use default mode instead. - if (!rootnode) rootnode = osgDB::readRefNodeFile("cessna.osgt"); - - if (!rootnode) + if (program->getNumShaders()>0) { - osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<setAttribute(program.get()); + ss->addUniform(new osg::Uniform("baseTexture",0)); } - // create specular highlights - create_specular_highlights(rootnode.get()); - // run optimization over the scene graph osgUtil::Optimizer optimzer; optimzer.optimize(rootnode);