summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Graunke <kenneth@whitecape.org>2010-11-07 01:10:45 -0800
committerKenneth Graunke <kenneth@whitecape.org>2010-11-07 01:10:45 -0800
commit5c119711fa71f1273f1b1771ed03e3c0ed9cb869 (patch)
tree8f783fe47ca57e06c54f2353da26ad0be67e8c1b
parent24a7295dccfd559501753950509964869f88bec1 (diff)
Implement a procedular bump map.
Based off the example shader in the Orange Book, 3rd ed., chapter 11.
-rwxr-xr-xsrc/phong.frag31
-rwxr-xr-xsrc/phong.vert2
2 files changed, 29 insertions, 4 deletions
diff --git a/src/phong.frag b/src/phong.frag
index a43a61d..0d53e85 100755
--- a/src/phong.frag
+++ b/src/phong.frag
@@ -1,20 +1,43 @@
varying vec3 light_ss;
varying vec3 eye_ss;
+varying vec2 uv_coord;
-const vec4 diffuse_color = vec4(1.0, 0.0, 0.0, 1.0);
+const vec3 base_color = vec3(1.0, 0.0, 0.0);
/* Specular exponent.
*/
uniform float s;
+/* Adapted from the Orange Book, 3rd edition, chapter 11. */
+vec3 perturbed_normal()
+{
+ const float BumpDensity = 30.0;
+ const float BumpSize = 0.15;
+
+ /* The incoming texture coordinate (uv_coord) ranges from 0 to 1.
+ * Multiplying by BumpDensity gives coordinates between 0 and 30.
+ * Then, fract gives us a value from 0 to 1...and subtracting 0.5
+ * gives us a perturbation value between -0.5 and 0.5.
+ */
+ vec2 c = BumpDensity * uv_coord;
+ vec2 p = fract(c) - vec2(0.5);
+
+ /* If not in a bump, return an unperturbed normal */
+ if (dot(p,p) >= BumpSize)
+ return vec3(0,0,1);
+
+ /* Otherwise, return the perturbed normal */
+ return normalize(vec3(p, 1));
+}
+
void main(void)
{
vec3 l = normalize(light_ss);
vec3 v = normalize(eye_ss);
vec3 h = normalize(l + v);
- vec3 n = vec3(0, 0, 1);
- vec4 diff = diffuse_color * dot(n, l);
+ vec3 n = perturbed_normal();
+ vec3 diff = base_color * dot(n, l);
float spec = pow(dot(n, h), s);
- gl_FragColor = step(0.0, dot(n, l)) * vec4(diff.xyz + vec3(spec), 1.0);
+ gl_FragColor = step(0.0, dot(n, l)) * vec4(diff + vec3(spec), 1.0);
}
diff --git a/src/phong.vert b/src/phong.vert
index a0c1fab..3a57004 100755
--- a/src/phong.vert
+++ b/src/phong.vert
@@ -11,6 +11,8 @@ varying vec3 light_ss;
// ???
varying vec3 eye_ss;
+varying vec2 uv_coord = uv;
+
/* Position of the light in eye-space.
*/
const vec3 light_pos_es = vec3(0.0, 3.0, 0.0);