Skip to content

Commit 02c47df

Browse files
committed
Add moon with higher resolution mesh
1 parent 4e482e5 commit 02c47df

1 file changed

Lines changed: 38 additions & 58 deletions

File tree

src/opengl_visualization/main.clj

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,22 @@ void main()
177177

178178
;; ### Setting up the vertex buffer
179179
;;
180-
;; We define a vertex array object (VAO) which acts like a context for the vertex and index buffer.
181-
(def vao (GL30/glGenVertexArrays))
182-
(GL30/glBindVertexArray vao)
183-
184-
;; We define a vertex buffer object (VBO) which contains the vertex data.
185-
(def vbo (GL15/glGenBuffers))
186-
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER vbo)
187-
(def vertices-buffer (make-float-buffer vertices))
188-
(GL15/glBufferData GL15/GL_ARRAY_BUFFER vertices-buffer GL15/GL_STATIC_DRAW)
189-
190-
;; We also define an index buffer object (IBO) which contains the index data.
191-
(def idx (GL15/glGenBuffers))
192-
(GL15/glBindBuffer GL15/GL_ELEMENT_ARRAY_BUFFER idx)
193-
(def indices-buffer (make-int-buffer indices))
194-
(GL15/glBufferData GL15/GL_ELEMENT_ARRAY_BUFFER indices-buffer GL15/GL_STATIC_DRAW)
180+
;; We add a convenience function to setup VAO, VBO, and IBO.
181+
;; * We define a vertex array object (VAO) which acts like a context for the vertex and index buffer.
182+
;; * We define a vertex buffer object (VBO) which contains the vertex data.
183+
;; * We also define an index buffer object (IBO) which contains the index data.
184+
(defn setup-vao [vertices indices]
185+
(let [vao (GL30/glGenVertexArrays)
186+
vbo (GL15/glGenBuffers)
187+
ibo (GL15/glGenBuffers)]
188+
(GL30/glBindVertexArray vao)
189+
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER vbo)
190+
(GL15/glBufferData GL15/GL_ARRAY_BUFFER (make-float-buffer vertices) GL15/GL_STATIC_DRAW)
191+
(GL15/glBindBuffer GL15/GL_ELEMENT_ARRAY_BUFFER ibo)
192+
(GL15/glBufferData GL15/GL_ELEMENT_ARRAY_BUFFER (make-int-buffer indices) GL15/GL_STATIC_DRAW)
193+
{:vao vao :vbo vbo :ibo ibo}))
194+
;; Now we use the function to setup the VAO, VBO, and IBO.
195+
(def vao (setup-vao vertices indices))
195196

196197
;; The data of each vertex is defined by 3 floats (x, y, z).
197198
;; We need to specify the layout of the vertex buffer object so that OpenGL knows how to interpret it.
@@ -294,14 +295,15 @@ void main()
294295
(screenshot)
295296

296297
;; ### Finishing up
297-
(do
298+
(defn teardown-vao [{:keys [vao vbo ibo]}]
298299
(GL15/glBindBuffer GL15/GL_ELEMENT_ARRAY_BUFFER 0)
299-
(GL15/glDeleteBuffers idx)
300+
(GL15/glDeleteBuffers ibo)
300301
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER 0)
301302
(GL15/glDeleteBuffers vbo)
302303
(GL30/glBindVertexArray 0)
303304
(GL15/glDeleteBuffers vao)
304305
(GL20/glDeleteProgram program))
306+
(teardown-vao vao)
305307

306308
;;; ## Render a 3D cube
307309
;;;
@@ -327,21 +329,8 @@ void main()
327329

