Skip to content

Commit 0fda31a

Browse files
committed
Create method to render pixel
1 parent 468c77f commit 0fda31a

1 file changed

Lines changed: 35 additions & 47 deletions

File tree

src/volumetric_clouds/main.clj

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,6 @@ void main()
556556
fragColor = vec4(1, 1, 1, 1);
557557
}")
558558

559-
(def vertex-shader (make-shader vertex-test GL20/GL_VERTEX_SHADER))
560-
(def fragment-shader (make-shader fragment-test GL20/GL_FRAGMENT_SHADER))
561-
(def program (make-program vertex-shader fragment-shader))
562559

563560
(defmacro def-make-buffer [method create-buffer]
564561
`(defn ~method [data#]
@@ -571,15 +568,6 @@ void main()
571568
(def-make-buffer make-int-buffer BufferUtils/createIntBuffer)
572569
(def-make-buffer make-byte-buffer BufferUtils/createByteBuffer)
573570

574-
(def vertices
575-
(float-array [ 1.0 1.0 0.0
576-
-1.0 1.0 0.0
577-
-1.0 -1.0 0.0
578-
1.0 -1.0 0.0]))
579-
580-
(def indices
581-
(int-array [0 1 2 3]))
582-
583571
(defn setup-vao [vertices indices]
584572
(let [vao (GL30/glGenVertexArrays)
585573
vbo (GL15/glGenBuffers)
@@ -593,6 +581,7 @@ void main()
593581
GL15/GL_STATIC_DRAW)
594582
{:vao vao :vbo vbo :ibo ibo}))
595583

584+
596585
(defn teardown-vao [{:keys [vao vbo ibo]}]
597586
(GL15/glBindBuffer GL15/GL_ELEMENT_ARRAY_BUFFER 0)
598587
(GL15/glDeleteBuffers ibo)
@@ -601,35 +590,6 @@ void main()
601590
(GL30/glBindVertexArray 0)
602591
(GL15/glDeleteBuffers vao))
603592

604-
(def vao (setup-vao vertices indices))
605-
606-
(def location (GL20/glGetAttribLocation program "point"))
607-
(GL20/glVertexAttribPointer location 3 GL11/GL_FLOAT false (* 3 Float/BYTES) (* 0 Float/BYTES))
608-
(GL20/glEnableVertexAttribArray location)
609-
610-
(def texture (GL11/glGenTextures))
611-
(GL11/glBindTexture GL11/GL_TEXTURE_2D texture)
612-
(GL42/glTexStorage2D GL11/GL_TEXTURE_2D 1 GL30/GL_RGBA32F 1 1)
613-
614-
(def fbo (GL30/glGenFramebuffers))
615-
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER fbo)
616-
(GL32/glFramebufferTexture GL30/GL_FRAMEBUFFER GL30/GL_COLOR_ATTACHMENT0 texture 0)
617-
(GL20/glDrawBuffers (make-int-buffer (int-array [GL30/GL_COLOR_ATTACHMENT0])))
618-
(GL11/glViewport 0 0 1 1)
619-
620-
(GL20/glUseProgram program)
621-
(GL11/glClearColor 1.0 0.5 0.25 1.0)
622-
(GL11/glClear GL11/GL_COLOR_BUFFER_BIT)
623-
(GL11/glDrawElements GL11/GL_QUADS (count indices) GL11/GL_UNSIGNED_INT 0)
624-
; (GL11/glGetError)
625-
626-
(GL30/glBindFramebuffer GL30/GL_FRAMEBUFFER 0)
627-
(GL30/glDeleteFramebuffers fbo)
628-
;(GLFW/glfwSwapBuffers window)
629-
630-
(GL11/glBindTexture GL11/GL_TEXTURE_2D texture)
631-
(def buf (BufferUtils/createFloatBuffer (* 1 1 3)))
632-
(GL11/glGetTexImage GL11/GL_TEXTURE_2D 0 GL12/GL_RGB GL11/GL_FLOAT buf)
633593

634594
(defn float-buffer->array
635595
"Convert float buffer to flaot array"
@@ -640,13 +600,41 @@ void main()
640600
(.flip ^java.nio.DirectFloatBufferU buffer)
641601
result))
642602

643-
(seq (float-buffer->array buf))
644-
645-
(GL11/glDeleteTextures texture)
646-
647-
(teardown-vao vao)
648603

649-
(GL20/glDeleteProgram program)
604+
(defn render-pixel
605+
[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])
607+
indices (int-array [0 1 2 3])
608+
vertex-shader (make-shader vertex-source GL20/GL_VERTEX_SHADER)
609+
fragment-shader (make-shader fragment-source GL20/GL_FRAGMENT_SHADER)
610+
program (make-program vertex-shader fragment-shader)
611+
location (GL20/glGetAttribLocation program "point")
612+
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))))
635+
636+
637+
(render-pixel vertex-test fragment-test)
650638

651639
(GLFW/glfwDestroyWindow window)
652640

0 commit comments

Comments
 (0)