Clean up code, create loading functions, check for gl thread
This commit is contained in:
parent
44ef432386
commit
f68619b25e
116
src/nol/core.clj
116
src/nol/core.clj
@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
(defmacro with-gl [& body]
|
(defmacro with-gl [& body]
|
||||||
`(let [result# (promise)]
|
`(let [result# (promise)]
|
||||||
|
(when-not (and @gl-thread (.isAlive @gl-thread))
|
||||||
|
(throw (RuntimeException. "GL thread is not running")))
|
||||||
(.put render-queue #(deliver result# (try (do ~@body)
|
(.put render-queue #(deliver result# (try (do ~@body)
|
||||||
(catch Exception e# e#))))
|
(catch Exception e# e#))))
|
||||||
(let [r# (deref result#)]
|
(let [r# (deref result#)]
|
||||||
@ -24,10 +26,6 @@
|
|||||||
(throw r#)
|
(throw r#)
|
||||||
r#))))
|
r#))))
|
||||||
|
|
||||||
(def vertex-shader-text (slurp "shaders/base.vert"))
|
|
||||||
|
|
||||||
(def fragment-shader-text (slurp "shaders/base.frag"))
|
|
||||||
|
|
||||||
(defn- compile-shader [type source]
|
(defn- compile-shader [type source]
|
||||||
(let [shader (GL20/glCreateShader type)]
|
(let [shader (GL20/glCreateShader type)]
|
||||||
(GL20/glShaderSource shader ^String source)
|
(GL20/glShaderSource shader ^String source)
|
||||||
@ -45,16 +43,8 @@
|
|||||||
(throw (RuntimeException. (str "Program link error: " (GL20/glGetProgramInfoLog program)))))
|
(throw (RuntimeException. (str "Program link error: " (GL20/glGetProgramInfoLog program)))))
|
||||||
program))
|
program))
|
||||||
|
|
||||||
(def vertices (float-array [0.0 1.0 0.0
|
(def loaded-scenes (atom []))
|
||||||
-1.0 -1.0 0.0
|
(def shaders (atom nil))
|
||||||
1.0 -1.0 0.0]))
|
|
||||||
|
|
||||||
(def loaded (atom false))
|
|
||||||
(def meshes nil)
|
|
||||||
(def textures nil)
|
|
||||||
(def program nil)
|
|
||||||
(def gl-meshes nil)
|
|
||||||
(def gl-textures nil)
|
|
||||||
|
|
||||||
(defn- update-loop [window stack data]
|
(defn- update-loop [window stack data]
|
||||||
(let [w-buf (.mallocInt stack 1)
|
(let [w-buf (.mallocInt stack 1)
|
||||||
@ -66,9 +56,11 @@
|
|||||||
(GL11/glViewport 0 0 w h)
|
(GL11/glViewport 0 0 w h)
|
||||||
(GL11/glClear GL11/GL_COLOR_BUFFER_BIT)
|
(GL11/glClear GL11/GL_COLOR_BUFFER_BIT)
|
||||||
|
|
||||||
(swap! state assoc :last-time t)
|
;; (swap! state assoc :last-time t)
|
||||||
(try
|
(try
|
||||||
(when @loaded
|
(when-let [program @shaders]
|
||||||
|
(doseq [scene @loaded-scenes
|
||||||
|
:let [{:keys [meshes gl-meshes gl-textures]} scene]]
|
||||||
(GL20/glUseProgram program)
|
(GL20/glUseProgram program)
|
||||||
(let [color-loc (GL20/glGetUniformLocation program "uColor")
|
(let [color-loc (GL20/glGetUniformLocation program "uColor")
|
||||||
tex-loc (GL20/glGetUniformLocation program "texture_id")]
|
tex-loc (GL20/glGetUniformLocation program "texture_id")]
|
||||||
@ -79,11 +71,10 @@
|
|||||||
(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))]
|
||||||
(println tex-idx tex-id)
|
|
||||||
(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-loc 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)))
|
||||||
|
|
||||||
@ -93,7 +84,6 @@
|
|||||||
(GLFW/glfwSwapBuffers window)))
|
(GLFW/glfwSwapBuffers window)))
|
||||||
|
|
||||||
(defn stop! []
|
(defn stop! []
|
||||||
(reset! loaded false)
|
|
||||||
(with-gl
|
(with-gl
|
||||||
(GLFW/glfwSetWindowShouldClose
|
(GLFW/glfwSetWindowShouldClose
|
||||||
(:window @state) true)))
|
(:window @state) true)))
|
||||||
@ -124,10 +114,11 @@
|
|||||||
(when (and (= key GLFW/GLFW_KEY_ESCAPE) (= action GLFW/GLFW_PRESS))
|
(when (and (= key GLFW/GLFW_KEY_ESCAPE) (= action GLFW/GLFW_PRESS))
|
||||||
(swap! state assoc :should-close true)))))
|
(swap! state assoc :should-close true)))))
|
||||||
|
|
||||||
(let []
|
|
||||||
(loop []
|
(loop []
|
||||||
(while (not (.isEmpty render-queue))
|
(loop []
|
||||||
((.take render-queue)))
|
(when-let [f (.poll render-queue)]
|
||||||
|
(f)
|
||||||
|
(recur)))
|
||||||
(when-not (or (:should-close @state)
|
(when-not (or (:should-close @state)
|
||||||
(GLFW/glfwWindowShouldClose window))
|
(GLFW/glfwWindowShouldClose window))
|
||||||
(GLFW/glfwPollEvents)
|
(GLFW/glfwPollEvents)
|
||||||
@ -136,7 +127,7 @@
|
|||||||
(update-loop window stack {})
|
(update-loop window stack {})
|
||||||
(finally
|
(finally
|
||||||
(MemoryStack/stackPop))))
|
(MemoryStack/stackPop))))
|
||||||
(recur))))
|
(recur)))
|
||||||
|
|
||||||
(GLFW/glfwDestroyWindow window)
|
(GLFW/glfwDestroyWindow window)
|
||||||
(GLFW/glfwTerminate)))
|
(GLFW/glfwTerminate)))
|
||||||
@ -199,36 +190,12 @@
|
|||||||
Assimp/aiProcess_FlipUVs)]
|
Assimp/aiProcess_FlipUVs)]
|
||||||
(Assimp/aiImportFile path flags)))
|
(Assimp/aiImportFile path flags)))
|
||||||
|
|
||||||
(comment
|
(defn load-scene [path]
|
||||||
(start!)
|
(let [scene (load-model path)
|
||||||
(stop!)
|
meshes (doall
|
||||||
|
|
||||||
(let [mesh (AIMesh/create (long (.get (.mMeshes scene) i)))
|
|
||||||
_ (Assimp/aiGetMaterialTexture material Assimp/aiTextureType_DIFFUSE 0 path no-int no-int no-float no-int no-int no-int)
|
|
||||||
data-path (.dataString path)]
|
|
||||||
(when (.startsWith data-path "*")
|
|
||||||
(println (parse-long (subs data-path 1)))))
|
|
||||||
|
|
||||||
(doseq [i (range (.mNumMeshes scene))]
|
|
||||||
(let [mesh (AIMesh/create (long (.get (.mMeshes scene) i)))
|
|
||||||
^IntBuffer no-int nil
|
|
||||||
^FloatBuffer no-float nil
|
|
||||||
path (AIString/create)
|
|
||||||
_ (Assimp/aiGetMaterialTexture material Assimp/aiTextureType_DIFFUSE 0 path no-int no-int no-float no-int no-int no-int)
|
|
||||||
data-path (.dataString path)]
|
|
||||||
(when (.startsWith data-path "*")
|
|
||||||
(println (parse-long (subs data-path 1))))))
|
|
||||||
|
|
||||||
(reset! loaded true)
|
|
||||||
(reset! loaded false)
|
|
||||||
|
|
||||||
(def ^AIScene scene (load-model "assets/model.glb"))
|
|
||||||
|
|
||||||
(def meshes (doall
|
|
||||||
(for [i (range (.mNumMeshes scene))]
|
(for [i (range (.mNumMeshes scene))]
|
||||||
(extract-mesh scene i))))
|
(extract-mesh scene i)))
|
||||||
|
textures (let [texs (.mTextures scene)]
|
||||||
(def textures (let [texs (.mTextures scene)]
|
|
||||||
(doall
|
(doall
|
||||||
(for [i (range (.mNumTextures scene))]
|
(for [i (range (.mNumTextures scene))]
|
||||||
(let [ai-tex (AITexture/create (.get texs i))
|
(let [ai-tex (AITexture/create (.get texs i))
|
||||||
@ -237,10 +204,11 @@
|
|||||||
h (int-array 1)
|
h (int-array 1)
|
||||||
channels (int-array 1)]
|
channels (int-array 1)]
|
||||||
{:data (STBImage/stbi_load_from_memory buf w h channels 4)
|
{:data (STBImage/stbi_load_from_memory buf w h channels 4)
|
||||||
:w (aget w 0) :h (aget h 0)})))))
|
:w (aget w 0) :h (aget h 0)}))))]
|
||||||
|
{:scene scene :meshes meshes :textures textures}))
|
||||||
|
|
||||||
(def gl-meshes
|
(defn upload-scene! [scene]
|
||||||
(with-gl
|
(let [gl-meshes
|
||||||
(mapv
|
(mapv
|
||||||
#(let [vbo (GL45/glCreateBuffers)
|
#(let [vbo (GL45/glCreateBuffers)
|
||||||
ebo (GL45/glCreateBuffers)
|
ebo (GL45/glCreateBuffers)
|
||||||
@ -261,10 +229,8 @@
|
|||||||
{:vbo vbo :vao vao :ebo ebo
|
{:vbo vbo :vao vao :ebo ebo
|
||||||
:mesh %
|
:mesh %
|
||||||
:indices-count (alength (:indices %))})
|
:indices-count (alength (:indices %))})
|
||||||
meshes)))
|
(:meshes scene))
|
||||||
|
gl-textures
|
||||||
(def gl-textures
|
|
||||||
(with-gl
|
|
||||||
(mapv
|
(mapv
|
||||||
#(let [w (:w %)
|
#(let [w (:w %)
|
||||||
h (:h %)
|
h (:h %)
|
||||||
@ -275,30 +241,24 @@
|
|||||||
(GL45/glTextureParameteri tex-id GL11/GL_TEXTURE_MAG_FILTER GL11/GL_LINEAR)
|
(GL45/glTextureParameteri tex-id GL11/GL_TEXTURE_MAG_FILTER GL11/GL_LINEAR)
|
||||||
(STBImage/stbi_image_free (:data %))
|
(STBImage/stbi_image_free (:data %))
|
||||||
(assoc % :gl-id tex-id))
|
(assoc % :gl-id tex-id))
|
||||||
textures)))
|
(:textures scene))]
|
||||||
|
(assoc scene :gl-textures gl-textures :gl-meshes gl-meshes)))
|
||||||
|
|
||||||
(def program (with-gl (let [vert (compile-shader GL20/GL_VERTEX_SHADER (slurp "shaders/base.vert"))
|
(defn load-shaders! [vert-path frag-path]
|
||||||
frag (compile-shader GL20/GL_FRAGMENT_SHADER (slurp "shaders/base.frag"))]
|
(let [vert (compile-shader GL20/GL_VERTEX_SHADER (slurp vert-path))
|
||||||
(link-program vert frag))))
|
frag (compile-shader GL20/GL_FRAGMENT_SHADER (slurp frag-path))]
|
||||||
|
(link-program vert frag)))
|
||||||
|
|
||||||
|
(comment
|
||||||
|
(start!)
|
||||||
|
(stop!)
|
||||||
|
|
||||||
|
(swap! loaded-scenes conj (with-gl (upload-scene! (load-scene "assets/model.glb"))))
|
||||||
|
(reset! shaders (with-gl (load-shaders! "shaders/base.vert" "shaders/base.frag")))
|
||||||
|
|
||||||
(with-gl
|
(with-gl
|
||||||
(GL11/glClearColor 0.392 0.584 0.929 1.0))
|
(GL11/glClearColor 0.392 0.584 0.929 1.0))
|
||||||
(with-gl
|
(with-gl
|
||||||
(GL11/glClearColor 0.0 0.0 0.0 1.0))
|
(GL11/glClearColor 0.0 0.0 0.0 1.0))
|
||||||
|
|
||||||
(with-gl
|
|
||||||
(GL45/glNamedBufferSubData (:vbo @state) 0 ^floats (float-array [ 0.0 0.5 0.0
|
|
||||||
-0.5 -0.5 0.0
|
|
||||||
0.5 -0.5 0.0])))
|
|
||||||
(with-gl
|
|
||||||
(GL45/glNamedBufferSubData (:vbo @state) 0 ^floats (float-array [ 0.0 1.0 0.0
|
|
||||||
-1.0 -1.0 0.0
|
|
||||||
1.0 -1.0 0.0])))
|
|
||||||
|
|
||||||
(with-gl (GL45/glVertexArrayVertexBuffer vao 0 vbo 0 12))
|
|
||||||
|
|
||||||
(with-gl (GL45/glEnableVertexArrayAttrib vao 0)
|
|
||||||
(GL45/glVertexArrayAttribFormat vao 0 3 GL45/GL_FLOAT false 0)
|
|
||||||
(GL45/glVertexArrayAttribBinding vao 0 0))
|
|
||||||
|
|
||||||
:-)
|
:-)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user