|
楼主 |
发表于 2011-7-13 22:47:49
|
显示全部楼层
回复 monai1987
去列表搜索:“Need help about Night Vision Effect”
论坛地址:
比较需要注意的 ...
aya_daxiang 发表于 2011-7-13 09:18
哥们,你给的链接我看到了,找到了例子,但是把代码贴进去用不了啊,能给我指点指点吗?谢谢了,代码贴出来了:- int _tmain(int argc, _TCHAR* argv[])
- {
- osg::Group* root = new osg::Group;
- osg::Node* node = osgDB::readNodeFile("cow.osg");
- osgViewer::Viewer viewer;
- node->addCullCallback(new myNodeCallback);
- osg::StateSet* stateset = root->getOrCreateStateSet();
- osg::Shader* vertexShader = new osg::Shader( osg::Shader::VERTEX );
- osg::Shader* fragmentShader = new osg::Shader( osg::Shader::FRAGMENT );
- vertexShader->loadShaderSourceFromFile( "yeshi.vert" );
- fragmentShader->loadShaderSourceFromFile( "yeshi.frag" );
- osg::Program* program = new osg::Program;
- program->addShader( vertexShader );
- program->addShader( fragmentShader );
- stateset->setAttributeAndModes( program, osg::StateAttribute::ON );
- //input:
- // sceneBuffer (sampler2D): the scene buffer.
- // noiseTex (sampler2D): noise texture to add some artifacts to the night vision device. The noise texture comes from this free noise texture pack.
- // maskTex (sampler2D): binocular mask of the night vision device.
- // elapsedTime (float): elapsed time in seconds.
- // luminanceThreshold (float): allows to change the luminance threshold. Default value: 0.2
- // colorAmplification (float): amplification factor for pixels with low luminance. Default value: 4.0
- // effectCoverage (float): effect screen coverage – only for demo needs! Default value: 0.5 (half screen)
- osg::Uniform* param4 = new osg::Uniform( "elapsedTime", 0 );
- stateset->addUniform( param4 );
- osg::Uniform* param5 = new osg::Uniform( "luminanceThreshold", 0.4f );
- stateset->addUniform( param5 );
- osg::Uniform* param6 = new osg::Uniform( "colorAmplification", 4.0f );
- stateset->addUniform( param6 );
- osg::Uniform* param7 = new osg::Uniform( "effectCoverage", 0.50f );
- stateset->addUniform( param7 );
- osg::Uniform* param8 = new osg::Uniform( "sceneBuffer", 0 );
- stateset->addUniform( param8 );
- osg::Uniform* param9 = new osg::Uniform( "noiseTex", 0 );
- stateset->addUniform( param9 );
- osg::Uniform* param10 = new osg::Uniform( "maskTex", 0 );
- stateset->addUniform( param10 );
- root->addChild(node);
- viewer.setSceneData(root);
- viewer.run();
- return 0;
- }
复制代码
*.frag代码如下:- uniform sampler2D sceneBuffer;
- uniform sampler2D noiseTex;
- uniform sampler2D maskTex;
- uniform float elapsedTime; // seconds
- uniform float luminanceThreshold; // 0.2
- uniform float colorAmplification; // 4.0
- uniform float effectCoverage; // 0.5
- void main ()
- {
- vec4 finalColor;
- // Set effectCoverage to 1.0 for normal use.
- if (gl_TexCoord[0].x < effectCoverage)
- {
- vec2 uv;
- uv.x = 0.4*sin(elapsedTime*50.0);
- uv.y = 0.4*cos(elapsedTime*50.0);
- float m = texture2D(maskTex, gl_TexCoord[0].st).r;
- vec3 n = texture2D(noiseTex,
- (gl_TexCoord[0].st*3.5) + uv).rgb;
- vec3 c = texture2D(sceneBuffer, gl_TexCoord[0].st
- + (n.xy*0.005)).rgb;
- // float lum = dot(vec3(0.30, 0.59, 0.11), c);
- float lum = dot(vec3(0.01, 0.01, 0.01), c);
- if (lum < luminanceThreshold)
- c *= colorAmplification;
- vec3 visionColor = vec3(0.1, 0.95, 0.2);
- finalColor.rgb = (c + (n*0.2)) * visionColor * m;
- }
- else
- {
- finalColor = texture2D(sceneBuffer,
- gl_TexCoord[0].st);
- }
- gl_FragColor.rgb = finalColor.rgb;
- gl_FragColor.a = 1.0;
- }
复制代码
*.vert代码如下:- void main()
- {
- gl_Position = ftransform();
- gl_TexCoord[0] = gl_MultiTexCoord0;
- }
复制代码
这样做之后,出来的效果是,奶牛的纹理颜色全没了,是我的那几个参数没用对吗? |
|