328330
;; ### Initialize vertex buffer array
329331
;;
330-
;; We define a vertex array object (VAO) which acts like a context for the vertex and index buffer.
331-
(def vao-cube (GL30/glGenVertexArrays))
332-
(GL30/glBindVertexArray vao-cube)
333-
334-
;; We define a vertex buffer object (VBO) which contains the vertex data.
335-
(def vbo-cube (GL15/glGenBuffers))
336-
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER vbo-cube)
337-
(def vertices-buffer-cube (make-float-buffer vertices-cube))
338-
(GL15/glBufferData GL15/GL_ARRAY_BUFFER vertices-buffer-cube GL15/GL_STATIC_DRAW)
339-
340-
;; We also define an index buffer object (IBO) which contains the index data.
341-
(def idx-cube (GL15/glGenBuffers))
342-
(GL15/glBindBuffer GL15/GL_ELEMENT_ARRAY_BUFFER idx-cube)
343-
(def indices-buffer-cube (make-int-buffer indices-cube))
344-
(GL15/glBufferData GL15/GL_ELEMENT_ARRAY_BUFFER indices-buffer-cube GL15/GL_STATIC_DRAW)
332+
;; We use the function from earlier to set up the VAO, VBO, and IBO.
333+
(def vao-cube (setup-vao vertices-cube indices-cube))
345334

346335
;;; ### Shader program mapping texture onto cube
347336
;;;
@@ -400,7 +389,7 @@ void main()
400389
(do
401390
(GL20/glUseProgram program-moon)
402391
(GL20/glUniform2f (GL20/glGetUniformLocation program-moon "iResolution") window-width window-height)
403-
(GL20/glUniform1f (GL20/glGetUniformLocation program-moon "fov") (to-radians 60.0))
392+
(GL20/glUniform1f (GL20/glGetUniformLocation program-moon "fov") (to-radians 50.0))
404393
(GL20/glUniform1f (GL20/glGetUniformLocation program-moon "alpha") (to-radians -30.0))
405394
(GL20/glUniform1f (GL20/glGetUniformLocation program-moon "beta") (to-radians 20.0))
406395
(GL20/glUniform1f (GL20/glGetUniformLocation program-moon "distance") 5.0)
@@ -419,13 +408,7 @@ void main()
419408
(screenshot))
420409

421410
;; ### Finishing up
422-
(do
423-
(GL15/glBindBuffer GL15/GL_ELEMENT_ARRAY_BUFFER 0)
424-
(GL15/glDeleteBuffers idx-cube)
425-
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER 0)
426-
(GL15/glDeleteBuffers vbo-cube)
427-
(GL30/glBindVertexArray 0)
428-
(GL15/glDeleteBuffers vao-cube))
411+
(teardown-vao vao-cube)
429412

430413
;; ## Approximating a sphere
431414
;;
@@ -449,18 +432,7 @@ v-vectors
449432
(def vertices-sphere (float-array (flatten (map (partial sphere-points n) corners u-vectors v-vectors))))
450433
(def indices-sphere (int-array (flatten (map (partial sphere-indices n) (range 6)))))
451434

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)
435+
(def vao-sphere (setup-vao vertices-sphere indices-sphere))
464436

465437
(do
466438
(GL20/glVertexAttribPointer (GL20/glGetAttribLocation program-moon "point") 3 GL11/GL_FLOAT false (* 3 Float/BYTES) (* 0 Float/BYTES))
@@ -471,14 +443,22 @@ v-vectors
471443
(GL11/glDrawElements GL11/GL_QUADS (count indices-sphere) GL11/GL_UNSIGNED_INT 0)
472444
(screenshot))
473445

446+
(def n2 16)
447+
(def vertices-sphere-2 (float-array (flatten (map (partial sphere-points n2) corners u-vectors v-vectors))))
448+
(def indices-sphere-2 (int-array (flatten (map (partial sphere-indices n2) (range 6)))))
449+
450+
(def vao-sphere (setup-vao vertices-sphere-2 indices-sphere-2))
451+
474452
(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))
453+
(GL20/glVertexAttribPointer (GL20/glGetAttribLocation program-moon "point") 3 GL11/GL_FLOAT false (* 3 Float/BYTES) (* 0 Float/BYTES))
454+
(GL20/glEnableVertexAttribArray 0))
455+
456+
(do
457+
(GL11/glClear (bit-or GL11/GL_COLOR_BUFFER_BIT GL11/GL_DEPTH_BUFFER_BIT))
458+
(GL11/glDrawElements GL11/GL_QUADS (count indices-sphere-2) GL11/GL_UNSIGNED_INT 0)
459+
(screenshot))
481460

461+
(teardown-vao vao-sphere)
482462

483463
(GL20/glDeleteProgram program)
484464
(GL11/glDeleteTextures texture)

0 commit comments

Comments
 (0)