diff --git a/src/charles.cc b/src/charles.cc index f41a268..09d2d9d 100644 --- a/src/charles.cc +++ b/src/charles.cc @@ -9,6 +9,7 @@ #include "basics.h" #include "light.h" +#include "material.h" #include "object_sphere.h" #include "scene.h" #include "writer_png.h" @@ -22,10 +23,20 @@ main(int argc, { Scene scene = Scene(); + Material *m1 = new Material(); + m1->set_color(Color(1, 0, 0, 1)); + Material *m2 = new Material(); + m2->set_color(Color(0, 1, 0, 1)); + Material *m3 = new Material(); + m3->set_color(Color(0, 0, 1, 1)); + // Make some spheres. Sphere *s1 = new Sphere(Vector3(233, 290, 0), 100.0); Sphere *s2 = new Sphere(Vector3(407, 290, 0), 100.0); Sphere *s3 = new Sphere(Vector3(320, 140, 0), 100.0); + s1->set_material(m1); + s2->set_material(m2); + s3->set_material(m3); scene.add_shape(s1); scene.add_shape(s2); scene.add_shape(s3); diff --git a/src/material.h b/src/material.h index 4f28fa0..c6b51de 100644 --- a/src/material.h +++ b/src/material.h @@ -14,7 +14,7 @@ class Material { public: - Color get_color(); + Color get_color() const; void set_color(const Color &c); private: diff --git a/src/object.cc b/src/object.cc index 5aff30f..44afa20 100644 --- a/src/object.cc +++ b/src/object.cc @@ -10,6 +10,7 @@ #include #include "basics.h" +#include "material.h" #include "object.h" #pragma mark - Objects @@ -74,46 +75,22 @@ Shape::Shape(Vector3 o) : Object(o) { } -/* - * sphere_point_lies_on_surface -- - * - * Determine if a point lies on the given sphere. - */ -#if 0 -int -sphere_point_lies_on_surface(Object *obj, Vector3 p) -{ - assert(obj != NULL && obj->type == ObjectTypeSphere); - - Vector3 loc = object_get_location(obj); - float x = p.x - loc.x; - float y = p.y - loc.y; - float z = p.z - loc.z; - float r = object_sphere_get_radius(obj); - - return (x * x) + (y * y) + (z * z) == (r * r); -} -#endif - /* - * sphere_compute_normal -- + * Shape::get_material -- + * Shape::set_material -- * - * Compute the normal for the given Object (which must be a Sphere) at the given point. This point must lie on the - * surface of the object. + * Get and set the Material applied to this shape. */ -#if 0 -/* static */ Vector3 -sphere_compute_normal(Object *obj, Vector3 p) +Material & +Shape::get_material() + const { - assert(obj != NULL && obj->type == ObjectTypeSphere); - - // Make sure the given point is actually on the surface of the sphere. - if (!sphere_point_lies_on_surface(obj, p)) { - return Vector3Zero; - } - - // The fun thing about sphere is the normal to any point on the sphere is the point itself. Woo! - return p; + return *material; +} + +void +Shape::set_material(Material *mat) +{ + material = mat; } -#endif diff --git a/src/object.h b/src/object.h index 40e0d65..4b84e5e 100644 --- a/src/object.h +++ b/src/object.h @@ -10,6 +10,7 @@ #define __OBJECT_H__ #include "basics.h" +#include "material.h" #include "texture.h" @@ -34,9 +35,15 @@ public: Shape(); Shape(Vector3 o); + Material &get_material() const; + void set_material(Material *mat); + virtual int does_intersect(const Ray &ray, float **t) const = 0; virtual bool point_is_on_surface(const Vector3 &p) const = 0; virtual Vector3 compute_normal(const Vector3 &p) const = 0; + +private: + Material *material; }; #endif