|
_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值,获取一个扰动纹理坐标,用于实现波动。从代码上看,感觉是没有问题,但效果就是不对。
请大牛们帮忙看下。
附件是整个程序的源码及资源。
先谢过。
|
|