Added shader defines for setting up vertex and fragment shaders with texture support

This commit is contained in:
Robert Osfield 2016-10-31 16:12:11 +00:00
parent 40e08813e2
commit 0bcfa8d980
2 changed files with 113 additions and 2 deletions

View File

@ -11,6 +11,7 @@
#include <osg/TexGen>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgGA/StateSetManipulator>
@ -137,8 +138,8 @@ int main(int argc, char** argv)
}
ACTIVE_TEXTURE->setElement(0, true);
//TEXTURE_GEN_MODE->setElement(0, 0);
TEXTURE_GEN_MODE->setElement(0, GL_SPHERE_MAP);
TEXTURE_GEN_MODE->setElement(0, 0);
//TEXTURE_GEN_MODE->setElement(0, GL_SPHERE_MAP);
TEXTURE_GEN_S->setElement(0, true);
TEXTURE_GEN_T->setElement(0, true);
@ -147,8 +148,69 @@ int main(int argc, char** argv)
stateset->addUniform(TEXTURE_GEN_T.get());
stateset->addUniform(TEXTURE_GEN_MODE.get());
stateset->addUniform(TEXTURE_ENV_MODE.get());
for(unsigned int i=0; i<maxTextureUnits;++i)
{
sstream.str("");
sstream<<"sampler"<<i;
OSG_NOTICE<<"****** texture unit : "<<sstream.str()<<std::endl;
stateset->addUniform(new osg::Uniform(sstream.str().c_str(), i));
// fragment shader texture defines
sstream.str("");
sstream<<"TEXTURE_VERT_DECLARE"<<i;
std::string textureVertDeclareDefine = sstream.str();
sstream.str("");
sstream<<"varying vec4 TexCoord"<<i<<";";
stateset->setDefine(textureVertDeclareDefine, sstream.str());
sstream.str("");
sstream<<"TEXTURE_VERT_BODY"<<i;
std::string textureVertBodyDefine = sstream.str();
sstream.str("");
sstream<<"{ TexCoord"<<i<<" = gl_MultiTexCoord"<<i<<"; }";
stateset->setDefine(textureVertBodyDefine, sstream.str());
// fragment shader texture defines
sstream.str("");
sstream<<"TEXTURE_FRAG_DECLARE"<<i;
std::string textureFragDeclareDefine = sstream.str();
sstream.str("");
sstream<<"uniform ";
sstream<<"sampler2D ";
sstream<<"sampler"<<i<<"; ";
sstream<<"varying vec4 TexCoord"<<i<<";";
stateset->setDefine(textureFragDeclareDefine, sstream.str());
sstream.str("");
sstream<<"TEXTURE_FRAG_BODY"<<i;
std::string textureFragBodyDefine = sstream.str();
sstream.str("");
sstream<<"(color) { color = texenv(color, ";
sstream<<"texture2D( sampler"<<i<<", TexCoord"<<i<<".st)";
sstream<<", "<<i<<"); }";
stateset->setDefine(textureFragBodyDefine, sstream.str());
}
}
osgDB::writeObjectFile(*stateset, "stateset.osgt");
viewer.realize();
return viewer.run();

View File

@ -270,6 +270,50 @@ static bool writeUniformList( osgDB::OutputStream& os, const osg::StateSet& ss )
return true;
}
// _defineList
static bool checkDefineList( const osg::StateSet& ss )
{
return ss.getDefineList().size()>0;
}
static bool readDefineList( osgDB::InputStream& is, osg::StateSet& ss )
{
unsigned int size = is.readSize(); is >> is.BEGIN_BRACKET;
for ( unsigned int i=0; i<size; ++i )
{
std::string defineName;
is.readWrappedString( defineName );
std::string defineValue;
is.readWrappedString( defineValue );
is >> is.PROPERTY("Value");
int overrideValue = readValue( is );
ss.setDefine(defineName, defineValue, overrideValue);
}
is >> is.END_BRACKET;
return true;
}
static bool writeDefineList( osgDB::OutputStream& os, const osg::StateSet& ss )
{
const osg::StateSet::DefineList& df = ss.getDefineList();
os.writeSize(df.size()); os << os.BEGIN_BRACKET << std::endl;
for ( osg::StateSet::DefineList::const_iterator itr=df.begin();
itr!=df.end(); ++itr )
{
os.writeWrappedString(itr->first);
os.writeWrappedString(itr->second.first);
os << os.PROPERTY("Value");
writeValue(os, itr->second.second);
os << std::endl;
}
os << os.END_BRACKET << std::endl;
return true;
}
REGISTER_OBJECT_WRAPPER( StateSet,
new osg::StateSet,
osg::StateSet,
@ -295,4 +339,9 @@ REGISTER_OBJECT_WRAPPER( StateSet,
ADD_BOOL_SERIALIZER( NestRenderBins, true ); // _nestRenderBins
ADD_OBJECT_SERIALIZER( UpdateCallback, osg::StateSet::Callback, NULL ); // _updateCallback
ADD_OBJECT_SERIALIZER( EventCallback, osg::StateSet::Callback, NULL ); // _eventCallback
{
UPDATE_TO_VERSION_SCOPED( 146 )
ADD_USER_SERIALIZER( DefineList ); // _defineList
}
}