图形着色器——理论与实践(第2版)
这个书在一些翻译上比较坑,而且案例都是都不完整,更重要的是中文翻译版本竟然没有书的网站也就是说没有案例代码。
所以在此我说一下网址:http://cgeducation.org/ShadersBookSecond/Source/
Demo.glib
Ortho -1. 1. -1. 1 .. 1 1000.
Vertex demo.vertFragment demo.fragProgram DomeColor 1. .5 0.QuadXY -2. 5. 200 200
demo.vert
in vec4 aVertex;in vec4 aColor;varying float vLightIntensity;
varying vec3 vColor;vec3 LIGHTPOS =vec3(0.,2.,0.);vec3 ECpos;void main(void){ vec4 thisPos=aVertex; vColor=aColor.rgb;float thisX=thisPos.x;
float thisY=thisPos.y; vec2 thisXY=thisPos.xy; thisPos.z=0.3*sin(dot(thisXY,thisXY)); //计算法线,求微分 vec3 xtangent=vec3(1.,0.,0.); xtangent.z=2.*0.3*thisX*cos(dot(thisXY,thisXY)); vec3 ytangent=vec3(0.,1.,0.); ytangent.z=2.*0.3*thisY*cos(dot(thisXY,thisXY));vec3 thisNormal=normalize(cross(xtangent,ytangent));
vec3 ECpos=vec3(gl_ModelViewMatrix*thisPos); vLightIntensity=dot(normalize(LIGHTPOS-ECpos),thisNormal); vLightIntensity=0.3+abs(vLightIntensity); //环境光 vLightIntensity=clamp(vLightIntensity,0.0,1.0); gl_Position=gl_ModelViewProjectionMatrix*thisPos;}
demo.frag
in vec4 vColor;in float vLightIntensity;
void
main(void){ gl_FragColor = vColor*vLightIntensity;}