From fccb0ef878c5c4d3f0bd2f31f00d24bbf9ee5574 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Tue, 10 Mar 2026 10:28:14 +0700 Subject: [PATCH] MVP matrix uniforms with identity --- shaders/base.vert | 6 ++++- src/nol/core.clj | 56 +++++++++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/shaders/base.vert b/shaders/base.vert index 8d743f0..119cd34 100644 --- a/shaders/base.vert +++ b/shaders/base.vert @@ -1,5 +1,9 @@ #version 330 +uniform mat4 uModel; +uniform mat4 uView; +uniform mat4 uProjection; + in vec3 vPos; in vec2 aUV; @@ -8,5 +12,5 @@ out vec2 vUV; void main() { vUV = aUV; - gl_Position = vec4(vPos, 1.0); + gl_Position = uProjection * uView * uModel * vec4(vPos, 1.0); } diff --git a/src/nol/core.clj b/src/nol/core.clj index 04c0250..e58cf35 100644 --- a/src/nol/core.clj +++ b/src/nol/core.clj @@ -7,6 +7,7 @@ [org.lwjgl.glfw GLFW GLFWKeyCallbackI] [org.lwjgl.opengl GL GL11 GL20 GL30 GL45] [org.lwjgl.stb STBImage] + [org.joml Matrix4f] [org.lwjgl.system MemoryStack MemoryUtil])) (def render-queue (java.util.concurrent.LinkedBlockingQueue.)) @@ -44,6 +45,7 @@ program)) (def loaded-scenes (atom [])) +(def uploaded-scenes (atom [])) (def shaders (atom nil)) (defn- update-loop [window stack data] @@ -58,23 +60,28 @@ ;; (swap! state assoc :last-time t) (try - (when-let [program @shaders] - (doseq [scene @loaded-scenes - :let [{:keys [meshes gl-meshes gl-textures]} scene]] + (when (and (seq @uploaded-scenes) @shaders) + (doseq [scene @uploaded-scenes + :let [{:keys [meshes gl-meshes gl-textures]} scene + {:keys [program uniforms]} @shaders]] (GL20/glUseProgram program) - (let [color-loc (GL20/glGetUniformLocation program "uColor") - tex-loc (GL20/glGetUniformLocation program "texture_id")] - (doseq [m gl-meshes - :let [[r g b a] (:color (:mesh m)) - tex-idx (:texture-idx (:mesh m))]] - (GL20/glUniform4f color-loc r g b a) - (GL30/glBindVertexArray (:vao m)) - (when tex-idx - (let [tex-id (:gl-id (nth gl-textures tex-idx))] - (GL45/glActiveTexture GL45/GL_TEXTURE0) - (GL11/glBindTexture GL11/GL_TEXTURE_2D tex-id) - (GL20/glUniform1i tex-loc 0))) - (GL11/glDrawElements GL11/GL_TRIANGLES (:indices-count m) GL11/GL_UNSIGNED_INT 0))))) + (doseq [m gl-meshes + :let [[r g b a] (:color (:mesh m)) + tex-idx (:texture-idx (:mesh m))]] + (GL20/glUniform4f (:color uniforms) r g b a) + (let [buf (.mallocFloat stack 16) + m (Matrix4f.)] + (.get m buf) + (GL20/glUniformMatrix4fv (:mm uniforms) false buf) + (GL20/glUniformMatrix4fv (:mv uniforms) false buf) + (GL20/glUniformMatrix4fv (:mp uniforms) false buf)) + (GL30/glBindVertexArray (:vao m)) + (when tex-idx + (let [tex-id (:gl-id (nth gl-textures tex-idx))] + (GL45/glActiveTexture GL45/GL_TEXTURE0) + (GL11/glBindTexture GL11/GL_TEXTURE_2D tex-id) + (GL20/glUniform1i (:tex uniforms) 0))) + (GL11/glDrawElements GL11/GL_TRIANGLES (:indices-count m) GL11/GL_UNSIGNED_INT 0)))) (catch Exception e (println "Update Loop GL thread error:" e))) @@ -84,6 +91,8 @@ (GLFW/glfwSwapBuffers window))) (defn stop! [] + (reset! uploaded-scenes []) + (reset! shaders nil) (with-gl (GLFW/glfwSetWindowShouldClose (:window @state) true))) @@ -246,14 +255,23 @@ (defn load-shaders! [vert-path frag-path] (let [vert (compile-shader GL20/GL_VERTEX_SHADER (slurp vert-path)) - frag (compile-shader GL20/GL_FRAGMENT_SHADER (slurp frag-path))] - (link-program vert frag))) + frag (compile-shader GL20/GL_FRAGMENT_SHADER (slurp frag-path)) + program (link-program vert frag)] + {:program program + :uniforms {:color (GL20/glGetUniformLocation program "uColor") + :tex (GL20/glGetUniformLocation program "texture_id") + :mm (GL20/glGetUniformLocation program "uModel") + :mv (GL20/glGetUniformLocation program "uView") + :mp (GL20/glGetUniformLocation program "uProjection")}})) (comment (start!) (stop!) - (swap! loaded-scenes conj (with-gl (upload-scene! (load-scene "assets/model.glb")))) + (@loaded-scenes) + + (swap! loaded-scenes conj (load-scene "assets/model.glb")) + (swap! uploaded-scenes conj (with-gl (upload-scene! (first @loaded-scenes)))) (reset! shaders (with-gl (load-shaders! "shaders/base.vert" "shaders/base.frag"))) (with-gl