查看: 706|回复: 1

求助:本人osg菜鸟一个,实现水效果,波动没有

[复制链接]

该用户从未签到

发表于 2013-11-1 23:37:42 | 显示全部楼层 |阅读模式
_surfaceStateSet->addUniform( new osg::Uniform("water_reflection",  _reflectionUnit ) , osg::StateAttribute::ON );
_surfaceStateSet->addUniform( new osg::Uniform("water_refraction",  _refractionUnit) , osg::StateAttribute::ON );
_surfaceStateSet->addUniform( new osg::Uniform("water_depthmap",  _refractionDepthUnit ) , osg::StateAttribute::ON );
_surfaceStateSet->addUniform( new osg::Uniform("water_dudvmap",  _dudvUnit ) , osg::StateAttribute::ON );
_surfaceStateSet->addUniform( new osg::Uniform("time", (float)( g_time ) ), osg::StateAttribute::ON );

Shader中需要传入4张纹理,和一个时间值。4张纹理分别是:反射纹理,折射纹理、折射深度纹理 和dudv图。

下面是绘制水面的shader,
const char *water_vs =
        "varying vec4 projectedVertex;\n"
"void main(void)\n"
"{\n"
"        gl_TexCoord[0] =  gl_MultiTexCoord0;\n"
"        projectedVertex = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
"        gl_Position = projectedVertex;\n"
"}";



const char *water_fs =
"#define WAVES_SCALE 30.0\n"

"uniform sampler2D water_reflection;\n"
"uniform sampler2D water_refraction;\n"
"uniform sampler2D water_dudvmap;\n"
"uniform sampler2D water_depthmap;\n"
"\n"
"uniform float time;\n"
"varying vec4 projectedVertex;\n"

"const vec4 waterColor = vec4(0.1,0.36,0.51,1.0);\n"
"void main(void)\n"
"{\n"
"vec4 tmp = vec4(1.0 / projectedVertex.w);\n"
"vec4 projCoord = projectedVertex * tmp;\n"
"projCoord += vec4(1.0);\n"
"projCoord *= vec4(0.5);\n"
"projCoord = clamp(projCoord, 0.001, 0.999);\n"
"vec2 texCoord = vec2(projCoord);\n"

"vec2 dir = vec2(1.0, 1.0)*0.08;\n"

"vec4 dudv = (texture2D(water_dudvmap, gl_TexCoord[0].xy * WAVES_SCALE + dir * time)/128.0)- vec4(1.0);\n"

"vec2 shift1                = -clamp( -(texCoord + dudv.xy), 0.001, 0.999 );\n"
"vec2 shift2                = clamp(texCoord - dudv.xy, 1.001 , 1.999);\n"
"vec4 reflection = texture2D(water_reflection, shift1);\n"
"vec4 refr                = texture2D(water_refraction, shift2);\n"
"vec4 wdepth                = texture2D(water_depthmap, texCoord);\n"

"float depthFactor = 0.9;\n"
"float frag_depth = gl_FragDepth;\n"
"float water_depth = wdepth.x;\n"
"float factor = abs(frag_depth-water_depth)*depthFactor;\n"
"float factor3 = factor*factor*factor;\n"

"gl_FragData[0] = reflection*0.5 + (1-factor3)*refr + 0.5*factor3*waterColor;\n"

"gl_FragData[1] = vec4(0.0, 0.0, 0.0, 1.0);\n"

"}";

可以看到反射、折射效果,却没有任何波动,不知道问题出在什么地方。

"vec2 shift1                = -clamp( -(texCoord + dudv.xy), 0.001, 0.999 );\n"
"vec2 shift2                = clamp(texCoord - dudv.xy, 1.001 , 1.999);\n"
我理解的 shift1 和 shift2 都是根据上面的dudv值,获取一个扰动纹理坐标,用于实现波动。从代码上看,感觉是没有问题,但效果就是不对。

请大牛们帮忙看下。

附件是整个程序的源码及资源。

先谢过。
无标题.jpg

该用户从未签到

发表于 2013-11-18 10:33:16 | 显示全部楼层
time是一个需要每帧更新的值,您是否每帧都更新这个uniform了呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表