Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Raytracing / 0005-h10-Implemented-SpotLight.patch
blob: f1fbfd6b9e009907b9823ee64a8339674092c86b (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
From 7b19946dab9bc0399214a36dc9067f56c4e0d3a8 Mon Sep 17 00:00:00 2001
From: Syndamia <kamen@syndamia.com>
Date: Mon, 27 May 2024 09:22:23 +0300
Subject: [PATCH] [h10] Implemented SpotLight

---
 data/boxed.hexray | 37 ++++++++++++++++++-------------------
 flake.lock        | 27 +++++++++++++++++++++++++++
 flake.nix         | 26 ++++++++++++++++++++++++++
 src/lights.cpp    | 17 +++++++++++++++++
 src/lights.h      | 19 +++++++++++++++++++
 src/main.cpp      |  2 +-
 src/scene.cpp     |  1 +
 src/vector.h      |  4 ++++
 8 files changed, 113 insertions(+), 20 deletions(-)
 create mode 100644 flake.lock
 create mode 100644 flake.nix

diff --git a/data/boxed.hexray b/data/boxed.hexray
index e128db3..9ed1793 100644
--- a/data/boxed.hexray
+++ b/data/boxed.hexray
@@ -18,25 +18,24 @@ Camera camera {
 //////////////////////// LIGHTS ////////////////////////////////////////////
 
 
-RectLight l2 {
-	xSubd 4
-	ySubd 4
-	translate	(50, 50, -20)
-	scale   (8, 8, 8)
-	color	(1, 1, 1)
-	power   4000
-	rotate  (0, 45, 0)
-}
-
-RectLight l_small {
-	translate  (0, 28, 50)
-	rotate     (0, -62, 0)
-	scale      (10, 10, 10)
-	color	   (1, 1, 1)
-	power      4000
-	xSubd  4
-	ySubd  4
-}
+SpotLight mylight {
+	color   (1, 1, 1)
+	power   7000
+	pos     (50, 50, -20)
+	dir     (-0.727, -0.582, 0.364)
+	innerAngle 13.8
+	outerAngle 15.5
+}
+
+// RectLight l_small {
+// 	translate  (0, 28, 50)
+// 	rotate     (0, -62, 0)
+// 	scale      (10, 10, 10)
+// 	color	   (1, 1, 1)
+// 	power      4000
+// 	xSubd  4
+// 	ySubd  4
+// }
 
 
 //////////////////////// GEOMETRIES ////////////////////////////////////////
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/lights.cpp b/src/lights.cpp
index d1b5a42..f1e6406 100644
--- a/src/lights.cpp
+++ b/src/lights.cpp
@@ -44,3 +44,20 @@ void RectLight::getNthSample(int sampleIdx, const Vector& shadePos, Vector& samp
         color.makeZero();
     }
 }
+
+void SpotLight::getNthSample(int sampleIdx, const Vector& shadePos, Vector& samplePos, Color& color) {
+	samplePos = pos;
+
+	Vector shadeDir = shadePos - pos;
+	double degr = degreesBetween(shadeDir, dir);
+	if (degr < innerAngle) {
+		color = this->color;
+	}
+	else if (degr > outerAngle) {
+		color = Color(0, 0, 0);
+	}
+	else {
+		double alpha = (degr - innerAngle) / (outerAngle - innerAngle);
+		color = (1 - alpha) * this->color + alpha * Color(0, 0, 0);
+	}
+}
diff --git a/src/lights.h b/src/lights.h
index 548b53f..c76b9be 100644
--- a/src/lights.h
+++ b/src/lights.h
@@ -79,3 +79,22 @@ public:
 	void getNthSample(int sampleIdx, const Vector& shadePos, Vector& samplePos, Color& color) override;
 };
 
+
+class SpotLight : public Light {
+	Vector pos;
+	Vector dir;
+	double innerAngle;
+	double outerAngle;
+public:
+	void fillProperties(ParsedBlock& pb) override
+	{
+		Light::fillProperties(pb);
+		pb.getVectorProp("pos", &pos);
+		pb.getVectorProp("dir", &dir);
+		pb.getDoubleProp("innerAngle", &innerAngle);
+		pb.getDoubleProp("outerAngle", &outerAngle);
+	}
+
+	int getNumSamples() const override { return 1; }
+	void getNthSample(int sampleIdx, const Vector& shadePos, Vector& samplePos, Color& color) override;
+};
diff --git a/src/main.cpp b/src/main.cpp
index d523f60..7a181cb 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -222,7 +222,7 @@ bool renderStatic()
 	return render(true);
 }
 
-const char* DEFAULT_SCENE = "data/meshes.hexray";
+const char* DEFAULT_SCENE = "data/boxed.hexray";
 
 int main(int argc, char** argv)
 {
diff --git a/src/scene.cpp b/src/scene.cpp
index f5fbdb0..82c8d98 100644
--- a/src/scene.cpp
+++ b/src/scene.cpp
@@ -799,6 +799,7 @@ SceneElement* DefaultSceneParser::newSceneElement(const char* className)
 	if (!strcmp(className, "Const")) return new Const;
 	if (!strcmp(className, "PointLight")) return new PointLight;
 	if (!strcmp(className, "RectLight")) return new RectLight;
+	if (!strcmp(className, "SpotLight")) return new SpotLight;
 
 	return NULL;
 }
diff --git a/src/vector.h b/src/vector.h
index a21b432..cd140fb 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -199,3 +199,7 @@ inline std::pair<Vector, Vector> othonormedBasis(const Vector& v)
 	result.second = result.first ^ v;
 	return result;
 }
+
+inline double degreesBetween(const Vector& a, const Vector& b) {
+	return acos(dot(normalize(a), normalize(b))) * 180 / M_PI;
+}
-- 
2.42.0