Skip to content

Commit db075ec

Browse files
committed
Refactor pixel rendering method
1 parent 0fda31a commit db075ec

1 file changed

Lines changed: 50 additions & 29 deletions

File tree

src/volumetric_clouds/main.clj

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,9 @@
482482

483483
(defn octaves
484484
[n decay]
485-
(let [series (take n (iterate #(* ^double % decay) 1.0))
485+
(let [series (take n (iterate #(* % decay) 1.0))
486486
sum (apply + series)]
487-
(mapv #(/ ^double % ^double sum) series)))
487+
(mapv #(/ % sum) series)))
488488

489489

490490
(defn noise-octaves
@@ -595,43 +595,64 @@ void main()
595595
"Convert float buffer to flaot array"
596596
{:malli/schema [:=> [:cat :some] seqable?]}
597597
[buffer]
598-
(let [result (float-array (.limit ^java.nio.DirectFloatBufferU buffer))]
599-
(.get ^java.nio.DirectFloatBufferU buffer result)
600-
(.flip ^java.nio.DirectFloatBufferU buffer)
598+
(let [result (float-array (.limit buffer))]
599+
(.get buffer result)
600+
(.flip buffer)
601601
result))
602602

603603

604+
(defn make-texture
605+
[width height]
606+
(let [texture (GL11/glGenTextures)]
607+
(GL11/glBindTexture GL11/GL_TEXTURE_2D texture)
608+
(GL42/glTexStorage2D GL11/GL_TEXTURE_2D 1 GL30/GL_RGBA32F width height)
609+
texture))
610+
611+
612+
(defn read-texture
613+
[texture width height]
614+
(let [buffer (BufferUtils/createFloatBuffer (* height width 4))]
615+
(GL11/glBindTexture GL11/GL_TEXTURE_2D texture)
616+
(GL11/glGetTexImage GL11/GL_TEXTURE_2D 0 GL12/GL_RGBA GL11/GL_FLOAT buffer)
617+
(seq (float-buffer->array buffer))))
618+
619+
620+
(defmacro framebuffer-render
621+
[texture width height & body]
622+
`(let [fbo# (GL30/glGenFramebuffers)]
623+
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER fbo#)
624+
(GL11/glBindTexture GL11/GL_TEXTURE_2D ~texture)
625+
(GL32/glFramebufferTexture GL30/GL_FRAMEBUFFER GL30/GL_COLOR_ATTACHMENT0 ~texture 0)
626+
(GL20/glDrawBuffers (volumetric-clouds.main/make-int-buffer (int-array [GL30/GL_COLOR_ATTACHMENT0])))
627+
(GL11/glViewport 0 0 ~width ~height)
628+
(let [result# (do ~@body)]
629+
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER 0)
630+
(GL30/glDeleteFramebuffers fbo#)
631+
result#)))
632+
633+
604634
(defn render-pixel
605635
[vertex-source fragment-source]
606-
(let [vertices (float-array [ 1.0 1.0 0.0, -1.0 1.0 0.0, -1.0 -1.0 0.0, 1.0 -1.0 0.0])
636+
(let [vertices (float-array [1.0 1.0 0.0, -1.0 1.0 0.0, -1.0 -1.0 0.0, 1.0 -1.0 0.0])
607637
indices (int-array [0 1 2 3])
608638
vertex-shader (make-shader vertex-source GL20/GL_VERTEX_SHADER)
609639
fragment-shader (make-shader fragment-source GL20/GL_FRAGMENT_SHADER)
610640
program (make-program vertex-shader fragment-shader)
611-
location (GL20/glGetAttribLocation program "point")
641+
point-attribute (GL20/glGetAttribLocation program "point")
612642
vao (setup-vao vertices indices)
613-
texture (GL11/glGenTextures)
614-
fbo (GL30/glGenFramebuffers)
615-
buf (BufferUtils/createFloatBuffer (* 1 1 3))]
616-
(GL20/glVertexAttribPointer location 3 GL11/GL_FLOAT false (* 3 Float/BYTES) (* 0 Float/BYTES))
617-
(GL20/glEnableVertexAttribArray location)
618-
(GL11/glBindTexture GL11/GL_TEXTURE_2D texture)
619-
(GL42/glTexStorage2D GL11/GL_TEXTURE_2D 1 GL30/GL_RGBA32F 1 1)
620-
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER fbo)
621-
(GL32/glFramebufferTexture GL30/GL_FRAMEBUFFER GL30/GL_COLOR_ATTACHMENT0 texture 0)
622-
(GL20/glDrawBuffers (make-int-buffer (int-array [GL30/GL_COLOR_ATTACHMENT0])))
623-
(GL11/glViewport 0 0 1 1)
624-
(GL20/glUseProgram program)
625-
(GL11/glClearColor 1.0 0.5 0.25 1.0)
626-
(GL11/glClear GL11/GL_COLOR_BUFFER_BIT)
627-
(GL11/glDrawElements GL11/GL_QUADS (count indices) GL11/GL_UNSIGNED_INT 0)
628-
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER 0)
629-
(GL30/glDeleteFramebuffers fbo)
630-
(GL11/glGetTexImage GL11/GL_TEXTURE_2D 0 GL12/GL_RGBA GL11/GL_FLOAT buf)
631-
(GL11/glDeleteTextures texture)
632-
(teardown-vao vao)
633-
(GL20/glDeleteProgram program)
634-
(seq (float-buffer->array buf))))
643+
texture (make-texture 1 1)]
644+
(GL20/glVertexAttribPointer point-attribute 3 GL11/GL_FLOAT false (* 3 Float/BYTES) (* 0 Float/BYTES))
645+
(GL20/glEnableVertexAttribArray point-attribute)
646+
(framebuffer-render texture 1 1
647+
(GL20/glUseProgram program)
648+
(GL11/glClearColor 1.0 0.5 0.25 1.0)
649+
(GL11/glClear GL11/GL_COLOR_BUFFER_BIT)
650+
(GL11/glDrawElements GL11/GL_QUADS (count indices) GL11/GL_UNSIGNED_INT 0))
651+
(let [result (read-texture texture 1 1)]
652+
(GL11/glDeleteTextures texture)
653+
(teardown-vao vao)
654+
(GL20/glDeleteProgram program)
655+
result)))
635656

636657

637658
(render-pixel vertex-test fragment-test)

0 commit comments

Comments
 (0)