summaryrefslogtreecommitdiff
path: root/src/material.rs
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-02 16:56:59 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-02 16:56:59 +0700
commit93b24b9d01f806cf69ecee86fada9e5b9bf06182 (patch)
treec4c941acdeced6cb5b9cc51eedabc994ceda0b38 /src/material.rs
parentd618968b9577cbaf6306f42157be19bd2a6a6aa0 (diff)
material
Diffstat (limited to 'src/material.rs')
-rw-r--r--src/material.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/material.rs b/src/material.rs
new file mode 100644
index 0000000..2b3deae
--- /dev/null
+++ b/src/material.rs
@@ -0,0 +1,41 @@
+use crate::calculus::calculus::{Ray, Vec3};
+use crate::common::Color;
+use crate::object::HitRecord;
+
+#[derive(Clone)]
+pub enum MaterialType {
+ Diffuse,
+ Metal,
+}
+
+pub struct Material {
+ pub material_type: MaterialType,
+ pub albedo: Color
+}
+
+impl Material {
+ pub fn new(albedo: Color, material_type: MaterialType) -> Self {
+ Self { albedo, material_type }
+ }
+
+ pub fn scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
+ scattered.direction = match self.material_type {
+ MaterialType::Diffuse => {
+ let scatter_direction = rec.normal.add(&Vec3::random_unit());
+ if scatter_direction.is_near_zero() {
+ rec.normal.clone()
+ } else {
+ scatter_direction
+ }
+ }
+ MaterialType::Metal => {
+ r_in.reflect(&rec.normal)
+ }
+ };
+ scattered.origin = rec.position;
+ attenuation.r = self.albedo.r;
+ attenuation.g = self.albedo.g;
+ attenuation.b = self.albedo.b;
+ true
+ }
+} \ No newline at end of file