RenderMonkey 练习 第六天 【OpenGL Water 水效】
不带倒影的水效
1. 新建一个OpenGL 空effect;
2. 添加相关变量
右击Effect节点选择Add Variable->float->predifined->fTime0_X添加时间参数
右击Effect节点选择Add Variable->float->float / float3 添加镜面光强度、灯光位置和相机位置参数:
float3 vec3LightPosition = {-100.0,100.0,100.0};
float3 vec3EyePosition = {0,0,100.0};
右击Effect节点选择AddTexture->Add 2DTexture 添加base和normal两张纹理图片:
base
normal
3. 修改Stream Mapping 中 添加Normal, TexCoord,Tangent
4. 在pass0中添加纹理对象 AddTextureObject 将base和normal图片添加进去,重新命名为baseMap和normalMap
5. 修改vertex shader :
uniform vec3 vec3LightPosition;
uniform vec3 vec3EyePosition;
varying vec3 lightDirection;
varying vec3 viewDirection;
attribute vec3 rm_Tangent;
varying vec3 norm;
varying vec3 lightdir;
varying vec3 eyedir;
varying vec3 halfvec;
uniform float fTime0_X;
void main(void)
{
gl_Position = ftransform();
vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
viewDirection = normalize(vec3EyePosition - fvObjectPosition.xyz);
lightDirection = normalize(vec3LightPosition - fvObjectPosition.xyz);
//模型空间下的TBN
norm = normalize(gl_NormalMatrix * gl_Normal);
vec3 vtangent = normalize(gl_NormalMatrix * rm_Tangent);
vec3 vbinormal = normalize(cross(norm,vtangent));
//将光源向量和视线向量转换到TBN切线空间
lightdir.x = dot(vtangent, lightDirection);
lightdir.y = dot(vbinormal, lightDirection);
lightdir.z = dot(norm , lightDirection);
lightdir = normalize(lightdir);
eyedir.x = dot(vtangent, viewDirection);
eyedir.y = dot(vbinormal, viewDirection);
eyedir.z = dot(norm , viewDirection);
eyedir = normalize(eyedir);
halfvec = normalize(lightdir + eyedir);
gl_TexCoord[0] = gl_MultiTexCoord0;
// 风向为[0.5,0.5]
gl_TexCoord[1].s = gl_MultiTexCoord0.s + fTime0_X*0.01;
gl_TexCoord[1].t = gl_MultiTexCoord0.t + fTime0_X*0.01;
}
6.修改pixel shader:
varying vec3 norm;
varying vec3 lightdir;
varying vec3 eyedir;
varying vec3 halfvec;
uniform sampler2D normalMap;
uniform sampler2D baseMap;
void main(void)
{
//base color
//vec4 _color = texture2D(baseMap, gl_TexCoord[0].xy);
vec3 _normC = texture2D(normalMap, gl_TexCoord[1].xy).xyz;
//调制水底纹理扰动
vec4 _color = texture2D(baseMap, gl_TexCoord[0].xy + _normC * 0.35);
//适当调节参数,摆动幅度
_normC = texture2D(normalMap, gl_TexCoord[1].xy + _normC * 0.01).xyz;
// 将[0,1]范围转到[-1,1]
_normC = normalize((_normC - vec3(0.5)) * 2.0);
float LdotN = max(dot(lightdir, _normC), 0.0);
float HdotN = max(dot(halfvec, _normC), 0.0);
if(HdotN > 0.0)
HdotN = pow(HdotN, 25.0);
vec4 ambient = vec4(0.3,0.3,0.3,1.0);
vec4 diffuse = vec4(0.8,0.8,0.8,1.0);
vec4 specular = vec4(0.6,0.6,0.6,1.0);
gl_FragColor = vec4(ambient.xyz * _color.xyz + diffuse.xyz * LdotN * _color.xyz + specular.xyz * HdotN ,_color.w);
}
7. 效果图