Skip to content

Commit 18c3d45

Browse files
committed
Changing layout of code to adapt to text width
1 parent 2d93edd commit 18c3d45

1 file changed

Lines changed: 74 additions & 33 deletions

File tree

src/volumetric_clouds/main.clj

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
(:require [clojure.math :refer (PI sqrt cos sin tan to-radians pow)]
1515
[midje.sweet :refer (fact facts tabular => roughly)]
1616
[fastmath.vector :refer (vec2 vec3 add mult sub div mag dot normalize)]
17-
[fastmath.matrix :refer (mat->float-array mulm mulv inverse rotation-matrix-3d-x rotation-matrix-3d-y)]
17+
[fastmath.matrix :refer (mat->float-array mulm mulv inverse
18+
rotation-matrix-3d-x rotation-matrix-3d-y)]
1819
[tech.v3.datatype :as dtype]
1920
[tech.v3.tensor :as tensor]
2021
[tech.v3.datatype.functional :as dfn]
@@ -254,13 +255,19 @@
254255
=> (vec3 (/ 1 (sqrt 3)) (/ 1 (sqrt 3)) (/ 1 (sqrt 3)))))
255256

256257

257-
(let [gradients (tensor/reshape (random-gradients (make-noise-params 256 8 2)) [(* 8 8)])
258-
points (tensor/reshape (tensor/compute-tensor [8 8] (fn [y x] (vec2 x y))) [(* 8 8)])
258+
(let [gradients (tensor/reshape (random-gradients (make-noise-params 256 8 2))
259+
[(* 8 8)])
260+
points (tensor/reshape (tensor/compute-tensor [8 8] (fn [y x] (vec2 x y)))
261+
[(* 8 8)])
259262
scatter (tc/dataset {:x (mapcat (fn [point gradient]
260-
[(point 0) (+ (point 0) (* 0.5 (gradient 0))) nil])
263+
[(point 0)
264+
(+ (point 0) (* 0.5 (gradient 0)))
265+
nil])
261266
points gradients)
262267
:y (mapcat (fn [point gradient]
263-
[(point 1) (+ (point 1) (* 0.5 (gradient 1))) nil])
268+
[(point 1)
269+
(+ (point 1) (* 0.5 (gradient 1)))
270+
nil])
264271
points gradients)})]
265272
(-> scatter
266273
(plotly/base {:=title "Random gradients" :=mode "lines"})
@@ -408,16 +415,20 @@
408415
[{:keys [size dimensions] :as params}]
409416
(let [gradients (random-gradients params)]
410417
(tensor/clone
411-
(tensor/compute-tensor (repeat dimensions size)
412-
(fn [& args]
413-
(let [center (add (apply vec-n (reverse args)) (apply vec-n (repeat dimensions 0.5)))]
414-
(perlin-sample params gradients center)))
415-
:double))))
418+
(tensor/compute-tensor
419+
(repeat dimensions size)
420+
(fn [& args]
421+
(let [center (add (apply vec-n (reverse args))
422+
(apply vec-n (repeat dimensions 0.5)))]
423+
(perlin-sample params gradients center)))
424+
:double))))
416425

417426

418427
(def perlin (perlin-noise (make-noise-params 256 8 2)))
419428

