From cffe13054fac450323c6e9a745519674fcd11118 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 7 Mar 2026 09:50:08 +0700 Subject: [PATCH] Extract and render colors for mesh --- src/nol/core.clj | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/nol/core.clj b/src/nol/core.clj index f335f69..342f49b 100644 --- a/src/nol/core.clj +++ b/src/nol/core.clj @@ -1,6 +1,6 @@ (ns nol.core (: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.opengl GL GL11 GL15 GL20 GL33 GL30 GL45] [org.lwjgl.system MemoryUtil MemoryStack] @@ -62,11 +62,11 @@ (GL20/glUseProgram program) (let [color-loc (GL20/glGetUniformLocation program "uColor")] - (GL20/glUniform4f color-loc 1.0 0.5 0.2 1.0)) - - (doseq [m meshes] - (GL30/glBindVertexArray (:vao m)) - (GL11/glDrawElements GL11/GL_TRIANGLES (:indices-count m) GL11/GL_UNSIGNED_INT 0)) + (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)) @@ -86,7 +86,7 @@ (GLFW/glfwWindowHint GLFW/GLFW_CONTEXT_VERSION_MINOR 5) (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) (GLFW/glfwTerminate) (throw (RuntimeException. "Failed to create window"))) @@ -136,7 +136,12 @@ (defn extract-mesh [^AIScene scene idx] (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) n (.mNumVertices mesh) vert-arr (float-array (* 3 n)) @@ -154,7 +159,11 @@ (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)}))) + {: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 @@ -181,10 +190,13 @@ (GL45/glVertexArrayAttribBinding vao 0 0) (GL45/glEnableVertexArrayAttrib vao 0) {:vbo vbo :vao vao :ebo ebo + :mesh mesh :indices-count (alength (:indices mesh))}))))) (with-gl (GL11/glClearColor 0.392 0.584 0.929 1.0)) + (with-gl + (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