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 <[email protected]> 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
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to