1111 [clojure.math :refer (to-radians )]
1212 [fastmath.vector :refer (vec3 sub add mult normalize)])
1313 (:import [javax.imageio ImageIO]
14- [java.awt.image BufferedImage]
1514 [org.lwjgl BufferUtils]
1615 [org.lwjgl.glfw GLFW]
1716 [org.lwjgl.opengl GL GL11 GL13 GL15 GL20 GL30]
@@ -301,10 +300,12 @@ void main()
301300 (GL15/glBindBuffer GL15/GL_ARRAY_BUFFER 0 )
302301 (GL15/glDeleteBuffers vbo)
303302 (GL30/glBindVertexArray 0 )
304- (GL15/glDeleteBuffers vao)
305- ( GL20/glDeleteProgram program))
303+ (GL15/glDeleteBuffers vao))
304+
306305(teardown-vao vao)
307306
307+ (GL20/glDeleteProgram tex-program)
308+
308309; ;; ## Render a 3D cube
309310; ;;
310311; ;; ### Create vertex data
@@ -370,7 +371,7 @@ out vec4 fragColor;
370371void main()
371372{
372373 // Convert vpoint to lat, lon
373- float lon = atan(vpoint.z, vpoint.x ) / (2.0 * PI) + 0.5;
374+ float lon = atan(vpoint.x, - vpoint.z ) / (2.0 * PI) + 0.5;
374375 float lat = atan(vpoint.y, length(vpoint.xz)) / PI + 0.5;
375376 fragColor = texture(moon, vec2(lon, lat));
376377}" )
@@ -389,10 +390,10 @@ void main()
389390(do
390391 (GL20/glUseProgram program-moon)
391392 (GL20/glUniform2f (GL20/glGetUniformLocation program-moon " iResolution" ) window-width window-height)
392- (GL20/glUniform1f (GL20/glGetUniformLocation program-moon " fov" ) (to-radians 50 .0 ))
393+ (GL20/glUniform1f (GL20/glGetUniformLocation program-moon " fov" ) (to-radians 25 .0 ))
393394 (GL20/glUniform1f (GL20/glGetUniformLocation program-moon " alpha" ) (to-radians -30.0 ))
394395 (GL20/glUniform1f (GL20/glGetUniformLocation program-moon " beta" ) (to-radians 20.0 ))
395- (GL20/glUniform1f (GL20/glGetUniformLocation program-moon " distance" ) 5 .0 )
396+ (GL20/glUniform1f (GL20/glGetUniformLocation program-moon " distance" ) 10 .0 )
396397 (GL20/glUniform1i (GL20/glGetUniformLocation program-moon " moon" ) 0 )
397398 (GL13/glActiveTexture GL13/GL_TEXTURE0)
398399 (GL11/glBindTexture GL11/GL_TEXTURE_2D texture))
@@ -468,9 +469,61 @@ v-vectors
468469 (GL11/glDrawElements GL11/GL_QUADS (count indices-sphere-2) GL11/GL_UNSIGNED_INT 0 )
469470 (screenshot ))
470471
472+ (GL20/glDeleteProgram program-moon)
473+
474+ ; ; ### Adding ambient and diffuse reflection
475+ (def fragment-moon-diffuse " #version 130
476+ #define PI 3.1415926535897932384626433832795
477+ uniform vec3 light;
478+ uniform float ambient;
479+ uniform float diffuse;
480+ uniform sampler2D moon;
481+ in vec3 vpoint;
482+ out vec4 fragColor;
483+ void main()
484+ {
485+ // Convert vpoint to lat, lon
486+ float phong = ambient + diffuse * max(0.0, dot(light, normalize(vpoint)));
487+ float lon = atan(vpoint.x, -vpoint.z) / (2.0 * PI) + 0.5;
488+ float lat = atan(vpoint.y, length(vpoint.xz)) / PI + 0.5;
489+ fragColor = vec4(texture(moon, vec2(lon, lat)).rgb * phong, 1);
490+ }" )
491+
492+ (def vertex-shader-diffuse (make-shader vertex-moon GL30/GL_VERTEX_SHADER))
493+ (def fragment-shader-diffuse (make-shader fragment-moon-diffuse GL30/GL_FRAGMENT_SHADER))
494+ (def program-diffuse (make-program vertex-shader-diffuse fragment-shader-diffuse))
495+
496+ (do
497+ (GL20/glVertexAttribPointer (GL20/glGetAttribLocation program-diffuse " point" ) 3 GL11/GL_FLOAT false (* 3 Float/BYTES) (* 0 Float/BYTES))
498+ (GL20/glEnableVertexAttribArray 0 ))
499+
500+ (def light (normalize (vec3 -1 0 -1 )))
501+
502+ (do
503+ (GL20/glUseProgram program-diffuse)
504+ (GL20/glUniform2f (GL20/glGetUniformLocation program-diffuse " iResolution" ) window-width window-height)
505+ (GL20/glUniform1f (GL20/glGetUniformLocation program-diffuse " fov" ) (to-radians 25.0 ))
506+ (GL20/glUniform1f (GL20/glGetUniformLocation program-diffuse " alpha" ) (to-radians 0.0 ))
507+ (GL20/glUniform1f (GL20/glGetUniformLocation program-diffuse " beta" ) (to-radians 0.0 ))
508+ (GL20/glUniform1f (GL20/glGetUniformLocation program-diffuse " distance" ) 10.0 )
509+ (GL20/glUniform1f (GL20/glGetUniformLocation program-diffuse " ambient" ) 0.2 )
510+ (GL20/glUniform1f (GL20/glGetUniformLocation program-diffuse " diffuse" ) 0.8 )
511+ (GL20/glUniform3f (GL20/glGetUniformLocation program-diffuse " light" ) (light 0 ) (light 1 ) (light 2 ))
512+ (GL20/glUniform1i (GL20/glGetUniformLocation program-diffuse " moon" ) 0 )
513+ (GL13/glActiveTexture GL13/GL_TEXTURE0)
514+ (GL11/glBindTexture GL11/GL_TEXTURE_2D texture))
515+
516+ ; ; Enable backface culling and render.
517+ (do
518+ (GL11/glClear (bit-or GL11/GL_COLOR_BUFFER_BIT GL11/GL_DEPTH_BUFFER_BIT))
519+ (GL11/glDrawElements GL11/GL_QUADS (count indices-sphere-2) GL11/GL_UNSIGNED_INT 0 )
520+ (screenshot ))
521+
522+ ; ; Destruct vertex array object
471523(teardown-vao vao-sphere)
524+ (GL20/glDeleteProgram program-diffuse)
472525
473- ( GL20/glDeleteProgram program)
526+ ; ; Delete the texture
474527(GL11/glDeleteTextures texture)
475528
476529; ; ### Finalizing GLFW
0 commit comments