Skip to content

Commit 1566afa

Browse files
committed
Revert 3D random gradient code, change structure of article
1 parent ed6b625 commit 1566afa

1 file changed

Lines changed: 27 additions & 42 deletions

File tree

src/volumetric_clouds/main.clj

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,9 @@
2828
[org.lwjgl.opengl GL GL11 GL12 GL13 GL15 GL20 GL30 GL32 GL42]))
2929

3030

31-
;; Procedural generation of volumetric clouds
32-
;;
33-
;; * Midje testing of shaders
34-
;; * Generating OpenGL shaders using templates
35-
;; * cloud shadows
36-
;; * powder function
37-
;; * video showing TDD with tmux
38-
;;
39-
;; References
40-
;; * https://adrianb.io/2014/08/09/perlinnoise.html
41-
;; * https://www.wedesoft.de/software/2023/05/03/volumetric-clouds/
31+
;; # Procedural generation of volumetric clouds
4232

43-
;; # Worley noise
33+
;; ## Worley noise
4434

4535

4636
(defn make-noise-params
@@ -203,12 +193,10 @@
203193

204194
(bufimg/tensor->image worley-norm)
205195

206-
;; # Perlin noise
207-
196+
;; ## Perlin noise
197+
;; https://adrianb.io/2014/08/09/perlinnoise.html
208198

209-
(defmulti random-gradient (fn [& args] (count args)))
210-
211-
(defmethod random-gradient 2
199+
(defn random-gradient
212200
[& args]
213201
(loop [args args]
214202
(let [random-vector (apply vec-n (map (fn [_x] (- (rand 2.0) 1.0)) args))
@@ -218,14 +206,6 @@
218206
(recur args)))))
219207

220208

221-
(defmethod random-gradient 3
222-
[& _args]
223-
(apply vec3
224-
(rand-nth [[1 1 0] [-1 1 0] [1 -1 0] [-1 -1 0]
225-
[1 0 1] [-1 0 1] [1 0 -1] [-1 0 -1]
226-
[0 1 1] [ 0 -1 1] [0 1 -1] [ 0 -1 -1]])))
227-
228-
229209
(defn roughly-vec
230210
[expected error]
231211
(fn [actual]
@@ -245,12 +225,11 @@
245225

246226

247227
(facts "Random gradients"
248-
(with-redefs [rand (constantly 1.5)
249-
rand-nth (fn [x] (first x))]
228+
(with-redefs [rand (constantly 1.5)]
250229
(dtype/shape (random-gradients {:divisions 8 :dimensions 2})) => [8 8]
251230
((random-gradients {:divisions 8 :dimensions 2}) 0 0) => (roughly-vec (vec2 (sqrt 0.5) (sqrt 0.5)) 1e-6)
252231
(dtype/shape (random-gradients {:divisions 8 :dimensions 3})) => [8 8 8]
253-
((random-gradients {:divisions 8 :dimensions 3}) 0 0 0) => (vec3 1 1 0)))
232+
((random-gradients {:divisions 8 :dimensions 3}) 0 0 0) => (vec3 (/ 1 (sqrt 3)) (/ 1 (sqrt 3)) (/ 1 (sqrt 3)))))
254233

255234

256235
(let [gradients (tensor/reshape (random-gradients (make-noise-params 256 8 2)) [(* 8 8)])
@@ -403,12 +382,12 @@
403382

404383
(bufimg/tensor->image perlin-norm)
405384

406-
;; # Combination of Worley and Perlin noise
385+
;; ## Combination of Worley and Perlin noise
407386
(def perlin-worley-norm (dfn/+ (dfn/* 0.3 perlin-norm) (dfn/* 0.7 worley-norm)))
408387

409388
(bufimg/tensor->image (dfn/+ (dfn/* 0.5 perlin-norm) (dfn/* 0.5 worley-norm)))
410389

411-
;; # Interpolation
390+
;; ## Interpolation
412391
(defn interpolate
413392
[tensor & args]
414393
(if (seq args)
@@ -437,7 +416,7 @@
437416
(interpolate y3 2.5 3.5 3.0) => 3.0
438417
(interpolate z3 2.5 3.5 5.5) => 2.0))
439418

440-
;; # Octaves of noise
419+
;; ## Octaves of noise
441420
(defn fractal-brownian-motion
442421
[base octaves & args]
443422
(let [scales (take (count octaves) (iterate #(* 2 %) 1))]
@@ -526,7 +505,7 @@
526505
(bufimg/tensor->image (noise-octaves perlin-worley-norm (octaves 4 0.6) 120 230))
527506

528507

529-
;; # Testing shaders
508+
;; ## Testing shaders
530509

531510
(GLFW/glfwInit)
532511

@@ -704,7 +683,7 @@ void main()
704683
(render-pixel [vertex-test] [fragment-test])
705684

706685

707-
;; # Noise octaves shader
686+
;; ## Noise octaves shader
708687

709688
(def noise-mock
710689
"#version 130
@@ -778,7 +757,7 @@ void main()
778757
1 0 0 [1.0 0.0] 1.0)
779758

780759

781-
;; # Shader for intersecting a ray with a box
760+
;; ## Shader for intersecting a ray with a box
782761

783762
(def ray-box
784763
"#version 130
@@ -830,7 +809,7 @@ void main()
830809
2 0 0 1 0 0 [0.0 0.0])
831810

832811

833-
;; # Shader for light transfer through clouds
812+
;; ## Shader for light transfer through clouds
834813

835814
(def fog
836815
(template/fn [v]
@@ -910,7 +889,7 @@ void main()
910889
0 1 0.5 0.5 [0.393 0.393 0.393 0.393])
911890

912891

913-
;; # Rendering of fog box
892+
;; ## Rendering of fog box
914893

915894
(def fragment-cloud
916895
"#version 130
@@ -969,7 +948,7 @@ void main()
969948
(bufimg/tensor->image (rgba-array->bufimg (render-fog 640 480) 640 480))
970949

971950

972-
;; # Rendering of 3D noise
951+
;; ## Rendering of 3D noise
973952

974953
(defn float-array->texture3d
975954
[data size]
@@ -1026,7 +1005,7 @@ float noise(vec3 idx)
10261005
(bufimg/tensor->image (rgba-array->bufimg (render-noise 640 480 constant-scatter no-shadow (cloud-transfer "noise" 0.01) noise-shader) 640 480))
10271006

10281007

1029-
;; # Remap and clamp 3D noise
1008+
;; ## Remap and clamp 3D noise
10301009

10311010
(def remap-clamp
10321011
"#version 130
@@ -1080,12 +1059,12 @@ float remap_noise(vec3 idx)
10801059
(bufimg/tensor->image (rgba-array->bufimg (render-noise 640 480 constant-scatter no-shadow (cloud-transfer "remap_noise" 0.01) remap-clamp (remap-noise "noise" 0.45 0.9 cloud-strength) noise-shader) 640 480))
10811060

10821061

1083-
;; # Octaves of 3D noise
1062+
;; ## Octaves of 3D noise
10841063

10851064
(bufimg/tensor->image (rgba-array->bufimg (render-noise 640 480 constant-scatter no-shadow (cloud-transfer "remap_noise" 0.01) remap-clamp (remap-noise "octaves" 0.45 0.9 cloud-strength) (noise-octaves (octaves 4 0.5)) noise-shader) 640 480))
10861065

10871066

1088-
;; # Mie scattering
1067+
;; ## Mie scattering
10891068

10901069
(def mie-scatter
10911070
(template/fn [g]
@@ -1129,7 +1108,7 @@ void main()
11291108
(bufimg/tensor->image (rgba-array->bufimg (render-noise 640 480 (mie-scatter 0.76) no-shadow (cloud-transfer "remap_noise" 0.01) remap-clamp (remap-noise "octaves" 0.45 0.9 cloud-strength) (noise-octaves (octaves 4 0.5)) noise-shader) 640 480))
11301109

11311110

1132-
;; # Self-shading of clouds
1111+
;; ## Self-shading of clouds
11331112
(def shadow
11341113
(template/fn [noise step]
11351114
"#version 130
@@ -1152,10 +1131,16 @@ float shadow(vec3 point)
11521131

11531132
(bufimg/tensor->image (rgba-array->bufimg (render-noise 640 480 (mie-scatter 0.76) (shadow "remap_noise" 0.01) (cloud-transfer "remap_noise" 0.01) remap-clamp (remap-noise "octaves" 0.45 0.9 cloud-strength) (noise-octaves (octaves 4 0.5)) noise-shader) 640 480))
11541133

1155-
;; # Tidy up
1134+
;; ## Tidy up
11561135
(GL11/glBindTexture GL12/GL_TEXTURE_3D 0)
11571136
(GL11/glDeleteTextures noise-texture)
11581137

11591138
(GLFW/glfwDestroyWindow window)
11601139

11611140
(GLFW/glfwTerminate)
1141+
1142+
;; ## Further topics
1143+
;; * vertical cloud profile
1144+
;; * powder function
1145+
;; * curl noise
1146+
;; * deep opacity maps https://www.wedesoft.de/software/2023/05/03/volumetric-clouds/

0 commit comments

Comments
 (0)