Hello,

I have been having some trouble rendering textured models with shaders. I am a 
little confused about how to set the sampler uniform to the textures. 

Also, from my understanding, if a vertex shader is not specified that 
functionality is pushed to the fixed pipeline?

Here is what I have. 

Application:

Code:

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
#include <iostream>
#include <string>

/* loadShaderSource from 
http://www.openscenegraph.org/projects/osg/wiki/Support/Tutorials/ShadersIntroduction
 */
bool loadShaderSource(osg::Shader* obj, const std::string& fileName )
{
  std::string fqFileName = osgDB::findDataFile(fileName);
  if( fqFileName.length() == 0 )
    {
      std::cout << "File \"" << fileName << "\" not found." << std::endl;
      return false;
    }
  bool success = obj->loadShaderSourceFromFile( fqFileName.c_str());
  if ( !success  )
    {
      std::cout << "Couldn't load file: " << fileName << std::endl;
      return false;
    }
  else
    {
      return true;
    }
}

int main(int argc, char ** argv)
{
  osgViewer::Viewer viewer;
  
  osg::ref_ptr<osg::Group> root = new osg::Group();
  osg::ref_ptr<osg::Node> snowplow = 
osgDB::readNodeFile("PlowModel/Plow_Main.DAE");

  osg::ref_ptr<osg::StateSet> snowplowState = snowplow->getOrCreateStateSet();

  /// load and compile shader

  osg::ref_ptr<osg::Program> program = new osg::Program();
  osg::ref_ptr<osg::Shader> fragshader = new osg::Shader(osg::Shader::FRAGMENT);
  program->addShader( fragshader );

  loadShaderSource( fragshader, "texture_fragment.glsl");

  osg::ref_ptr<osg::Uniform> baseTexture = new osg::Uniform("textureSampler", 
0);

  snowplowState->addUniform( baseTexture );

  snowplowState->setAttributeAndModes( program, osg::StateAttribute::ON );


  root->addChild(snowplow.get());

  viewer.setSceneData( root.get() );
  return viewer.run();
}





Shader:

Code:


/************************/
/*  added for texturing */
/************************/
uniform sampler2D textureSampler;
/************************/

void main()
{
        /************************/
        /*  added for texturing */
        /************************/
        vec3 texturedColor = vec3 (texture2D (textureSampler, 
gl_TexCoord[0].st));
        /************************/
        
        /*************************/
        /* changed for texturing */
        /* gl_FragColor = gl_Color; */
        gl_FragColor = vec4 (texturedColor.rg, 1.0, 1.0);

}





Thanks for your help!

Michele

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=16499#16499





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to