@@ -1138,29 +1138,35 @@ void main()
11381138
11391139
11401140; ; ### Rendering of 3D noise
1141-
1141+ ; ;
1142+ ; ; This method converts a floating point array to a buffer and initialises a 3D texture with it.
1143+ ; ; It is also necessary to set the texture parameters for interpolation and wrapping.
11421144(defn float-array->texture3d
11431145 [data size]
11441146 (let [buffer (make-float-buffer data)
11451147 texture (GL11/glGenTextures )]
11461148 (GL11/glBindTexture GL12/GL_TEXTURE_3D texture)
1149+ (GL12/glTexImage3D GL12/GL_TEXTURE_3D 0 GL30/GL_R32F size size size 0
1150+ GL11/GL_RED GL11/GL_FLOAT buffer)
11471151 (GL11/glTexParameteri GL12/GL_TEXTURE_3D GL11/GL_TEXTURE_MIN_FILTER GL11/GL_LINEAR)
11481152 (GL11/glTexParameteri GL12/GL_TEXTURE_3D GL11/GL_TEXTURE_MAG_FILTER GL11/GL_LINEAR)
11491153 (GL11/glTexParameteri GL12/GL_TEXTURE_3D GL11/GL_TEXTURE_WRAP_S GL11/GL_REPEAT)
11501154 (GL11/glTexParameteri GL12/GL_TEXTURE_3D GL11/GL_TEXTURE_WRAP_T GL11/GL_REPEAT)
11511155 (GL11/glTexParameteri GL12/GL_TEXTURE_3D GL12/GL_TEXTURE_WRAP_R GL11/GL_REPEAT)
1152- (GL12/glTexImage3D GL12/GL_TEXTURE_3D 0 GL30/GL_R32F size size size 0
1153- GL11/GL_RED GL11/GL_FLOAT buffer)
11541156 texture))
11551157
1156-
1158+ ; ; Here a mixture of 3D Perlin and Worley noise is created.
11571159(def noise3d (dfn/- (dfn/* 0.3 (perlin-noise (make-noise-params 32 4 3 )))
11581160 (dfn/* 0.7 (worley-noise (make-noise-params 32 4 3 )))))
1161+
1162+ ; ; The noise is normalised to be between 0 and 1.
11591163(def noise-3d-norm (dfn/* (/ 1.0 (- (dfn/reduce-max noise3d) (dfn/reduce-min noise3d)))
11601164 (dfn/- noise3d (dfn/reduce-min noise3d))))
1161- (def noise-texture (float-array->texture3d (dtype/->float-array noise-3d-norm) 32 ))
11621165
1166+ ; ; Then the noise data is converted to a 3D texture.
1167+ (def noise-texture (float-array->texture3d (dtype/->float-array noise-3d-norm) 32 ))
11631168
1169+ ; ; Instead of a constant density fog, we can use the noise as a density function.
11641170(def noise-shader
11651171" #version 130
11661172uniform sampler3D noise3d;
@@ -1169,15 +1175,15 @@ float noise(vec3 idx)
11691175 return texture(noise3d, idx).r;
11701176}" )
11711177
1172-
1178+ ; ; We also set the uniform sampler to texture slot 0 and bind the noise texture to that slot.
11731179(defn setup-noise-uniforms
11741180 [program width height]
11751181 (setup-fog-uniforms program width height)
11761182 (GL20/glUniform1i (GL20/glGetUniformLocation program " noise3d" ) 0 )
11771183 (GL13/glActiveTexture GL13/GL_TEXTURE0)
11781184 (GL11/glBindTexture GL12/GL_TEXTURE_3D noise-texture))
11791185
1180-
1186+ ; ; Similar to the fog example above, we define a method to render the noise.
11811187(defn render-noise
11821188 [width height & cloud-shaders]
11831189 (let [fragment-sources (concat cloud-shaders [ray-box fragment-cloud])
@@ -1192,7 +1198,7 @@ float noise(vec3 idx)
11921198 (teardown-vao vao)
11931199 (GL20/glDeleteProgram program)))))
11941200
1195-
1201+ ; ; The noise is rendered with a step size of 0.01.
11961202(rgba-array->bufimg
11971203 (render-noise 640 480
11981204 constant-scatter no-shadow (cloud-transfer " noise" 0.01 ) noise-shader)
@@ -1201,6 +1207,7 @@ float noise(vec3 idx)
12011207
12021208; ; ### Remap and clamp 3D noise
12031209
1210+ ; ; We define a method to map a range of input values to a range of output values and clamp the result.
12041211(def remap-clamp
12051212" #version 130
12061213float remap_clamp(float value, float low1, float high1, float low2, float high2)
@@ -1209,7 +1216,7 @@ float remap_clamp(float value, float low1, float high1, float low2, float high2)
12091216 return clamp(low2 + t * (high2 - low2), low2, high2);
12101217}" )
12111218
1212-
1219+ ; ; A probing shader is used to test the remap_clamp function.
12131220(def remap-probe
12141221 (template/fn [value low1 high1 low2 high2]
12151222" #version 130
@@ -1220,7 +1227,7 @@ void main()
12201227 fragColor = vec4(remap_clamp(<%= value %>, <%= low1 %>, <%= high1 %>, <%= low2 %>, <%= high2 %>));
12211228}" ))
12221229
1223-
1230+ ; ; `remap_clamp` is tested using a parametrized tests.
12241231(tabular " Remap and clamp input parameter values"
12251232 (fact (first (render-pixel
12261233 [vertex-passthrough]
@@ -1237,7 +1244,8 @@ void main()
12371244 0 1 2 1 2 1.0
12381245 3 1 2 1 2 2.0 )
12391246
1240-
1247+ ; ; We use the `remap-noise` method to map the 3D noise to the output range.
1248+ ; ; The base noise function and the remapping parameters are template parameters.
12411249(def remap-noise
12421250 (template/fn [base low1 high1 high2]
12431251" #version 130
0 commit comments