本文文件是renderMonkey软件里面的一个案例:RenderMonkey 1.82\Examples\GL2\earth.rfx
纹理原图:
顶点着色器:
// Earth vertex shader computes lighting coefficients.
//
// Note: For optimal light animation set the RenderMonkey cycle time
// for predefined variables to 20 seconds. (Found in the Edit->Preferences).
// predefined variables used to animate light direction
uniform float cos_time_0_2PI, sin_time_0_2PI;
// artist variable to control season
uniform float season;
// varying variables passed to fragment shader
varying float Diffuse;
varying vec3 Specular;
varying vec2 TexCoord;
void main(void)
{
// calculate vertex position in eye coordinates
vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;//<span style="color:#ff0000;">眼睛空间</span>
// compute the transformed normal
vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);
// compute the light vector pointing toward the sun, in model coordinates
// x,y compose the longitude and z the (seasonal) lattitude of the nadir point.
vec3 lightVec = normalize(gl_NormalMatrix * vec3(cos_time_0_2PI, season,sin_time_0_2PI));//<span style="color:#ff0000;">产生旋转</span>
// compute the reflection vector
vec3 reflectVec = reflect(-lightVec, tnorm);
// compute a unit vector in direction of viewing position
vec3 viewVec = normalize(vec3 (-ecPosition));
// Calculate specular light intensity, scale down and
// apply a slightly yellowish tint.
float specIntensity = pow(max(dot(reflectVec, viewVec), 0.0), 8.0);
specIntensity = 0.3 * specIntensity;
Specular = specIntensity * vec3 (1.0, 0.941, 0.898);
// Calculate a diffuse light intensity
Diffuse = dot(lightVec, tnorm);
// Pass texture coordinates fragment shader
TexCoord = vec2 (gl_MultiTexCoord0);
TexCoord.x = TexCoord.x;
// output final vertex information
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;//<span style="color:#ff6666;">裁剪空间</span>
}
片源着色器:
// Earth fragment shader blends day/night/cloud/gloss textures based on local time of day.
uniform sampler2D EarthDay, EarthNight, EarthCloudGloss;
uniform bool cloudCover;
varying float Diffuse;
varying vec3 Specular;
varying vec2 TexCoord;
const float Terminator = 0.3;
const float InvTerminator = 1.0 / (2.0 * Terminator);
void main (void)
{
// separate maps are packed into the color components of this texture
// clouds.r = cloud opacity
// clouds.g = water glossiness
vec2 cg = texture2D(EarthCloudGloss, TexCoord).rg;
float clouds = cg.r * float(cloudCover);
float gloss = cg.g;
// load daytime color, plus a specular component modulated by the gloss map
vec3 daytime = texture2D(EarthDay, TexCoord).rgb * Diffuse + Specular * gloss;//<span style="color:#ff0000;">有水的地方有高光,陆地没有高光</span>
// mix in diffusely-lit clouds, modulated by the cloud opacity
daytime = mix(daytime, vec3(abs(Diffuse)), clouds);//<span style="color:#ff0000;">产生白云,但是我没想明白</span>
// load night image, modulated by cloud opacity
vec3 nighttime = texture2D(EarthNight, TexCoord).rgb * (1.0 - clouds);
// assume day, to start
vec3 color = daytime;
// if fully dark, select night
if (Diffuse < -Terminator)
{
color = nighttime;
}
// within the twilight zone, mix night/day
if (abs(Diffuse) < Terminator )
{
color = mix(nighttime, daytime, (Diffuse + Terminator) * InvTerminator);
}
gl_FragColor = vec4 (color, 1.0);
}
个人感想:每顶点渲染,每片元渲染,今天领悟又进了一步,但是还没彻底明白。