Get the child nodes translated and rendering and spinning
This commit is contained in:
parent
beed54fd18
commit
7dc8ebf8b2
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
/.cpcache/
|
/.cpcache/
|
||||||
/.nrepl-port
|
/.nrepl-port
|
||||||
|
/.clj-kondo/
|
||||||
|
/.lsp/
|
||||||
|
|||||||
109
src/nol/core.clj
109
src/nol/core.clj
@ -44,12 +44,18 @@
|
|||||||
(throw (RuntimeException. (str "Program link error: " (GL20/glGetProgramInfoLog program)))))
|
(throw (RuntimeException. (str "Program link error: " (GL20/glGetProgramInfoLog program)))))
|
||||||
program))
|
program))
|
||||||
|
|
||||||
(defn ai-matrix->matrix4f [^AIMatrix4x4 m]
|
#_(defn ai-matrix->matrix4f [^AIMatrix4x4 m]
|
||||||
(Matrix4f. (.a1 m) (.a2 m) (.a3 m) (.a4 m)
|
(Matrix4f. (.a1 m) (.a2 m) (.a3 m) (.a4 m)
|
||||||
(.b1 m) (.b2 m) (.b3 m) (.b4 m)
|
(.b1 m) (.b2 m) (.b3 m) (.b4 m)
|
||||||
(.c1 m) (.c2 m) (.c3 m) (.c4 m)
|
(.c1 m) (.c2 m) (.c3 m) (.c4 m)
|
||||||
(.d1 m) (.d2 m) (.d3 m) (.d4 m)))
|
(.d1 m) (.d2 m) (.d3 m) (.d4 m)))
|
||||||
|
|
||||||
|
(defn ai-matrix->matrix4f [^AIMatrix4x4 m]
|
||||||
|
(Matrix4f. (.a1 m) (.b1 m) (.c1 m) (.d1 m)
|
||||||
|
(.a2 m) (.b2 m) (.c2 m) (.d2 m)
|
||||||
|
(.a3 m) (.b3 m) (.c3 m) (.d3 m)
|
||||||
|
(.a4 m) (.b4 m) (.c4 m) (.d4 m)))
|
||||||
|
|
||||||
(defn matrix4f->vectors [^Matrix4f m]
|
(defn matrix4f->vectors [^Matrix4f m]
|
||||||
[(vector (.m00 m) (.m01 m) (.m02 m) (.m03 m))
|
[(vector (.m00 m) (.m01 m) (.m02 m) (.m03 m))
|
||||||
(vector (.m10 m) (.m11 m) (.m12 m) (.m13 m))
|
(vector (.m10 m) (.m11 m) (.m12 m) (.m13 m))
|
||||||
@ -71,27 +77,31 @@
|
|||||||
(def uploaded-scene (atom nil))
|
(def uploaded-scene (atom nil))
|
||||||
(def shaders (atom nil))
|
(def shaders (atom nil))
|
||||||
|
|
||||||
(defn render-scene [scene node mtx]
|
(defn render-scene [stack scene node mtx]
|
||||||
(when-let [mesh-indices (seq (:meshes node))]
|
(let [buf (.mallocFloat stack 16)
|
||||||
(let [{:keys [gl-meshes gl-textures]} scene
|
model-mtx (Matrix4f.)]
|
||||||
{:keys [program uniforms]} @shaders]
|
(.mul mtx (:transform node) model-mtx)
|
||||||
(doseq [idx mesh-indices
|
(.get model-mtx buf)
|
||||||
:let [gl-mesh (nth gl-meshes idx)
|
(when-let [mesh-indices (seq (:meshes node))]
|
||||||
mesh (:mesh gl-mesh)
|
(let [{:keys [gl-meshes gl-textures]} scene
|
||||||
[r g b a] (:color mesh)
|
{:keys [program uniforms]} @shaders]
|
||||||
tex-idx (:texture-idx mesh)]]
|
(doseq [idx mesh-indices
|
||||||
(GL20/glUniform4f (:color uniforms) r g b a)
|
:let [gl-mesh (nth gl-meshes idx)
|
||||||
(GL20/glUniformMatrix4fv (:mm uniforms) false mtx #_(:transform node))
|
mesh (:mesh gl-mesh)
|
||||||
(GL30/glBindVertexArray (:vao gl-mesh))
|
[r g b a] (:color mesh)
|
||||||
(when tex-idx
|
tex-idx (:texture-idx mesh)]]
|
||||||
(let [tex-id (:gl-id (nth gl-textures tex-idx))]
|
(GL20/glUniform4f (:color uniforms) r g b a)
|
||||||
(GL45/glActiveTexture GL45/GL_TEXTURE0)
|
(GL20/glUniformMatrix4fv (:mm uniforms) false buf)
|
||||||
(GL11/glBindTexture GL11/GL_TEXTURE_2D tex-id)
|
(GL30/glBindVertexArray (:vao gl-mesh))
|
||||||
(GL20/glUniform1i (:tex uniforms) 0)))
|
(when tex-idx
|
||||||
(GL11/glDrawElements GL11/GL_TRIANGLES (:indices-count gl-mesh) GL11/GL_UNSIGNED_INT 0))))
|
(let [tex-id (:gl-id (nth gl-textures tex-idx))]
|
||||||
(when-let [children (seq (:children node))]
|
(GL45/glActiveTexture GL45/GL_TEXTURE0)
|
||||||
(doseq [child children]
|
(GL11/glBindTexture GL11/GL_TEXTURE_2D tex-id)
|
||||||
(render-scene scene child mtx))))
|
(GL20/glUniform1i (:tex uniforms) 0)))
|
||||||
|
(GL11/glDrawElements GL11/GL_TRIANGLES (:indices-count gl-mesh) GL11/GL_UNSIGNED_INT 0))))
|
||||||
|
(when-let [children (seq (:children node))]
|
||||||
|
(doseq [child children]
|
||||||
|
(render-scene stack scene child model-mtx)))))
|
||||||
|
|
||||||
(defn- update-loop [window stack data]
|
(defn- update-loop [window stack data]
|
||||||
(let [w-buf (.mallocInt stack 1)
|
(let [w-buf (.mallocInt stack 1)
|
||||||
@ -110,12 +120,27 @@
|
|||||||
(let [scene @uploaded-scene
|
(let [scene @uploaded-scene
|
||||||
{:keys [program uniforms]} @shaders]
|
{:keys [program uniforms]} @shaders]
|
||||||
(GL20/glUseProgram program)
|
(GL20/glUseProgram program)
|
||||||
(let [buf (.mallocFloat stack 16)
|
(let [vbuf (.mallocFloat stack 16)
|
||||||
m (Matrix4f.)]
|
pbuf (.mallocFloat stack 16)
|
||||||
(.get m buf)
|
radius 10.0
|
||||||
(GL20/glUniformMatrix4fv (:mv uniforms) false buf)
|
cam-x (* radius (Math/sin t))
|
||||||
(GL20/glUniformMatrix4fv (:mp uniforms) false buf)
|
cam-z (* radius (Math/cos t))
|
||||||
(render-scene scene (:hiearchy scene) buf))))
|
vm (-> (Matrix4f.)
|
||||||
|
(.identity)
|
||||||
|
(.lookAt (float cam-x) (float 0.0) (float cam-z)
|
||||||
|
(float 0.0) (float 0.0) (float 0.0)
|
||||||
|
(float 0.0) (float 1.0) (float 0.0)))
|
||||||
|
pm (-> (Matrix4f.)
|
||||||
|
(.identity)
|
||||||
|
(.perspective (float (Math/toRadians 45))
|
||||||
|
(float (/ (float w) (float h)))
|
||||||
|
(float 0.1)
|
||||||
|
(float 100.0)))]
|
||||||
|
(.get vm vbuf)
|
||||||
|
(.get pm pbuf)
|
||||||
|
(GL20/glUniformMatrix4fv (:mv uniforms) false vbuf)
|
||||||
|
(GL20/glUniformMatrix4fv (:mp uniforms) false pbuf)
|
||||||
|
(render-scene stack scene (:hiearchy scene) (Matrix4f.)))))
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
(println "Update Loop GL thread error:" e)))
|
(println "Update Loop GL thread error:" e)))
|
||||||
|
|
||||||
@ -125,11 +150,20 @@
|
|||||||
(GLFW/glfwSwapBuffers window)))
|
(GLFW/glfwSwapBuffers window)))
|
||||||
|
|
||||||
(defn stop! []
|
(defn stop! []
|
||||||
(reset! uploaded-scene nil)
|
|
||||||
(reset! shaders nil)
|
|
||||||
(with-gl
|
(with-gl
|
||||||
|
(when @uploaded-scene
|
||||||
|
(doseq [{:keys [vbo vao ebo]} (:gl-meshes @uploaded-scene)]
|
||||||
|
(GL45/glDeleteBuffers vbo)
|
||||||
|
(GL45/glDeleteBuffers ebo)
|
||||||
|
(GL30/glDeleteVertexArrays vao))
|
||||||
|
(doseq [{:keys [gl-id]} (:gl-textures @uploaded-scene)]
|
||||||
|
(GL11/glDeleteTextures gl-id)))
|
||||||
|
(when @shaders
|
||||||
|
(GL20/glDeleteProgram (:program @shaders)))
|
||||||
(GLFW/glfwSetWindowShouldClose
|
(GLFW/glfwSetWindowShouldClose
|
||||||
(:window @state) true)))
|
(:window @state) true))
|
||||||
|
(reset! uploaded-scene nil)
|
||||||
|
(reset! shaders nil))
|
||||||
|
|
||||||
(defn -main []
|
(defn -main []
|
||||||
(when-not (GLFW/glfwInit)
|
(when-not (GLFW/glfwInit)
|
||||||
@ -147,6 +181,9 @@
|
|||||||
|
|
||||||
(GLFW/glfwMakeContextCurrent window)
|
(GLFW/glfwMakeContextCurrent window)
|
||||||
(GL/createCapabilities)
|
(GL/createCapabilities)
|
||||||
|
(GL11/glEnable GL11/GL_DEPTH_TEST)
|
||||||
|
(GL11/glEnable GL11/GL_CULL_FACE)
|
||||||
|
(GL11/glCullFace GL11/GL_BACK)
|
||||||
(GLFW/glfwSwapInterval 1)
|
(GLFW/glfwSwapInterval 1)
|
||||||
|
|
||||||
(swap! state assoc :window window :should-close false)
|
(swap! state assoc :window window :should-close false)
|
||||||
@ -188,6 +225,11 @@
|
|||||||
(.setDaemon true)
|
(.setDaemon true)
|
||||||
(.start))))
|
(.start))))
|
||||||
|
|
||||||
|
(defn load-model [path]
|
||||||
|
(let [flags (bit-or Assimp/aiProcess_Triangulate
|
||||||
|
Assimp/aiProcess_FlipUVs)]
|
||||||
|
(Assimp/aiImportFile path flags)))
|
||||||
|
|
||||||
(defn extract-mesh [^AIScene scene idx]
|
(defn extract-mesh [^AIScene scene idx]
|
||||||
(let [meshes (.mMeshes scene)]
|
(let [meshes (.mMeshes scene)]
|
||||||
(let [mesh (AIMesh/create (long (.get meshes idx)))
|
(let [mesh (AIMesh/create (long (.get meshes idx)))
|
||||||
@ -228,11 +270,6 @@
|
|||||||
(parse-long (subs data-path 1)))
|
(parse-long (subs data-path 1)))
|
||||||
:color [(.r mat-color) (.g mat-color) (.b mat-color) (.a mat-color)]})))
|
:color [(.r mat-color) (.g mat-color) (.b mat-color) (.a mat-color)]})))
|
||||||
|
|
||||||
(defn load-model [path]
|
|
||||||
(let [flags (bit-or Assimp/aiProcess_Triangulate
|
|
||||||
Assimp/aiProcess_FlipUVs)]
|
|
||||||
(Assimp/aiImportFile path flags)))
|
|
||||||
|
|
||||||
(defn traverse [^AINode node]
|
(defn traverse [^AINode node]
|
||||||
{:node node
|
{:node node
|
||||||
:meshes (let [buf (.mMeshes node)]
|
:meshes (let [buf (.mMeshes node)]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user