|
9 | 9 | (ns opengl-visualization.main |
10 | 10 | (:require [clojure.java.io :as io] |
11 | 11 | [clojure.math :refer (to-radians)] |
12 | | - [fastmath.vector :refer (vec3 sub add mult)] |
13 | | - ) |
| 12 | + [fastmath.vector :refer (vec3 sub add mult normalize)]) |
14 | 13 | (:import [javax.imageio ImageIO] |
15 | 14 | [java.awt.image BufferedImage] |
16 | 15 | [org.lwjgl BufferUtils] |
@@ -291,7 +290,7 @@ void main() |
291 | 290 | (GL13/glActiveTexture GL13/GL_TEXTURE0) |
292 | 291 | (GL11/glBindTexture GL11/GL_TEXTURE_2D texture)) |
293 | 292 |
|
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) |
295 | 294 | (screenshot) |
296 | 295 |
|
297 | 296 | ;; ### Finishing up |
@@ -426,25 +425,61 @@ void main() |
426 | 425 | (GL15/glBindBuffer GL15/GL_ARRAY_BUFFER 0) |
427 | 426 | (GL15/glDeleteBuffers vbo-cube) |
428 | 427 | (GL30/glBindVertexArray 0) |
429 | | - (GL15/glDeleteBuffers vao)) |
| 428 | + (GL15/glDeleteBuffers vao-cube)) |
430 | 429 |
|
431 | 430 | ;; ## Approximating a sphere |
432 | 431 | ;; |
433 | 432 | ;; 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))) |
435 | 437 | corners |
436 | 438 |
|
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))) |
438 | 440 | u-vectors |
439 | 441 |
|
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))) |
441 | 443 | v-vectors |
442 | 444 |
|
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))))))) |
444 | 446 | (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)]))) |
445 | 447 |
|
446 | 448 | (def n 2) |
447 | 449 | (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 | + |
448 | 483 | (GL20/glDeleteProgram program) |
449 | 484 | (GL11/glDeleteTextures texture) |
450 | 485 |
|
|
0 commit comments