Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Raytracing / 9001-project-easy-Implemented-semi-transparent-refraction.patch
blob: 67dd56c974426a581200b1605e25d4754a93aebe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
From d4c19a8ade2a2f2d8d6f5a2acea1f801170533c1 Mon Sep 17 00:00:00 2001
From: Syndamia <kamen@syndamia.com>
Date: Mon, 1 Jul 2024 15:26:42 +0300
Subject: [PATCH] [project-easy] Implemented semi-transparent refractions

---
 flake.lock      | 27 +++++++++++++++++++++++++++
 flake.nix       | 26 ++++++++++++++++++++++++++
 src/main.cpp    |  2 +-
 src/shading.cpp |  8 +++++++-
 src/shading.h   |  4 ++++
 5 files changed, 65 insertions(+), 2 deletions(-)
 create mode 100644 flake.lock
 create mode 100644 flake.nix

diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..25db693
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,27 @@
+{
+  "nodes": {
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1716061101,
+        "narHash": "sha256-H0eCta7ahEgloGIwE/ihkyGstOGu+kQwAiHvwVoXaA0=",
+        "owner": "nixos",
+        "repo": "nixpkgs",
+        "rev": "e7cc61784ddf51c81487637b3031a6dd2d6673a2",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nixos",
+        "ref": "nixos-23.11",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "root": {
+      "inputs": {
+        "nixpkgs": "nixpkgs"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..d0a99fd
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,26 @@
+# Този файл, заедно с flake.lock са си за моета система, да имам копие ако случайно изтрия нещо
+{
+  description = "hexray raytracer in FMI elective";
+
+  inputs = {
+    nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
+  };
+
+  outputs = inputs:
+  let
+    pkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
+  in {
+    devShells."x86_64-linux".default = pkgs.mkShell {
+      nativeBuildInputs = with pkgs; [
+        cmake
+        pkg-config
+      ];
+
+      buildInputs = with pkgs; [
+        SDL2
+        openexr_3
+        zlib
+      ];
+    };
+  };
+}
diff --git a/src/main.cpp b/src/main.cpp
index fdca0f5..865a271 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -525,7 +525,7 @@ void renderThreadEntry() {
 	}
 }
 
-const char* DEFAULT_SCENE = "data/simple.hexray";
+const char* DEFAULT_SCENE = "data/beer.hexray";
 
 int main(int argc, char** argv)
 {
diff --git a/src/shading.cpp b/src/shading.cpp
index 94c417f..3d63dad 100644
--- a/src/shading.cpp
+++ b/src/shading.cpp
@@ -216,19 +216,25 @@ inline std::optional<Vector> refract(const Vector& i, const Vector& n, float ior
 Color Refraction::computeColor(const Ray& ray, const IntersectionInfo& info)
 {
 	std::optional<Vector> refr;
+	double transmittance = 1.0;
 	if (dot(ray.dir, info.norm) < 0) {
 		// entering the geometry
 		refr = refract(ray.dir, info.norm, 1 / ior);
 	} else {
 		// leaving the geometry
 		refr = refract(ray.dir, -info.norm, ior);
+
+		// Beer-Lambert law
+		double depth = (ray.start - info.ip).length();
+		// If absorption = 0; then transmittance = 1;
+		transmittance = exp(-1.0 * absorption * depth);
 	}
 	if (!refr) return Color(0, 0, 0);
 	Ray newRay = ray;
 	newRay.start = info.ip - faceforward(ray.dir, info.norm) * 0.000001;
 	newRay.dir = refr.value();
 	newRay.depth++;
-	return raytrace(newRay) * refrColor;
+	return raytrace(newRay) * refrColor * transmittance + insideColor * (1.0 - transmittance);
 }
 
 Color Refraction::eval(const IntersectionInfo& x, const Vector& w_in, const Vector& w_out)
diff --git a/src/shading.h b/src/shading.h
index 3a00527..005f203 100644
--- a/src/shading.h
+++ b/src/shading.h
@@ -148,6 +148,8 @@ class Refraction: public Shader {
 public:
 	Color refrColor = Color(0.95f, 0.95f, 0.95f);
 	double ior = 1.33;
+	Color insideColor = Color(0.0, 0.0, 0.0);
+	double absorption = 0.0;
 	virtual Color computeColor(const Ray& ray, const IntersectionInfo& info) override;
 	// from BRDF:
 	Color eval(const IntersectionInfo& x, const Vector& w_in, const Vector& w_out) override;
@@ -160,6 +162,8 @@ public:
             refrColor = Color(multiplier, multiplier, multiplier);
         } else pb.getColorProp("refrColor", &refrColor);
 		pb.getDoubleProp("ior", &ior, 1e-6, 10);
+        pb.getColorProp("insideColor", &insideColor);
+        pb.getDoubleProp("absorption", &absorption);
 	}
 };
 
-- 
2.42.0