您的位置:首页 > 电脑网络 > 电脑配件 > Tutorial 10-Toon Shading

Tutorial 10-Toon Shading

luyued 发布于 2011-02-06 03:06   浏览 N 次  
Opengl着色语言指南

E文原址: http://www.clockworkcoders.com/oglsl

作者: Martin Christen, christen@clockworkcoders.com;

译:薛现伟, xue_long_0@163.com

Tutorial 10-Toon Shading


第十课:阴影



Toon Shading: Using a step function to define visible colors (usually 3-5 colors).
阴影:使用步进函数定义可见色(通常3-5种颜色).
I implemented the step functions in the code without using 1D Textures and I decided to use constant eye and light coordinates to make the example as simple as possible, it would be better to use uniforms here.

我实现的步进函数没有使用一维纹理,为了使例子尽可能简单我定义眼睛和光照的坐标为常量,如果使用uniform变量的话效果会更好.

Vertex Shader

 varying vec3 vNormal; 
 varying vec3 vVertex; 
              
 void main(void) 
 { 
  vVertex = gl_Vertex.xyz; 
  vNormal = gl_Normal; 
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 
 } 

Fragment Shader

   
 varying vec3 vNormal; 
 varying vec3 vVertex; 
 #define shininess 20.0 
 void main (void) 
 { 
           
 // Material Color: 
 vec4 color0 = vec4(0.8, 0.0, 0.0, 1.0); 
           
 // Silhouette Color: 
 vec4 color1 = vec4(0.0, 0.0, 0.0, 1.0); 
           
 // Specular Color: 
 vec4 color2 = vec4(0.8, 0.0, 0.0, 1.0); 
              
 // Lighting 
 vec3 eyePos = vec3(0.0,0.0,5.0); 
 vec3 lightPos = vec3(0.0,5.0,5.0); 
 vec3 Normal = normalize(gl_NormalMatrix * vNormal); 
 vec3 EyeVert = normalize(eyePos - vVertex); 
 vec3 LightVert = normalize(lightPos - vVertex); 
 vec3 EyeLight = normalize(LightVert+EyeVert); 
 // Simple Silhouette 
 float sil = max(dot(Normal,EyeVert), 0.0); 
 if (sil < 0.3) gl_FragColor = color1; 
 else  
   { 
    gl_FragColor = color0; 
    // Specular part 
    float spec = pow(max(dot(Normal,EyeLight),0.0), shininess); 
    if (spec < 0.2) gl_FragColor *= 0.8; 
    else gl_FragColor = color2; 
    // Diffuse part 
    float diffuse = max(dot(Normal,LightVert),0.0); 
    if (diffuse < 0.5) gl_FragColor *=0.8;
}
}
   


MSN空间完美搬家到新浪博客!

上一篇:2011年01月30日 下一篇:2011年01月29日
图文资讯
广告赞助商