From ce0a8341172fc7f78132609dc6faa589e08ea091 Mon Sep 17 00:00:00 2001 From: austin Date: Thu, 15 May 2025 14:48:25 -0400 Subject: [PATCH] move matricies inside rendering engine --- .../ui/components/CharacterCustomizer.java | 2 +- .../electrosphere/controls/CameraHandler.java | 2 +- .../java/electrosphere/engine/Globals.java | 7 --- .../client/particle/ClientParticleTree.java | 2 +- .../renderer/RenderingEngine.java | 48 +++++++++++++++---- .../renderer/light/LightManager.java | 4 +- .../electrosphere/renderer/model/Mesh.java | 6 +-- .../renderer/pipelines/ShadowMapPipeline.java | 4 +- .../pipelines/VolumeBufferPipeline.java | 2 +- 9 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/main/java/electrosphere/client/ui/components/CharacterCustomizer.java b/src/main/java/electrosphere/client/ui/components/CharacterCustomizer.java index 9c04e8bc..86598213 100644 --- a/src/main/java/electrosphere/client/ui/components/CharacterCustomizer.java +++ b/src/main/java/electrosphere/client/ui/components/CharacterCustomizer.java @@ -64,7 +64,7 @@ public class CharacterCustomizer { //spawn camera so renderer doesn't crash (once render pipeline is modularized this shouldn't be necessary) Globals.clientState.playerCamera = CameraEntityUtils.spawnBasicCameraEntity(new Vector3d(0,0,0), new Vector3d(0,0.3f,1).normalize()); - Globals.viewMatrix = CameraEntityUtils.getCameraViewMatrix(Globals.clientState.playerCamera); + Globals.renderingEngine.getViewMatrix().set(CameraEntityUtils.getCameraViewMatrix(Globals.clientState.playerCamera)); //create actor panel Actor characterActor = ActorUtils.createActorFromModelPath(selectedRaceType.getGraphicsTemplate().getModel().getPath()); diff --git a/src/main/java/electrosphere/controls/CameraHandler.java b/src/main/java/electrosphere/controls/CameraHandler.java index 36965038..9a250fdf 100644 --- a/src/main/java/electrosphere/controls/CameraHandler.java +++ b/src/main/java/electrosphere/controls/CameraHandler.java @@ -163,7 +163,7 @@ public class CameraHandler { } //the view matrix - Globals.viewMatrix = CameraEntityUtils.getCameraViewMatrix(Globals.clientState.playerCamera); + Globals.renderingEngine.getViewMatrix().set(CameraEntityUtils.getCameraViewMatrix(Globals.clientState.playerCamera)); //update the cursor on client side Globals.cursorState.updatePlayerCursor(); diff --git a/src/main/java/electrosphere/engine/Globals.java b/src/main/java/electrosphere/engine/Globals.java index 4b3689b2..1a1728fb 100644 --- a/src/main/java/electrosphere/engine/Globals.java +++ b/src/main/java/electrosphere/engine/Globals.java @@ -2,8 +2,6 @@ package electrosphere.engine; import java.lang.management.ManagementFactory; -import org.joml.Matrix4d; - import electrosphere.audio.AudioEngine; import electrosphere.audio.VirtualAudioSourceManager; import electrosphere.audio.collision.HitboxAudioService; @@ -200,11 +198,6 @@ public class Globals { public static float aspectRatio = 2.0f; public static float nearClip = 0.01f; - //matrices for drawing models - public static Matrix4d viewMatrix = new Matrix4d(); - public static Matrix4d projectionMatrix; - public static Matrix4d lightDepthMatrix = new Matrix4d(); - //locations for shadow map specific variables public static int depthMapShaderProgramLoc = 0; diff --git a/src/main/java/electrosphere/entity/state/client/particle/ClientParticleTree.java b/src/main/java/electrosphere/entity/state/client/particle/ClientParticleTree.java index 693d4197..f0456d5f 100644 --- a/src/main/java/electrosphere/entity/state/client/particle/ClientParticleTree.java +++ b/src/main/java/electrosphere/entity/state/client/particle/ClientParticleTree.java @@ -120,7 +120,7 @@ public class ClientParticleTree implements BehaviorTree { } //rotate the model to face the camera - Matrix4f rotationMatrix = new Matrix4f(Globals.viewMatrix).invert(); + Matrix4f rotationMatrix = new Matrix4f(Globals.renderingEngine.getViewMatrix()).invert(); Quaternionf rotation = new Quaternionf(rotationMatrix.getRotation(new AxisAngle4f())); EntityUtils.getRotation(parent).set(rotation); diff --git a/src/main/java/electrosphere/renderer/RenderingEngine.java b/src/main/java/electrosphere/renderer/RenderingEngine.java index 4d51cf09..a3d4e516 100644 --- a/src/main/java/electrosphere/renderer/RenderingEngine.java +++ b/src/main/java/electrosphere/renderer/RenderingEngine.java @@ -146,6 +146,11 @@ public class RenderingEngine { static float aspectRatio = 1.0f; static float verticalFOV = 90.0f; + //matrices for drawing models + private Matrix4d viewMatrix = new Matrix4d(); + private Matrix4d projectionMatrix = new Matrix4d(); + private Matrix4d lightDepthMatrix = new Matrix4d(); + /** * The default material */ @@ -466,13 +471,11 @@ public class RenderingEngine { // // Projection and View matrix creation // - Globals.projectionMatrix = new Matrix4d(); - Globals.viewMatrix = new Matrix4d(); verticalFOV = (float)(Globals.verticalFOV * Math.PI /180.0f); //set local aspect ratio and global aspect ratio at the same time aspectRatio = Globals.aspectRatio = Globals.WINDOW_WIDTH / (float)Globals.WINDOW_HEIGHT; - Globals.projectionMatrix.setPerspective(verticalFOV, Globals.aspectRatio, Globals.nearClip, Globals.userSettings.getGraphicsViewDistance()); - Globals.viewMatrix.translation(new Vector3d(0.0f,0.0f,-3.0f)); + this.projectionMatrix.setPerspective(verticalFOV, Globals.aspectRatio, Globals.nearClip, Globals.userSettings.getGraphicsViewDistance()); + this.viewMatrix.translation(new Vector3d(0.0f,0.0f,-3.0f)); /** * Alert everyone that the rendering engine is ready @@ -574,7 +577,7 @@ public class RenderingEngine { * Updates the frustum box of the render pipeline */ private void updateFrustumBox(){ - renderPipelineState.updateFrustumIntersection(Globals.projectionMatrix, Globals.viewMatrix); + renderPipelineState.updateFrustumIntersection(this.projectionMatrix, this.viewMatrix); } public void bindFramebuffer(int framebufferPointer){ @@ -614,18 +617,21 @@ public class RenderingEngine { public static void setFOV(float verticalFOV){ RenderingEngine.verticalFOV = verticalFOV; - calculateProjectionMatrix(); + Globals.renderingEngine.calculateProjectionMatrix(); } public static void setAspectRatio(float aspectRatio){ RenderingEngine.aspectRatio = aspectRatio; - calculateProjectionMatrix(); + Globals.renderingEngine.calculateProjectionMatrix(); } - static void calculateProjectionMatrix(){ + /** + * Calculates the projection matrix + */ + public void calculateProjectionMatrix(){ float radVerticalFOV = (float)(RenderingEngine.verticalFOV * Math.PI /180.0f); float nearClip = 0.001f; - Globals.projectionMatrix.setPerspective(radVerticalFOV, RenderingEngine.aspectRatio, nearClip, Globals.userSettings.getGraphicsViewDistance()); + this.projectionMatrix.setPerspective(radVerticalFOV, RenderingEngine.aspectRatio, nearClip, Globals.userSettings.getGraphicsViewDistance()); } /** @@ -700,6 +706,30 @@ public class RenderingEngine { return this.materialDefault; } + /** + * Gets the view matrix + * @return The view matrix + */ + public Matrix4d getViewMatrix(){ + return viewMatrix; + } + + /** + * Gets the projection matrix + * @return The projection matrix + */ + public Matrix4d getProjectionMatrix(){ + return projectionMatrix; + } + + /** + * Gets the light depth matrix + * @return The light depth matrix + */ + public Matrix4d getLightDepthMatrix(){ + return lightDepthMatrix; + } + /** * Tries to recapture the screen */ diff --git a/src/main/java/electrosphere/renderer/light/LightManager.java b/src/main/java/electrosphere/renderer/light/LightManager.java index dc3a5566..dfedea49 100644 --- a/src/main/java/electrosphere/renderer/light/LightManager.java +++ b/src/main/java/electrosphere/renderer/light/LightManager.java @@ -204,7 +204,7 @@ public class LightManager { openGLState.setActiveShader(renderPipelineState, clusterComp); clusterComp.setUniform(openGLState, "zNear", CameraEntityUtils.getNearClip(camera)); clusterComp.setUniform(openGLState, "zFar", CameraEntityUtils.getFarClip(camera)); - clusterComp.setUniform(openGLState, "inverseProjection", new Matrix4d(Globals.projectionMatrix).invert()); + clusterComp.setUniform(openGLState, "inverseProjection", new Matrix4d(Globals.renderingEngine.getViewMatrix()).invert()); clusterComp.setUniform(openGLState, "gridSize", new Vector3i(LIGHT_CLUSTER_WIDTH_X,LIGHT_CLUSTER_WIDTH_Y,LIGHT_CLUSTER_WIDTH_Z)); clusterComp.setUniform(openGLState, "screenDimensions", openGLState.getViewport()); @@ -216,7 +216,7 @@ public class LightManager { //cull lights ComputeShader lightCull = Globals.assetManager.fetchComputeShader(AssetDataStrings.COMPUTE_LIGHT_CULL); openGLState.setActiveShader(renderPipelineState, lightCull); - lightCull.setUniform(openGLState, "viewMatrix", Globals.viewMatrix); + lightCull.setUniform(openGLState, "viewMatrix", Globals.renderingEngine.getViewMatrix()); lightCull.setUniform(openGLState, "lightCount", this.entityPointLightMap.values().size()); int dispatchLocal = (LIGHT_CLUSTER_WIDTH_X * LIGHT_CLUSTER_WIDTH_Y * LIGHT_CLUSTER_WIDTH_Z) / CULL_LOCAL_SIZE; diff --git a/src/main/java/electrosphere/renderer/model/Mesh.java b/src/main/java/electrosphere/renderer/model/Mesh.java index fb128e41..13f366fa 100644 --- a/src/main/java/electrosphere/renderer/model/Mesh.java +++ b/src/main/java/electrosphere/renderer/model/Mesh.java @@ -481,12 +481,12 @@ public class Mesh { //buffer model/view/proj matrices try(MemoryStack stack = MemoryStack.stackPush()){ openGLState.getActiveShader().setUniform(openGLState, "model", parent.getModelMatrix()); - openGLState.getActiveShader().setUniform(openGLState, "view", Globals.viewMatrix); - openGLState.getActiveShader().setUniform(openGLState, "projection", Globals.projectionMatrix); + openGLState.getActiveShader().setUniform(openGLState, "view", Globals.renderingEngine.getViewMatrix()); + openGLState.getActiveShader().setUniform(openGLState, "projection", Globals.renderingEngine.getProjectionMatrix()); openGLState.getActiveShader().setUniform(openGLState, "viewPos", CameraEntityUtils.getCameraEye(Globals.clientState.playerCamera)); Vector3f worldPos = new Vector3f((float)parent.getWorldPos().x,(float)parent.getWorldPos().y,(float)parent.getWorldPos().z); openGLState.getActiveShader().setUniform(openGLState, "modelWorldPos", worldPos); - openGLState.getActiveShader().setUniform(openGLState, "lightSpaceMatrix", Globals.lightDepthMatrix); + openGLState.getActiveShader().setUniform(openGLState, "lightSpaceMatrix", Globals.renderingEngine.getLightDepthMatrix()); openGLState.getActiveShader().setUniform(openGLState, "frame", (int)Globals.engineState.timekeeper.getNumberOfRenderFramesElapsed()); openGLState.getActiveShader().setUniform(openGLState, "time", (float)Globals.engineState.timekeeper.getCurrentRendererTime()); } diff --git a/src/main/java/electrosphere/renderer/pipelines/ShadowMapPipeline.java b/src/main/java/electrosphere/renderer/pipelines/ShadowMapPipeline.java index b80fed70..a74ba037 100644 --- a/src/main/java/electrosphere/renderer/pipelines/ShadowMapPipeline.java +++ b/src/main/java/electrosphere/renderer/pipelines/ShadowMapPipeline.java @@ -82,9 +82,9 @@ public class ShadowMapPipeline implements RenderPipeline { new Vector3d( 0.0f, 0.0f, 0.0f), SpatialMathUtils.getUpVector() ); - Globals.lightDepthMatrix = lightProjection.mul(lightView); + Globals.renderingEngine.getLightDepthMatrix().set(lightProjection.mul(lightView)); - openGLState.getActiveShader().setUniform(openGLState, "lightSpaceMatrix", Globals.lightDepthMatrix); + openGLState.getActiveShader().setUniform(openGLState, "lightSpaceMatrix", Globals.renderingEngine.getLightDepthMatrix()); // glCullFace(GL_FRONT); diff --git a/src/main/java/electrosphere/renderer/pipelines/VolumeBufferPipeline.java b/src/main/java/electrosphere/renderer/pipelines/VolumeBufferPipeline.java index 612f8027..a7013d02 100644 --- a/src/main/java/electrosphere/renderer/pipelines/VolumeBufferPipeline.java +++ b/src/main/java/electrosphere/renderer/pipelines/VolumeBufferPipeline.java @@ -47,7 +47,7 @@ public class VolumeBufferPipeline implements RenderPipeline { GL40.glClear(GL40.GL_DEPTH_BUFFER_BIT); openGLState.glActiveTexture(GL40.GL_TEXTURE0); - GL40.glUniformMatrix4fv(GL40.glGetUniformLocation(openGLState.getActiveShader().getId(), "view"), false, Globals.viewMatrix.get(new float[16])); + GL40.glUniformMatrix4fv(GL40.glGetUniformLocation(openGLState.getActiveShader().getId(), "view"), false, Globals.renderingEngine.getViewMatrix().get(new float[16])); GL40.glUniformMatrix4fv(GL40.glGetUniformLocation(openGLState.getActiveShader().getId(), "projection"), false, RenderingEngine.nearVolumeProjectionMatrix.get(new float[16])); GL40.glUniform1f(GL40.glGetUniformLocation(openGLState.getActiveShader().getId(), "linearCoef"), RenderingEngine.volumeDepthLinearCoef); GL40.glUniform1f(GL40.glGetUniformLocation(openGLState.getActiveShader().getId(), "quadCoef"), RenderingEngine.volumeDepthQuadCoef);