NGL  6.5
The NCCA Graphics Library
ToonShaders.h
Go to the documentation of this file.
1 #ifndef TOONSHADERS_H_
2 #define TOONSHADERS_H_
3 #include <string>
4 
6 R"DELIM(
7  #version 150
8  in vec3 inVert;
9  in vec3 inNormal;
10  in vec2 inUV;
11  uniform mat4 MVP;
12  uniform mat3 normalMatrix;
13  out vec3 normalEyeSpace;
14  void main()
15  {
16  normalEyeSpace = normalMatrix * inNormal;
17  gl_Position = MVP*vec4(inVert,1.0);
18  }
19 )DELIM";
20 
21 // shader modified from http://prideout.net/blog/?p=22
23  R"DELIM(
24  #version 150
25  in vec3 normalEyeSpace;
26  out vec4 FragColor;
27  uniform vec4 ambient;//=vec4(0.1,0.1,0.1,1.0);
28  uniform vec4 Colour;//=vec4(1.0,1.0,1.0,1.0);
29  uniform vec4 specular;//=vec4(1.0,1.0,1.0,1.0);
30  uniform float shininess;//=20;
31  uniform vec3 lightPos;//=vec3(1,1,1);
32 
33 
34  float stepmix(float edge0, float edge1, float E, float x)
35  {
36  float T = clamp(0.5 * (x - edge0 + E) / E, 0.0, 1.0);
37  return mix(edge0, edge1, T);
38  }
39 
40  void main()
41  {
42  vec3 N = normalize(normalEyeSpace);
43  vec3 L = normalize(lightPos);
44  vec3 Eye = vec3(0, 0, 1);
45  vec3 H = normalize(L + Eye);
46  float df = max(0.0, dot(N, L));
47  float sf = max(0.0, dot(N, H));
48  sf = pow(sf, shininess);
49  const float A = 0.1;
50  const float B = 0.3;
51  const float C = 0.6;
52  const float D = 1.0;
53  float E = fwidth(df);
54  if (df > A - E && df < A + E) df = stepmix(A, B, E, df);
55  else if (df > B - E && df < B + E) df = stepmix(B, C, E, df);
56  else if (df > C - E && df < C + E) df = stepmix(C, D, E, df);
57  else if (df < A) df = 0.0;
58  else if (df < B) df = B;
59  else if (df < C) df = C;
60  else df = D;
61  E = fwidth(sf);
62  if (sf > 0.5 - E && sf < 0.5 + E)
63  {
64  sf = smoothstep(0.5 - E, 0.5 + E, sf);
65  }
66  else
67  {
68  sf = step(0.5, sf);
69  }
70  FragColor = ambient + df * Colour + sf * specular;
71  }
72 )DELIM";
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 #endif
86 
const std::string toonVertexShader
Definition: ToonShaders.h:5
const std::string toonFragmentShader
Definition: ToonShaders.h:22
GLsizei const GLchar *const * string
Definition: glew.h:1847