MVP matrix uniforms with identity

This commit is contained in:
Joseph Ferano 2026-03-10 10:28:14 +07:00
parent f68619b25e
commit fccb0ef878
2 changed files with 42 additions and 20 deletions

View File

@ -1,5 +1,9 @@
#version 330 #version 330
uniform mat4 uModel;
uniform mat4 uView;
uniform mat4 uProjection;
in vec3 vPos; in vec3 vPos;
in vec2 aUV; in vec2 aUV;
@ -8,5 +12,5 @@ out vec2 vUV;
void main() void main()
{ {
vUV = aUV; vUV = aUV;
gl_Position = vec4(vPos, 1.0); gl_Position = uProjection * uView * uModel * vec4(vPos, 1.0);
} }

View File

@ -7,6 +7,7 @@
[org.lwjgl.glfw GLFW GLFWKeyCallbackI] [org.lwjgl.glfw GLFW GLFWKeyCallbackI]
[org.lwjgl.opengl GL GL11 GL20 GL30 GL45] [org.lwjgl.opengl GL GL11 GL20 GL30 GL45]
[org.lwjgl.stb STBImage] [org.lwjgl.stb STBImage]
[org.joml Matrix4f]
[org.lwjgl.system MemoryStack MemoryUtil])) [org.lwjgl.system MemoryStack MemoryUtil]))
(def render-queue (java.util.concurrent.LinkedBlockingQueue.)) (def render-queue (java.util.concurrent.LinkedBlockingQueue.))
@ -44,6 +45,7 @@
program)) program))
(def loaded-scenes (atom [])) (def loaded-scenes (atom []))
(def uploaded-scenes (atom []))
(def shaders (atom nil)) (def shaders (atom nil))
(defn- update-loop [window stack data] (defn- update-loop [window stack data]
@ -58,23 +60,28 @@
;; (swap! state assoc :last-time t) ;; (swap! state assoc :last-time t)
(try (try
(when-let [program @shaders] (when (and (seq @uploaded-scenes) @shaders)
(doseq [scene @loaded-scenes (doseq [scene @uploaded-scenes
:let [{:keys [meshes gl-meshes gl-textures]} scene]] :let [{:keys [meshes gl-meshes gl-textures]} scene
{:keys [program uniforms]} @shaders]]
(GL20/glUseProgram program) (GL20/glUseProgram program)
(let [color-loc (GL20/glGetUniformLocation program "uColor")
tex-loc (GL20/glGetUniformLocation program "texture_id")]
(doseq [m gl-meshes (doseq [m gl-meshes
:let [[r g b a] (:color (:mesh m)) :let [[r g b a] (:color (:mesh m))
tex-idx (:texture-idx (:mesh m))]] tex-idx (:texture-idx (:mesh m))]]
(GL20/glUniform4f color-loc r g b a) (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)) (GL30/glBindVertexArray (:vao m))
(when tex-idx (when tex-idx
(let [tex-id (:gl-id (nth gl-textures tex-idx))] (let [tex-id (:gl-id (nth gl-textures tex-idx))]
(GL45/glActiveTexture GL45/GL_TEXTURE0) (GL45/glActiveTexture GL45/GL_TEXTURE0)
(GL11/glBindTexture GL11/GL_TEXTURE_2D tex-id) (GL11/glBindTexture GL11/GL_TEXTURE_2D tex-id)
(GL20/glUniform1i tex-loc 0))) (GL20/glUniform1i (:tex uniforms) 0)))
(GL11/glDrawElements GL11/GL_TRIANGLES (:indices-count m) GL11/GL_UNSIGNED_INT 0))))) (GL11/glDrawElements GL11/GL_TRIANGLES (:indices-count m) GL11/GL_UNSIGNED_INT 0))))
(catch Exception e (catch Exception e
(println "Update Loop GL thread error:" e))) (println "Update Loop GL thread error:" e)))
@ -84,6 +91,8 @@
(GLFW/glfwSwapBuffers window))) (GLFW/glfwSwapBuffers window)))
(defn stop! [] (defn stop! []
(reset! uploaded-scenes [])
(reset! shaders nil)
(with-gl (with-gl
(GLFW/glfwSetWindowShouldClose (GLFW/glfwSetWindowShouldClose
(:window @state) true))) (:window @state) true)))
@ -246,14 +255,23 @@
(defn load-shaders! [vert-path frag-path] (defn load-shaders! [vert-path frag-path]
(let [vert (compile-shader GL20/GL_VERTEX_SHADER (slurp vert-path)) (let [vert (compile-shader GL20/GL_VERTEX_SHADER (slurp vert-path))
frag (compile-shader GL20/GL_FRAGMENT_SHADER (slurp frag-path))] frag (compile-shader GL20/GL_FRAGMENT_SHADER (slurp frag-path))
(link-program vert frag))) 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 (comment
(start!) (start!)
(stop!) (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"))) (reset! shaders (with-gl (load-shaders! "shaders/base.vert" "shaders/base.frag")))
(with-gl (with-gl