osg android texture shader 不渲染
本帖最后由 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 = 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.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::Program * 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::DrawArrays(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::LINEAR_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);
请帮忙解决下,谢谢
楼主解决这个问题了吗,我也碰到了,可否告知一下解决方案,giserpeng@163.com,谢谢 楼主写的送着色器是计算机上的OpenGL着色器,如果要让这段代码在手机上跑得起来,你写的着色器必须遵守OpenGL ES的规范。像gl_ModelViewProjectionMatrix , gl_Vertex, gl_TexCoord, gl_MultiTexCoord0等等只是计算机上的OpenGL着色器的内置变量,OpenGL ES的着色器里规范里面没有这些变量,它们都是通过用户自定义的着色器来替代的。建议你阅读一下《OpenGL ES Programming Guide》里面有详细的讲解。嵌入式版本的规范与计算机版本的规范存在很大的差异性。 所以你写的这段着色器代码在手机上是编译不过的。 the_mercury 发表于 2014-7-16 09:49
楼主写的送着色器是计算机上的OpenGL着色器,如果要让这段代码在手机上跑得起来,你写的着色器必须遵守Open ...
我遇到过这个问题,问题出现在 gl_TexCoord 是es版本不支持,但是gl_ModelViewProjectionMatrix , gl_Vertex, gl_MultiTexCoord0 都是支持的。。
其中还有要注意的是手机的gpu仅支持mediump 的变量(默认是highp)
我的shader如下,在三星 N7000手机可以跑起来
vert.glsl
static const char gVertexShader[] =
"varying vec2 v_texCoord; \n"
"void main() { \n"
" gl_Position= gl_ModelViewProjectionMatrix * gl_Vertex; \n"
" v_texCoord = gl_MultiTexCoord0.xy; \n"
"} \n";
frag.glsl
static const char gFragmentShader[] =
"varyingmediump vec2 v_texCoord; \n"
"uniform sampler2D sam; \n"
"void main() { \n"
"gl_FragColor = texture2D(sam, v_texCoord); \n"
"} \n"; 恰好搜到这个帖子,上面的 shader 解决了我的 gles 2.0 素材不能加载的问题。:victory:
页:
[1]