Skip to content

Commit 84b8ca0

Browse files
committed
Use fastmath to generate more cube coordinates
1 parent 838a22b commit 84b8ca0

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

deps.edn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
org.lwjgl/lwjgl-stb {:mvn/version "3.3.6"}
3737
org.lwjgl/lwjgl-stb$natives-linux {:mvn/version "3.3.6"}
3838
org.lwjgl/lwjgl-stb$natives-windows {:mvn/version "3.3.6"}
39-
org.lwjgl/lwjgl-stb$natives-macos {:mvn/version "3.3.6"}}
39+
org.lwjgl/lwjgl-stb$natives-macos {:mvn/version "3.3.6"}
40+
generateme/fastmath {:mvn/version "2.4.0"
41+
:exclusions [com.github.haifengl/smile-mkl org.bytedeco/openblas]}}
4042

4143
:aliases
4244
{;; Build the site with `clojure -M:clay -A:markdown`

src/opengl_visualization/main.clj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
:tags [:visualization]}}}
99
(ns opengl-visualization.main
1010
(:require [clojure.java.io :as io]
11-
[clojure.math :refer (to-radians)])
11+
[clojure.math :refer (to-radians)]
12+
[fastmath.vector :refer (vec3 sub add mult)]
13+
)
1214
(:import [javax.imageio ImageIO]
1315
[java.awt.image BufferedImage]
1416
[org.lwjgl BufferUtils]
@@ -241,6 +243,8 @@ void main()
241243
(def height (int-array 1))
242244
(def channels (int-array 1))
243245
(def buffer (STBImage/stbi_load moon-png width height channels 4)))
246+
(aget width 0)
247+
(aget height 0)
244248

245249
;; ### Set up the texture
246250
(do
@@ -425,8 +429,22 @@ void main()
425429
(GL15/glDeleteBuffers vao))
426430

427431
;; ## Approximating a sphere
432+
;;
433+
;; Get corners of cube
434+
(def corners (map #(apply vec3 %) (partition 3 vertices-cube)))
435+
corners
436+
437+
(def u-vectors (map (fn [[i j _ _]] (sub (nth corners j) (nth corners i))) (partition 4 indices-cube)))
438+
u-vectors
439+
440+
(def v-vectors (map (fn [[i _ _ l]] (sub (nth corners l) (nth corners i))) (partition 4 indices-cube)))
441+
v-vectors
428442

443+
(defn sphere-points [n c u v] (for [j (range (inc n)) i (range (inc n))] (add c (add (mult u (/ i n)) (mult v (/ j n))))))
444+
(defn sphere-indices [n face] (for [j (range n) i (range n)] (let [offset (+ (* face (inc n) (inc n)) (* j (inc n)) i)] [offset (inc offset) (+ offset n 2) (+ offset n 1)])))
429445

446+
(def n 2)
447+
(def vertices-sphere (float-array (flatten (map (partial sphere-points n) corners u-vectors v-vectors))))
430448
(GL20/glDeleteProgram program)
431449
(GL11/glDeleteTextures texture)
432450

0 commit comments

Comments
 (0)