Browse Source

添加金属材质

master
ubuntu20 4 years ago
parent
commit
6815404ba8
  1. 6
      Material.cpp
  2. 6
      Material.h
  3. 4
      Scene.cpp
  4. 5
      Vector3.cpp
  5. 1
      Vector3.h

6
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;
}

6
Material.h

@ -11,3 +11,9 @@ public:
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);
};

4
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));
}

5
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());

1
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;

Loading…
Cancel
Save