From 69af65ab53f792cbecfd4af616fc56b349dc336b Mon Sep 17 00:00:00 2001 From: austin Date: Sun, 18 May 2025 22:49:30 -0400 Subject: [PATCH] rendering code cleanup work --- docs/src/progress/renderertodo.md | 2 + .../electrosphere/renderer/actor/Actor.java | 46 +++-- .../electrosphere/renderer/model/Mesh.java | 172 +++++++++++------- .../electrosphere/renderer/model/Model.java | 67 +++++-- 4 files changed, 195 insertions(+), 92 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index b03341cf..32e3238f 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1886,6 +1886,8 @@ Support lack of macro data for chunk gens Support multichunk select all blocks More block types New door furniture item +Commenting rendering classes +Convert Mesh.java to only use GL45 diff --git a/src/main/java/electrosphere/renderer/actor/Actor.java b/src/main/java/electrosphere/renderer/actor/Actor.java index 3e89aabb..f86a6d15 100644 --- a/src/main/java/electrosphere/renderer/actor/Actor.java +++ b/src/main/java/electrosphere/renderer/actor/Actor.java @@ -44,35 +44,55 @@ public class Actor { */ public static final int INVALID_ANIMATION = -1; - //the model path of the model backing the actor + /** + * the model path of the model backing the actor + */ String modelPath; - //used for statically binding textures in pipelines that dont bind textures on a per-mesh level - //ie when in the ui, this can be set to bind a texture at the actor level + /** + * used for statically binding textures in pipelines that dont bind textures on a per-mesh level + * ie when in the ui, this can be set to bind a texture at the actor level + */ String textureOverride; - //scales the time that animations are played at + /** + * scales the time that animations are played at + */ float animationScalar = 1.0f; - //The stack of animations being applied to a given actor + /** + * The stack of animations being applied to a given actor + */ Set animationQueue = new TreeSet(); - //Mask for overwriting meshes in a given actor + /** + * Mask for overwriting meshes in a given actor + */ ActorMeshMask meshMask = new ActorMeshMask(); - //optional overrides for specific shaders + /** + * optional overrides for specific shaders + */ List shaderMasks = new LinkedList(); - //optional overrides for textures + /** + * optional overrides for textures + */ Map textureMap = null; - //bone rotators + /** + * bone rotators + */ Map boneRotators = new HashMap(); - //static morph for this specific actor + /** + * static morph for this specific actor + */ ActorStaticMorph staticMorph; - //The list of bone groups + /** + * The list of bone groups + */ List boneGroups; /** @@ -80,7 +100,9 @@ public class Actor { */ ActorUniformMap uniformMap = new ActorUniformMap(); - //Controls whether the actor should obey frustum culling + /** + * Controls whether the actor should obey frustum culling + */ boolean frustumCull = true; /** diff --git a/src/main/java/electrosphere/renderer/model/Mesh.java b/src/main/java/electrosphere/renderer/model/Mesh.java index f5999275..0c3c159c 100644 --- a/src/main/java/electrosphere/renderer/model/Mesh.java +++ b/src/main/java/electrosphere/renderer/model/Mesh.java @@ -25,24 +25,6 @@ import org.joml.Vector3d; import org.joml.Vector3f; import org.joml.Vector3i; import org.joml.Vector4f; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL15; -import org.lwjgl.opengl.GL40; - -import static org.lwjgl.opengl.GL11.GL_FLOAT; -import static org.lwjgl.opengl.GL11.GL_INT; -import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; -import static org.lwjgl.opengl.GL11.GL_TRIANGLES; -import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; -import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; -import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; -import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; -import static org.lwjgl.opengl.GL15.glBindBuffer; -import static org.lwjgl.opengl.GL15.glGenBuffers; -import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; -import static org.lwjgl.opengl.GL20.glVertexAttribPointer; -import static org.lwjgl.opengl.GL30.glBindVertexArray; -import static org.lwjgl.opengl.GL30.glGenVertexArrays; import org.lwjgl.opengl.GL45; import org.lwjgl.system.MemoryStack; @@ -52,24 +34,64 @@ import org.lwjgl.system.MemoryStack; */ public class Mesh { - //the name of the mesh + /** + * The name of this mesh + */ private String meshName; - //THIS IS NOT GUARANTEED TO BE THE PARENT MODEL THAT THIS WAS LOADED IN - //THIS CAN BE POST-LOAD SET IN MODEL VIA MODELMASK BEHAVIOR + /** + * The parent model + *

+ * THIS IS NOT GUARANTEED TO BE THE PARENT MODEL THAT THIS WAS LOADED IN + * THIS CAN BE POST-LOAD SET IN MODEL VIA MODELMASK BEHAVIOR + *

+ */ private Model parent; //various buffers that may or may not be allocated + /** + * Pointer to the vertex buffer + */ private int vertexBuffer; + + /** + * Pointer to the normal buffer + */ private int normalBuffer; + + /** + * Pointer to the element array buffer + */ private int elementArrayBuffer; - private int elementCount; //pulls double duty as both the element array count as well as the direct array count, based on whichever is used + + /** + * pulls double duty as both the element array count as well as the direct array count, based on whichever is used + */ + private int elementCount; + + /** + * Pointer to the VAO + */ private int vertexArrayObject; + + /** + * Pointer to the bone weight buffer + */ private int boneWeightBuffer; + + /** + * Pointer to the bone index buffer + */ private int boneIndexBuffer; + + /** + * Pointer to the texture coordinate buffer + */ private int textureCoordBuffer; - //uses element instances + /** + * Tracks whether the mesh uses element instances or not + */ private boolean useElementArray = true; @@ -78,20 +100,34 @@ public class Mesh { private List bones = new ArrayList(); private ArrayList boneIdList = new ArrayList(); - //the texture mask that may or may not be masking the mesh + /** + * the texture mask that may or may not be masking the mesh + */ private ActorTextureMask textureMask; - //the shaders currently associated with the mesh + /** + * The main shader for the mesh + */ private VisualShader shader; + + /** + * The OIT shader + */ private VisualShader oitShader; - //the uniforms to be sent to the gpu + /** + * the uniforms to be sent to the gpu + */ private HashMap uniforms = new HashMap(); - //the material currently associated with the mesh + /** + * the material currently associated with the mesh + */ private Material material; - //the bounding sphere for this mesh + /** + * the bounding sphere for this mesh + */ private Sphered boundingSphere; /** @@ -107,15 +143,15 @@ public class Mesh { * Generates the VAO for this mesh */ public void generateVAO(){ - vertexArrayObject = glGenVertexArrays(); - glBindVertexArray(vertexArrayObject); + vertexArrayObject = GL45.glGenVertexArrays(); + GL45.glBindVertexArray(vertexArrayObject); } /** * Frees this mesh */ public void free(){ - GL40.glDeleteBuffers(new int[]{ + GL45.glDeleteBuffers(new int[]{ vertexBuffer, normalBuffer, elementArrayBuffer, @@ -123,7 +159,7 @@ public class Mesh { boneIndexBuffer, textureCoordBuffer, }); - GL40.glDeleteVertexArrays(vertexArrayObject); + GL45.glDeleteVertexArrays(vertexArrayObject); } /** @@ -155,9 +191,9 @@ public class Mesh { */ public void bufferFaces(IntBuffer faces, int elementCount){ if(!EngineState.EngineFlags.HEADLESS){ - elementArrayBuffer = glGenBuffers(); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBuffer); - GL15.glBufferData(GL_ELEMENT_ARRAY_BUFFER, faces, GL_STATIC_DRAW); + elementArrayBuffer = GL45.glGenBuffers(); + GL45.glBindBuffer(GL45.GL_ELEMENT_ARRAY_BUFFER, elementArrayBuffer); + GL45.glBufferData(GL45.GL_ELEMENT_ARRAY_BUFFER, faces, GL45.GL_STATIC_DRAW); this.elementCount = elementCount; } } @@ -186,11 +222,11 @@ public class Mesh { * @param buffer The buffer containing the bone indices */ public void bufferBoneIndices(FloatBuffer buffer){ - boneIndexBuffer = glGenBuffers(); - glBindBuffer(GL_ARRAY_BUFFER, boneIndexBuffer); - GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); - glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); - glEnableVertexAttribArray(3); + boneIndexBuffer = GL45.glGenBuffers(); + GL45.glBindBuffer(GL45.GL_ARRAY_BUFFER, boneIndexBuffer); + GL45.glBufferData(GL45.GL_ARRAY_BUFFER, buffer, GL45.GL_STATIC_DRAW); + GL45.glVertexAttribPointer(3, 4, GL45.GL_FLOAT, false, 0, 0); + GL45.glEnableVertexAttribArray(3); } /** @@ -198,11 +234,11 @@ public class Mesh { * @param buffer The buffer containing the bone weights */ public void bufferBoneWeights(FloatBuffer buffer){ - boneWeightBuffer = glGenBuffers(); - glBindBuffer(GL_ARRAY_BUFFER, boneWeightBuffer); - GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); - glVertexAttribPointer(2, 4, GL_FLOAT, false, 0, 0); - glEnableVertexAttribArray(2); + boneWeightBuffer = GL45.glGenBuffers(); + GL45.glBindBuffer(GL45.GL_ARRAY_BUFFER, boneWeightBuffer); + GL45.glBufferData(GL45.GL_ARRAY_BUFFER, buffer, GL45.GL_STATIC_DRAW); + GL45.glVertexAttribPointer(2, 4, GL45.GL_FLOAT, false, 0, 0); + GL45.glEnableVertexAttribArray(2); } /** @@ -215,11 +251,11 @@ public class Mesh { public int bufferCustomFloatAttribArray(FloatBuffer buffer, int bufferDimension, int attribIndex){ int bufferPointer = 0; if(!EngineState.EngineFlags.HEADLESS){ - bufferPointer = glGenBuffers(); - glBindBuffer(GL_ARRAY_BUFFER, bufferPointer); - GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); - glVertexAttribPointer(attribIndex, bufferDimension, GL_FLOAT, false, 0, 0); - glEnableVertexAttribArray(attribIndex); + bufferPointer = GL45.glGenBuffers(); + GL45.glBindBuffer(GL45.GL_ARRAY_BUFFER, bufferPointer); + GL45.glBufferData(GL45.GL_ARRAY_BUFFER, buffer, GL45.GL_STATIC_DRAW); + GL45.glVertexAttribPointer(attribIndex, bufferDimension, GL45.GL_FLOAT, false, 0, 0); + GL45.glEnableVertexAttribArray(attribIndex); } return bufferPointer; } @@ -234,11 +270,11 @@ public class Mesh { public int bufferCustomIntAttribArray(IntBuffer buffer, int bufferDimension, int attribIndex){ int bufferPointer = 0; if(!EngineState.EngineFlags.HEADLESS){ - bufferPointer = glGenBuffers(); - glBindBuffer(GL_ARRAY_BUFFER, bufferPointer); - GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); - GL40.glVertexAttribIPointer(attribIndex, bufferDimension, GL_INT, 0, 0); - glEnableVertexAttribArray(attribIndex); + bufferPointer = GL45.glGenBuffers(); + GL45.glBindBuffer(GL45.GL_ARRAY_BUFFER, bufferPointer); + GL45.glBufferData(GL45.GL_ARRAY_BUFFER, buffer, GL45.GL_STATIC_DRAW); + GL45.glVertexAttribIPointer(attribIndex, bufferDimension, GL45.GL_INT, 0, 0); + GL45.glEnableVertexAttribArray(attribIndex); } return bufferPointer; } @@ -254,11 +290,11 @@ public class Mesh { public int bufferCustomUIntAttribArray(IntBuffer buffer, int bufferDimension, int attribIndex){ int bufferPointer = 0; if(!EngineState.EngineFlags.HEADLESS){ - bufferPointer = glGenBuffers(); - glBindBuffer(GL_ARRAY_BUFFER, bufferPointer); - GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); - GL40.glVertexAttribIPointer(attribIndex, bufferDimension, GL_UNSIGNED_INT, 0, 0); - glEnableVertexAttribArray(attribIndex); + bufferPointer = GL45.glGenBuffers(); + GL45.glBindBuffer(GL45.GL_ARRAY_BUFFER, bufferPointer); + GL45.glBufferData(GL45.GL_ARRAY_BUFFER, buffer, GL45.GL_STATIC_DRAW); + GL45.glVertexAttribIPointer(attribIndex, bufferDimension, GL45.GL_UNSIGNED_INT, 0, 0); + GL45.glEnableVertexAttribArray(attribIndex); } return bufferPointer; } @@ -313,7 +349,7 @@ public class Mesh { uniforms.put(key, o); } - void bufferAllUniforms(OpenGLState openGLState){ + private void bufferAllUniforms(OpenGLState openGLState){ for(String key : uniforms.keySet()){ Object currentUniformRaw = uniforms.get(key); if(currentUniformRaw instanceof Matrix4f){ @@ -350,7 +386,7 @@ public class Mesh { Globals.profiler.beginAggregateCpuSample("Mesh.complexDraw"); //bind vao off the rip - GL40.glBindVertexArray(vertexArrayObject); + GL45.glBindVertexArray(vertexArrayObject); Globals.renderingEngine.checkError(); if(renderPipelineState.getUseMeshShader()){ @@ -441,9 +477,9 @@ public class Mesh { if(renderPipelineState.getUseShadowMap()){ int shadowMapTextureUnit = 3; - openGLState.glActiveTexture(GL40.GL_TEXTURE0 + shadowMapTextureUnit); + openGLState.glActiveTexture(GL45.GL_TEXTURE0 + shadowMapTextureUnit); Globals.renderingEngine.checkError(); - openGLState.glBindTexture(GL_TEXTURE_2D, RenderingEngine.lightBufferDepthTexture.getTexturePointer()); + openGLState.glBindTexture(GL45.GL_TEXTURE_2D, RenderingEngine.lightBufferDepthTexture.getTexturePointer()); Globals.renderingEngine.checkError(); openGLState.getActiveShader().setUniform(openGLState, "shadowMap", shadowMapTextureUnit); } @@ -509,23 +545,23 @@ public class Mesh { if(renderPipelineState.getInstanced()){ if(elementCount > 0 ){ - GL45.glDrawElementsInstanced(GL_TRIANGLES, elementCount, GL_UNSIGNED_INT, 0, renderPipelineState.getInstanceCount()); + GL45.glDrawElementsInstanced(GL45.GL_TRIANGLES, elementCount, GL45.GL_UNSIGNED_INT, 0, renderPipelineState.getInstanceCount()); Globals.renderingEngine.checkError(); } } else { if(useElementArray){ if(elementCount > 0){ - GL11.glDrawElements(GL_TRIANGLES, elementCount, GL_UNSIGNED_INT, 0); + GL45.glDrawElements(GL45.GL_TRIANGLES, elementCount, GL45.GL_UNSIGNED_INT, 0); Globals.renderingEngine.checkError(); } } else { if(elementCount > 0){ - GL11.glDrawArrays(GL_TRIANGLES, 0, elementCount); + GL45.glDrawArrays(GL45.GL_TRIANGLES, 0, elementCount); Globals.renderingEngine.checkError(); } } } - GL40.glBindVertexArray(0); + GL45.glBindVertexArray(0); Globals.renderingEngine.checkError(); Globals.profiler.endCpuSample(); } diff --git a/src/main/java/electrosphere/renderer/model/Model.java b/src/main/java/electrosphere/renderer/model/Model.java index 313011d8..a66cf821 100644 --- a/src/main/java/electrosphere/renderer/model/Model.java +++ b/src/main/java/electrosphere/renderer/model/Model.java @@ -41,41 +41,84 @@ import org.lwjgl.assimp.AINode; */ public class Model { - //the model matrix for this model + /** + * The model matrix of the model + */ private Matrix4d modelMatrix = new Matrix4d(); - //the real world coordinates of this object (not model space) + /** + * the real world coordinates of this object (not model space) + */ private Vector3d worldPos = new Vector3d(); - //an optional global transform applied to the parent bone. Typically found in models loaded from files + /** + * an optional global transform applied to the parent bone. Typically found in models loaded from files + */ private Matrix4d rootTransform = new Matrix4d(); + + /** + * The inverse root transform + */ private Matrix4d globalInverseTransform = new Matrix4d(); - //the meshes in the model + /** + * The list of meshes in the model + */ private List meshes = new ArrayList(); - //the materials in the model + /** + * The materials of the model + */ private List materials = new ArrayList(); - //bone data for the model + /** + * The list of all bones + */ private List bones = new ArrayList(); + + /** + * The map of name -> bone + */ private Map boneMap = new HashMap(); - //these structures are used to parse the tree of bones of the model + /** + * The root animation node + */ private AnimNode rootAnimNode; + /** + * The map of animation node name -> node + */ private Map nodeMap = new HashMap(); - //animation data for the model + /** + * The animations in the model + */ private List animations = new ArrayList(); + + /** + * The map of animation name -> animation + */ private Map animMap = new HashMap(); - //these are masks that can be applied to the model to overwrite meshes, shaders, and textures of it + /** + * A mask that overwrites meshes themsselves + */ private ActorMeshMask meshMask; + + /** + * A mask that overwrites shaders on a given mesh + */ private Map shaderMask = new HashMap(); + + /** + * A mask that overwrites textures on a given mesh + */ private Map textureMap = new HashMap(); - //The bounding sphere for this particular model + /** + * The bounding sphere for this particular model + */ private Sphered boundingSphere = new Sphered(); @@ -272,7 +315,7 @@ public class Model { * @param oldShader The original shader on the mesh * @return The correct shader program to use for this mesh */ - VisualShader getCorrectShader(Map shaderMask, Mesh mesh, VisualShader oldShader){ + private VisualShader getCorrectShader(Map shaderMask, Mesh mesh, VisualShader oldShader){ VisualShader rVal = oldShader; if(shaderMask.containsKey(mesh.getMeshName())){ ActorShaderMask specificMask = shaderMask.get(mesh.getMeshName()); @@ -392,7 +435,7 @@ public class Model { * @param boneRotators The bone rotators to apply * @param staticMorph The static morph to apply */ - void updateNodeTransform(AnimNode n, Map boneRotators, ActorStaticMorph staticMorph){ + private void updateNodeTransform(AnimNode n, Map boneRotators, ActorStaticMorph staticMorph){ Matrix4d parentTransform = new Matrix4d(); if(n.parent != null){ parentTransform = new Matrix4d(n.parent.getTransform());