Created
August 30, 2021 18:42
-
-
Save matyklug18/8f74175f258ed073309fb820634110a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns game.core | |
(:import | |
(org.lwjgl.glfw GLFW GLFWKeyCallbackI GLFWWindowSizeCallbackI) | |
(org.lwjgl.opengl GL GL11 GL15 GL20 GL30) | |
(org.lwjgl.system MemoryUtil MemoryStack))) | |
(def width 300) | |
(def height 300) | |
(defn set-uniform [x y & {:keys [shader name :as opts]}] | |
(GL20/glUniform2fv (GL20/glGetUniformLocation (shader :pid) name), (float-array [x y]))) | |
(defn init-window [] | |
(GLFW/glfwInit) | |
(GLFW/glfwWindowHint GLFW/GLFW_VISIBLE GLFW/GLFW_FALSE) | |
(GLFW/glfwWindowHint GLFW/GLFW_RESIZABLE GLFW/GLFW_TRUE) | |
(def window (GLFW/glfwCreateWindow width height "Hello World" MemoryUtil/NULL MemoryUtil/NULL)) | |
(GLFW/glfwMakeContextCurrent window) | |
(GLFW/glfwSwapInterval 1) | |
(GLFW/glfwShowWindow window) | |
(GL/createCapabilities) | |
(GL11/glClearColor 0.0 1.0 1.0 1.0) | |
(GLFW/glfwSetKeyCallback | |
window | |
(reify GLFWKeyCallbackI | |
(invoke [this window key scancode action mods] | |
(when (and (= key GLFW/GLFW_KEY_ESCAPE) (= action GLFW/GLFW_RELEASE)) | |
(GLFW/glfwSetWindowShouldClose window true))))) | |
(GLFW/glfwSetWindowSizeCallback | |
window | |
(reify GLFWWindowSizeCallbackI | |
(invoke [this window width height] | |
(GL11/glViewport 0 0 width height) | |
(def width width) | |
(def height height))))) | |
(defn init-mesh [buf] | |
(def vao (GL30/glGenVertexArrays)) | |
(GL30/glBindVertexArray vao) | |
(def vbo (GL15/glGenBuffers)) | |
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER vbo) | |
(GL15/glBufferData GL15/GL_ARRAY_BUFFER (float-array buf) GL15/GL_STATIC_DRAW) | |
(GL30/glVertexAttribPointer 0 3 GL11/GL_FLOAT false 0 0) | |
(GL15/glBindBuffer GL15/GL_ARRAY_BUFFER 0) | |
(GL30/glEnableVertexAttribArray 0) | |
(GL30/glBindVertexArray 0) | |
{:vao vao :vbo vbo}) | |
(defn init-shader [vf ff] | |
(let [pid (GL20/glCreateProgram) | |
vid (GL20/glCreateShader GL20/GL_VERTEX_SHADER) | |
fid (GL20/glCreateShader GL20/GL_FRAGMENT_SHADER)] | |
(GL20/glShaderSource vid (slurp vf)) | |
(GL20/glCompileShader vid) | |
(GL20/glShaderSource fid (slurp ff)) | |
(GL20/glCompileShader fid) | |
(GL20/glAttachShader pid vid) | |
(GL20/glAttachShader pid fid) | |
(GL20/glLinkProgram pid) | |
(GL20/glValidateProgram pid) | |
{:pid pid})) | |
(defn make-trig [] | |
[-1 +1 +0 +1 +1 +0 +1 -1 +0 | |
-1 +1 +0 -1 -1 +0 +1 -1 +0]) | |
(defn init [] | |
(init-window) | |
(def shader (init-shader "shaders/vert.glsl" "shaders/frag.glsl")) | |
(def mesh (init-mesh (make-trig)))) | |
(defn render [mesh] | |
(GL30/glBindVertexArray (mesh :vao)) | |
(GL20/glUseProgram (shader :pid)) | |
(set-uniform width height :shader shader :name "res") | |
(GL11/glDrawArrays GL11/GL_TRIANGLES 0 (count (make-trig))) | |
(GLFW/glfwSwapBuffers window) | |
(GL30/glBindVertexArray 0) | |
(GL20/glUseProgram 0)) | |
(defn run [] | |
(loop [] | |
(when (not (GLFW/glfwWindowShouldClose window)) | |
(GL11/glClear (bit-or GL11/GL_COLOR_BUFFER_BIT GL11/GL_DEPTH_BUFFER_BIT)) | |
(render mesh) | |
(GLFW/glfwPollEvents) | |
(recur)))) | |
(defn -main [] | |
(init) | |
(run)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment