]> Dogcows Code - chaz/rasterize/blobdiff - element.hh
added texturing
[chaz/rasterize] / element.hh
index 7eb898861187b00a90a15d7ab3efbf9c63779910..5130a8ed7fee9ba15fcc2a51dd37219ccaf9f642 100644 (file)
@@ -11,6 +11,7 @@
 #include "color.hh"
 #include "contact.hh"
 #include "light.hh"
+#include "raster.hh"
 #include "ray.hh"
 #include "scene.hh"
 
@@ -23,8 +24,15 @@ namespace rt {
  */
 class element
 {
+    color_t     kd;
+    raster_t*   tx;
+
 public:
 
+    element() :
+        kd(COLOR_WHITE), tx(NULL)
+    {}
+
     virtual ~element()
     {}
 
@@ -38,7 +46,15 @@ public:
      */
     virtual color_t color(vec_t point) const
     {
-        return COLOR_WHITE;
+        return kd;
+    }
+
+    /*
+     * Get texture-coordinates from a surface point.
+     */
+    virtual vec_t txcoord(vec_t point) const
+    {
+        return VEC_ZERO;
     }
 
     /*
@@ -46,8 +62,9 @@ public:
      */
     color_t cast(const contact_t& hit, const scene_t* scene) const
     {
-        color_t Ia = color_mult(scene_ambient(scene), color(hit.p));
-        color_t Id = diffuse(hit, scene);
+        color_t kd = txcolor(hit.p);
+        color_t Ia = color_mult(scene_ambient(scene), kd);
+        color_t Id = diffuse(kd, hit, scene);
         return color_clamp(color_add(Ia, Id));
     }
 
@@ -75,9 +92,8 @@ public:
      * Get the color of the element at a surface point after diffuse
      * lighting.
      */
-    color_t diffuse(const contact_t& hit, const scene_t* scene) const
+    color_t diffuse(color_t kd, const contact_t& hit, const scene_t* scene) const
     {
-        color_t kd = color(hit.p);
         vec_t   n = vec_normalize(hit.n);
 
         color_t c = COLOR_BLACK;
@@ -94,6 +110,31 @@ public:
 
         return c;
     }
+
+    color_t txcolor(vec_t point) const
+    {
+#if TEXTURING
+        if (tx != NULL) {
+            vec_t uv = txcoord(point);
+#if QUIRKS
+            return raster_uv(tx, uv);
+#else
+            return color_mult(color(point), raster_uv(tx, uv));
+#endif
+        }
+#endif
+        return color(point);
+    }
+
+    void material(color_t color)
+    {
+        kd = color;
+    }
+
+    void texture(raster_t* texture)
+    {
+        tx = texture;
+    }
 };
 
 
This page took 0.021982 seconds and 4 git commands to generate.