420-
(def perlin-norm (dfn/* (/ 255 (- (dfn/reduce-max perlin) (dfn/reduce-min perlin))) (dfn/- perlin (dfn/reduce-min perlin))))
429+
(def perlin-norm
430+
(dfn/* (/ 255 (- (dfn/reduce-max perlin) (dfn/reduce-min perlin)))
431+
(dfn/- perlin (dfn/reduce-min perlin))))
421432

422433
(bufimg/tensor->image perlin-norm)
423434

@@ -459,12 +470,15 @@
459470
(defn fractal-brownian-motion
460471
[base octaves & args]
461472
(let [scales (take (count octaves) (iterate #(* 2 %) 1))]
462-
(reduce + 0.0 (map (fn [amplitude scale] (* amplitude (apply base (map #(* scale %) args)))) octaves scales))))
473+
(reduce + 0.0
474+
(map (fn [amplitude scale] (* amplitude (apply base (map #(* scale %) args))))
475+
octaves scales))))
463476

464477

465478
(facts "Fractal Brownian motion"
466479
(let [base1 (fn [x] (if (>= (mod x 2.0) 1.0) 1.0 0.0))
467-
base2 (fn [y x] (if (= (Math/round (mod y 2.0)) (Math/round (mod x 2.0))) 0.0 1.0))]
480+
base2 (fn [y x] (if (= (Math/round (mod y 2.0)) (Math/round (mod x 2.0)))
481+
0.0 1.0))]
468482
(fractal-brownian-motion base2 [1.0] 0 0) => 0.0
469483
(fractal-brownian-motion base2 [1.0] 0 1) => 1.0
470484
(fractal-brownian-motion base2 [1.0] 1 0) => 1.0
@@ -487,7 +501,8 @@
487501

488502

489503
(tabular "Remap values of tensor"
490-
(fact ((remap (tensor/->tensor [?value]) ?low1 ?high1 ?low2 ?high2) 0) => ?expected)
504+
(fact ((remap (tensor/->tensor [?value]) ?low1 ?high1 ?low2 ?high2) 0)
505+
=> ?expected)
491506
?value ?low1 ?high1 ?low2 ?high2 ?expected
492507
0 0 1 0 1 0
493508
1 0 1 0 1 1
@@ -580,9 +595,9 @@
580595

581596

582597
(defn make-program-with-shaders
583-
[vertex-shader-sources fragment-shader-sources]
584-
(let [vertex-shaders (map #(make-shader % GL20/GL_VERTEX_SHADER) vertex-shader-sources)
585-
fragment-shaders (map #(make-shader % GL20/GL_FRAGMENT_SHADER) fragment-shader-sources)
598+
[vertex-sources fragment-sources]
599+
(let [vertex-shaders (map #(make-shader % GL20/GL_VERTEX_SHADER) vertex-sources)
600+
fragment-shaders (map #(make-shader % GL20/GL_FRAGMENT_SHADER) fragment-sources)
586601
program (apply make-program (concat vertex-shaders fragment-shaders))]
587602
program))
588603

@@ -674,8 +689,10 @@ void main()
674689
(try
675690
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER fbo#)
676691
(GL11/glBindTexture GL11/GL_TEXTURE_2D ~texture)
677-
(GL32/glFramebufferTexture GL30/GL_FRAMEBUFFER GL30/GL_COLOR_ATTACHMENT0 ~texture 0)
678-
(GL20/glDrawBuffers (volumetric-clouds.main/make-int-buffer (int-array [GL30/GL_COLOR_ATTACHMENT0])))
692+
(GL32/glFramebufferTexture GL30/GL_FRAMEBUFFER GL30/GL_COLOR_ATTACHMENT0
693+
~texture 0)
694+
(GL20/glDrawBuffers (volumetric-clouds.main/make-int-buffer
695+
(int-array [GL30/GL_COLOR_ATTACHMENT0])))
679696
(GL11/glViewport 0 0 ~width ~height)
680697
~@body
681698
(finally
@@ -686,7 +703,8 @@ void main()
686703
(defn setup-point-attribute
687704
[program]
688705
(let [point-attribute (GL20/glGetAttribLocation program "point")]
689-
(GL20/glVertexAttribPointer point-attribute 3 GL11/GL_FLOAT false (* 3 Float/BYTES) (* 0 Float/BYTES))
706+
(GL20/glVertexAttribPointer point-attribute 3 GL11/GL_FLOAT false
707+
(* 3 Float/BYTES) (* 0 Float/BYTES))
690708
(GL20/glEnableVertexAttribArray point-attribute)))
691709

692710

@@ -747,7 +765,8 @@ void main()
747765

748766

749767
(tabular "Test noise mock"
750-
(fact (nth (render-pixel [vertex-test] [noise-mock (noise-probe ?x ?y ?z)]) 0) => ?result)
768+
(fact (nth (render-pixel [vertex-test] [noise-mock (noise-probe ?x ?y ?z)]) 0)
769+
=> ?result)
751770
?x ?y ?z ?result
752771
0 0 0 0.0
753772
1 0 0 1.0
@@ -787,7 +806,9 @@ void main()
787806

788807

789808
(tabular "Test octaves of noise"
790-
(fact (first (render-pixel [vertex-test] [noise-mock (noise-octaves ?octaves) (octaves-probe ?x ?y ?z)]))
809+
(fact (first (render-pixel [vertex-test]
810+
[noise-mock (noise-octaves ?octaves)
811+
(octaves-probe ?x ?y ?z)]))
791812
=> ?result)
792813
?x ?y ?z ?octaves ?result
793814
0 0 0 [1.0] 0.0
@@ -834,7 +855,9 @@ void main()
834855

835856

836857
(tabular "Test intersection of ray with box"
837-
(fact ((juxt first second) (render-pixel [vertex-test] [ray-box (ray-box-probe ?ox ?oy ?oz ?dx ?dy ?dz)]))
858+
(fact ((juxt first second)
859+
(render-pixel [vertex-test]
860+
[ray-box (ray-box-probe ?ox ?oy ?oz ?dx ?dy ?dz)]))
838861
=> ?result)
839862
?ox ?oy ?oz ?dx ?dy ?dz ?result
840863
-2 0 0 1 0 0 [1.0 3.0]
@@ -917,11 +940,15 @@ void main()
917940
[expected error]
918941
(fn [actual]
919942
(and (== (count expected) (count actual))
920-
(<= (apply + (mapv (fn [a b] (* (- b a) (- b a))) actual expected)) (* error error)))))
943+
(<= (apply + (mapv (fn [a b] (* (- b a) (- b a))) actual expected))
944+
(* error error)))))
921945

922946

923947
(tabular "Test cloud transfer"
924-
(fact (seq (render-pixel [vertex-test] [(fog ?density) constant-scatter no-shadow (cloud-transfer "fog" ?step) (cloud-transfer-probe ?a ?b)]))
948+
(fact (seq (render-pixel [vertex-test]
949+
[(fog ?density) constant-scatter no-shadow
950+
(cloud-transfer "fog" ?step)
951+
(cloud-transfer-probe ?a ?b)]))
925952
=> (roughly-vector ?result 1e-3))
926953
?a ?b ?step ?density ?result
927954
0 0 1 0.0 [0.0 0.0 0.0 0.0]
@@ -955,12 +982,14 @@ void main()
955982

956983
(defn setup-fog-uniforms
957984
[program width height]
958-
(let [rotation (mulm (rotation-matrix-3d-y (to-radians 40.0)) (rotation-matrix-3d-x (to-radians -20.0)))
985+
(let [rotation (mulm (rotation-matrix-3d-y (to-radians 40.0))
986+
(rotation-matrix-3d-x (to-radians -20.0)))
959987
focal-length (/ (* 0.5 width) (tan (to-radians 30.0)))
960988
light (normalize (vec3 6 1 10))]
961989
(GL20/glUseProgram program)
962990
(GL20/glUniform2f (GL20/glGetUniformLocation program "resolution") width height)
963-
(GL20/glUniform3f (GL20/glGetUniformLocation program "light") (light 0) (light 1) (light 2))
991+
(GL20/glUniform3f (GL20/glGetUniformLocation program "light")
992+
(light 0) (light 1) (light 2))
964993
(GL20/glUniformMatrix3fv (GL20/glGetUniformLocation program "rotation") true
965994
(make-float-buffer (mat->float-array rotation)))
966995
(GL20/glUniform1f (GL20/glGetUniformLocation program "focal_length") focal-length)
@@ -969,7 +998,8 @@ void main()
969998

970999
(defn render-fog
9711000
[width height]
972-
(let [fragment-sources [ray-box constant-scatter no-shadow (cloud-transfer "fog" 0.01) (fog 1.0) fragment-cloud]
1001+
(let [fragment-sources [ray-box constant-scatter no-shadow (cloud-transfer "fog" 0.01)
1002+
(fog 1.0) fragment-cloud]
9731003
program (make-program-with-shaders [vertex-test] fragment-sources)
9741004
vao (setup-quad-vao)]
9751005
(setup-point-attribute program)
@@ -983,7 +1013,10 @@ void main()
9831013

9841014

9851015
(defn rgba-array->bufimg [data width height]
986-
(-> data tensor/->tensor (tensor/reshape [height width 4]) (tensor/select :all :all [2 1 0]) (dfn/* 255) (clamp 0 255)))
1016+
(-> data
1017+
tensor/->tensor
1018+
(tensor/reshape [height width 4])
1019+
(tensor/select :all :all [2 1 0]) (dfn/* 255) (clamp 0 255)))
9871020

9881021

9891022
(bufimg/tensor->image (rgba-array->bufimg (render-fog 640 480) 640 480))
@@ -1001,13 +1034,15 @@ void main()
10011034
(GL11/glTexParameteri GL12/GL_TEXTURE_3D GL11/GL_TEXTURE_WRAP_S GL11/GL_REPEAT)
10021035
(GL11/glTexParameteri GL12/GL_TEXTURE_3D GL11/GL_TEXTURE_WRAP_T GL11/GL_REPEAT)
10031036
(GL11/glTexParameteri GL12/GL_TEXTURE_3D GL12/GL_TEXTURE_WRAP_R GL11/GL_REPEAT)
1004-
(GL12/glTexImage3D GL12/GL_TEXTURE_3D 0 GL30/GL_R32F size size size 0 GL11/GL_RED GL11/GL_FLOAT buffer)
1037+
(GL12/glTexImage3D GL12/GL_TEXTURE_3D 0 GL30/GL_R32F size size size 0
1038+
GL11/GL_RED GL11/GL_FLOAT buffer)
10051039
texture))
10061040

10071041

10081042
(def noise3d (dfn/- (dfn/* 0.3 (perlin-noise (make-noise-params 32 4 3)))
10091043
(dfn/* 0.7 (worley-noise (make-noise-params 32 4 3)))))
1010-
(def noise-3d-norm (dfn/* (/ 1.0 (- (dfn/reduce-max noise3d) (dfn/reduce-min noise3d))) (dfn/- noise3d (dfn/reduce-min noise3d))))
1044+
(def noise-3d-norm (dfn/* (/ 1.0 (- (dfn/reduce-max noise3d) (dfn/reduce-min noise3d)))
1045+
(dfn/- noise3d (dfn/reduce-min noise3d))))
10111046
(def noise-texture (float-array->texture3d (dtype/->float-array noise-3d-norm) 32))
10121047

10131048

@@ -1043,7 +1078,11 @@ float noise(vec3 idx)
10431078
(GL20/glDeleteProgram program)))))
10441079

10451080

1046-
(bufimg/tensor->image (rgba-array->bufimg (render-noise 640 480 constant-scatter no-shadow (cloud-transfer "noise" 0.01) noise-shader) 640 480))
1081+
(bufimg/tensor->image
1082+
(rgba-array->bufimg
1083+
(render-noise 640 480
1084+
constant-scatter no-shadow (cloud-transfer "noise" 0.01) noise-shader)
1085+
640 480))
10471086

10481087

10491088
;; ## Remap and clamp 3D noise
@@ -1069,7 +1108,9 @@ void main()
10691108

10701109

10711110
(tabular "Remap and clamp input parameter values"
1072-
(fact (first (render-pixel [vertex-test] [remap-clamp (remap-probe ?value ?low1 ?high1 ?low2 ?high2)]))
1111+
(fact (first (render-pixel
1112+
[vertex-test]
1113+
[remap-clamp (remap-probe ?value ?low1 ?high1 ?low2 ?high2)]))
10731114
=> ?expected)
10741115
?value ?low1 ?high1 ?low2 ?high2 ?expected
10751116
0 0 1 0 1 0.0

0 commit comments

Comments
 (0)