Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Raytracing / 0004-h9-Added-Wireframe-shader.patch
blob: 8c0d3f7bbfdf726690a5c8a51e4ec35aec9981f9 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
From f9cb883ef657806c5de5d5268856f740c151ff69 Mon Sep 17 00:00:00 2001
From: Syndamia <kamen@syndamia.com>
Date: Sun, 26 May 2024 14:42:39 +0300
Subject: [PATCH] [h9] Added Wireframe shader

---
 data/hw9/dragon.hexray    |  86 ++++++++++++++++++++++
 data/hw9/nonconvex.hexray | 119 ++++++++++++++++++++++++++++++
 data/hw9/testmesh.hexray  | 150 ++++++++++++++++++++++++++++++++++++++
 data/hw9/wireframe.hexray |  72 ++++++++++++++++++
 flake.lock                |  27 +++++++
 flake.nix                 |  26 +++++++
 src/geometry.h            |   4 +-
 src/main.cpp              |   2 +-
 src/mesh.cpp              |   2 +
 src/scene.cpp             |   1 +
 src/shading.cpp           |   8 ++
 src/shading.h             |  20 +++++
 12 files changed, 515 insertions(+), 2 deletions(-)
 create mode 100644 data/hw9/dragon.hexray
 create mode 100644 data/hw9/nonconvex.hexray
 create mode 100644 data/hw9/testmesh.hexray
 create mode 100644 data/hw9/wireframe.hexray
 create mode 100644 flake.lock
 create mode 100644 flake.nix

