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