<div>#include <osgViewer/Viewer></div><div>#include <osgDB/ReadFile></div><div>#include <osg/GraphicsContext></div><div>#include <osg/Camera></div><div>#include <osg/Viewport></div><div>#include <osg/StateSet></div><div>#include <osg/Program></div><div>#include <osg/Shader></div><div>#include <osgUtil/Optimizer></div><div><br></div><div>void configureShaders( osg::StateSet* stateSet )</div><div>{</div><div> const std::string vertexSource =</div><div> "#version 130 \n"</div><div> " \n"</div><div> "uniform mat4 osg_ModelViewProjectionMatrix; \n"</div><div> "uniform mat3 osg_NormalMatrix; \n"</div><div> "uniform vec3 ecLightDir; \n"</div><div><span style="white-space:pre"> </span></div><div> " \n"</div><div> "in vec4 osg_Vertex; \n"</div><div> "in vec3 osg_Normal; \n"</div><div><span style="white-space:pre"> </span>"in vec4 osg_MultiTexCoord0; \n"</div><div> "out vec4 color; \n"</div><div><span style="white-space:pre"> </span>"out vec2 texCoord; \n"</div><div> " \n"</div><div> "void main() \n"</div><div> "{ \n"</div><div> " vec3 ecNormal = normalize( osg_NormalMatrix * osg_Normal ); \n"</div><div> " float diffuse = max( dot( ecLightDir, ecNormal ), 0. ); \n"</div><div> " color = vec4( vec3( diffuse ), 1. ); \n"</div><div><span style="white-space:pre"> </span>" texCoord = osg_MultiTexCoord0.xy; \n"</div><div> " \n"</div><div> " gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex; \n"</div><div> "} \n";</div><div> osg::Shader* vShader = new osg::Shader( osg::Shader::VERTEX, vertexSource );</div><div><br></div><div> const std::string fragmentSource =</div><div> "#version 130 \n"</div><div> " \n"</div><div><span style="white-space:pre"> </span>"in vec2 texCoord; \n"</div><div> "in vec4 color; \n"</div><div> "out vec4 fragData; \n"</div><div><span style="white-space:pre"> </span>"uniform sampler2D tex; \n"</div><div> " \n"</div><div> "void main() \n"</div><div> "{ \n"</div><div> " fragData = texture2D(tex,texCoord); \n"</div><div> "} \n";</div><div> osg::Shader* fShader = new osg::Shader( osg::Shader::FRAGMENT, fragmentSource );</div><div><br></div><div> osg::Program* program = new osg::Program;</div><div> program->addShader( vShader );</div><div> program->addShader( fShader );</div><div> stateSet->setAttribute( program );</div><div><br></div><div> osg::Vec3f lightDir( 0., 0.5, 1. );</div><div> lightDir.normalize();</div><div> stateSet->addUniform( new osg::Uniform( "ecLightDir", lightDir ) );</div><div><span style="white-space:pre"> </span>stateSet->addUniform( new osg::Uniform( "tex" ,0 ) );</div><div>}</div><div><br></div><div>int main( int argc, char** argv )</div><div>{</div><div> osg::ArgumentParser arguments( &argc, argv );</div><div><br></div><div> osg::ref_ptr<osg::Node> root = osgDB::readNodeFile("cow.osg");</div><div> if( root == NULL )</div><div> {</div><div> osg::notify( osg::FATAL ) << "Unable to load model from command line." << std::endl;</div><div> return( 1 );</div><div> }</div><div><br></div><div> osgUtil::Optimizer optimizer;</div><div> optimizer.optimize(root.get(), osgUtil::Optimizer::ALL_OPTIMIZATIONS | osgUtil::Optimizer::TESSELLATE_GEOMETRY);</div><div><br></div><div> configureShaders( root->getOrCreateStateSet() );</div><div><br></div><div> const int width( 800 ), height( 450 );</div><div> const std::string version( "3.0" );</div><div> osg::ref_ptr< osg::GraphicsContext::Traits > traits = new osg::GraphicsContext::Traits();</div><div> traits->x = 20; traits->y = 30;</div><div> traits->width = width; traits->height = height;</div><div> traits->windowDecoration = true;</div><div> traits->doubleBuffer = true;</div><div> traits->glContextVersion = version;</div><div> traits->readDISPLAY();</div><div> traits->setUndefinedScreenDetailsToDefaultScreen();</div><div> osg::ref_ptr< osg::GraphicsContext > gc = osg::GraphicsContext::createGraphicsContext( traits.get() );</div><div> if( !gc.valid() )</div><div> {</div><div> osg::notify( osg::FATAL ) << "Unable to create OpenGL v" << version << " context." << std::endl;</div><div> return( 1 );</div><div> }</div><div><br></div><div> osgViewer::Viewer viewer;</div><div><br></div><div> // Create a Camera that uses the above OpenGL context.</div><div> osg::Camera* cam = viewer.getCamera();</div><div> cam->setGraphicsContext( gc.get() );</div><div> // Must set perspective projection for fovy and aspect.</div><div> cam->setProjectionMatrix( osg::Matrix::perspective( 30., (double)width/(double)height, 1., 100. ) );</div><div> // Unlike OpenGL, OSG viewport does *not* default to window dimensions.</div><div> cam->setViewport( new osg::Viewport( 0, 0, width, height ) );</div><div><br></div><div> viewer.setSceneData( root );</div><div><br></div><div> // for non GL3/GL4 and non GLES2 platforms we need enable the osg_ uniforms that the shaders will use,</div><div> // you don't need thse two lines on GL3/GL4 and GLES2 specific builds as these will be enable by default.</div><div> gc->getState()->setUseModelViewAndProjectionUniforms(true);</div><div> gc->getState()->setUseVertexAttributeAliasing(true);</div><div><br></div><div> return( viewer.run() );</div><div>}</div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:osg-users+unsubscribe@googlegroups.com">osg-users+unsubscribe@googlegroups.com</a>.<br />
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/osg-users/031ecc10-5d72-4004-82d0-23baf6d6fa43n%40googlegroups.com?utm_medium=email&utm_source=footer">https://groups.google.com/d/msgid/osg-users/031ecc10-5d72-4004-82d0-23baf6d6fa43n%40googlegroups.com</a>.<br />