diff --git a/data/hw9/dragon.hexray b/data/hw9/dragon.hexray
new file mode 100644
index 0000000..ba924da
--- /dev/null
+++ b/data/hw9/dragon.hexray
@@ -0,0 +1,86 @@
+//
+// A scene with a "heavy" mesh (the stanford dragon)
+//
+//
+
+// 1. First, some global settings
+GlobalSettings {
+	frameWidth          768
+	frameHeight         512
+	ambientLight        (0.28, 0.30, 0.35)
+	lightPos           (160, 80, 40)
+	lightIntensity      5000
+}
+
+// 2. A camera
+Camera camera {
+	pos          (140, 23, 70)
+	yaw           66
+	pitch        -10
+	roll          0.0
+	fov           90
+	aspectRatio   1.5
+}
+
+Plane floor {
+	y      0
+	limit   128
+}
+
+CheckerTexture checker {
+	color1    (0.5, 0.5, 0.25)
+	color2    (0.25, 0.25, 0.25)
+	scaling 8
+}
+
+Lambert floorDiffuse {
+	color (1, 1, 1)
+	texture  checker
+}
+
+Reflection floorMirror {
+	reflColor (0.05, 0.03, 0.03)
+}
+
+Reflection floorGlossy {
+	glossiness 0.50
+	numSamples 25
+	reflColor (0.06, 0.06, 0.10)
+}
+
+Layered floorShader {
+	layer floorMirror (1, 1, 1)
+	layer floorGlossy (0.05, 0.05, 0.075)
+}
+
+// 3. A floor node, using a plane as a geometry, and a flat shader with a checker texture
+Node floorNode {
+	geometry  floor
+	shader    floorGlossy
+	translate (100, 0, 96)
+}
+
+Mesh dragon {
+	useKDTree true
+	file      "dragon.obj"
+	useSAH    off
+}
+
+Phong white {
+	color (0.7, 0.7, 0.7)
+	exponent 133
+}
+
+Node dragonNode {
+	geometry   dragon
+	shader     white
+	translate  (104, 13.44, 93)
+	rotate     (90, 90, 0)
+	scale      (30, 30, 30)
+}
+
+
+// 5. The cubemap environment:
+CubemapEnvironment environment {
+	folder "../env/forest"
+}
diff --git a/data/hw9/nonconvex.hexray b/data/hw9/nonconvex.hexray
new file mode 100644
index 0000000..bb609ac
--- /dev/null
+++ b/data/hw9/nonconvex.hexray
@@ -0,0 +1,119 @@
+//
+// Sample scene with non-convex geometries
+//
+
+GlobalSettings {
+	frameWidth          640
+	frameHeight         480
+	ambientLight (0, 0, 0)
+	lightPos     (-30, 100, -250)
+	lightIntensity      50000
+	
+}
+
+Camera camera {
+	pos     (0, 50, -50)
+	//pos       (0, 100, -100)
+	aspectRatio   1.33333
+	yaw      0
+	pitch   -30
+	roll     0
+	fov     90
+}
+
+Plane floor {
+	y      2
+}
+
+CheckerTexture checker {
+	color1 (0,   0, 0)
+	color2 (0, 0.5, 1)
+	size    5
+}
+
+Lambert floor_shader {
+	texture checker
+}
+
+Node floor {
+	geometry floor
+	shader   floor_shader
+}
+
+
+Mesh 1 {
+	file "fig1.obj"
+	faceted true
+}
+
+Lambert L1 {
+	color (0.6, 0.5, 0.5)
+}
+
+Node 1 {
+	geometry 1
+	shader  L1
+	scale (10, 10, 10)
+}
+
+/*
+Mesh 2 {
+	file "fig2.obj"
+	faceted true
+}
+
+Lambert L2 {
+	color (0.5, 0.7, 0.5)
+}
+
+Node 2 {
+	geometry 2
+	shader  L2
+	scale (10, 10, 10)
+}
+
+Mesh 3 {
+	file "fig3.obj"
+	faceted true
+}
+
+Lambert L3 {
+	color (0.5, 0.5, 0.7)
+}
+
+Node 3 {
+	geometry 3
+	shader  L3
+	scale (10, 10, 10)
+}
+
+Mesh 4 {
+	file "fig4.obj"
+	faceted true
+}
+
+Lambert L4 {
+	color (0.7, 0.5, 0.5)
+}
+
+Node 4 {
+	geometry 4
+	shader  L4
+	scale (10, 10, 10)
+}
+
+Mesh 5 {
+	file "fig5.obj"
+	faceted true
+}
+
+Lambert L5 {
+	color (0.5, 0.7, 0.7)
+}
+
+Node 5 {
+	geometry 5
+	shader  L5
+	scale (10, 10, 10)
+}
+*/
diff --git a/data/hw9/testmesh.hexray b/data/hw9/testmesh.hexray
new file mode 100644
index 0000000..3ef4847
--- /dev/null
+++ b/data/hw9/testmesh.hexray
@@ -0,0 +1,150 @@
+//
+// An example scene, which represents the same scenery as what the initializeScene()
+// function used to create around the tag 'homework6'
+// Namely:
+//  1) A shiny floor, made of a woody Lambert shader and a mirror layer on top
+//  2) A soccer ball
+//  3) A shiny ball using a glossy shader
+//  4) Three small phong-lit balls, using red, yellow and green colors
+
+// 1. First, some global settings
+GlobalSettings {
+	frameWidth          640
+	frameHeight         480
+	ambientLight (0.08, 0.08, 0.08)
+	lightPos (-750, 1200, -750)
+	lightIntensity 1200000
+}
+
+// 2. A camera
+Camera camera {
+	pos     (90, 160, -320)
+	aspectRatio   1.33333
+	yaw      15
+	pitch   -5
+	roll     0
+	fov     75
+//	focalPlaneDist 300
+//	fNumber 2.0
+//	dof     false
+//	numSamples 100
+}
+
+// 3. The floor node:
+Plane floor {
+	y      -0.01
+	limit    200
+}
+
+BitmapTexture wood_tex {
+	file "../texture/wood.bmp"
+	scaling 400
+}
+
+Lambert floor_diffuse {
+	texture wood_tex
+}
+
+Reflection mirror {
+}
+
+Fresnel mirror_fresnel {
+	ior 1.33
+}
+
+Layered floor_shader {
+	layer floor_diffuse (1, 1, 1)
+	layer mirror        (1, 1, 1) mirror_fresnel
+}
+
+Node floor {
+	geometry floor
+	shader   floor_shader
+}
+
+Reflection refl {
+}
+
+Refraction refr {
+	ior 1.6
+}
+
+Fresnel fresnel {
+	ior 1.6
+}
+
+Layered glass {
+	layer refr (1, 1, 1)
+	layer refl (1, 1, 1) fresnel
+}
+
+// 4. The mesh:
+Mesh mesh {
+//	file        "geom/3pyramid.obj"
+//	file        "geom/teapot_lowres.obj"
+//	file        "geom/heart.obj"
+	file		"../geom/newwine.obj"
+//	file	 	"geom/truncated_cube.obj"
+//	backfaceCulling false
+	faceted     false
+}
+
+CheckerTexture checker {
+	color1 (0.70, 0.70, 0.70)
+	color2 (0.15, 0.15, 0.15)
+	scaling 100
+}
+
+Lambert checker_shader {
+	texture checker
+}
+
+BitmapTexture heart_lava {
+	file "../texture/zar-texture.bmp"
+}
+
+Lambert heart_shader {
+	texture heart_lava
+}
+
+Reflection reflection {
+}
+
+Refraction refraction {
+	ior 1.5
+}
+
+Fresnel fresnel {
+	ior 1.5
+}
+
+Layered glass {
+	layer refraction (1, 1, 1)
+	layer reflection (1, 1, 1) fresnel
+}
+
+CheckerTexture checker1 {
+	color1 (0.2, 0.2, 0.2)
+	color2 (0.8, 0.8, 0.8)
+	scaling 10
+}
+
+Lambert lambert {
+	color (0.7, 0.7, 0.7)
+	texture checker
+}
+
+Node soccer_ball {
+	geometry     mesh
+	shader       glass
+	scale        (230, 230, 230)
+	rotate       (135, 0, 0)
+	translate    (0, 0, 0)
+//	bump bumptex
+}
+
+
+// 7. And, finally, an environment
+CubemapEnvironment {
+	folder "../env/forest"
+}
diff --git a/data/hw9/wireframe.hexray b/data/hw9/wireframe.hexray
new file mode 100644
index 0000000..0f0a54b
--- /dev/null
+++ b/data/hw9/wireframe.hexray
@@ -0,0 +1,72 @@
+//
+// A sample scene for the homeworks for Lecture 9
+//
+
+// 1. First, some global settings
+GlobalSettings {
+	frameWidth          640
+	frameHeight         480
+	ambientLight        (0.28, 0.30, 0.35)
+	lightPos            (160, 80, 33)
+	lightIntensity      10000
+}
+
+// 2. A camera
+Camera camera {
+	pos          (120, 33, 40)
+	yaw           10
+	pitch         -15
+	roll          0.0
+	fov           90
+	aspectRatio   1.333
+}
+
+Plane floor {
+	y        0
+	limit 128
+}
+
+CheckerTexture checker {
+	color1    (0.5, 0.5, 0.25)
+	color2    (0.25, 0.25, 0.25)
+	scaling    8
+}
+
+Lambert floorShader {
+	color (1, 1, 1)
+	texture  checker
+}
+
+// 3. A floor node, using a plane as a geometry, and a flat shader with a checker texture
+Node floorNode {
+	geometry  floor
+	shader    floorShader
+	translate (100, 0, 96)
+}
+
+Mesh heart {
+	file              "../geom/heart.obj"
+}
+
+Lambert white_flat {
+	color (0.7, 0.7, 0.7)
+}
+
+Wireframe wireframe {
+	wireSize  0.08
+	wireColor (0, 1, 0)
+	outColor  (0, 0, 0)
+}
+
+Node heartNode {
+	geometry    heart
+	shader      wireframe
+	translate   (100, 12.1, 80)
+	rotate      (120, 0, 0)
+	scale       (6, 6, 6)
+}
+
+// 5. The cubemap environment:
+CubemapEnvironment environment {
+	folder "../env/forest"
+}
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/geometry.h b/src/geometry.h
index 4af6360..e7b4a8c 100644
--- a/src/geometry.h
+++ b/src/geometry.h
@@ -37,6 +37,8 @@ struct IntersectionInfo {
     Vector dNdx, dNdy;
     double u, v;
     Geometry *geom;
+	double lambda2;
+	double lambda3;
 };
 
 /**
@@ -133,4 +135,4 @@ public:
 class CSGDiff: public CSGBase { // A - B
 public:
     virtual bool inside(bool inA, bool inB) override { return inA && !inB; }
-};
\ No newline at end of file
+};
diff --git a/src/main.cpp b/src/main.cpp
index 780804c..93a18ea 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -183,7 +183,7 @@ bool renderStatic()
 	return render(true);
 }
 
-const char* DEFAULT_SCENE = "data/heightfield.hexray";
+const char* DEFAULT_SCENE = "data/hw9/wireframe.hexray";
 
 int main(int argc, char** argv)
 {
diff --git a/src/mesh.cpp b/src/mesh.cpp
index 2f6da05..86a6b0b 100644
--- a/src/mesh.cpp
+++ b/src/mesh.cpp
@@ -160,8 +160,10 @@ bool Mesh::intersectTriangle(const Ray& ray, const Triangle& t, IntersectionInfo
 	double rDcr = 1/Dcr;
 	double lambda2 = det(H, AC, -ray.dir) * rDcr;
 	if (lambda2 < 0 || lambda2 > 1) return false;
+	info.lambda2 = lambda2;
 	double lambda3 = det(AB, H, -ray.dir) * rDcr;
 	if (lambda3 < 0 || lambda3 > 1) return false;
+	info.lambda3 = lambda3;
 	double lambda1 = 1 - (lambda2 + lambda3);
 	if (lambda1 < 0 || lambda1 > 1) return false;
 	double gamma = det(AB, AC, H) * rDcr;
diff --git a/src/scene.cpp b/src/scene.cpp
index 149320e..5ac4633 100644
--- a/src/scene.cpp
+++ b/src/scene.cpp
@@ -804,6 +804,7 @@ SceneElement* DefaultSceneParser::newSceneElement(const char* className)
 	if (!strcmp(className, "Heightfield")) return new Heightfield;
 	if (!strcmp(className, "BumpTexture")) return new BumpTexture;
 	if (!strcmp(className, "Bumps")) return new Bumps;
+	if (!strcmp(className, "Wireframe")) return new Wireframe;
 	if (!strcmp(className, "Const")) return new Const;
 
 	return NULL;
diff --git a/src/shading.cpp b/src/shading.cpp
index 25f3c80..f4d3f0a 100644
--- a/src/shading.cpp
+++ b/src/shading.cpp
@@ -270,3 +270,11 @@ void Bumps::modifyNormal(IntersectionInfo& data)
 		data.norm.normalize();
 	}
 }
+
+Color Wireframe::computeColor(Ray ray, const IntersectionInfo& info) {
+	double lambda1 = 1.0 - info.lambda2 - info.lambda3;
+	if (lambda1 < wireSize || info.lambda2 < wireSize || info.lambda3 < wireSize) {
+		return wireColor;
+	}
+	return outColor;
+}
diff --git a/src/shading.h b/src/shading.h
index 95e26c5..5a77b71 100644
--- a/src/shading.h
+++ b/src/shading.h
@@ -199,3 +199,23 @@ public:
 	}
 
 };
+
+class Wireframe : public Shader {
+public:
+	double wireSize = 0.08;
+	Color wireColor = Color(0, 1, 0);
+	Color outColor  = Color(0, 0, 0);
+	Wireframe(double wireSize = 0.08,
+	          Color wireColor = Color(0, 1, 0),
+	          Color outColor  = Color(0, 0, 0)) :
+		wireSize(wireSize), wireColor(wireColor), outColor(outColor)
+	{}
+	Color computeColor(Ray ray, const IntersectionInfo& info) override;
+
+	void fillProperties(ParsedBlock& pb) override
+	{
+		pb.getDoubleProp("wireSize", &wireSize);
+		pb.getColorProp("wireColor", &wireColor);
+		pb.getColorProp("outColor", &outColor);
+	}
+};
-- 
2.42.0