summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Anholt <eric@anholt.net>2009-06-17 16:02:13 -0700
committerEric Anholt <eric@anholt.net>2009-06-17 16:02:13 -0700
commitf233055d5a520233f782e24dc282b9de03878667 (patch)
tree4279c41fe9f880a1795c39e2cd4f448b305a7f9e
parentf507997d96e0080e8ccca9cba0dfe188629f2e60 (diff)
Stop rendering all those times and just use ward.
-rw-r--r--glass.c33
-rw-r--r--glass.frag172
2 files changed, 59 insertions, 146 deletions
diff --git a/glass.c b/glass.c
index 4ae1182..e536df2 100644
--- a/glass.c
+++ b/glass.c
@@ -58,20 +58,10 @@ struct revolved_object {
int num_verts, num_steps;
};
-enum rendering_mode {
- RENDER_PHONG,
- RENDER_FRESNEL,
- RENDER_COOK_TORRANCE,
- RENDER_WARD_ANISO,
- MAX_RENDER_MODE,
-};
-
enum uniform_list {
UNIFORM_LIGHT_EYE,
UNIFORM_NORMAL_SAMPLER,
UNIFORM_HEIGHTMAP_SAMPLER,
- UNIFORM_RENDER_MODE,
- UNIFORM_RENDER_ITER,
UNIFORM_NI,
UNIFORM_F0,
};
@@ -83,8 +73,6 @@ struct uniform_desc {
[UNIFORM_LIGHT_EYE] = { "light_eye" },
[UNIFORM_NORMAL_SAMPLER] = { "normal_sampler" },
[UNIFORM_HEIGHTMAP_SAMPLER] = { "heightmap_sampler" },
- [UNIFORM_RENDER_MODE] = { "render_mode" },
- [UNIFORM_RENDER_ITER] = { "render_iter" },
[UNIFORM_NI] = { "ni" },
[UNIFORM_F0] = { "F0" },
};
@@ -258,21 +246,11 @@ draw(void)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, ring.vbo);
- for (i = 0; i < MAX_RENDER_MODE; i++) {
- for (j = 0; j < 3; j++) {
- glUniform1f(uniforms[UNIFORM_RENDER_MODE].location, i);
- glUniform1f(uniforms[UNIFORM_RENDER_ITER].location, j);
- glViewport(win_width * j / 3,
- win_height * i / MAX_RENDER_MODE,
- win_width / 3,
- win_height / MAX_RENDER_MODE);
+ glViewport(0, 0, win_width, win_height);
- for (instance = 0; instance < 3; instance++) {
- install_transform(instance);
-
- do_ring_drawelements();
- }
- }
+ for (instance = 0; instance < 3; instance++) {
+ install_transform(instance);
+ do_ring_drawelements();
}
glDisable(GL_VERTEX_ARRAY);
@@ -450,8 +428,7 @@ reshape(int width, int height)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
- gluPerspective(80, (win_width / 3) / ((float)win_height / MAX_RENDER_MODE),
- 0.2, 40);
+ gluPerspective(80, win_width / (float)win_height, 0.2, 40);
glutPostRedisplay();
}
diff --git a/glass.frag b/glass.frag
index 8db3251..6575a0b 100644
--- a/glass.frag
+++ b/glass.frag
@@ -4,7 +4,6 @@ varying vec3 tangent_surf;
varying vec2 texcoord;
uniform sampler2D normal_sampler;
uniform sampler2D heightmap_sampler;
-uniform float render_mode, render_iter;
uniform float F0, ni;
float schlick_fresnel(float n_dot_l)
@@ -42,133 +41,70 @@ void main()
float n_dot_v = dot(n, v);
float n_dot_h = dot(n, h);
float v_dot_h = dot(v, h);
+ float s = .7;
+ float d = 1 - s;
+ float Ii = 0.9; /*intensity of incoming light */
+ float Iia = .1 * Ii; /*intensity of ambient light */
+
+ float cos2_alpha = n_dot_h * n_dot_h;
+ float tan2_alpha = (1 - cos2_alpha) / cos2_alpha;
+ float Rs;
+ float D;
+
+ /* Aniso BRDF from Ward's "Measuring and Modeling
+ * Anisotropic Reflection".
+ */
- if (render_mode == 0) {
- float spec_exponent;
- if (render_iter == 0)
- spec_exponent = 5;
- else if (render_iter == 1)
- spec_exponent = 16;
- else
- spec_exponent = 40;
-
- vec3 diffuse = material_color.xyz * max(n_dot_l, 0);
- float specular = pow(h.z, spec_exponent);
- gl_FragColor = n_dot_l * vec4(diffuse + vec3(specular),
- material_color.w);
- } else if (render_mode == 1) {
- float spec_exponent;
- if (render_iter == 0)
- spec_exponent = 5;
- else if (render_iter == 1)
- spec_exponent = 16;
- else
- spec_exponent = 40;
-
- vec3 diffuse = material_color.xyz * max(n_dot_l, 0);
- float specular = pow(h.z, 16.0);
- float F = schlick_fresnel(n_dot_l);
- gl_FragColor = n_dot_l * vec4(mix(diffuse, vec3(specular), F),
- material_color.w);
- } else {
- float s = .7;
- float d = 1 - s;
- float Ii = 0.9; /*intensity of incoming light */
- float Iia = .1 * Ii; /*intensity of ambient light */
-
- float m = .15 * render_iter;
- float cos2_alpha = n_dot_h * n_dot_h;
- float tan2_alpha = (1 - cos2_alpha) / cos2_alpha;
- float Rs;
- float D;
-
- if (render_mode == 2) {
- /* Beckmann distribution function from Torrance */
- D = exp(-tan2_alpha / (m * m)) /
- (4 * m * m * cos2_alpha * cos2_alpha);
-
- float G1 = 2 * n_dot_h * n_dot_v / v_dot_h;
- float G2 = 2 * n_dot_h * n_dot_l / v_dot_h;
- float G = clamp(min(G1, G2), 0, 1);
-
-#if 0
- float F = fresnel(v_dot_h);
-#else
- float F = schlick_fresnel(n_dot_l);
-#endif
- Rs = 2 * F * D * G / (n_dot_l * n_dot_v);
- } else {
- /* Aniso BRDF from Ward's "Measuring and Modeling
- * Anisotropic Reflection".
- */
-
- /* alpha_x and alpha_y. Values below from Ward */
- float n, m;
-
- if (render_iter == 0) {
- /* rolled brass*/
- n = .05;
- m = .16;
- } else if (render_iter == 1) {
- /* ivory computer plastic */
- n = m = .13;
- } else {
- /* brushed metal */
- n = .037;
- m = .063;
- }
+ /* brushed metal */
+ float ward_n = .037;
+ float ward_m = .063;
- /* Make phi be the angle between the projections of
- * the tangent and half-angle vectors onto the
- * surface plane (z=0). Doing it right would involve
- * projecting onto the plane defined by n.
- */
- float cos_phi = dot(normalize(t.xy), normalize(h.xy));
+ /* Make phi be the angle between the projections of
+ * the tangent and half-angle vectors onto the
+ * surface plane (z=0). Doing it right would involve
+ * projecting onto the plane defined by n.
+ */
+ float cos_phi = dot(normalize(t.xy), normalize(h.xy));
- float cos2_phi_over_m2 = ((cos_phi * cos_phi) /
- (m * m));
- float sin2_phi_over_n2 = ((1 - cos_phi * cos_phi) /
- (n * n));
+ float cos2_phi_over_m2 = ((cos_phi * cos_phi) / (ward_m * ward_m));
+ float sin2_phi_over_n2 = ((1 - cos_phi * cos_phi) / (ward_n * ward_n));
#if 1
- D = exp(-tan2_alpha * (cos2_phi_over_m2 +
- sin2_phi_over_n2));
+ D = exp(-tan2_alpha * (cos2_phi_over_m2 + sin2_phi_over_n2));
#else
- /* Ward's "computationally convenient" equation.
- * Doesn't work.
- */
- D = exp(-2 * (cos2_phi_over_m2 +
- sin2_phi_over_n2) /
- (1 + n_dot_h));
+ /* Ward's "computationally convenient" equation.
+ * Doesn't work.
+ */
+ D = exp(-2 * (cos2_phi_over_m2 +
+ sin2_phi_over_n2) /
+ (1 + n_dot_h));
#endif
- Rs = 2 * schlick_fresnel(n_dot_l) * D /
- sqrt(n_dot_l * n_dot_v) / (m * n);
- }
+ Rs = 2 * schlick_fresnel(n_dot_l) * D /
+ sqrt(n_dot_l * n_dot_v) / (ward_m * ward_n);
- Rs *= step(0, n_dot_l);
- Rs *= step(0, n_dot_v);
+ Rs *= step(0, n_dot_l);
+ Rs *= step(0, n_dot_v);
- float Rd = (1 - F0) * 2;
- /* Ambient occlusion factor -- sample the height map we
- * used to generate the normal map, and reduce intensity in
- * the valleys.
- */
- float heightmap = texture2D(heightmap_sampler, texcoord).x;
- float Ra = Rd * (.8 + .2 * heightmap);
-
- gl_FragColor = n_dot_l * step(0, n_dot_l) *
- vec4(material_color.xyz *
- (Rd * d + Rs * s),
- material_color.w) +
- Iia * Ra * material_color.xyzw;
-
- /* Debugging scalars -- Map [0,1] to [0.5,1] to catch negative
- * values. Multiply by the step function to catch when
- * the scalar won't come into play because Rs == 0.
- */
+ float Rd = (1 - F0) * 2;
+ /* Ambient occlusion factor -- sample the height map we
+ * used to generate the normal map, and reduce intensity in
+ * the valleys.
+ */
+ float heightmap = texture2D(heightmap_sampler, texcoord).x;
+ float Ra = Rd * (.8 + .2 * heightmap);
+
+ gl_FragColor = n_dot_l * step(0, n_dot_l) *
+ vec4(material_color.xyz *
+ (Rd * d + Rs * s),
+ material_color.w) +
+ Iia * Ra * material_color.xyzw;
+
+ /* Debugging scalars -- Map [0,1] to [0.5,1] to catch negative
+ * values. Multiply by the step function to catch when
+ * the scalar won't come into play because Rs == 0.
+ */
#if 0
- gl_FragColor = vec4(vec3(F0 / 2 + .5), 1);
+ gl_FragColor = vec4(vec3(F0 / 2 + .5), 1);
#endif
- }
/* Normal visualization */
/*
vec3 temp = vec3((normal.x + 1) / 2,