|
本帖最后由 lllhy888 于 2013-3-19 16:11 编辑
vert shader 及 frag shader 如下:
static const char gTVertexShader[] =
"varying vec4 color; \n"
"void main() { \n"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
"} \n";
static const char gTFragmentShader[] =
"precision mediump float; \n"
"varying mediump vec4 color; \n"
"uniform sampler2D colorTex; \n"
"void main() { \n"
" gl_FragColor = texture2D(colorTex, gl_TexCoord[0].xy); \n"
"} \n";
C++代码片段:
osg::Shader * tvshader = new osg::Shader(osg::Shader::VERTEX, gTVertexShader );
osg::Shader * tfshader = new osg::Shader(osg::Shader::FRAGMENT, gTFragmentShader );
osg:rogram * tprog = new osg::Program;
tprog->addShader ( tvshader );
tprog->addShader ( tfshader );
.............
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back( osg::Vec3(-10.0f, 0.0f, 10.0f) );
vertices->push_back( osg::Vec3(-10.0f, 0.0f, -10.0f) );
vertices->push_back( osg::Vec3(10.0f, 0.0f, 10.0f) );
vertices->push_back( osg::Vec3(10.0f, 0.0f, -10.0f) );
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
normals->push_back( osg::Vec3(0.0f,-1.0f, 0.0f) );
osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array;
texcoords->push_back( osg::Vec2(0.0f, 0.0f) );
texcoords->push_back( osg::Vec2(0.0f, 1.0f) );
texcoords->push_back( osg::Vec2(1.0f, 1.0f) );
texcoords->push_back( osg::Vec2(1.0f, 0.0f) );
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back( osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) );
colors->push_back( osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) );
colors->push_back( osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) );
colors->push_back( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
quad->setVertexArray( vertices );
quad->setTexCoordArray( 0, texcoords );
quad->addPrimitiveSet( new osg:rawArrays(GL_TRIANGLE_STRIP, 0, 4) );
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setWrap( osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE );
texture->setWrap( osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE );
texture->setWrap( osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE );
// texture->setFilter( osg::Texture::GENERATE_MIPMAP, osg::Texture:INEAR_MIPMAP_LINEAR );
texture->setFilter( osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR );
texture->setFilter( osg::Texture::MAG_FILTER, osg::Texture::LINEAR );
texture->setResizeNonPowerOfTwoHint( false );
osg::ref_ptr<osg::Image> image =
osgDB::readImageFile( "/mnt/sdcard/OsgGame.first/files/negx.jpg" );
if (image.valid())
{
osg::notify(osg::ALWAYS)<<"There are image data !!"<<std::endl;
}
texture->setImage( image.get() );
osg::ref_ptr<osg::Geode> root = new osg::Geode;
root->getOrCreateStateSet()->setTextureAttributeAndModes(
0, texture.get(),osg::StateAttribute::ON );
osg::ref_ptr<osg::Uniform> uniform = new osg::Uniform("colorTex", 0 );
root->addDrawable(quad);
quad->getOrCreateStateSet()->setAttribute (tprog);
quad->getOrCreateStateSet()->addUniform(uniform);
_root->addChild(root);
请帮忙解决下,谢谢
|
|