|
本帖最后由 liu7667715 于 2012-12-10 11:52 编辑
osg在callback中设置shader出错,并且不会更改颜色;
shader代码如下:
- //脚本:
- const char* ChangeColor::base_vert = {
- "varying vec3 vNormal;\n"
- "varying vec2 vTC;\n"
- "uniform sampler2D Base;\n"
- "void main(void)\n"
- "{\n"
- "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
- "vTC = gl_MultiTexCoord0.st;\n"
- "}\n"
- };
- const char* ChangeColor::yellow_frag = {
- "//yellow\n"
- "varying vec3 vNormal;\n"
- "varying vec2 vTC;\n"
- "uniform sampler2D Base;\n"
- "void main(void)\n"
- "{\n"
- "gl_FragColor = vec4((texture2D( Base, vTC ).rgb * 0.5f + vec3(0.953f,0.486f,0.015f)*0.5f),1.0f);\n"
- "}\n"
- };
- const char* ChangeColor::transparent_frag = {
- "//blue\n"
- "varying vec3 vNormal;\n"
- "varying vec2 vTC;\n"
- "uniform sampler2D Base;\n"
- "void main(void)\n"
- "{\n"
- "gl_FragColor = vec4( vec3(0.0,0.0,0.0), 0.0 );\n"
- "}\n"
- };
- //更改颜色代码:
- void ChangeColor::change(osg::StateSet* stateset)
- {
- if(0 == stateset)
- return;
- osg::ref_ptr<osg::Program> programObject = dynamic_cast<osg::Program*>(stateset->getAttribute(osg::StateAttribute::PROGRAM, 0));
- if(!programObject)
- programObject=new osg::Program();
- if (programObject->getNumShaders()==0)
- {
- //Alpha混合开启
- stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
- programObject->addShader(new osg::Shader(osg::Shader::VERTEX,base_vert));
- programObject->addShader(new osg::Shader(osg::Shader::FRAGMENT,yellow_frag));
- stateset->setAttributeAndModes(programObject,osg::StateAttribute::ON);
- }
- }
- //设置透明代码:
- void ChangeColor::changeTransparent(osg::StateSet* stateset)
- {
- if(0 == stateset)
- return;
- osg::ref_ptr<osg::Program> programObject = dynamic_cast<osg::Program*>(stateset->getAttribute(osg::StateAttribute::PROGRAM, 0));
- if(!programObject)
- programObject=new osg::Program();
- if (programObject->getNumShaders()==0)
- {
- //Alpha混合开启
- stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
- programObject->addShader(new osg::Shader(osg::Shader::VERTEX,base_vert));
- programObject->addShader(new osg::Shader(osg::Shader::FRAGMENT,transparent_frag));
- stateset->setAttributeAndModes(programObject,osg::StateAttribute::ON);
- stateset->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
- // 启用深度检测,使不透明的多边形遮挡它后面的透明物体
- stateset->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON );
- }
- }
- //恢复颜色代码:
- void ChangeColor::resume(osg::StateSet* stateset)
- {
- if(0 == stateset)
- return;
-
- osg::ref_ptr<osg::Program> programObject = dynamic_cast<osg::Program*>(stateset->getAttribute(osg::StateAttribute::PROGRAM, 0));
- if(programObject!=NULL&&programObject->getNumShaders()>0)
- {
- for (int i=programObject->getNumShaders() - 1; i >= 0; --i)
- {
- programObject->removeShader(programObject->getShader(i));
- }
- stateset->setAttributeAndModes(programObject,osg::StateAttribute::ON);
- }
- }
复制代码
错误出现在 设置为透明后,恢复不透明状态,然后在callback函数中执行更改颜色就会出错;
请大神予以解答,谢谢!
错误3
错误2
错误1
ChangeColor.h
(1.11 KB, 下载次数: 0)
|
|