1 | #version 100 |
---|
2 | precision mediump int; |
---|
3 | precision mediump float; |
---|
4 | |
---|
5 | /* Cel shading vertex program for single-pass rendering |
---|
6 | In this program, we want to calculate the diffuse and specular |
---|
7 | ramp components, and the edge factor (for doing simple outlining) |
---|
8 | For the outlining to look good, we need a pretty well curved model. |
---|
9 | */ |
---|
10 | // Parameters |
---|
11 | attribute vec4 vertex; |
---|
12 | attribute vec3 normal; |
---|
13 | |
---|
14 | uniform vec3 lightPosition; // object space |
---|
15 | uniform vec3 eyePosition; // object space |
---|
16 | uniform vec4 shininess; |
---|
17 | uniform mat4 worldViewProj; |
---|
18 | |
---|
19 | varying float diffuseIn; |
---|
20 | varying float specularIn; |
---|
21 | varying float edge; |
---|
22 | |
---|
23 | void main() |
---|
24 | { |
---|
25 | // calculate output position |
---|
26 | gl_Position = worldViewProj * vertex; |
---|
27 | |
---|
28 | // calculate light vector |
---|
29 | vec3 N = normalize(normal); |
---|
30 | vec3 L = normalize(lightPosition - vertex.xyz); |
---|
31 | vec3 E = normalize(eyePosition - vertex.xyz); |
---|
32 | |
---|
33 | // Calculate diffuse component |
---|
34 | diffuseIn = max(dot(N, L) , 0.0); |
---|
35 | |
---|
36 | // Mask off specular if diffuse is 0 |
---|
37 | if (diffuseIn == 0.0) |
---|
38 | { |
---|
39 | specularIn = 0.0; |
---|
40 | } |
---|
41 | else |
---|
42 | { |
---|
43 | // Calculate specular component |
---|
44 | vec3 H = normalize(L + E); |
---|
45 | specularIn = pow(max(dot(N, H), 0.0), shininess.x); |
---|
46 | } |
---|
47 | |
---|
48 | // Edge detection, dot eye and normal vectors |
---|
49 | edge = max(dot(N, E), 0.0); |
---|
50 | } |
---|