From 305a6bb459a780b814919e25272087f5562847d6 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sun, 30 Jun 2024 14:02:44 -0500 Subject: [PATCH] Improve generic rendering pos objects --- .../render/IDhApiCustomRenderRegister.java | 3 +- .../render/IDhApiRenderableBoxGroup.java | 7 +- .../api/objects/math/DhApiVec3f.java | 93 +++++++++++++++++++ .../coreapi/util/math/Vec3f.java | 10 ++ .../renderer/GenericObjectRenderer.java | 65 ++++++------- 5 files changed, 137 insertions(+), 41 deletions(-) create mode 100644 api/src/main/java/com/seibel/distanthorizons/api/objects/math/DhApiVec3f.java diff --git a/api/src/main/java/com/seibel/distanthorizons/api/interfaces/render/IDhApiCustomRenderRegister.java b/api/src/main/java/com/seibel/distanthorizons/api/interfaces/render/IDhApiCustomRenderRegister.java index 0f3ef03a8..1dfff0a29 100644 --- a/api/src/main/java/com/seibel/distanthorizons/api/interfaces/render/IDhApiCustomRenderRegister.java +++ b/api/src/main/java/com/seibel/distanthorizons/api/interfaces/render/IDhApiCustomRenderRegister.java @@ -1,5 +1,6 @@ package com.seibel.distanthorizons.api.interfaces.render; +import com.seibel.distanthorizons.api.objects.math.DhApiVec3f; import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBox; import java.util.List; @@ -12,7 +13,7 @@ public interface IDhApiCustomRenderRegister IDhApiRenderableBoxGroup createForSingleBox(DhApiRenderableBox cube); - IDhApiRenderableBoxGroup createRelativePositionedGroup(float originBlockX, float originBlockY, float originBlockZ, List cubeList); + IDhApiRenderableBoxGroup createRelativePositionedGroup(DhApiVec3f originBlockPos, List cubeList); IDhApiRenderableBoxGroup createAbsolutePositionedGroup(List cubeList); } diff --git a/api/src/main/java/com/seibel/distanthorizons/api/interfaces/render/IDhApiRenderableBoxGroup.java b/api/src/main/java/com/seibel/distanthorizons/api/interfaces/render/IDhApiRenderableBoxGroup.java index 2ecd2e900..b37b43a4c 100644 --- a/api/src/main/java/com/seibel/distanthorizons/api/interfaces/render/IDhApiRenderableBoxGroup.java +++ b/api/src/main/java/com/seibel/distanthorizons/api/interfaces/render/IDhApiRenderableBoxGroup.java @@ -1,6 +1,7 @@ package com.seibel.distanthorizons.api.interfaces.render; import com.seibel.distanthorizons.api.methods.events.sharedParameterObjects.DhApiRenderParam; +import com.seibel.distanthorizons.api.objects.math.DhApiVec3f; import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBox; import java.util.List; @@ -11,10 +12,8 @@ public interface IDhApiRenderableBoxGroup extends List long getId(); - void setOriginBlockPos(float x, float y, float z); - float getOriginBlockX(); - float getOriginBlockY(); - float getOriginBlockZ(); + void setOriginBlockPos(DhApiVec3f pos); + DhApiVec3f getOriginBlockPos(); void setPreRenderFunc(Consumer renderEventParam); diff --git a/api/src/main/java/com/seibel/distanthorizons/api/objects/math/DhApiVec3f.java b/api/src/main/java/com/seibel/distanthorizons/api/objects/math/DhApiVec3f.java new file mode 100644 index 000000000..5fa60d8d1 --- /dev/null +++ b/api/src/main/java/com/seibel/distanthorizons/api/objects/math/DhApiVec3f.java @@ -0,0 +1,93 @@ +/* + * This file is part of the Distant Horizons mod + * licensed under the GNU LGPL v3 License. + * + * Copyright (C) 2020-2023 James Seibel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.seibel.distanthorizons.api.objects.math; + +/** + * Often used to store block positions or any other + * position in 3D space. + * + * @author James Seibel + * @version 2024-6-3 + * @since API 2.2.0 + */ +public class DhApiVec3f +{ + public float x; + public float y; + public float z; + + + + /** creates a Vec3 at (0,0,0) */ + public DhApiVec3f() + { + this.x = 0; + this.y = 0; + this.z = 0; + } + + public DhApiVec3f(float x, float y, float z) + { + this.x = x; + this.y = y; + this.z = z; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + else if (obj != null && this.getClass() == obj.getClass()) + { + DhApiVec3f Vec3f = (DhApiVec3f) obj; + if (Float.compare(Vec3f.x, this.x) != 0) + { + return false; + } + else if (Float.compare(Vec3f.y, this.y) != 0) + { + return false; + } + else + { + return Float.compare(Vec3f.z, this.z) == 0; + } + } + else + { + return false; + } + } + + @Override + public int hashCode() + { + int i = Float.floatToIntBits(this.x); + i = 31 * i + Float.floatToIntBits(this.y); + return 31 * i + Float.floatToIntBits(this.z); + } + + @Override + public String toString() { return "[" + this.x + ", " + this.y + ", " + this.z + "]"; } + +} diff --git a/api/src/main/java/com/seibel/distanthorizons/coreapi/util/math/Vec3f.java b/api/src/main/java/com/seibel/distanthorizons/coreapi/util/math/Vec3f.java index b45ba98be..50488535a 100644 --- a/api/src/main/java/com/seibel/distanthorizons/coreapi/util/math/Vec3f.java +++ b/api/src/main/java/com/seibel/distanthorizons/coreapi/util/math/Vec3f.java @@ -19,6 +19,7 @@ package com.seibel.distanthorizons.coreapi.util.math; +import com.seibel.distanthorizons.api.objects.math.DhApiVec3f; import com.seibel.distanthorizons.coreapi.util.MathUtil; /** @@ -56,6 +57,15 @@ public class Vec3f this.z = z; } + public Vec3f(DhApiVec3f pos) + { + this.x = pos.x; + this.y = pos.y; + this.z = pos.z; + } + + + @Override public boolean equals(Object obj) { diff --git a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/GenericObjectRenderer.java b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/GenericObjectRenderer.java index 2302915ac..d5383a64b 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/GenericObjectRenderer.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/render/renderer/GenericObjectRenderer.java @@ -25,6 +25,7 @@ import com.seibel.distanthorizons.api.enums.config.EDhApiLoggerMode; import com.seibel.distanthorizons.api.interfaces.render.IDhApiRenderableBoxGroup; import com.seibel.distanthorizons.api.interfaces.render.IDhApiCustomRenderRegister; import com.seibel.distanthorizons.api.methods.events.sharedParameterObjects.DhApiRenderParam; +import com.seibel.distanthorizons.api.objects.math.DhApiVec3f; import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBox; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; import com.seibel.distanthorizons.core.logging.ConfigBasedSpamLogger; @@ -223,14 +224,14 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister new Color(Color.MAGENTA.getRed(), Color.MAGENTA.getGreen(), Color.MAGENTA.getBlue()))); } IDhApiRenderableBoxGroup relativePosCubeGroup = DhApi.Delayed.renderRegister.createRelativePositionedGroup( - 24f, 140f, 24f, + new DhApiVec3f(24f, 140f, 24f), relCubeList); relativePosCubeGroup.setPreRenderFunc((event) -> { - float x = relativePosCubeGroup.getOriginBlockX(); - x += event.partialTicks / 2; - x %= 32; - relativePosCubeGroup.setOriginBlockPos(x, relativePosCubeGroup.getOriginBlockY(), relativePosCubeGroup.getOriginBlockZ()); + DhApiVec3f pos = relativePosCubeGroup.getOriginBlockPos(); + pos.x += event.partialTicks / 2; + pos.x %= 32; + relativePosCubeGroup.setOriginBlockPos(pos); }); DhApi.Delayed.renderRegister.add(relativePosCubeGroup); @@ -249,17 +250,17 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister } } IDhApiRenderableBoxGroup massRelativePosCubeGroup = DhApi.Delayed.renderRegister.createRelativePositionedGroup( - -25f, 140f, 0f, + new DhApiVec3f(-25f, 140f, 0f), massRelCubeList); massRelativePosCubeGroup.setPreRenderFunc((event) -> { - float y = massRelativePosCubeGroup.getOriginBlockY(); - y += event.partialTicks / 4; - if (y > 150f) + DhApiVec3f blockPos = massRelativePosCubeGroup.getOriginBlockPos(); + blockPos.y += event.partialTicks / 4; + if (blockPos.y > 150f) { - y = 140f; + blockPos.y = 140f; } - massRelativePosCubeGroup.setOriginBlockPos(massRelativePosCubeGroup.getOriginBlockX(), y, massRelativePosCubeGroup.getOriginBlockZ()); + massRelativePosCubeGroup.setOriginBlockPos(blockPos); }); DhApi.Delayed.renderRegister.add(massRelativePosCubeGroup); @@ -303,12 +304,12 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister } @Override - public IDhApiRenderableBoxGroup createRelativePositionedGroup(float originBlockX, float originBlockY, float originBlockZ, List cubeList) - { return new DhApiRenderableBoxGroup(originBlockX, originBlockY, originBlockZ, cubeList, true); } + public IDhApiRenderableBoxGroup createRelativePositionedGroup(DhApiVec3f originBlockPos, List cubeList) + { return new DhApiRenderableBoxGroup(new Vec3f(originBlockPos), cubeList, true); } @Override public IDhApiRenderableBoxGroup createAbsolutePositionedGroup(List boxList) - { return new DhApiRenderableBoxGroup(0, 0, 0, boxList, false); } + { return new DhApiRenderableBoxGroup(new Vec3f(0, 0, 0), boxList, false); } @@ -457,9 +458,9 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister this.shader.setUniform(this.instancedShaderOffsetUniformLocation, new Vec3f( - boxGroup.originBlockX, - boxGroup.originBlockY, - boxGroup.originBlockZ + boxGroup.originBlockPos.x, + boxGroup.originBlockPos.y, + boxGroup.originBlockPos.z )); this.shader.setUniform(this.instancedShaderCameraPosUniformLocation, @@ -547,9 +548,9 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister float originOffsetZ = 0; if (cubeGroup.positionCubesRelativeToGroupOrigin) { - originOffsetX = cubeGroup.originBlockX; - originOffsetY = cubeGroup.originBlockY; - originOffsetZ = cubeGroup.originBlockZ; + originOffsetX = cubeGroup.originBlockPos.x; + originOffsetY = cubeGroup.originBlockPos.y; + originOffsetZ = cubeGroup.originBlockPos.z; } Mat4f boxTransform = Mat4f.createTranslateMatrix( @@ -590,9 +591,7 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister private final ArrayList cubeList; - private float originBlockX; - private float originBlockY; - private float originBlockZ; + private final Vec3f originBlockPos; @Nullable public Consumer beforeRenderFunc; @@ -612,25 +611,21 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister public long getId() { return this.id; } @Override - public void setOriginBlockPos(float x, float y, float z) + public void setOriginBlockPos(DhApiVec3f pos) { - this.originBlockX = x; - this.originBlockY = y; - this.originBlockZ = z; + this.originBlockPos.x = pos.x; + this.originBlockPos.y = pos.y; + this.originBlockPos.z = pos.z; } @Override - public float getOriginBlockX() { return this.originBlockX; } - @Override - public float getOriginBlockY() { return this.originBlockY; } - @Override - public float getOriginBlockZ() { return this.originBlockZ; } + public DhApiVec3f getOriginBlockPos() { return new DhApiVec3f(this.originBlockPos.x, this.originBlockPos.y, this.originBlockPos.z); } // constructor // - public DhApiRenderableBoxGroup(float originBlockX, float originBlockY, float originBlockZ, List cubeList, boolean positionCubesRelativeToGroupOrigin) + public DhApiRenderableBoxGroup(Vec3f originBlockPos, List cubeList, boolean positionCubesRelativeToGroupOrigin) { // TODO save to database // TODO when? @@ -638,9 +633,7 @@ public class GenericObjectRenderer implements IDhApiCustomRenderRegister this.id = NEXT_ID_ATOMIC_INT.getAndIncrement(); this.cubeList = new ArrayList<>(cubeList); - this.originBlockX = originBlockX; - this.originBlockY = originBlockY; - this.originBlockZ = originBlockZ; + this.originBlockPos = originBlockPos; this.positionCubesRelativeToGroupOrigin = positionCubesRelativeToGroupOrigin; }