|
482 | 482 |
|
483 | 483 | (defn octaves |
484 | 484 | [n decay] |
485 | | - (let [series (take n (iterate #(* ^double % decay) 1.0)) |
| 485 | + (let [series (take n (iterate #(* % decay) 1.0)) |
486 | 486 | sum (apply + series)] |
487 | | - (mapv #(/ ^double % ^double sum) series))) |
| 487 | + (mapv #(/ % sum) series))) |
488 | 488 |
|
489 | 489 |
|
490 | 490 | (defn noise-octaves |
@@ -595,43 +595,64 @@ void main() |
595 | 595 | "Convert float buffer to flaot array" |
596 | 596 | {:malli/schema [:=> [:cat :some] seqable?]} |
597 | 597 | [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) |
601 | 601 | result)) |
602 | 602 |
|
603 | 603 |
|
| 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 | + |
604 | 634 | (defn render-pixel |
605 | 635 | [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]) |
607 | 637 | indices (int-array [0 1 2 3]) |
608 | 638 | vertex-shader (make-shader vertex-source GL20/GL_VERTEX_SHADER) |
609 | 639 | fragment-shader (make-shader fragment-source GL20/GL_FRAGMENT_SHADER) |
610 | 640 | program (make-program vertex-shader fragment-shader) |
611 | | - location (GL20/glGetAttribLocation program "point") |
| 641 | + point-attribute (GL20/glGetAttribLocation program "point") |
612 | 642 | 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))) |
635 | 656 |
|
636 | 657 |
|
637 | 658 | (render-pixel vertex-test fragment-test) |
|
0 commit comments