From 6815404ba8b5625cedaa22e3abeadc814f263880 Mon Sep 17 00:00:00 2001 From: ubuntu20 Date: Sun, 8 Nov 2020 14:18:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=87=91=E5=B1=9E=E6=9D=90?= =?UTF-8?q?=E8=B4=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Material.cpp | 6 ++++++ Material.h | 6 ++++++ Scene.cpp | 4 +++- Vector3.cpp | 5 +++++ Vector3.h | 1 + 5 files changed, 21 insertions(+), 1 deletion(-) 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;