Skip to content

Commit 4e482e5

Browse files
committed
Add more corners to the moon surface
1 parent 84b8ca0 commit 4e482e5

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

src/opengl_visualization/main.clj

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
(ns opengl-visualization.main
1010
(:require [clojure.java.io :as io]
1111
[clojure.math :refer (to-radians)]
12-
[fastmath.vector :refer (vec3 sub add mult)]
13-
)
12+
[fastmath.vector :refer (vec3 sub add mult normalize)])
1413
(:import [javax.imageio ImageIO]
1514
[java.awt.image BufferedImage]
1615
[org.lwjgl BufferUtils]
@@ -291,7 +290,7 @@ void main()
291290
(GL13/glActiveTexture GL13/GL_TEXTURE0)
292291
(GL11/glBindTexture GL11/GL_TEXTURE_2D texture))
293292

294-
(GL11/glDrawElements GL11/GL_QUADS 4 GL11/GL_UNSIGNED_INT 0)
293+
(GL11/glDrawElements GL11/GL_QUADS (count indices) GL11/GL_UNSIGNED_INT 0)
295294
(screenshot)
296295

297296
;; ### Finishing up
@@ -426,25 +425,61 @@ void main()
426425
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER 0)
427426
(GL15/glDeleteBuffers vbo-cube)
428427
(GL30/glBindVertexArray 0)
429-
(GL15/glDeleteBuffers vao))
428+
(GL15/glDeleteBuffers vao-cube))
430429

431430
;; ## Approximating a sphere
432431
;;
433432
;; Get corners of cube
434-
(def corners (map #(apply vec3 %) (partition 3 vertices-cube)))
433+
(def points (map #(apply vec3 %) (partition 3 vertices-cube)))
434+
points
435+
436+
(def corners (map (fn [[i _ _ _]] (nth points i)) (partition 4 indices-cube)))
435437
corners
436438

437-
(def u-vectors (map (fn [[i j _ _]] (sub (nth corners j) (nth corners i))) (partition 4 indices-cube)))
439+
(def u-vectors (map (fn [[i j _ _]] (sub (nth points j) (nth points i))) (partition 4 indices-cube)))
438440
u-vectors
439441

440-
(def v-vectors (map (fn [[i _ _ l]] (sub (nth corners l) (nth corners i))) (partition 4 indices-cube)))
442+
(def v-vectors (map (fn [[i _ _ l]] (sub (nth points l) (nth points i))) (partition 4 indices-cube)))
441443
v-vectors
442444

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))))))
445+
(defn sphere-points [n c u v] (for [j (range (inc n)) i (range (inc n))] (normalize (add c (add (mult u (/ i n)) (mult v (/ j n)))))))
444446
(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)])))
445447

446448
(def n 2)
447449
(def vertices-sphere (float-array (flatten (map (partial sphere-points n) corners u-vectors v-vectors))))
450+
(def indices-sphere (int-array (flatten (map (partial sphere-indices n) (range 6)))))
451+
452+
(def vao-sphere (GL30/glGenVertexArrays))
453+
(GL30/glBindVertexArray vao-sphere)
454+
455+
(def vbo-sphere (GL15/glGenBuffers))
456+
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER vbo-sphere)
457+
(def vertices-buffer-sphere (make-float-buffer vertices-sphere))
458+
(GL15/glBufferData GL15/GL_ARRAY_BUFFER vertices-buffer-sphere GL15/GL_STATIC_DRAW)
459+
460+
(def idx-sphere (GL15/glGenBuffers))
461+
(GL15/glBindBuffer GL15/GL_ELEMENT_ARRAY_BUFFER idx-sphere)
462+
(def indices-buffer-sphere (make-int-buffer indices-sphere))
463+
(GL15/glBufferData GL15/GL_ELEMENT_ARRAY_BUFFER indices-buffer-sphere GL15/GL_STATIC_DRAW)
464+
465+
(do
466+
(GL20/glVertexAttribPointer (GL20/glGetAttribLocation program-moon "point") 3 GL11/GL_FLOAT false (* 3 Float/BYTES) (* 0 Float/BYTES))
467+
(GL20/glEnableVertexAttribArray 0))
468+
469+
(do
470+
(GL11/glClear (bit-or GL11/GL_COLOR_BUFFER_BIT GL11/GL_DEPTH_BUFFER_BIT))
471+
(GL11/glDrawElements GL11/GL_QUADS (count indices-sphere) GL11/GL_UNSIGNED_INT 0)
472+
(screenshot))
473+
474+
(do
475+
(GL15/glBindBuffer GL15/GL_ELEMENT_ARRAY_BUFFER 0)
476+
(GL15/glDeleteBuffers idx-sphere)
477+
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER 0)
478+
(GL15/glDeleteBuffers vbo-sphere)
479+
(GL30/glBindVertexArray 0)
480+
(GL15/glDeleteBuffers vao-sphere))
481+
482+
448483
(GL20/glDeleteProgram program)
449484
(GL11/glDeleteTextures texture)
450485

0 commit comments

Comments
 (0)