diff --git a/Material.cpp b/Material.cpp index c5f1c27..dd140fa 100644 --- a/Material.cpp +++ b/Material.cpp @@ -8,3 +8,9 @@ bool LamberMaterial::Scatter(const Ray & input_ray, const HitPoint & hit_point, out_ray.Set(hit_point.mPosition, new_direction, input_ray.mLightAttenuation*mDiffuse); return true; } +bool MetalMaterial::Scatter(const Ray & input_ray, const HitPoint & hit_point, Ray & out_ray) +{ + Vector3 new_direction = Reflect(input_ray.mDirection, hit_point.mNormal); + out_ray.Set(hit_point.mPosition, new_direction, input_ray.mLightAttenuation*mDiffuse); + return true; +} \ No newline at end of file diff --git a/Material.h b/Material.h index a0a2472..44aa762 100644 --- a/Material.h +++ b/Material.h @@ -10,4 +10,10 @@ public: Vector3 mDiffuse; LamberMaterial(const Vector3 diffuse) { mDiffuse = diffuse; } bool Scatter(const Ray& input_ray, const HitPoint& hit_point, Ray& out_ray); +}; +class MetalMaterial : public Material { +public: + Vector3 mDiffuse; + MetalMaterial(const Vector3 diffuse) { mDiffuse = diffuse; } + bool Scatter(const Ray& input_ray, const HitPoint& hit_point, Ray& out_ray); }; \ No newline at end of file diff --git a/Scene.cpp b/Scene.cpp index ea2169a..2a1fcc6 100644 --- a/Scene.cpp +++ b/Scene.cpp @@ -13,7 +13,7 @@ static Camera* sCamera = nullptr; Sphere sphere(Vector3(0.0f, 0.5f, 0.0f), 0.5f); static Object* sRootObject = nullptr; static Material* lambert = nullptr; -static int sSampleCount = 64; +static int sSampleCount = 128; static int sMaxBounceTime = 30; void AddObject(Object* object) { if (sRootObject == nullptr) { @@ -31,7 +31,9 @@ void Init(int width, int height) sCamera = new Camera(45.0f, float(width) / float(height)); sCamera->LookAt(Vector3(3.0f, 1.0f, 3.0f), Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.0f)); Material* earth_material = new LamberMaterial(Vector3(0.5f, 0.3f, 0.1f)); + Material* metal_material = new MetalMaterial(Vector3(0.9f,0.9f,0.9f)); AddObject(new Object(new Sphere(Vector3(0.0f, -1000.0f, 0.0), 1000.0f), earth_material)); + AddObject(new Object(new Sphere(Vector3(-1.0f, 0.5f, 0.3), 0.5f), metal_material)); lambert = new LamberMaterial(Vector3(0.1f, 0.4f, 0.7f)); AddObject(new Object(&sphere, lambert)); } diff --git a/Vector3.cpp b/Vector3.cpp index 5d791a9..7bfc18d 100644 --- a/Vector3.cpp +++ b/Vector3.cpp @@ -71,6 +71,11 @@ Vector3 Cross(const Vector3 & l, const Vector3 & r) return Vector3(l.y*r.z - l.z*r.y, l.z*r.x - l.x*r.z, l.x*r.y - l.y*r.x); } +Vector3 Reflect(const Vector3 & v, const Vector3 & n) +{ + return v - 2.0f * (Dot(v, n) * n); +} + Vector3 UnitRandomVector3InSphere() { Vector3 random_vector(srandf(), srandf(), srandf()); diff --git a/Vector3.h b/Vector3.h index 301334c..e537efc 100644 --- a/Vector3.h +++ b/Vector3.h @@ -38,6 +38,7 @@ public: Vector3 operator*(float f, const Vector3& v); float Dot(const Vector3& l, const Vector3& r); Vector3 Cross(const Vector3& l, const Vector3& r); +Vector3 Reflect(const Vector3& v, const Vector3& n); class Material; struct HitPoint { Vector3 mPosition;