|
10 | 10 | :tags [:visualization]}}} |
11 | 11 |
|
12 | 12 | (ns volumetric-clouds.main |
13 | | - (:require [scicloj.kindly.v4.kind :as kind] |
14 | | - [clojure.math :refer (PI sqrt)] |
| 13 | + (:require [clojure.math :refer (sqrt)] |
15 | 14 | [midje.sweet :refer (fact facts tabular => roughly)] |
16 | 15 | [fastmath.vector :refer (vec2 vec3 add mult sub div mag dot)] |
17 | 16 | [tech.v3.datatype :as dtype] |
18 | 17 | [tech.v3.tensor :as tensor] |
19 | 18 | [tech.v3.datatype.functional :as dfn] |
20 | 19 | [tablecloth.api :as tc] |
21 | 20 | [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] |
26 | 24 | [org.lwjgl BufferUtils] |
27 | 25 | [org.lwjgl.glfw GLFW] |
28 | 26 | [org.lwjgl.opengl GL GL11 GL12 GL15 GL20 GL30 GL32 GL42])) |
@@ -556,7 +554,6 @@ void main() |
556 | 554 | fragColor = vec4(1, 1, 1, 1); |
557 | 555 | }") |
558 | 556 |
|
559 | | - |
560 | 557 | (defmacro def-make-buffer [method create-buffer] |
561 | 558 | `(defn ~method [data#] |
562 | 559 | (let [buffer# (~create-buffer (count data#))] |
@@ -631,31 +628,56 @@ void main() |
631 | 628 | result#))) |
632 | 629 |
|
633 | 630 |
|
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")] |
644 | 634 | (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 |
647 | 649 | (GL20/glUseProgram program) |
648 | 650 | (GL11/glClearColor 1.0 0.5 0.25 1.0) |
649 | 651 | (GL11/glClear GL11/GL_COLOR_BUFFER_BIT) |
650 | 652 | (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)] |
652 | 654 | (GL11/glDeleteTextures texture) |
653 | 655 | (teardown-vao vao) |
654 | 656 | (GL20/glDeleteProgram program) |
655 | 657 | result))) |
656 | 658 |
|
| 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 | +}")) |
657 | 678 |
|
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)) |
659 | 681 |
|
660 | 682 | (GLFW/glfwDestroyWindow window) |
661 | 683 |
|
|
0 commit comments