]> Dogcows Code - chaz/rasterize/blobdiff - raster.c
add scene lighting constructs; real stdin support
[chaz/rasterize] / raster.c
index 23b1d69d19f5f0f855ec843ff4532ee379ed6fa7..aca2c5a860315c0cc70f44c8cab3069fb1ce0441 100644 (file)
--- a/raster.c
+++ b/raster.c
@@ -30,14 +30,13 @@ struct raster
     list_t*     lights;
     color_t     ambient;
     vec_t       eye;
+    color_t     specular;
+    scal_t      shininess;
 #endif
-#if RASTER_STATS
+#if VERBOSITY >= 2
     unsigned    total;
     unsigned    clipped;
     unsigned    culled;
-#define IF_RASTER_STATS(X) X
-#else
-#define IF_RASTER_STATS(X)
 #endif
 };
 
@@ -57,8 +56,10 @@ raster_t* raster_alloc(int width, int height, color_t fill)
     p->dirty = false;
 
 #if LIGHTING
-    p->ambient = color_new(S(0.05), S(0.05), S(0.05), S(1.0));
+    p->ambient = color_new(S(0.2), S(0.2), S(0.2), S(1.0));
     p->lights = NULL;
+    p->specular = COLOR_WHITE;
+    p->shininess = S(1.0);
 #endif
  /*= light_new(COLOR_WHITE, vec_new(S(-2.0), S(4.0), S(0.0)));*/
     p->zbuf = (scal_t*)mem_alloc(sizeof(scal_t) * size);
@@ -73,13 +74,16 @@ void raster_destroy(raster_t* p)
 {
     mem_free(p->pixels);
     mem_free(p->zbuf);
+#if LIGHTING
+    list_destroy(&p->lights);
+#endif
     mem_free(p);
 }
 
 
 void raster_printstats(raster_t* p)
 {
-#if RASTER_STATS
+#if VERBOSITY >= 2
     unsigned drawn = p->total - p->clipped - p->culled;
     float percent = 100.0f * (float)drawn / (float)p->total;
     printf("culled\t%u\n"
@@ -96,7 +100,7 @@ void raster_clear(raster_t* p, color_t fill)
     for (int i = 0; i < size; ++i) {
         p->pixels[i] = fill;
     }
-#if RASTER_STATS
+#if VERBOSITY >= 2
     p->total = 0;
     p->clipped = 0;
     p->culled = 0;
@@ -139,15 +143,29 @@ void raster_eye(raster_t* p, vec_t eye)
 #endif
 }
 
+void raster_ambient(raster_t* p, color_t ambient)
+{
+#if LIGHTING
+    p->ambient = ambient;
+#endif
+}
+
 void raster_light(raster_t* p, light_t light)
 {
 #if LIGHTING
-    light_t* l = (light_t*)mem_alloc(sizeof(light_t));
-    memcpy(l, &light, sizeof(light_t));
+    light_t* l = light_copy(light);
     list_push2(&p->lights, l, mem_free);
 #endif
 }
 
+void raster_material(raster_t* p, color_t specular, scal_t shininess)
+{
+#if LIGHTING
+    p->specular = specular;
+    p->shininess = shininess;
+#endif
+}
+
 
 #define _CHECK_WRITE(X) if ((X) <= 0) goto fail
 
@@ -293,7 +311,7 @@ color_t _get_vertex_color(raster_t* p, vert_t vert)
     for (list_t* i = p->lights; i; i = i->link) {
         light_t light = *(light_t*)i->val;
         vec_t   mpos = vert.v;
-        vec_t   lpos = light.position;
+        vec_t   lpos = light.v;
         vec_t   vpos = p->eye;
         vec_t   n = vert.n;
         vec_t   l = vec_normalize(vec_sub(lpos, mpos));
@@ -301,23 +319,13 @@ color_t _get_vertex_color(raster_t* p, vert_t vert)
         vec_t   v = vec_normalize(vec_sub(vpos, mpos));
 
         scal_t  kd = scal_max(vec_dot(l, n), S(0.0));
-        color_t Id = color_new(
-            light.color.r * vert.c.r * kd,
-            light.color.g * vert.c.g * kd,
-            light.color.b * vert.c.b * kd,
-            S(1.0)
-        );
-        scal_t  ks = scal_pow(scal_max(vec_dot(r, v), S(0.0)), S(64.0));
-        color_t Is = color_new(
-            light.color.r * COLOR_WHITE.r * ks,
-            light.color.g * COLOR_WHITE.g * ks,
-            light.color.b * COLOR_WHITE.b * ks,
-            S(1.0)
-        );
+        color_t Id = color_scale2(light.d, vert.c, kd);
+        scal_t  ks = scal_pow(scal_max(vec_dot(r, v), S(0.0)), p->shininess);
+        color_t Is = color_scale2(light.s, p->specular, ks);
 
         color = color_add2(color, Id, Is);
     }
-    color_t Ia = p->ambient;
+    color_t Ia = color_mult(p->ambient, vert.c);
     return color_clamp(color_add(color, Ia));
 #else
     return vert.c;
@@ -357,7 +365,7 @@ void raster_draw_tri(raster_t* p, const tri_t* triangle)
     }
 
     tri_t   temp = tri_transform(*triangle, p->model);
-#if LIGHTING && (!FIND_NORMALS || (!SMOOTH_COLOR && FIND_NORMALS == 2))
+#if LIGHTING && (!PRE_NORMALS || (!SMOOTH_COLOR && PRE_NORMALS >= 2))
     temp.a.n = temp.b.n = temp.c.n = vec_normalize(tri_normal(temp));
 #endif
 #if !SMOOTH_COLOR
This page took 0.042594 seconds and 4 git commands to generate.