rendering code cleanup work

This commit is contained in:
austin 2025-05-18 22:49:30 -04:00
parent 5507a5c261
commit 69af65ab53
4 changed files with 195 additions and 92 deletions

View File

@ -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

View File

@ -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<ActorAnimationMask> animationQueue = new TreeSet<ActorAnimationMask>();
//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<ActorShaderMask> shaderMasks = new LinkedList<ActorShaderMask>();
//optional overrides for textures
/**
* optional overrides for textures
*/
Map<String,ActorTextureMask> textureMap = null;
//bone rotators
/**
* bone rotators
*/
Map<String,ActorBoneRotator> boneRotators = new HashMap<String,ActorBoneRotator>();
//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<BoneGroup> 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;
/**

View File

@ -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
* <p>
* 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
* </p>
*/
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<Bone> bones = new ArrayList<Bone>();
private ArrayList<String> boneIdList = new ArrayList<String>();
//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<String,Object> uniforms = new HashMap<String,Object>();
//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();
}

View File

@ -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<Mesh> meshes = new ArrayList<Mesh>();
//the materials in the model
/**
* The materials of the model
*/
private List<Material> materials = new ArrayList<Material>();
//bone data for the model
/**
* The list of all bones
*/
private List<Bone> bones = new ArrayList<Bone>();
/**
* The map of name -> bone
*/
private Map<String,Bone> boneMap = new HashMap<String,Bone>();
//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<String,AnimNode> nodeMap = new HashMap<String,AnimNode>();
//animation data for the model
/**
* The animations in the model
*/
private List<Animation> animations = new ArrayList<Animation>();
/**
* The map of animation name -> animation
*/
private Map<String,Animation> animMap = new HashMap<String,Animation>();
//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<String,ActorShaderMask> shaderMask = new HashMap<String,ActorShaderMask>();
/**
* A mask that overwrites textures on a given mesh
*/
private Map<String,ActorTextureMask> textureMap = new HashMap<String,ActorTextureMask>();
//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<String,ActorShaderMask> shaderMask, Mesh mesh, VisualShader oldShader){
private VisualShader getCorrectShader(Map<String,ActorShaderMask> 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<String,ActorBoneRotator> boneRotators, ActorStaticMorph staticMorph){
private void updateNodeTransform(AnimNode n, Map<String,ActorBoneRotator> boneRotators, ActorStaticMorph staticMorph){
Matrix4d parentTransform = new Matrix4d();
if(n.parent != null){
parentTransform = new Matrix4d(n.parent.getTransform());