Extract and render colors for mesh

This commit is contained in:
Joseph Ferano 2026-03-07 09:50:08 +07:00
parent ef02f61771
commit cffe13054f

View File

@ -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]
@ -62,11 +62,11 @@
(GL20/glUseProgram program) (GL20/glUseProgram program)
(let [color-loc (GL20/glGetUniformLocation program "uColor")] (let [color-loc (GL20/glGetUniformLocation program "uColor")]
(GL20/glUniform4f color-loc 1.0 0.5 0.2 1.0)) (doseq [m meshes
:let [[r g b a] (:color (:mesh m))]]
(doseq [m meshes] (GL20/glUniform4f color-loc r g b a)
(GL30/glBindVertexArray (:vao m)) (GL30/glBindVertexArray (:vao m))
(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)))
(GL30/glBindVertexArray 0) (GL30/glBindVertexArray 0)
(GL20/glUseProgram 0)) (GL20/glUseProgram 0))
@ -86,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 1900 #_300 1100 "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")))
@ -136,7 +136,12 @@
(defn extract-mesh [^AIScene scene idx] (defn extract-mesh [^AIScene scene idx]
(let [meshes (.mMeshes scene)] (let [meshes (.mMeshes scene)]
(let [mesh (AIMesh/create (.get meshes idx)) (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) verts (.mVertices mesh)
n (.mNumVertices mesh) n (.mNumVertices mesh)
vert-arr (float-array (* 3 n)) vert-arr (float-array (* 3 n))
@ -154,7 +159,11 @@
(aset idx-arr (+ (* i 3) 0) (.get idxs 0)) (aset idx-arr (+ (* i 3) 0) (.get idxs 0))
(aset idx-arr (+ (* i 3) 1) (.get idxs 1)) (aset idx-arr (+ (* i 3) 1) (.get idxs 1))
(aset idx-arr (+ (* i 3) 2) (.get idxs 2)))) (aset idx-arr (+ (* i 3) 2) (.get idxs 2))))
{:verts vert-arr :indices idx-arr :mat-idx (.mMaterialIndex mesh)}))) {: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] (defn load-model [path]
(let [flags (bit-or Assimp/aiProcess_Triangulate (let [flags (bit-or Assimp/aiProcess_Triangulate
@ -181,10 +190,13 @@
(GL45/glVertexArrayAttribBinding vao 0 0) (GL45/glVertexArrayAttribBinding vao 0 0)
(GL45/glEnableVertexArrayAttrib vao 0) (GL45/glEnableVertexArrayAttrib vao 0)
{:vbo vbo :vao vao :ebo ebo {:vbo vbo :vao vao :ebo ebo
:mesh mesh
:indices-count (alength (:indices 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