From Mike Weiblen, added \n newlines to inline shader program strings.
From Robert Osfield, changed tabs to spaces.
This commit is contained in:
parent
4e67b2c989
commit
0b291e60bb
@ -10,8 +10,8 @@
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/* file: examples/osgshaders/GL2Scene.cpp
|
||||
* author: Mike Weiblen 2005-04-15
|
||||
/* file: examples/osgshaders/GL2Scene.cpp
|
||||
* author: Mike Weiblen 2005-05-01
|
||||
*
|
||||
* Compose a scene of several instances of a model, with a different
|
||||
* OpenGL Shading Language shader applied to each.
|
||||
@ -51,9 +51,9 @@ make3DNoiseImage(int texSize)
|
||||
{
|
||||
osg::Image* image = new osg::Image;
|
||||
image->setImage(texSize, texSize, texSize,
|
||||
4, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
new unsigned char[4 * texSize * texSize * texSize],
|
||||
osg::Image::USE_NEW_DELETE);
|
||||
4, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
new unsigned char[4 * texSize * texSize * texSize],
|
||||
osg::Image::USE_NEW_DELETE);
|
||||
|
||||
const int startFrequency = 4;
|
||||
const int numOctaves = 4;
|
||||
@ -69,27 +69,27 @@ make3DNoiseImage(int texSize)
|
||||
|
||||
for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
|
||||
{
|
||||
SetNoiseFrequency(frequency);
|
||||
ptr = image->data();
|
||||
ni[0] = ni[1] = ni[2] = 0;
|
||||
SetNoiseFrequency(frequency);
|
||||
ptr = image->data();
|
||||
ni[0] = ni[1] = ni[2] = 0;
|
||||
|
||||
inci = 1.0 / (texSize / frequency);
|
||||
for (i = 0; i < texSize; ++i, ni[0] += inci)
|
||||
{
|
||||
incj = 1.0 / (texSize / frequency);
|
||||
for (j = 0; j < texSize; ++j, ni[1] += incj)
|
||||
{
|
||||
inck = 1.0 / (texSize / frequency);
|
||||
for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
|
||||
{
|
||||
*(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
inci = 1.0 / (texSize / frequency);
|
||||
for (i = 0; i < texSize; ++i, ni[0] += inci)
|
||||
{
|
||||
incj = 1.0 / (texSize / frequency);
|
||||
for (j = 0; j < texSize; ++j, ni[1] += incj)
|
||||
{
|
||||
inck = 1.0 / (texSize / frequency);
|
||||
for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
|
||||
{
|
||||
*(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
osg::notify(osg::INFO) << "DONE" << std::endl;
|
||||
return image;
|
||||
return image;
|
||||
}
|
||||
|
||||
static osg::Texture3D*
|
||||
@ -114,20 +114,20 @@ make1DSineImage( int texSize )
|
||||
|
||||
osg::Image* image = new osg::Image;
|
||||
image->setImage(texSize, 1, 1,
|
||||
4, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
new unsigned char[4 * texSize],
|
||||
osg::Image::USE_NEW_DELETE);
|
||||
4, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
new unsigned char[4 * texSize],
|
||||
osg::Image::USE_NEW_DELETE);
|
||||
|
||||
GLubyte* ptr = image->data();
|
||||
float inc = 2. * PI / (float)texSize;
|
||||
for(int i = 0; i < texSize; i++)
|
||||
{
|
||||
*ptr++ = (GLubyte)((sinf(i * inc) * 0.5 + 0.5) * 255.);
|
||||
*ptr++ = 0;
|
||||
*ptr++ = 0;
|
||||
*ptr++ = 1;
|
||||
*ptr++ = (GLubyte)((sinf(i * inc) * 0.5 + 0.5) * 255.);
|
||||
*ptr++ = 0;
|
||||
*ptr++ = 0;
|
||||
*ptr++ = 1;
|
||||
}
|
||||
return image;
|
||||
return image;
|
||||
}
|
||||
|
||||
static osg::Texture1D*
|
||||
@ -142,24 +142,24 @@ make1DSineTexture( int texSize )
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// OpenGL Shading Language source code for the "microshader" example,
|
||||
// which simply colors a fragment based on its location.
|
||||
// in-line GLSL source code for the "microshader" example
|
||||
|
||||
static const char *microshaderVertSource = {
|
||||
"varying vec4 color;"
|
||||
"void main(void)"
|
||||
"{"
|
||||
"color = gl_Vertex;"
|
||||
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
|
||||
"}"
|
||||
"// microshader - colors a fragment based on its position\n"
|
||||
"varying vec4 color;\n"
|
||||
"void main(void)\n"
|
||||
"{\n"
|
||||
" color = gl_Vertex;\n"
|
||||
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
|
||||
"}\n"
|
||||
};
|
||||
|
||||
static const char *microshaderFragSource = {
|
||||
"varying vec4 color;"
|
||||
"void main(void)"
|
||||
"{"
|
||||
"gl_FragColor = clamp( color, 0.0, 1.0 );"
|
||||
"}"
|
||||
"varying vec4 color;\n"
|
||||
"void main(void)\n"
|
||||
"{\n"
|
||||
" gl_FragColor = clamp( color, 0.0, 1.0 );\n"
|
||||
"}\n"
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
@ -200,11 +200,11 @@ LoadShaderSource( osg::Shader* shader, const std::string& fileName )
|
||||
std::string fqFileName = osgDB::findDataFile(fileName);
|
||||
if( fqFileName.length() != 0 )
|
||||
{
|
||||
shader->loadShaderSourceFromFile( fqFileName.c_str() );
|
||||
shader->loadShaderSourceFromFile( fqFileName.c_str() );
|
||||
}
|
||||
else
|
||||
{
|
||||
osg::notify(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl;
|
||||
osg::notify(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -241,36 +241,36 @@ class AnimateCallback: public osg::Uniform::Callback
|
||||
COLOR2
|
||||
};
|
||||
|
||||
AnimateCallback(Operation op) : _enabled(true),_operation(op) {}
|
||||
AnimateCallback(Operation op) : _enabled(true),_operation(op) {}
|
||||
|
||||
virtual void operator() ( osg::Uniform* uniform, osg::NodeVisitor* nv )
|
||||
{
|
||||
if( _enabled )
|
||||
{
|
||||
float angle = 2.0 * nv->getFrameStamp()->getReferenceTime();
|
||||
float sine = sinf( angle ); // -1 -> 1
|
||||
float v01 = 0.5f * sine + 0.5f; // 0 -> 1
|
||||
float v10 = 1.0f - v01; // 1 -> 0
|
||||
virtual void operator() ( osg::Uniform* uniform, osg::NodeVisitor* nv )
|
||||
{
|
||||
if( _enabled )
|
||||
{
|
||||
float angle = 2.0 * nv->getFrameStamp()->getReferenceTime();
|
||||
float sine = sinf( angle ); // -1 -> 1
|
||||
float v01 = 0.5f * sine + 0.5f; // 0 -> 1
|
||||
float v10 = 1.0f - v01; // 1 -> 0
|
||||
switch(_operation)
|
||||
{
|
||||
case OFFSET : uniform->set( osg::Vec3(0.505f, 0.8f*v01, 0.0f) ); break;
|
||||
case SIN : uniform->set( sine ); break;
|
||||
case COLOR1 : uniform->set( osg::Vec3(v10, 0.0f, 0.0f) ); break;
|
||||
case COLOR2 : uniform->set( osg::Vec3(v01, v01, v10) ); break;
|
||||
case COLOR2 : uniform->set( osg::Vec3(v01, v01, v10) ); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
bool _enabled;
|
||||
bool _enabled;
|
||||
Operation _operation;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Compose a scenegraph with examples of GL2 shaders
|
||||
|
||||
#define TEXUNIT_SINE 1
|
||||
#define TEXUNIT_NOISE 2
|
||||
#define TEXUNIT_SINE 1
|
||||
#define TEXUNIT_NOISE 2
|
||||
|
||||
osg::ref_ptr<osg::Group>
|
||||
GL2Scene::buildScene()
|
||||
@ -284,21 +284,21 @@ GL2Scene::buildScene()
|
||||
|
||||
// attach some Uniforms to the root, to be inherited by Programs.
|
||||
{
|
||||
osg::Uniform* OffsetUniform = new osg::Uniform( "Offset", osg::Vec3(0.0f, 0.0f, 0.0f) );
|
||||
osg::Uniform* SineUniform = new osg::Uniform( "Sine", 0.0f );
|
||||
osg::Uniform* Color1Uniform = new osg::Uniform( "Color1", osg::Vec3(0.0f, 0.0f, 0.0f) );
|
||||
osg::Uniform* Color2Uniform = new osg::Uniform( "Color2", osg::Vec3(0.0f, 0.0f, 0.0f) );
|
||||
osg::Uniform* OffsetUniform = new osg::Uniform( "Offset", osg::Vec3(0.0f, 0.0f, 0.0f) );
|
||||
osg::Uniform* SineUniform = new osg::Uniform( "Sine", 0.0f );
|
||||
osg::Uniform* Color1Uniform = new osg::Uniform( "Color1", osg::Vec3(0.0f, 0.0f, 0.0f) );
|
||||
osg::Uniform* Color2Uniform = new osg::Uniform( "Color2", osg::Vec3(0.0f, 0.0f, 0.0f) );
|
||||
|
||||
OffsetUniform->setUpdateCallback(new AnimateCallback(AnimateCallback::OFFSET));
|
||||
SineUniform->setUpdateCallback(new AnimateCallback(AnimateCallback::SIN));
|
||||
Color1Uniform->setUpdateCallback(new AnimateCallback(AnimateCallback::COLOR1));
|
||||
Color2Uniform->setUpdateCallback(new AnimateCallback(AnimateCallback::COLOR2));
|
||||
|
||||
osg::StateSet* ss = rootNode->getOrCreateStateSet();
|
||||
ss->addUniform( OffsetUniform );
|
||||
ss->addUniform( SineUniform );
|
||||
ss->addUniform( Color1Uniform );
|
||||
ss->addUniform( Color2Uniform );
|
||||
osg::StateSet* ss = rootNode->getOrCreateStateSet();
|
||||
ss->addUniform( OffsetUniform );
|
||||
ss->addUniform( SineUniform );
|
||||
ss->addUniform( Color1Uniform );
|
||||
ss->addUniform( Color2Uniform );
|
||||
|
||||
|
||||
//ss->setUpdateCallback(new AnimateCallback2);
|
||||
@ -306,62 +306,62 @@ GL2Scene::buildScene()
|
||||
|
||||
// the simple Microshader (its source appears earlier in this file)
|
||||
{
|
||||
osg::StateSet* ss = ModelInstance();
|
||||
osg::Program* program = new osg::Program;
|
||||
program->setName( "microshader" );
|
||||
_programList.push_back( program );
|
||||
program->addShader( new osg::Shader( osg::Shader::VERTEX, microshaderVertSource ) );
|
||||
program->addShader( new osg::Shader( osg::Shader::FRAGMENT, microshaderFragSource ) );
|
||||
ss->setAttributeAndModes( program, osg::StateAttribute::ON );
|
||||
osg::StateSet* ss = ModelInstance();
|
||||
osg::Program* program = new osg::Program;
|
||||
program->setName( "microshader" );
|
||||
_programList.push_back( program );
|
||||
program->addShader( new osg::Shader( osg::Shader::VERTEX, microshaderVertSource ) );
|
||||
program->addShader( new osg::Shader( osg::Shader::FRAGMENT, microshaderFragSource ) );
|
||||
ss->setAttributeAndModes( program, osg::StateAttribute::ON );
|
||||
}
|
||||
|
||||
// the "blocky" shader, a simple animation test
|
||||
{
|
||||
osg::StateSet* ss = ModelInstance();
|
||||
BlockyProgram = new osg::Program;
|
||||
BlockyProgram->setName( "blocky" );
|
||||
_programList.push_back( BlockyProgram );
|
||||
BlockyVertObj = new osg::Shader( osg::Shader::VERTEX );
|
||||
BlockyFragObj = new osg::Shader( osg::Shader::FRAGMENT );
|
||||
BlockyProgram->addShader( BlockyFragObj );
|
||||
BlockyProgram->addShader( BlockyVertObj );
|
||||
ss->setAttributeAndModes(BlockyProgram, osg::StateAttribute::ON);
|
||||
osg::StateSet* ss = ModelInstance();
|
||||
BlockyProgram = new osg::Program;
|
||||
BlockyProgram->setName( "blocky" );
|
||||
_programList.push_back( BlockyProgram );
|
||||
BlockyVertObj = new osg::Shader( osg::Shader::VERTEX );
|
||||
BlockyFragObj = new osg::Shader( osg::Shader::FRAGMENT );
|
||||
BlockyProgram->addShader( BlockyFragObj );
|
||||
BlockyProgram->addShader( BlockyVertObj );
|
||||
ss->setAttributeAndModes(BlockyProgram, osg::StateAttribute::ON);
|
||||
}
|
||||
|
||||
// the "eroded" shader, uses a noise texture to discard fragments
|
||||
{
|
||||
osg::StateSet* ss = ModelInstance();
|
||||
ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture);
|
||||
ErodedProgram = new osg::Program;
|
||||
ErodedProgram->setName( "eroded" );
|
||||
_programList.push_back( ErodedProgram );
|
||||
ErodedVertObj = new osg::Shader( osg::Shader::VERTEX );
|
||||
ErodedFragObj = new osg::Shader( osg::Shader::FRAGMENT );
|
||||
ErodedProgram->addShader( ErodedFragObj );
|
||||
ErodedProgram->addShader( ErodedVertObj );
|
||||
ss->setAttributeAndModes(ErodedProgram, osg::StateAttribute::ON);
|
||||
osg::StateSet* ss = ModelInstance();
|
||||
ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture);
|
||||
ErodedProgram = new osg::Program;
|
||||
ErodedProgram->setName( "eroded" );
|
||||
_programList.push_back( ErodedProgram );
|
||||
ErodedVertObj = new osg::Shader( osg::Shader::VERTEX );
|
||||
ErodedFragObj = new osg::Shader( osg::Shader::FRAGMENT );
|
||||
ErodedProgram->addShader( ErodedFragObj );
|
||||
ErodedProgram->addShader( ErodedVertObj );
|
||||
ss->setAttributeAndModes(ErodedProgram, osg::StateAttribute::ON);
|
||||
|
||||
ss->addUniform( new osg::Uniform("LightPosition", osg::Vec3(0.0f, 0.0f, 4.0f)) );
|
||||
ss->addUniform( new osg::Uniform("Scale", 1.0f) );
|
||||
ss->addUniform( new osg::Uniform("sampler3d", TEXUNIT_NOISE) );
|
||||
ss->addUniform( new osg::Uniform("LightPosition", osg::Vec3(0.0f, 0.0f, 4.0f)) );
|
||||
ss->addUniform( new osg::Uniform("Scale", 1.0f) );
|
||||
ss->addUniform( new osg::Uniform("sampler3d", TEXUNIT_NOISE) );
|
||||
}
|
||||
|
||||
// the "marble" shader, uses two textures
|
||||
{
|
||||
osg::StateSet* ss = ModelInstance();
|
||||
ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture);
|
||||
ss->setTextureAttribute(TEXUNIT_SINE, sineTexture);
|
||||
MarbleProgram = new osg::Program;
|
||||
MarbleProgram->setName( "marble" );
|
||||
_programList.push_back( MarbleProgram );
|
||||
MarbleVertObj = new osg::Shader( osg::Shader::VERTEX );
|
||||
MarbleFragObj = new osg::Shader( osg::Shader::FRAGMENT );
|
||||
MarbleProgram->addShader( MarbleFragObj );
|
||||
MarbleProgram->addShader( MarbleVertObj );
|
||||
ss->setAttributeAndModes(MarbleProgram, osg::StateAttribute::ON);
|
||||
osg::StateSet* ss = ModelInstance();
|
||||
ss->setTextureAttribute(TEXUNIT_NOISE, noiseTexture);
|
||||
ss->setTextureAttribute(TEXUNIT_SINE, sineTexture);
|
||||
MarbleProgram = new osg::Program;
|
||||
MarbleProgram->setName( "marble" );
|
||||
_programList.push_back( MarbleProgram );
|
||||
MarbleVertObj = new osg::Shader( osg::Shader::VERTEX );
|
||||
MarbleFragObj = new osg::Shader( osg::Shader::FRAGMENT );
|
||||
MarbleProgram->addShader( MarbleFragObj );
|
||||
MarbleProgram->addShader( MarbleVertObj );
|
||||
ss->setAttributeAndModes(MarbleProgram, osg::StateAttribute::ON);
|
||||
|
||||
ss->addUniform( new osg::Uniform("NoiseTex", TEXUNIT_NOISE) );
|
||||
ss->addUniform( new osg::Uniform("SineTex", TEXUNIT_SINE) );
|
||||
ss->addUniform( new osg::Uniform("NoiseTex", TEXUNIT_NOISE) );
|
||||
ss->addUniform( new osg::Uniform("SineTex", TEXUNIT_SINE) );
|
||||
}
|
||||
|
||||
#ifdef INTERNAL_3DLABS //[
|
||||
@ -420,10 +420,10 @@ GL2Scene::toggleShaderEnable()
|
||||
{
|
||||
_shadersEnabled = ! _shadersEnabled;
|
||||
osg::notify(osg::WARN) << "shader enable = " <<
|
||||
((_shadersEnabled) ? "ON" : "OFF") << std::endl;
|
||||
((_shadersEnabled) ? "ON" : "OFF") << std::endl;
|
||||
for( unsigned int i = 0; i < _programList.size(); i++ )
|
||||
{
|
||||
//_programList[i]->enable( _shadersEnabled );
|
||||
//_programList[i]->enable( _shadersEnabled );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,9 +40,9 @@ int BM;
|
||||
|
||||
void SetNoiseFrequency(int frequency)
|
||||
{
|
||||
start = 1;
|
||||
B = frequency;
|
||||
BM = B-1;
|
||||
start = 1;
|
||||
B = frequency;
|
||||
BM = B-1;
|
||||
}
|
||||
|
||||
double noise1(double arg)
|
||||
|
@ -10,8 +10,8 @@
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/* file: examples/osgglsl/osgshaders.cpp
|
||||
* author: Mike Weiblen 2005-04-05
|
||||
/* file: examples/osgglsl/osgshaders.cpp
|
||||
* author: Mike Weiblen 2005-04-05
|
||||
*
|
||||
* A demo of the OpenGL Shading Language shaders using core OSG.
|
||||
*
|
||||
@ -35,29 +35,29 @@ using namespace osg;
|
||||
class KeyHandler: public osgGA::GUIEventHandler
|
||||
{
|
||||
public:
|
||||
KeyHandler( GL2ScenePtr gl2Scene ) :
|
||||
_gl2Scene(gl2Scene)
|
||||
{}
|
||||
KeyHandler( GL2ScenePtr gl2Scene ) :
|
||||
_gl2Scene(gl2Scene)
|
||||
{}
|
||||
|
||||
bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& )
|
||||
{
|
||||
if( ea.getEventType() != osgGA::GUIEventAdapter::KEYDOWN )
|
||||
return false;
|
||||
bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& )
|
||||
{
|
||||
if( ea.getEventType() != osgGA::GUIEventAdapter::KEYDOWN )
|
||||
return false;
|
||||
|
||||
switch( ea.getKey() )
|
||||
{
|
||||
case 'x':
|
||||
_gl2Scene->reloadShaderSource();
|
||||
return true;
|
||||
case 'y':
|
||||
_gl2Scene->toggleShaderEnable();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
switch( ea.getKey() )
|
||||
{
|
||||
case 'x':
|
||||
_gl2Scene->reloadShaderSource();
|
||||
return true;
|
||||
case 'y':
|
||||
_gl2Scene->toggleShaderEnable();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
GL2ScenePtr _gl2Scene;
|
||||
GL2ScenePtr _gl2Scene;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
@ -70,7 +70,7 @@ int main( int argc, char **argv )
|
||||
// set up the usage document
|
||||
args.getApplicationUsage()->setApplicationName(args.getApplicationName());
|
||||
args.getApplicationUsage()->setDescription(args.getApplicationName() +
|
||||
" demonstrates the OpenGL Shading Language using core OSG");
|
||||
" demonstrates the OpenGL Shading Language using core OSG");
|
||||
args.getApplicationUsage()->setCommandLineUsage(args.getApplicationName());
|
||||
args.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user