Hello Robert,

Yes, you are right. The reason is that I didn't set up the texture coordinates. 
I did the following change, and I could texturing the model in the shader. I 
think the problem is that gl_MultiTexCoord0 actually didn't give out any 
texture coordinates in my first code.

vertex shader:
varying vec2 texCoord;
void main()
{

        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        texCoord = gl_Vertex.xy;
}

fragment shader:
uniform sampler2D texture;
varying vec2 texCoord ;
void main()
{
       vec4 color = texture2D(texture, texCoord );
        gl_FragColor = color;
}

But in this way, I set the texture coordinate according to gl_Vertex, which is 
still not what I wanted. 

If I want to render the cow.osg model using shaders, how could I get the 
texture coordinates from the osg file? I checked the original osg file of the 
cow model, and found that the texture coordinates are all set to (0,0) in this 
file. I wonder whether it's necessary to use texture coordinates if I want to 
render the cow model.

If I want to render the exact same cow model using shaders, would you tell me 
how could I do it?

Thanks,
Ying

robertosfield wrote:
> Hi Ying,
> 
> It's not possible to pinpoint what is wrong as you provide no guidance of how 
> you set up your osg::Geometry.  My best guess would be that you don't set up 
> the texture coordinates.
> 
> 
> Have a look at the osgshaders example for inspiration,
> 
> 
> Robert.
> 
> 
> 
> On 5 August 2014 16:31, ying song < ()> wrote:
> 
> > Hello everyone,
> > 
> > I tried texturing a cube by using shaders, but failed. I already tried all 
> > the way I could think about, but the rendered cube is still totally black 
> > if I use shaders. Could someone help me on this?
> > 
> > Here is mycode:
> > cpp file:
> >         osg::ref_ptr<osg::Image> image = 
> > osgDB::readImageFile("Images/reflect.rgb");
> >         osg::ref_ptr<osg::Node> node = createNode(); //create the cube
> > 
> > osg::StateSet* squareState = node->getOrCreateStateSet();
> >          osg::Program* squareProgramObject = new osg::Program;
> >        osg::Shader* squareVertexObject = new osg::Shader( 
> > osg::Shader::VERTEX );
> >        osg::Shader* squareFragmentObject = new osg::Shader( 
> > osg::Shader::FRAGMENT );
> >        squareProgramObject->addShader( squareFragmentObject );
> >        squareProgramObject->addShader( squareVertexObject );
> >            squareVertexObject->loadShaderSourceFromFile( 
> > "shaders/cubeRender.vert");
> >            
> > squareFragmentObject->loadShaderSourceFromFile("shaders/cubeRender.frag");
> >        squareState->setAttributeAndModes(squareProgramObject, 
> > osg::StateAttribute::ON);
> > 
> >            osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D();
> >         texture->setImage(image.get());
> >         
> > texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST_MIPMAP_NEAREST);
> >  //choose the nearest texture unit value
> >         texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); 
> > //choose the average of the nearest 4 pixel values as texture
> > 
> >         osg::Uniform* myTextureU = new 
> > osg::Uniform("myTexture",texture.get());
> >         squareState->addUniform(myTextureU);
> > 
> >          viewer->setSceneData(node.get());
> > 
> > vertex shader:
> > 
> > void main()
> > {
> > 
> >         gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
> >         gl_TexCoord[0] = gl_MultiTexCoord0;
> > }
> > 
> > fragment shader:
> > uniform sampler2D myTexture;
> > 
> > void main()
> > {
> >         vec4 color = texture2D(myTexture, gl_TexCoord[0].st);
> >         gl_FragColor = color;
> > }
> > 
> > Thanks a lot!
> > 
> > Cheers,
> > ying[/b]
> > 
> > ------------------
> > Read this topic online here:
> > http://forum.openscenegraph.org/viewtopic.php?p=60539#60539 
> > (http://forum.openscenegraph.org/viewtopic.php?p=60539#60539)
> > 
> > 
> > 
> > 
> > 
> > _______________________________________________
> > osg-users mailing list
> >  ()
> > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org 
> > (http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org)
> > 
> 
> 
>  ------------------
> Post generated by Mail2Forum


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





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to