git.haldean.org subd / f9f1837
Cel shading: hit 'c' to activate Will Brown 9 years ago
6 changed file(s) with 120 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
1515 n: Toggle vertex normal drawing (must be in vertex normal mode)
1616 v: Toggle vertex drawing (this is very expensive for large meshes)
1717 h: Toggle hull drawing at subdivision levels > 0
18 c: Toggle cel shading
1819 1: Use face normals only
1920 2: Set vertex normals to be the average of their adjacent face normals
2021 <: Decrease subdivision level
0 /* vim: set filetype=c : */
1 varying vec3 N;
2 varying vec3 v;
3
4 #define MAX_LIGHTS 3
5 void main (void) {
6 vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0);
7
8 for (int i=0;i<MAX_LIGHTS;i++) {
9 vec3 L = normalize(gl_LightSource[i].position.xyz - v);
10 vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)
11 vec3 R = normalize(-reflect(L,N));
12
13 //calculate Ambient Term:
14 vec4 Iamb = gl_FrontLightProduct[i].ambient;
15 //calculate Diffuse Term:
16 vec4 Idiff = gl_FrontLightProduct[i].diffuse * max(dot(N,L), 0.0);
17 Idiff = clamp(Idiff, 0.0, 1.0);
18
19 // calculate Specular Term:
20 vec4 Ispec = gl_FrontLightProduct[i].specular
21 * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
22 Ispec = clamp(Ispec, 0.0, 1.0);
23
24 finalColor += Iamb + Idiff + Ispec;
25 }
26
27 finalColor += gl_FrontLightModelProduct.sceneColor;
28
29 // calculate apparent brightness of color
30 float bright = (finalColor[0] + finalColor[1] + finalColor[2]) / 3.0;
31
32 float alpha = finalColor[3];
33 if (dot(N,normalize(v)) > -0.35 || bright < 1e-5)
34 gl_FragColor = vec4(0., 0., 0., alpha);
35 else if (bright < 0.2)
36 gl_FragColor = vec4(.4, .2, 0., alpha);
37 else if (bright < 0.5)
38 gl_FragColor = vec4(.8, .4, 0., alpha);
39 else if (bright < 0.7)
40 gl_FragColor = vec4(1., .6, 0., alpha);
41 else if (bright < 0.9)
42 gl_FragColor = vec4(1., .8, 0., alpha);
43 else
44 gl_FragColor = vec4(1., 1., 0., alpha);
45 }
153153 opts.drawNormals = false;
154154 opts.drawFaces = true;
155155 opts.drawVerteces = false;
156 opts.drawHull = true;
156 opts.drawHull = false;
157
158 opts.useCelShader = false;
157159
158160 opts.normalColor[0] = 1.;
159161 opts.normalColor[1] = .5;
99 bool drawFaces;
1010 bool drawVerteces;
1111 bool drawHull;
12 bool useCelShader;
1213 GLfloat edgeColor[4];
1314 GLfloat meshColor[4];
1415 GLfloat normalColor[4];
2424
2525 // Parameters
2626
27 GLuint celProgram;
2728 drawopts drawOptions;
2829 GLfloat camRotX, camRotY, camPosX, camPosY, camPosZ;
2930 GLboolean isSmooth;
4142
4243 mesh *globalMesh;
4344 void drawGlobalMesh() {
45 if (drawOptions.useCelShader) {
46 glUseProgram(celProgram);
47 } else {
48 glUseProgram(0);
49 }
4450 drawMesh(*globalMesh, drawOptions);
4551 if (subdivLevel > 0) drawHull(*subdivLevels[0], drawOptions);
4652 }
7682 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
7783 }
7884
85 void initShaders() {
86 ifstream vert_file("vert.glsl");
87 if (!vert_file.good()) {
88 cout << "Could not open vertex shader (vert.glsl)" << endl;
89 exit(-1);
90 }
91 string vert_source((istreambuf_iterator<char>(vert_file)), istreambuf_iterator<char>());
92
93 ifstream frag_file("frag.glsl");
94 if (!frag_file.good()) {
95 cout << "Could not open fragment shader (frag.glsl)" << endl;
96 exit(-1);
97 }
98 string frag_source((istreambuf_iterator<char>(frag_file)), istreambuf_iterator<char>());
99
100
101 unsigned int vert_shader = glCreateShader(GL_VERTEX_SHADER),
102 frag_shader = glCreateShader(GL_FRAGMENT_SHADER);
103 const char *vs = vert_source.c_str(), *fs = frag_source.c_str();
104 glShaderSource(vert_shader, 1, (const char **) &vs, NULL);
105 glShaderSource(frag_shader, 1, (const char **) &fs, NULL);
106
107 GLint shader_compiled;
108
109 glCompileShader(vert_shader);
110 glGetShaderiv(vert_shader, GL_COMPILE_STATUS, &shader_compiled);
111 if (shader_compiled == GL_FALSE) {
112 cout << "Failed to compile vertex shader." << endl;
113 char err[512];
114 glGetShaderInfoLog(vert_shader, 512, NULL, err);
115 cout << err << endl;
116 exit(-1);
117 }
118
119 glCompileShader(frag_shader);
120 glGetShaderiv(frag_shader, GL_COMPILE_STATUS, &shader_compiled);
121 if (shader_compiled == GL_FALSE) {
122 cout << "Failed to compile fragment shader." << endl;
123 char err[512];
124 glGetShaderInfoLog(frag_shader, 512, NULL, err);
125 cout << err << endl;
126 exit(-1);
127 }
128
129 unsigned int program = glCreateProgram();
130 glAttachShader(program, vert_shader);
131 glAttachShader(program, frag_shader);
132
133 glLinkProgram(program);
134 celProgram = program;
135 }
136
79137 void setCamera() {
80138 glTranslatef(0, 0, camPosZ);
81139 glRotatef(camRotX, 1, 0, 0);
92150 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
93151
94152 initLights();
153 initShaders();
95154
96155 // Place Camera
97156 camRotX = 350.0f;
165224 } else {
166225 glShadeModel(GL_FLAT);
167226 }
227 } else if (key == 'c') {
228 drawOptions.useCelShader = !drawOptions.useCelShader;
168229
169230 } else if (key == 'g') {
170231 drawOptions.drawEdges = !drawOptions.drawEdges;
0 varying vec3 N;
1 varying vec3 v;
2
3 void main(void) {
4 v = vec3(gl_ModelViewMatrix * gl_Vertex);
5 N = normalize(gl_NormalMatrix * gl_Normal);
6 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
7 }