From e151bdc34eb1de5e964b9f49e61cc4bb46ff7fd4 Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 19 May 2025 12:12:36 -0400 Subject: [PATCH] materials read some properties from assimp --- assets/Shaders/lib/material.fs | 16 +----- docs/src/progress/renderertodo.md | 1 + .../renderer/model/Material.java | 55 +++++++++++++++++++ 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/assets/Shaders/lib/material.fs b/assets/Shaders/lib/material.fs index 92ce647a..76679cd6 100644 --- a/assets/Shaders/lib/material.fs +++ b/assets/Shaders/lib/material.fs @@ -16,20 +16,10 @@ struct Material { /** * The shininess value */ - float metallic; + float shininess; /** - * The shininess value + * The reflectivity value */ - float roughness; - - /** - * The shininess value - */ - float ior; - - /** - * The shininess value - */ - float alpha; + float reflectivity; }; diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index a88b9ae2..ead9531c 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1899,6 +1899,7 @@ Shader uniform parsing from source code Visual shader uniform location caching Remove old uniforms from shaders and code to push uniforms Break out shader material into dedicated library file +Materials read some properties from assimp data diff --git a/src/main/java/electrosphere/renderer/model/Material.java b/src/main/java/electrosphere/renderer/model/Material.java index cf3de0f0..6ca5ac66 100644 --- a/src/main/java/electrosphere/renderer/model/Material.java +++ b/src/main/java/electrosphere/renderer/model/Material.java @@ -14,8 +14,11 @@ import org.lwjgl.assimp.AIString; import org.lwjgl.assimp.AITexture; import org.lwjgl.assimp.Assimp; import org.lwjgl.opengl.GL45; +import org.lwjgl.system.MemoryStack; import java.io.File; +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.file.Path; @@ -23,6 +26,16 @@ import java.nio.file.Path; * A material */ public class Material { + + /** + * Default shininess + */ + public static final double DEFAULT_SHININESS = 0.0f; + + /** + * The default reflectivity + */ + public static final double DEFAULT_REFLECTIVITY = 0.0f; /** @@ -55,6 +68,16 @@ public class Material { * texture pointer for the normal */ private int normalPointer; + + /** + * The shininess value + */ + private double shininess = Material.DEFAULT_SHININESS; + + /** + * The reflectivity value + */ + private double reflectivity = Material.DEFAULT_REFLECTIVITY; /** * A material that contains textures @@ -83,6 +106,11 @@ public class Material { AIString aiPathString = AIString.calloc(); //read props + try(MemoryStack stack = MemoryStack.stackPush()){ + //grab specific values + rVal.reflectivity = Material.readFloatProp(stack, Assimp.AI_MATKEY_REFLECTIVITY, input, Material.DEFAULT_REFLECTIVITY); + rVal.shininess = Material.readFloatProp(stack, Assimp.AI_MATKEY_SHININESS, input, Material.DEFAULT_SHININESS); + } //read textures PointerBuffer texturePtrBuff = scene.mTextures(); @@ -270,6 +298,28 @@ public class Material { normalPointer = pointer; usesFetch = false; } + + /** + * Reads a float property from a material + * @param stack the memory stack + * @param key The key for the property + * @param input The input material + * @param defaultVal The default value + * @return The float value + */ + private static float readFloatProp(MemoryStack stack, String key, AIMaterial input, double defaultVal){ + int numFloats = 1; + FloatBuffer buff = stack.callocFloat(numFloats); + IntBuffer floatCountBuff = stack.ints(numFloats); + ByteBuffer keyBuff = stack.ASCII(key); + if(Assimp.aiReturn_SUCCESS == Assimp.aiGetMaterialFloatArray(input, keyBuff, Assimp.aiTextureType_NONE, 0, buff, floatCountBuff)){ + int numFloatsFound = floatCountBuff.get(); + if(numFloatsFound > 0){ + return buff.get(); + } + } + return (float)defaultVal; + } /** * Applies the material @@ -299,6 +349,11 @@ public class Material { openGLState.glBindTextureUnit(GL45.GL_TEXTURE0, texturePointer, GL45.GL_TEXTURE_2D); Globals.renderingEngine.checkError(); } + //send physical properties + openGLState.getActiveShader().setUniform(openGLState, "material.shininess", this.shininess); + Globals.renderingEngine.checkError(); + openGLState.getActiveShader().setUniform(openGLState, "material.reflectivity", this.reflectivity); + Globals.renderingEngine.checkError(); } /**