From c2b4a07ecde6d0a11ddce1499ac9709609a0c7da Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 6 Feb 2019 20:35:18 +0200 Subject: [PATCH] Use a call list to speed up sphere rendering --- .../unicopia/model/ModelSphere.java | 94 +++++++++++++++++++ .../particle/client/ParticleSphere.java | 39 ++------ 2 files changed, 103 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/model/ModelSphere.java diff --git a/src/main/java/com/minelittlepony/unicopia/model/ModelSphere.java b/src/main/java/com/minelittlepony/unicopia/model/ModelSphere.java new file mode 100644 index 00000000..2efe3446 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/model/ModelSphere.java @@ -0,0 +1,94 @@ +package com.minelittlepony.unicopia.model; + +import org.lwjgl.util.glu.GLU; +import org.lwjgl.util.glu.Sphere; + +import net.minecraft.client.renderer.GLAllocation; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; + +public class ModelSphere { + + private int displayList; + private boolean baked; + + protected double posX; + protected double posY; + protected double posZ; + + protected float rotX; + protected float rotY; + protected float rotZ; + + public void setPosition(double x, double y, double z) { + posX = x - TileEntityRendererDispatcher.staticPlayerX; + posY = y - TileEntityRendererDispatcher.staticPlayerY; + posZ = z - TileEntityRendererDispatcher.staticPlayerZ; + } + + public void setRotation(float x, float y, float z) { + rotX = x; + rotY = y; + rotZ = z; + } + + public void render(float scale) { + if (scale == 0) { + return; + } + + if (!baked) { + baked = true; + bake(); + } + + GlStateManager.pushMatrix(); + + if (posX != 0 && posY != 9 && posZ != 0) { + GlStateManager.translate(posX, posY, posZ); + } + + glRotate(rotX, 1, 0, 0); + glRotate(rotY, 0, 1, 0); + glRotate(rotZ, 0, 0, 1); + + GlStateManager.scale(scale, scale, scale); + + GlStateManager.callList(displayList); + + GlStateManager.popMatrix(); + } + + private void bake() { + displayList = GLAllocation.generateDisplayLists(1); + GlStateManager.glNewList(displayList, 4864); + + GlStateManager.disableTexture2D(); + GlStateManager.enableAlpha(); + GlStateManager.enableColorMaterial(); + GlStateManager.colorMaterial(1032, 5634); + + drawShape(); + + GlStateManager.disableColorMaterial(); + GlStateManager.disableAlpha(); + GlStateManager.enableTexture2D(); + + GlStateManager.glEndList(); + } + + protected void drawShape() { + Sphere sphere = new Sphere(); + + sphere.setDrawStyle(GLU.GLU_FILL); + sphere.setNormals(GLU.GLU_SMOOTH); + sphere.draw(1, 32, 32); + } + + static void glRotate(float angle, float x, float y, float z) { + if (angle != 0) { + GlStateManager.rotate(angle, x, y, z); + } + } + +} diff --git a/src/main/java/com/minelittlepony/unicopia/particle/client/ParticleSphere.java b/src/main/java/com/minelittlepony/unicopia/particle/client/ParticleSphere.java index c5b15b23..afc65c79 100644 --- a/src/main/java/com/minelittlepony/unicopia/particle/client/ParticleSphere.java +++ b/src/main/java/com/minelittlepony/unicopia/particle/client/ParticleSphere.java @@ -3,12 +3,9 @@ package com.minelittlepony.unicopia.particle.client; import net.minecraft.client.particle.Particle; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.entity.Entity; import net.minecraft.world.World; -import org.lwjgl.util.glu.GLU; -import org.lwjgl.util.glu.Sphere; - +import com.minelittlepony.unicopia.model.ModelSphere; import com.minelittlepony.unicopia.particle.IAttachableParticle; import com.minelittlepony.unicopia.spell.ICaster; import com.minelittlepony.util.render.Color; @@ -17,13 +14,15 @@ public class ParticleSphere extends Particle implements IAttachableParticle { private final float baseAlpha; - private int tint; - private float alpha; + protected int tint; + protected float alpha; - private int radius; + protected int radius; private ICaster caster; + private static final ModelSphere model = new ModelSphere(); + public ParticleSphere(int id, World w, double x, double y, double z, double vX, double vY, double vZ, int... args) { this(w, x, y, z, args[0], args[1], args[2]/255F); } @@ -67,37 +66,17 @@ public class ParticleSphere extends Particle implements IAttachableParticle { } } - public void renderParticle(BufferBuilder buffer, Entity entityIn, float partialTicks, float rotationX, float rotationZ, float rotationYZ, float rotationXY, float rotationXZ) { + public void renderParticle(BufferBuilder buffer, Entity viewer, float partialTicks, float x, float z, float yz, float xy, float xz) { if (alpha <= 0) { return; } - GlStateManager.pushMatrix(); - GlStateManager.disableTexture2D(); - GlStateManager.enableAlpha(); - GlStateManager.enableColorMaterial(); - GlStateManager.colorMaterial(1032, 5634); - - GlStateManager.translate( - posX - TileEntityRendererDispatcher.staticPlayerX, - posY - TileEntityRendererDispatcher.staticPlayerY, - posZ - TileEntityRendererDispatcher.staticPlayerZ - ); - Color.glColor(tint, alpha); - final Sphere s = new Sphere(); - - s.setDrawStyle(GLU.GLU_FILL); - s.setNormals(GLU.GLU_SMOOTH); - s.draw(radius, 32, 32); + model.setPosition(posX, posY, posZ); + model.render(radius); GlStateManager.color(1, 1, 1, 1); - - GlStateManager.disableColorMaterial(); - GlStateManager.disableAlpha(); - GlStateManager.enableTexture2D(); - GlStateManager.popMatrix(); } @Override