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 dfdb946d60
commit 257c2deb86

View File

@ -40,33 +40,101 @@
#include <string>
#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_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)"<<std::endl;
tcm->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<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
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<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)
if (program->getNumShaders()>0)
{
osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
return 1;
ss->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);