Added greater control of how cubemap is set up and controlled

This commit is contained in:
Robert Osfield 2018-09-11 08:11:27 +01:00
parent e72caf957c
commit b913367126

View File

@ -40,33 +40,101 @@
#include <string> #include <string>
#include <vector> #include <vector>
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<osg::TextureCubeMap> 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_S, osg::Texture::CLAMP);
tcm->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP); tcm->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
tcm->setWrap(osg::Texture::WRAP_R, 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]) if (arguments.read("--no-mip-map"))
osgUtil::HighlightMapGenerator *mapgen = new osgUtil::HighlightMapGenerator( {
osg::Vec3(1, 1, -1), // light direction tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
osg::Vec4(1, 0.9f, 0.8f, 1), // light color tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
8); // specular exponent }
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)"<<std::endl;
tcm->setUseHardwareMipMapGeneration(true);
}
// assign the six images to the texture object std::string filename;
tcm->setImage(osg::TextureCubeMap::POSITIVE_X, mapgen->getImage(osg::TextureCubeMap::POSITIVE_X)); if (arguments.read("--posx", filename)) tcm->setImage(osg::TextureCubeMap::POSITIVE_X, osgDB::readImageFile(filename));
tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_X)); if (arguments.read("--negx", filename)) tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, osgDB::readImageFile(filename));
tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Y)); if (arguments.read("--posy", filename)) tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, osgDB::readImageFile(filename));
tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Y)); if (arguments.read("--negy", filename)) tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, osgDB::readImageFile(filename));
tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Z)); if (arguments.read("--posz", filename)) tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, osgDB::readImageFile(filename));
tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Z)); 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<osg::Program> program = new osg::Program;
std::string shaderFilename;
while (arguments.read("-s", shaderFilename))
{
osg::ref_ptr<osg::Shader> shader = osgDB::readRefShaderFile(shaderFilename);
if (shader) program->addShader(shader);
}
// load the nodes from the commandline arguments.
osg::ref_ptr<osg::Node> 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."<<std::endl;
return 1;
}
osg::StateSet *ss = rootnode->getOrCreateStateSet();
// enable texturing, replacing any textures in the subgraphs // enable texturing, replacing any textures in the subgraphs
ss->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); 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->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR);
te->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); te->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
}
int main(int argc, char *argv[]) if (program->getNumShaders()>0)
{
// 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<osg::Node> 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."<<std::endl; ss->setAttribute(program.get());
return 1; ss->addUniform(new osg::Uniform("baseTexture",0));
} }
// create specular highlights
create_specular_highlights(rootnode.get());
// run optimization over the scene graph // run optimization over the scene graph
osgUtil::Optimizer optimzer; osgUtil::Optimizer optimzer;
optimzer.optimize(rootnode); optimzer.optimize(rootnode);