Compare commits
2 Commits
10e37cc33f
...
cffe13054f
| Author | SHA1 | Date | |
|---|---|---|---|
| cffe13054f | |||
| ef02f61771 |
@ -1,6 +1,6 @@
|
|||||||
(ns nol.core
|
(ns nol.core
|
||||||
(:require [clojure.math])
|
(:require [clojure.math])
|
||||||
(:import [org.lwjgl.assimp Assimp AIFace AIMaterial AIMesh AIScene AIVector3D]
|
(:import [org.lwjgl.assimp AIColor4D AIFace AIMaterial AIMaterial$Buffer AIMesh AIScene AIVector3D Assimp]
|
||||||
[org.lwjgl.glfw GLFW GLFWErrorCallbackI GLFWKeyCallbackI]
|
[org.lwjgl.glfw GLFW GLFWErrorCallbackI GLFWKeyCallbackI]
|
||||||
[org.lwjgl.opengl GL GL11 GL15 GL20 GL33 GL30 GL45]
|
[org.lwjgl.opengl GL GL11 GL15 GL20 GL33 GL30 GL45]
|
||||||
[org.lwjgl.system MemoryUtil MemoryStack]
|
[org.lwjgl.system MemoryUtil MemoryStack]
|
||||||
@ -43,10 +43,6 @@
|
|||||||
(throw (RuntimeException. (str "Program link error: " (GL20/glGetProgramInfoLog program)))))
|
(throw (RuntimeException. (str "Program link error: " (GL20/glGetProgramInfoLog program)))))
|
||||||
program))
|
program))
|
||||||
|
|
||||||
(defn load-model [path]
|
|
||||||
(Assimp/aiImportFile path (bit-or Assimp/aiProcess_Triangulate
|
|
||||||
Assimp/aiProcess_FlipUVs)))
|
|
||||||
|
|
||||||
(def vertices (float-array [0.0 1.0 0.0
|
(def vertices (float-array [0.0 1.0 0.0
|
||||||
-1.0 -1.0 0.0
|
-1.0 -1.0 0.0
|
||||||
1.0 -1.0 0.0]))
|
1.0 -1.0 0.0]))
|
||||||
@ -62,6 +58,18 @@
|
|||||||
(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)
|
||||||
|
(do
|
||||||
|
(GL20/glUseProgram program)
|
||||||
|
|
||||||
|
(let [color-loc (GL20/glGetUniformLocation program "uColor")]
|
||||||
|
(doseq [m meshes
|
||||||
|
:let [[r g b a] (:color (:mesh m))]]
|
||||||
|
(GL20/glUniform4f color-loc r g b a)
|
||||||
|
(GL30/glBindVertexArray (:vao m))
|
||||||
|
(GL11/glDrawElements GL11/GL_TRIANGLES (:indices-count m) GL11/GL_UNSIGNED_INT 0)))
|
||||||
|
|
||||||
|
(GL30/glBindVertexArray 0)
|
||||||
|
(GL20/glUseProgram 0))
|
||||||
|
|
||||||
(GLFW/glfwSwapBuffers window)))
|
(GLFW/glfwSwapBuffers window)))
|
||||||
|
|
||||||
@ -78,7 +86,7 @@
|
|||||||
(GLFW/glfwWindowHint GLFW/GLFW_CONTEXT_VERSION_MINOR 5)
|
(GLFW/glfwWindowHint GLFW/GLFW_CONTEXT_VERSION_MINOR 5)
|
||||||
(GLFW/glfwWindowHint GLFW/GLFW_OPENGL_PROFILE GLFW/GLFW_OPENGL_CORE_PROFILE)
|
(GLFW/glfwWindowHint GLFW/GLFW_OPENGL_PROFILE GLFW/GLFW_OPENGL_CORE_PROFILE)
|
||||||
|
|
||||||
(let [window (GLFW/glfwCreateWindow 960 300 #_592 "NOL" MemoryUtil/NULL MemoryUtil/NULL)]
|
(let [window (GLFW/glfwCreateWindow 960 #_1900 300 #_1100 "NOL" MemoryUtil/NULL MemoryUtil/NULL)]
|
||||||
(when (= window MemoryUtil/NULL)
|
(when (= window MemoryUtil/NULL)
|
||||||
(GLFW/glfwTerminate)
|
(GLFW/glfwTerminate)
|
||||||
(throw (RuntimeException. "Failed to create window")))
|
(throw (RuntimeException. "Failed to create window")))
|
||||||
@ -126,15 +134,69 @@
|
|||||||
(.setDaemon true)
|
(.setDaemon true)
|
||||||
(.start))))
|
(.start))))
|
||||||
|
|
||||||
|
(defn extract-mesh [^AIScene scene idx]
|
||||||
|
(let [meshes (.mMeshes scene)]
|
||||||
|
(let [mesh (AIMesh/create (long (.get meshes idx)))
|
||||||
|
mat-idx (.mMaterialIndex mesh)
|
||||||
|
mat-ptr ^long (.get (.mMaterials scene) mat-idx)
|
||||||
|
material (AIMaterial/create (long mat-ptr))
|
||||||
|
mat-color (AIColor4D/create)
|
||||||
|
_ (Assimp/aiGetMaterialColor material Assimp/AI_MATKEY_COLOR_DIFFUSE 0 0 ^AIColor4D mat-color)
|
||||||
|
verts (.mVertices mesh)
|
||||||
|
n (.mNumVertices mesh)
|
||||||
|
vert-arr (float-array (* 3 n))
|
||||||
|
faces (.mFaces mesh)
|
||||||
|
nf (.mNumFaces mesh)
|
||||||
|
idx-arr (int-array (* 3 nf))]
|
||||||
|
(dotimes [i n]
|
||||||
|
(let [v (.get verts i)]
|
||||||
|
(aset vert-arr (+ (* i 3) 0) (.x v))
|
||||||
|
(aset vert-arr (+ (* i 3) 1) (.y v))
|
||||||
|
(aset vert-arr (+ (* i 3) 2) (.z v))))
|
||||||
|
(dotimes [i nf]
|
||||||
|
(let [face (.get faces i)
|
||||||
|
idxs (.mIndices face)]
|
||||||
|
(aset idx-arr (+ (* i 3) 0) (.get idxs 0))
|
||||||
|
(aset idx-arr (+ (* i 3) 1) (.get idxs 1))
|
||||||
|
(aset idx-arr (+ (* i 3) 2) (.get idxs 2))))
|
||||||
|
{:verts vert-arr :indices idx-arr :mat-idx (.mMaterialIndex mesh)
|
||||||
|
:color (let [color [(.r mat-color) (.g mat-color) (.b mat-color) (.a mat-color)]]
|
||||||
|
(if (= color [1.0 1.0 1.0 1.0])
|
||||||
|
[(rand) (rand) (rand) 1.0]
|
||||||
|
color))})))
|
||||||
|
|
||||||
|
(defn load-model [path]
|
||||||
|
(let [flags (bit-or Assimp/aiProcess_Triangulate
|
||||||
|
Assimp/aiProcess_FlipUVs)]
|
||||||
|
(Assimp/aiImportFile path flags)))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(start!)
|
(start!)
|
||||||
(stop!)
|
(stop!)
|
||||||
(def ^AIScene scene (load-model "assets/model.glb"))
|
(def ^AIScene scene (load-model "assets/model.glb"))
|
||||||
(.mNumMeshes scene) ;; 54
|
|
||||||
(.mNumMaterials scene) ;; 17
|
(def meshes (with-gl
|
||||||
|
(doall
|
||||||
|
(for [i (range (.mNumMeshes scene))]
|
||||||
|
(let [mesh (extract-mesh scene i)
|
||||||
|
vbo (GL45/glCreateBuffers)
|
||||||
|
ebo (GL45/glCreateBuffers)
|
||||||
|
vao (GL45/glCreateVertexArrays)]
|
||||||
|
(GL45/glNamedBufferStorage vbo (:verts mesh) GL45/GL_DYNAMIC_STORAGE_BIT)
|
||||||
|
(GL45/glNamedBufferStorage ebo (:indices mesh) GL45/GL_DYNAMIC_STORAGE_BIT)
|
||||||
|
(GL45/glVertexArrayElementBuffer vao ebo)
|
||||||
|
(GL45/glVertexArrayVertexBuffer vao 0 vbo 0 (* 3 Float/BYTES))
|
||||||
|
(GL45/glVertexArrayAttribFormat vao 0 3 GL11/GL_FLOAT false 0)
|
||||||
|
(GL45/glVertexArrayAttribBinding vao 0 0)
|
||||||
|
(GL45/glEnableVertexArrayAttrib vao 0)
|
||||||
|
{:vbo vbo :vao vao :ebo ebo
|
||||||
|
:mesh mesh
|
||||||
|
:indices-count (alength (:indices mesh))})))))
|
||||||
|
|
||||||
(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
|
||||||
|
(GL11/glClearColor 0.0 0.0 0.0 1.0))
|
||||||
|
|
||||||
(with-gl
|
(with-gl
|
||||||
(GL45/glNamedBufferSubData (:vbo @state) 0 ^floats (float-array [ 0.0 0.5 0.0
|
(GL45/glNamedBufferSubData (:vbo @state) 0 ^floats (float-array [ 0.0 0.5 0.0
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user