|
大家好,
我想实现一个简单的功能,就是用着色器来对一个立方体进行纹理渲染,但是最后渲染出来的立方体是黑色的。我试了很多办法都没法把纹理贴图渲染到立方体上。希望大家可以帮我看一下问题在哪里,感激不尽。
下面是我的代码:
cpp 文件:
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("Images/reflect.rgb");
osg::ref_ptr<osg::Node> node = createNode(); //创建立方体
osg::StateSet* squareState = node->getOrCreateStateSet();
osg:rogram* 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);
texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture:INEAR);
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;
}
|
|