From 717c918bd4aac545ad0e4bdc37f4d38d3812d751 Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Fri, 5 Jan 2024 14:38:51 -0800 Subject: [PATCH] VBO Clouds (#135) * Backport VBO Clouds from Forge 1.12 * Add a 1.12+ style tessellator/buffer builder API to CapturingTessellator * Update VBOManager to handle adding items out of order, and switch to ObjectArrayList --- .../client/renderer/CapturingTessellator.java | 123 ++++++ .../compat/mojang/DefaultVertexFormat.java | 6 +- .../angelica/compat/mojang/VertexBuffer.java | 10 + .../PositionColorTextureQuadWriter.java | 47 +++ .../PositionTextureColorQuadWriter.java | 48 +++ .../angelica/glsm/VBOManager.java | 16 +- .../early/angelica/vbo/MixinRenderGlobal.java | 24 ++ .../early/sodium/MixinRenderGlobal.java | 9 - .../interfaces/TessellatorAccessor.java | 49 --- .../angelica/proxy/ClientProxy.java | 9 + .../angelica/render/CloudRenderer.java | 399 ++++++++++++++++++ src/main/resources/META-INF/angelica_at.cfg | 1 + 12 files changed, 674 insertions(+), 67 deletions(-) create mode 100644 src/main/java/com/gtnewhorizons/angelica/compat/nd/writers/PositionColorTextureQuadWriter.java create mode 100644 src/main/java/com/gtnewhorizons/angelica/compat/nd/writers/PositionTextureColorQuadWriter.java delete mode 100644 src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/TessellatorAccessor.java create mode 100644 src/main/java/com/gtnewhorizons/angelica/render/CloudRenderer.java diff --git a/src/main/java/com/gtnewhorizons/angelica/client/renderer/CapturingTessellator.java b/src/main/java/com/gtnewhorizons/angelica/client/renderer/CapturingTessellator.java index 93a70bcf0..bad26252f 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/renderer/CapturingTessellator.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/renderer/CapturingTessellator.java @@ -4,15 +4,23 @@ import com.gtnewhorizons.angelica.compat.mojang.BlockPos; import com.gtnewhorizons.angelica.compat.mojang.VertexFormat; import com.gtnewhorizons.angelica.compat.nd.Quad; import com.gtnewhorizons.angelica.utils.ObjectPooler; +import cpw.mods.fml.relauncher.ReflectionHelper; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import me.jellysquid.mods.sodium.client.render.pipeline.BlockRenderer; import net.minecraft.client.renderer.Tessellator; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Field; import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; import java.util.List; +import static net.minecraft.util.MathHelper.clamp_int; + /* * To be used in conjunction with the TessellatorManager * @@ -24,6 +32,10 @@ import java.util.List; */ @SuppressWarnings("unused") public class CapturingTessellator extends Tessellator { + + // Access Transformers don't work on Forge Fields :rage: + private static final MethodHandle sRawBufferSize; + private static final MethodHandle gRawBufferSize; private final BlockRenderer.Flags FLAGS = new BlockRenderer.Flags(true, true, true, true); private final ObjectPooler quadBuf = new ObjectPooler<>(Quad::new); private final List collectedQuads = new ObjectArrayList<>(); @@ -35,6 +47,32 @@ public class CapturingTessellator extends Tessellator { this.offset.set(pos); } + static { + Field rbs = ReflectionHelper.findField(Tessellator.class, "rawBufferSize"); + rbs.setAccessible(true); + try { + sRawBufferSize = MethodHandles.lookup().unreflectSetter(rbs); + gRawBufferSize = MethodHandles.lookup().unreflectGetter(rbs); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + public void setRawBufferSize(int size) { + try { + sRawBufferSize.invokeExact((Tessellator)this, size); + } catch (Throwable throwable) { + throw new RuntimeException(throwable); + } + } + public int getRawBufferSize() { + try { + return (int) gRawBufferSize.invokeExact((Tessellator)this); + } catch (Throwable throwable) { + throw new RuntimeException(throwable); + } + } + @Override public int draw() { // Adapted from Neodymium @@ -99,4 +137,89 @@ public class CapturingTessellator extends Tessellator { return byteBuffer; } + // API from newer MC + public CapturingTessellator pos(double x, double y, double z) { + ensureBuffer(); + + this.rawBuffer[this.rawBufferIndex + 0] = Float.floatToRawIntBits((float)(x + this.xOffset)); + this.rawBuffer[this.rawBufferIndex + 1] = Float.floatToRawIntBits((float)(y + this.yOffset)); + this.rawBuffer[this.rawBufferIndex + 2] = Float.floatToRawIntBits((float)(z + this.zOffset)); + + return this; + } + public CapturingTessellator tex(double u, double v) { + this.rawBuffer[this.rawBufferIndex + 3] = Float.floatToRawIntBits((float)u); + this.rawBuffer[this.rawBufferIndex + 4] = Float.floatToRawIntBits((float)v); + + return this; + } + + public CapturingTessellator color(float red, float green, float blue, float alpha) { + return this.color((int)(red * 255.0F), (int)(green * 255.0F), (int)(blue * 255.0F), (int)(alpha * 255.0F)); + } + + public CapturingTessellator color(int red, int green, int blue, int alpha) { + if(this.isColorDisabled) return this; + red = clamp_int(red, 0, 255); + green = clamp_int(green, 0, 255); + blue = clamp_int(blue, 0, 255); + alpha = clamp_int(alpha, 0, 255); + + final int color; + if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) { + color = alpha << 24 | blue << 16 | green << 8 | red; + } else { + color = red << 24 | green << 16 | blue << 8 | alpha; + } + this.rawBuffer[this.rawBufferIndex + 5] = color; + + return this; + + } + + public CapturingTessellator normal(float x, float y, float z) { + final byte b0 = (byte)((int)(x * 127.0F)); + final byte b1 = (byte)((int)(y * 127.0F)); + final byte b2 = (byte)((int)(z * 127.0F)); + + this.rawBuffer[this.rawBufferIndex + 6] = b0 & 255 | (b1 & 255) << 8 | (b2 & 255) << 16; + return this; + } + + + public CapturingTessellator brightness(int brightness) { + this.rawBuffer[this.rawBufferIndex + 7] = brightness; + + return this; + } + public CapturingTessellator lightmap(int skyLight, int blockLight) { + return brightness(createBrightness(skyLight, blockLight)); + } + + + public static int createBrightness(int sky, int block) { + return sky << 20 | block << 4; + } + + public CapturingTessellator endVertex() { + this.rawBufferIndex += 8; + ++this.vertexCount; + + return this; + } + + public void ensureBuffer() { + int rawBufferSize = getRawBufferSize(); + if (rawBufferIndex >= rawBufferSize - 32) { + if (rawBufferSize == 0) { + rawBufferSize = 0x10000; + setRawBufferSize(rawBufferSize); + rawBuffer = new int[rawBufferSize]; + } else { + rawBufferSize *= 2; + setRawBufferSize(rawBufferSize); + rawBuffer = Arrays.copyOf(rawBuffer, rawBufferSize); + } + } + } } diff --git a/src/main/java/com/gtnewhorizons/angelica/compat/mojang/DefaultVertexFormat.java b/src/main/java/com/gtnewhorizons/angelica/compat/mojang/DefaultVertexFormat.java index a0f8aac17..f5b463ee3 100644 --- a/src/main/java/com/gtnewhorizons/angelica/compat/mojang/DefaultVertexFormat.java +++ b/src/main/java/com/gtnewhorizons/angelica/compat/mojang/DefaultVertexFormat.java @@ -4,7 +4,9 @@ import com.google.common.collect.ImmutableList; import com.gtnewhorizons.angelica.compat.mojang.VertexFormatElement.Type; import com.gtnewhorizons.angelica.compat.mojang.VertexFormatElement.Usage; import com.gtnewhorizons.angelica.compat.nd.writers.ItemVBOQuadWriter; +import com.gtnewhorizons.angelica.compat.nd.writers.PositionColorTextureQuadWriter; import com.gtnewhorizons.angelica.compat.nd.writers.PositionQuadWriter; +import com.gtnewhorizons.angelica.compat.nd.writers.PositionTextureColorQuadWriter; import com.gtnewhorizons.angelica.compat.nd.writers.PositionTextureQuadWriter; public class DefaultVertexFormat { @@ -25,8 +27,8 @@ public class DefaultVertexFormat { public static final VertexFormat POSITION_COLOR = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(COLOR_ELEMENT).build()); public static final VertexFormat POSITION_COLOR_LIGHT = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(COLOR_ELEMENT).add(LIGHT_ELEMENT).build()); public static final VertexFormat POSITION_TEXTURE = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).build(), new PositionTextureQuadWriter()); - public static final VertexFormat POSITION_COLOR_TEXTURE = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(COLOR_ELEMENT).add(TEXTURE_ELEMENT).build()); - public static final VertexFormat POSITION_TEXTURE_COLOR = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).add(COLOR_ELEMENT).build()); + public static final VertexFormat POSITION_COLOR_TEXTURE = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(COLOR_ELEMENT).add(TEXTURE_ELEMENT).build(), new PositionColorTextureQuadWriter()); + public static final VertexFormat POSITION_TEXTURE_COLOR = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).add(COLOR_ELEMENT).build(), new PositionTextureColorQuadWriter()); public static final VertexFormat POSITION_COLOR_TEX_LIGHTMAP = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(COLOR_ELEMENT).add(TEXTURE_ELEMENT).add(LIGHT_ELEMENT).build()); public static final VertexFormat POSITION_TEXTURE_LIGHT_COLOR = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).add(LIGHT_ELEMENT).add(COLOR_ELEMENT).build()); public static final VertexFormat POSITION_TEXTURE_COLOR_NORMAL = new VertexFormat(new ImmutableList.Builder().add(POSITION_ELEMENT).add(TEXTURE_ELEMENT).add(COLOR_ELEMENT).add(NORMAL_ELEMENT).add(PADDING_ELEMENT).build()); diff --git a/src/main/java/com/gtnewhorizons/angelica/compat/mojang/VertexBuffer.java b/src/main/java/com/gtnewhorizons/angelica/compat/mojang/VertexBuffer.java index ec0f0f9cd..9a998d1b5 100644 --- a/src/main/java/com/gtnewhorizons/angelica/compat/mojang/VertexBuffer.java +++ b/src/main/java/com/gtnewhorizons/angelica/compat/mojang/VertexBuffer.java @@ -62,6 +62,16 @@ public class VertexBuffer implements AutoCloseable { GL11.glDrawArrays(mode, 0, this.vertexCount); } + public void setupState() { + if(format == null) throw new IllegalStateException("No format specified for VBO setup"); + bind(); + format.setupBufferState(0L); + } + + public void cleanupState() { + format.clearBufferState(); + unbind(); + } public void render(int mode) { if(format == null) throw new IllegalStateException("No format specified for VBO render"); bind(); diff --git a/src/main/java/com/gtnewhorizons/angelica/compat/nd/writers/PositionColorTextureQuadWriter.java b/src/main/java/com/gtnewhorizons/angelica/compat/nd/writers/PositionColorTextureQuadWriter.java new file mode 100644 index 000000000..f0159efb5 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/compat/nd/writers/PositionColorTextureQuadWriter.java @@ -0,0 +1,47 @@ +package com.gtnewhorizons.angelica.compat.nd.writers; + +import com.gtnewhorizons.angelica.compat.nd.IWriteQuads; +import com.gtnewhorizons.angelica.compat.nd.Quad; + +import java.nio.ByteBuffer; + +public class PositionColorTextureQuadWriter implements IWriteQuads { + private boolean direct; + + public PositionColorTextureQuadWriter() { + init(false); + } + + public void init(boolean direct) { + // This would need to be re-initialized if the direct vs indirect changes + this.direct = direct; + } + @Override + public void writeQuad(Quad quad, ByteBuffer buf) { + if(direct) { + writeQuadDirect(quad, buf); + } else { + writeQuadIndirect(quad, buf); + } + } + + protected void writeQuadDirect(Quad quad, ByteBuffer buf) { + throw new UnsupportedOperationException("Direct mode not supported yet"); + } + + protected void writeQuadIndirect(Quad quad, ByteBuffer buf) { + for(int idx = 0; idx < 4; ++idx) { + // Position + buf.putFloat(quad.getX(idx)); + buf.putFloat(quad.getY(idx)); + buf.putFloat(quad.getZ(idx)); + + // Color + buf.putInt(quad.getColor(idx)); + + // Texture + buf.putFloat(quad.getTexU(idx)); + buf.putFloat(quad.getTexV(idx)); + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/compat/nd/writers/PositionTextureColorQuadWriter.java b/src/main/java/com/gtnewhorizons/angelica/compat/nd/writers/PositionTextureColorQuadWriter.java new file mode 100644 index 000000000..b9c3c6cc5 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/compat/nd/writers/PositionTextureColorQuadWriter.java @@ -0,0 +1,48 @@ +package com.gtnewhorizons.angelica.compat.nd.writers; + +import com.gtnewhorizons.angelica.compat.nd.IWriteQuads; +import com.gtnewhorizons.angelica.compat.nd.Quad; + +import java.nio.ByteBuffer; + +public class PositionTextureColorQuadWriter implements IWriteQuads { + private boolean direct; + + public PositionTextureColorQuadWriter() { + init(false); + } + + public void init(boolean direct) { + // This would need to be re-initialized if the direct vs indirect changes + this.direct = direct; + } + @Override + public void writeQuad(Quad quad, ByteBuffer buf) { + if(direct) { + writeQuadDirect(quad, buf); + } else { + writeQuadIndirect(quad, buf); + } + } + + protected void writeQuadDirect(Quad quad, ByteBuffer buf) { + throw new UnsupportedOperationException("Direct mode not supported yet"); + } + + protected void writeQuadIndirect(Quad quad, ByteBuffer buf) { + for(int idx = 0; idx < 4; ++idx) { + // Position + buf.putFloat(quad.getX(idx)); + buf.putFloat(quad.getY(idx)); + buf.putFloat(quad.getZ(idx)); + + // Texture + buf.putFloat(quad.getTexU(idx)); + buf.putFloat(quad.getTexV(idx)); + + // Color + buf.putInt(quad.getColor(idx)); + + } + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/VBOManager.java b/src/main/java/com/gtnewhorizons/angelica/glsm/VBOManager.java index c96fbff38..e91f384d6 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/VBOManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/VBOManager.java @@ -1,8 +1,8 @@ package com.gtnewhorizons.angelica.glsm; import com.gtnewhorizons.angelica.compat.mojang.VertexBuffer; - -import java.util.ArrayList; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import it.unimi.dsi.fastutil.objects.ObjectList; public class VBOManager { // Not thread safe, only expected to be called from the main thread @@ -13,7 +13,7 @@ public class VBOManager { return nextDisplayList++; } - private static ArrayList vertexBuffers = new ArrayList<>(); + private static ObjectList vertexBuffers = new ObjectArrayList<>(1); /* * Allocate a (range) of "display list IDs" that will refer to a VBO in the arraylist of vertex buffers. @@ -30,11 +30,13 @@ public class VBOManager { return id; } - public static void registerVBO(int displayList, VertexBuffer vertexBuffer) { - if(displayList > vertexBuffers.size()) { - vertexBuffers.ensureCapacity(displayList * 2); + public static VertexBuffer registerVBO(int displayList, VertexBuffer vertexBuffer) { + final int requestedSize = displayList + 1; + if(requestedSize + 1 > vertexBuffers.size()) { + vertexBuffers.size(requestedSize); } - vertexBuffers.add(displayList, vertexBuffer); + vertexBuffers.set(displayList, vertexBuffer); + return vertexBuffer; } public static VertexBuffer get(int list) { diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/vbo/MixinRenderGlobal.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/vbo/MixinRenderGlobal.java index 1b5453acb..374deb0c9 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/vbo/MixinRenderGlobal.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/vbo/MixinRenderGlobal.java @@ -5,11 +5,16 @@ import com.gtnewhorizons.angelica.config.AngelicaConfig; import com.gtnewhorizons.angelica.glsm.TessellatorManager; import com.gtnewhorizons.angelica.glsm.VBOManager; import com.gtnewhorizons.angelica.mixins.interfaces.IRenderGlobalVBOCapture; +import com.gtnewhorizons.angelica.render.CloudRenderer; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.client.renderer.GLAllocation; import net.minecraft.client.renderer.RenderGlobal; import net.minecraft.client.renderer.Tessellator; +import net.minecraftforge.client.IRenderHandler; import org.lwjgl.opengl.GL11; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; @@ -19,6 +24,9 @@ public class MixinRenderGlobal implements IRenderGlobalVBOCapture { @Shadow public int starGLCallList; @Shadow private int glSkyList; @Shadow private int glSkyList2; + @Shadow public WorldClient theWorld; + @Shadow public Minecraft mc; + @Shadow private int cloudTickCounter; @Redirect(method="", at = @At(value="INVOKE", target="Lnet/minecraft/client/renderer/GLAllocation;generateDisplayLists(I)I", ordinal = 0)) private int generateGLRenderListBaseDisplayLists(int range) { @@ -76,4 +84,20 @@ public class MixinRenderGlobal implements IRenderGlobalVBOCapture { public void renderSky(int list) { VBOManager.get(list).render(GL11.GL_QUADS); } + + /** + * @author mitchej123 + * @reason VBO Clouds + */ + @Overwrite + public void renderClouds(float partialTicks) { + IRenderHandler renderer; + if((renderer = theWorld.provider.getCloudRenderer()) != null) { + renderer.render(partialTicks, theWorld, mc); + return; + } + if(mc.theWorld.provider.isSurfaceWorld()) { + CloudRenderer.getCloudRenderer().render(cloudTickCounter, partialTicks); + } + } } diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderGlobal.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderGlobal.java index 6d617dd4f..58516628f 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderGlobal.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderGlobal.java @@ -6,9 +6,7 @@ import com.gtnewhorizons.angelica.config.AngelicaConfig; import com.gtnewhorizons.angelica.mixins.interfaces.IRenderGlobalExt; import com.gtnewhorizons.angelica.rendering.AngelicaRenderQueue; import com.gtnewhorizons.angelica.rendering.RenderingState; - import lombok.Getter; -import me.jellysquid.mods.sodium.client.SodiumClientMod; import me.jellysquid.mods.sodium.client.gl.device.RenderDevice; import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer; import me.jellysquid.mods.sodium.client.render.chunk.passes.BlockRenderPass; @@ -25,12 +23,10 @@ import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.culling.Frustrum; import net.minecraft.client.renderer.culling.ICamera; -import net.minecraft.client.settings.GameSettings; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.init.Blocks; import org.lwjgl.opengl.GL11; -import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; @@ -301,9 +297,4 @@ public class MixinRenderGlobal implements IRenderGlobalExt { return e.isInRangeToRender3d(x, y, z) && SodiumWorldRenderer.getInstance().isEntityVisible(e); } - @Redirect(method = "renderClouds", at = @At(value = "FIELD", target = "Lnet/minecraft/client/settings/GameSettings;fancyGraphics:Z", opcode = Opcodes.GETFIELD, ordinal = 0)) - private boolean redirectGetFancyClouds(GameSettings settings) { - return SodiumClientMod.options().quality.cloudQuality.isFancy(); - } - } diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/TessellatorAccessor.java b/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/TessellatorAccessor.java deleted file mode 100644 index 446627919..000000000 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/TessellatorAccessor.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.gtnewhorizons.angelica.mixins.interfaces; - -import java.nio.ByteBuffer; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; -import java.nio.ShortBuffer; - -public interface TessellatorAccessor { - - ByteBuffer angelica$getByteBuffer(); - - IntBuffer angelica$getIntBuffer(); - - FloatBuffer angelica$getFloatBuffer(); - - ShortBuffer angelica$getShortBuffer(); - - float[] angelica$getVertexPos(); - - float angelica$getNormalX(); - - float angelica$getNormalY(); - - float angelica$getNormalZ(); - - float angelica$getMidTextureU(); - - float angelica$getMidTextureV(); - - void angelica$setByteBuffer(ByteBuffer angelica$byteBuffer); - - void angelica$setIntBuffer(IntBuffer angelica$intBuffer); - - void angelica$setFloatBuffer(FloatBuffer angelica$floatBuffer); - - void angelica$setShortBuffer(ShortBuffer angelica$shortBuffer); - - void angelica$setVertexPos(float[] angelica$vertexPos); - - void angelica$setNormalX(float angelica$normalX); - - void angelica$setNormalY(float angelica$normalY); - - void angelica$setNormalZ(float angelica$normalZ); - - void angelica$setMidTextureU(float angelica$midTextureU); - - void angelica$setMidTextureV(float angelica$midTextureV); -} diff --git a/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java b/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java index 3244be5ec..8314eb7fc 100644 --- a/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java +++ b/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java @@ -5,6 +5,7 @@ import com.gtnewhorizons.angelica.compat.ModStatus; import com.gtnewhorizons.angelica.config.AngelicaConfig; import com.gtnewhorizons.angelica.hudcaching.HUDCaching; import com.gtnewhorizons.angelica.loading.AngelicaTweaker; +import com.gtnewhorizons.angelica.render.CloudRenderer; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; @@ -188,4 +189,12 @@ public class ClientProxy extends CommonProxy { event.blue = 0f; } + @SubscribeEvent + public void onTick(TickEvent.ClientTickEvent event) { + if (event.phase == TickEvent.Phase.END) { + CloudRenderer.getCloudRenderer().checkSettings(); + } + } + + } diff --git a/src/main/java/com/gtnewhorizons/angelica/render/CloudRenderer.java b/src/main/java/com/gtnewhorizons/angelica/render/CloudRenderer.java new file mode 100644 index 000000000..39094f710 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/render/CloudRenderer.java @@ -0,0 +1,399 @@ +/* + * Backported from Minecraft Forge 1.12 under the LGPL 2.1 + * + * Minecraft Forge + * Copyright (c) 2016. + * + * This library 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 2.1 + * of the License. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package com.gtnewhorizons.angelica.render; + +import com.gtnewhorizons.angelica.client.renderer.CapturingTessellator; +import com.gtnewhorizons.angelica.compat.mojang.DefaultVertexFormat; +import com.gtnewhorizons.angelica.compat.mojang.VertexBuffer; +import com.gtnewhorizons.angelica.compat.mojang.VertexFormat; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.TessellatorManager; +import com.gtnewhorizons.angelica.glsm.VBOManager; +import me.jellysquid.mods.sodium.client.SodiumClientMod; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.client.renderer.OpenGlHelper; +import net.minecraft.client.renderer.texture.DynamicTexture; +import net.minecraft.client.resources.IReloadableResourceManager; +import net.minecraft.client.resources.IResourceManager; +import net.minecraft.client.resources.IResourceManagerReloadListener; +import net.minecraft.entity.Entity; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.Vec3; +import org.lwjgl.opengl.GL11; + +public class CloudRenderer implements IResourceManagerReloadListener { + // Shared constants. + private static final float PX_SIZE = 1 / 256F; + + // Building constants. + private static final VertexFormat FORMAT = DefaultVertexFormat.POSITION_TEXTURE_COLOR; + private static final int TOP_SECTIONS = 12; // Number of slices a top face will span. + private static final int HEIGHT = 4; + private static final float INSET = 0.001F; + private static final float ALPHA = 0.8F; + + // Debug + private static final boolean WIREFRAME = false; + + // Instance fields + private final Minecraft mc = Minecraft.getMinecraft(); + private final ResourceLocation texture = new ResourceLocation("textures/environment/clouds.png"); + + private int displayList = -1; + private VertexBuffer vbo; + private int cloudMode = -1; + private int renderDistance = -1; + + private DynamicTexture COLOR_TEX = null; + + private int texW; + private int texH; + + public CloudRenderer() { + // Resource manager should always be reloadable. + ((IReloadableResourceManager) mc.getResourceManager()).registerReloadListener(this); + } + + private static CloudRenderer instance; + public static CloudRenderer getCloudRenderer() { + if (instance == null) + instance = new CloudRenderer(); + return instance; + } + + + private int getScale() { + return cloudMode == 2 ? 12 : 8; + } + + private float ceilToScale(float value) { + float scale = getScale(); + return (float)Math.ceil(value / scale) * scale; + } + + private void vertices(CapturingTessellator tessellator) { + boolean fancy = SodiumClientMod.options().quality.cloudQuality.isFancy(); + + float scale = getScale(); + float CULL_DIST = 2 * scale; + + float bCol = fancy ? 0.7F : 1F; + + float sectEnd = ceilToScale((renderDistance * 2) * 16); + float sectStart = -sectEnd; + + float sectStep = ceilToScale(sectEnd * 2 / TOP_SECTIONS); + float sectPx = PX_SIZE / scale; + + tessellator.startDrawingQuads(); + + float sectX0 = sectStart; + float sectX1 = sectX0; + + while (sectX1 < sectEnd) { + sectX1 += sectStep; + + if (sectX1 > sectEnd) + sectX1 = sectEnd; + + float sectZ0 = sectStart; + float sectZ1 = sectZ0; + + while (sectZ1 < sectEnd) { + sectZ1 += sectStep; + + if (sectZ1 > sectEnd) + sectZ1 = sectEnd; + + float u0 = sectX0 * sectPx; + float u1 = sectX1 * sectPx; + float v0 = sectZ0 * sectPx; + float v1 = sectZ1 * sectPx; + + // Bottom + tessellator.pos(sectX0, 0, sectZ0).tex(u0, v0).color(bCol, bCol, bCol, ALPHA).endVertex(); + tessellator.pos(sectX1, 0, sectZ0).tex(u1, v0).color(bCol, bCol, bCol, ALPHA).endVertex(); + tessellator.pos(sectX1, 0, sectZ1).tex(u1, v1).color(bCol, bCol, bCol, ALPHA).endVertex(); + tessellator.pos(sectX0, 0, sectZ1).tex(u0, v1).color(bCol, bCol, bCol, ALPHA).endVertex(); + + if (fancy) { + // Top + tessellator.pos(sectX0, HEIGHT, sectZ0).tex(u0, v0).color(1, 1, 1, ALPHA).endVertex(); + tessellator.pos(sectX0, HEIGHT, sectZ1).tex(u0, v1).color(1, 1, 1, ALPHA).endVertex(); + tessellator.pos(sectX1, HEIGHT, sectZ1).tex(u1, v1).color(1, 1, 1, ALPHA).endVertex(); + tessellator.pos(sectX1, HEIGHT, sectZ0).tex(u1, v0).color(1, 1, 1, ALPHA).endVertex(); + + float slice; + float sliceCoord0; + float sliceCoord1; + + for (slice = sectX0; slice < sectX1; ) { + sliceCoord0 = slice * sectPx; + sliceCoord1 = sliceCoord0 + PX_SIZE; + + // X sides + if (slice > -CULL_DIST) { + slice += INSET; + tessellator.pos(slice, 0, sectZ1).tex(sliceCoord0, v1).color(0.9F, 0.9F, 0.9F, ALPHA).endVertex(); + tessellator.pos(slice, HEIGHT, sectZ1).tex(sliceCoord1, v1).color(0.9F, 0.9F, 0.9F, ALPHA).endVertex(); + tessellator.pos(slice, HEIGHT, sectZ0).tex(sliceCoord1, v0).color(0.9F, 0.9F, 0.9F, ALPHA).endVertex(); + tessellator.pos(slice, 0, sectZ0).tex(sliceCoord0, v0).color(0.9F, 0.9F, 0.9F, ALPHA).endVertex(); + slice -= INSET; + } + + slice += scale; + + if (slice <= CULL_DIST) { + slice -= INSET; + tessellator.pos(slice, 0, sectZ0).tex(sliceCoord0, v0).color(0.9F, 0.9F, 0.9F, ALPHA).endVertex(); + tessellator.pos(slice, HEIGHT, sectZ0).tex(sliceCoord1, v0).color(0.9F, 0.9F, 0.9F, ALPHA).endVertex(); + tessellator.pos(slice, HEIGHT, sectZ1).tex(sliceCoord1, v1).color(0.9F, 0.9F, 0.9F, ALPHA).endVertex(); + tessellator.pos(slice, 0, sectZ1).tex(sliceCoord0, v1).color(0.9F, 0.9F, 0.9F, ALPHA).endVertex(); + slice += INSET; + } + } + + for (slice = sectZ0; slice < sectZ1; ) { + sliceCoord0 = slice * sectPx; + sliceCoord1 = sliceCoord0 + PX_SIZE; + + // Z sides + if (slice > -CULL_DIST) { + slice += INSET; + tessellator.pos(sectX0, 0, slice).tex(u0, sliceCoord0).color(0.8F, 0.8F, 0.8F, ALPHA).endVertex(); + tessellator.pos(sectX0, HEIGHT, slice).tex(u0, sliceCoord1).color(0.8F, 0.8F, 0.8F, ALPHA).endVertex(); + tessellator.pos(sectX1, HEIGHT, slice).tex(u1, sliceCoord1).color(0.8F, 0.8F, 0.8F, ALPHA).endVertex(); + tessellator.pos(sectX1, 0, slice).tex(u1, sliceCoord0).color(0.8F, 0.8F, 0.8F, ALPHA).endVertex(); + slice -= INSET; + } + + slice += scale; + + if (slice <= CULL_DIST) { + slice -= INSET; + tessellator.pos(sectX1, 0, slice).tex(u1, sliceCoord0).color(0.8F, 0.8F, 0.8F, ALPHA).endVertex(); + tessellator.pos(sectX1, HEIGHT, slice).tex(u1, sliceCoord1).color(0.8F, 0.8F, 0.8F, ALPHA).endVertex(); + tessellator.pos(sectX0, HEIGHT, slice).tex(u0, sliceCoord1).color(0.8F, 0.8F, 0.8F, ALPHA).endVertex(); + tessellator.pos(sectX0, 0, slice).tex(u0, sliceCoord0).color(0.8F, 0.8F, 0.8F, ALPHA).endVertex(); + slice += INSET; + } + } + } + + sectZ0 = sectZ1; + } + + sectX0 = sectX1; + } + } + + private void dispose() { + if (vbo != null) { + vbo.close(); + vbo = null; + } + } + + private void build() { + TessellatorManager.startCapturing(); + CapturingTessellator tess = (CapturingTessellator) TessellatorManager.get(); + + vertices(tess); + + if(displayList == -1) { + this.displayList = VBOManager.generateDisplayLists(1); + } + this.vbo = VBOManager.registerVBO(this.displayList, TessellatorManager.stopCapturingToVBO(FORMAT)); + } + + private int fullCoord(double coord, int scale) { // Corrects misalignment of UV offset when on negative coords. + return ((int) coord / scale) - (coord < 0 ? 1 : 0); + } + + private boolean isBuilt() { + return vbo != null; + } + + public void checkSettings() { + final int cloudQualitySetting = SodiumClientMod.options().quality.cloudQuality.isFancy() ? 2 : 1; + boolean newEnabled = mc.gameSettings.shouldRenderClouds() + && mc.theWorld != null + && mc.theWorld.provider.isSurfaceWorld(); + + if (isBuilt() + && (!newEnabled + || cloudQualitySetting != cloudMode + || mc.gameSettings.renderDistanceChunks != renderDistance)) { + dispose(); + } + + cloudMode = cloudQualitySetting; + renderDistance = mc.gameSettings.renderDistanceChunks; + + if (newEnabled && !isBuilt()) { + build(); + } + } + + public boolean render(int cloudTicks, float partialTicks) { + if (!isBuilt()) + return false; + + Entity entity = mc.renderViewEntity; + + double totalOffset = cloudTicks + partialTicks; + + double x = entity.prevPosX + (entity.posX - entity.prevPosX) * partialTicks + totalOffset * 0.03; + double y = mc.theWorld.provider.getCloudHeight() - (entity.lastTickPosY + (entity.posY - entity.lastTickPosY) * partialTicks) + 0.33; + double z = entity.prevPosZ + (entity.posZ - entity.prevPosZ) * partialTicks; + + int scale = getScale(); + + if (cloudMode == 2) + z += 0.33 * scale; + + // Integer UVs to translate the texture matrix by. + int offU = fullCoord(x, scale); + int offV = fullCoord(z, scale); + + GLStateManager.glPushMatrix(); + + // Translate by the remainder after the UV offset. + GLStateManager.glTranslated((offU * scale) - x, y, (offV * scale) - z); + + // Modulo to prevent texture samples becoming inaccurate at extreme offsets. + offU = offU % texW; + offV = offV % texH; + + // Translate the texture. + GLStateManager.glMatrixMode(GL11.GL_TEXTURE); + GLStateManager.glTranslated(offU * PX_SIZE, offV * PX_SIZE, 0); + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + + GLStateManager.disableCull(); + + GLStateManager.enableBlend(); + GLStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO); + + // Color multiplier. + Vec3 color = mc.theWorld.getCloudColour(partialTicks); + float r = (float) color.xCoord; + float g = (float) color.yCoord; + float b = (float) color.zCoord; + + if (mc.gameSettings.anaglyph) { + float tempR = r * 0.3F + g * 0.59F + b * 0.11F; + float tempG = r * 0.3F + g * 0.7F; + float tempB = r * 0.3F + b * 0.7F; + r = tempR; + g = tempG; + b = tempB; + } + + if (COLOR_TEX == null) + COLOR_TEX = new DynamicTexture(1, 1); + + // Apply a color multiplier through a texture upload if shaders aren't supported. + COLOR_TEX.getTextureData()[0] = 255 << 24 + | ((int) (r * 255)) << 16 + | ((int) (g * 255)) << 8 + | (int) (b * 255); + COLOR_TEX.updateDynamicTexture(); + + GLStateManager.glActiveTexture(OpenGlHelper.lightmapTexUnit); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, COLOR_TEX.getGlTextureId()); + GLStateManager.enableTexture(); + + // Bind the clouds texture last so the shader's sampler2D is correct. + GLStateManager.glActiveTexture(OpenGlHelper.defaultTexUnit); + mc.renderEngine.bindTexture(texture); + + vbo.setupState(); + + // Depth pass to prevent insides rendering from the outside. + GLStateManager.glColorMask(false, false, false, false); + + vbo.draw(GL11.GL_QUADS); + + // Full render. + if (!mc.gameSettings.anaglyph) { + GLStateManager.glColorMask(true, true, true, true); + } else { + switch (EntityRenderer.anaglyphField) { + case 0: + GLStateManager.glColorMask(false, true, true, true); + break; + case 1: + GLStateManager.glColorMask(true, false, false, true); + break; + } + } + + // Wireframe for debug. + if (WIREFRAME) { + GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE); + GL11.glLineWidth(2.0F); + GLStateManager.disableTexture(); + GLStateManager.glDepthMask(false); + GLStateManager.disableFog(); + vbo.draw(GL11.GL_QUADS); + GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL); + GLStateManager.glDepthMask(true); + GLStateManager.enableTexture(); + GLStateManager.enableFog(); + } + + vbo.draw(GL11.GL_QUADS); + vbo.cleanupState(); // Unbind buffer and disable pointers. + + // Disable our coloring. + GLStateManager.glActiveTexture(OpenGlHelper.lightmapTexUnit); + GLStateManager.disableTexture(); + GLStateManager.glActiveTexture(OpenGlHelper.defaultTexUnit); + + // Reset texture matrix. + GLStateManager.glMatrixMode(GL11.GL_TEXTURE); + GLStateManager.glLoadIdentity(); + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + + GLStateManager.disableBlend(); + GLStateManager.enableCull(); + + GLStateManager.glPopMatrix(); + + return true; + } + + private void reloadTextures() { + if (mc.renderEngine != null) { + mc.renderEngine.bindTexture(texture); + texW = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH); + texH = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_HEIGHT); + } + } + + @Override + public void onResourceManagerReload(IResourceManager resourceManager) { + reloadTextures(); + } +} diff --git a/src/main/resources/META-INF/angelica_at.cfg b/src/main/resources/META-INF/angelica_at.cfg index 6c4318eef..d6400b3ce 100644 --- a/src/main/resources/META-INF/angelica_at.cfg +++ b/src/main/resources/META-INF/angelica_at.cfg @@ -20,6 +20,7 @@ public net.minecraft.client.renderer.Tessellator field_78407_w # yOffset public net.minecraft.client.renderer.Tessellator field_78417_x # zOffset public net.minecraft.client.renderer.Tessellator field_78415_z # isDrawing public net.minecraft.client.renderer.Tessellator field_78388_E # bufferSize +public net.minecraft.client.renderer.Tessellator field_78410_t # isColorDisabled public net.minecraft.client.renderer.Tessellator func_78379_d()V # void reset() public net.minecraft.client.model.ModelRenderer field_78812_q # compiled