Skip to content

Commit 3534ace

Browse files
committed
Test a mock shader
1 parent db075ec commit 3534ace

1 file changed

Lines changed: 43 additions & 21 deletions

File tree

src/volumetric_clouds/main.clj

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010
:tags [:visualization]}}}
1111

1212
(ns volumetric-clouds.main
13-
(:require [scicloj.kindly.v4.kind :as kind]
14-
[clojure.math :refer (PI sqrt)]
13+
(:require [clojure.math :refer (sqrt)]
1514
[midje.sweet :refer (fact facts tabular => roughly)]
1615
[fastmath.vector :refer (vec2 vec3 add mult sub div mag dot)]
1716
[tech.v3.datatype :as dtype]
1817
[tech.v3.tensor :as tensor]
1918
[tech.v3.datatype.functional :as dfn]
2019
[tablecloth.api :as tc]
2120
[scicloj.tableplot.v1.plotly :as plotly]
22-
[tech.v3.libs.buffered-image :as bufimg])
23-
(:import [javax.imageio ImageIO]
24-
[org.lwjgl.opengl GL11]
25-
[org.lwjgl.stb STBImageWrite]
21+
[tech.v3.libs.buffered-image :as bufimg]
22+
[comb.template :as template])
23+
(:import [org.lwjgl.opengl GL11]
2624
[org.lwjgl BufferUtils]
2725
[org.lwjgl.glfw GLFW]
2826
[org.lwjgl.opengl GL GL11 GL12 GL15 GL20 GL30 GL32 GL42]))
@@ -556,7 +554,6 @@ void main()
556554
fragColor = vec4(1, 1, 1, 1);
557555
}")
558556

559-
560557
(defmacro def-make-buffer [method create-buffer]
561558
`(defn ~method [data#]
562559
(let [buffer# (~create-buffer (count data#))]
@@ -631,31 +628,56 @@ void main()
631628
result#)))
632629

633630

634-
(defn render-pixel
635-
[vertex-source fragment-source]
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])
637-
indices (int-array [0 1 2 3])
638-
vertex-shader (make-shader vertex-source GL20/GL_VERTEX_SHADER)
639-
fragment-shader (make-shader fragment-source GL20/GL_FRAGMENT_SHADER)
640-
program (make-program vertex-shader fragment-shader)
641-
point-attribute (GL20/glGetAttribLocation program "point")
642-
vao (setup-vao vertices indices)
643-
texture (make-texture 1 1)]
631+
(defn setup-point-attribute
632+
[program]
633+
(let [point-attribute (GL20/glGetAttribLocation program "point")]
644634
(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
635+
(GL20/glEnableVertexAttribArray point-attribute)))
636+
637+
638+
(defn render-pixels
639+
[vertex-sources fragment-sources width height]
640+
(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])
641+
indices (int-array [0 1 2 3])
642+
vertex-shader (map #(make-shader % GL20/GL_VERTEX_SHADER) vertex-sources)
643+
fragment-shaders (map #(make-shader % GL20/GL_FRAGMENT_SHADER) fragment-sources)
644+
program (apply make-program (concat vertex-shader fragment-shaders))
645+
vao (setup-vao vertices indices)
646+
texture (make-texture width height)]
647+
(setup-point-attribute program)
648+
(framebuffer-render texture width height
647649
(GL20/glUseProgram program)
648650
(GL11/glClearColor 1.0 0.5 0.25 1.0)
649651
(GL11/glClear GL11/GL_COLOR_BUFFER_BIT)
650652
(GL11/glDrawElements GL11/GL_QUADS (count indices) GL11/GL_UNSIGNED_INT 0))
651-
(let [result (read-texture texture 1 1)]
653+
(let [result (read-texture texture width height)]
652654
(GL11/glDeleteTextures texture)
653655
(teardown-vao vao)
654656
(GL20/glDeleteProgram program)
655657
result)))
656658

659+
(render-pixels [vertex-test] [fragment-test] 1 1)
660+
661+
(def noise-mock
662+
"#version 130
663+
float noise (vec3 idx)
664+
{
665+
ivec3 v = ivec3(floor(idx.x), floor(idx.y), floor(idx.z)) % 2;
666+
return ((v.x == 1) == (v.y == 1)) == (v.z == 1) ? 1.0 : 0.0;
667+
}")
668+
669+
(def noise-probe
670+
(template/fn [x y z]
671+
"#version 130
672+
out vec4 fragColor;
673+
float noise(vec3 idx);
674+
void main()
675+
{
676+
fragColor = vec4(noise(vec3(<%= x %>, <%= y %>, <%= z %>)));
677+
}"))
657678

658-
(render-pixel vertex-test fragment-test)
679+
(for [z [0 1] y [0 1] x [0 1]]
680+
(nth (render-pixels [vertex-test] [noise-mock (noise-probe x y z)] 1 1) 0))
659681

660682
(GLFW/glfwDestroyWindow window)
661683

0 commit comments

Comments
 (0)