Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce RAM allocation when rendering #11

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

dependencies {
api("com.github.GTNewHorizons:ForgeMultipart:1.5.0:dev")
api("com.github.GTNewHorizons:CodeChickenCore:1.2.1:dev")
api("com.github.GTNewHorizons:CodeChickenCore:1.3.8:dev")
implementation("com.github.GTNewHorizons:CodeChickenLib:1.3.0:dev")
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

public class RenderTracker extends RenderEntity implements IItemRenderer {

private static CCModel model;
private static final CCModel model;
private static final Vector3 Y_AXIS = new Vector3(0, 1, 0);

static {
model = CCModel.parseObjModels(new ResourceLocation("wrcbe_addons", "models/tracker.obj"), 7, new SwapYZ())
Expand All @@ -46,58 +47,67 @@ public void renderTracker(int freq) {

TextureUtils.bindAtlas(0);
final CCRenderState state = CCRenderState.instance();
state.reset();
state.startDrawing(7);
state.setColour(0xFFFFFFFF);
state.resetInstance();
state.startDrawingInstance(7);
state.setColourInstance(0xFFFFFFFF);
model.render(new IconTransformation(Blocks.obsidian.getIcon(0, 0)));
state.draw();
state.drawInstance();

Matrix4 pearlMat = CCModelLibrary.getRenderMatrix(
new Vector3(0, 0.44 + RedstoneEther.getSineWave(ClientUtils.getRenderTime(), 7) * 0.02, 0),
new Rotation(RedstoneEther.getRotation(ClientUtils.getRenderTime(), freq), new Vector3(0, 1, 0)),
0,
0.44 + RedstoneEther.getSineWave(ClientUtils.getRenderTime(), 7) * 0.02,
0,
new Rotation(RedstoneEther.getRotation(ClientUtils.getRenderTime(), freq), Y_AXIS),
0.04);

state.changeTexture("wrcbe_core:textures/hedronmap.png");
state.startDrawing(4);
state.setColour(freq == 0 ? 0xC0C0C0FF : 0xFFFFFFFF);
CCRenderState.changeTexture("wrcbe_core:textures/hedronmap.png");
state.startDrawingInstance(4);
state.setColourInstance(freq == 0 ? 0xC0C0C0FF : 0xFFFFFFFF);
CCModelLibrary.icosahedron4.render(pearlMat);
state.draw();
state.drawInstance();

GL11.glEnable(GL11.GL_LIGHTING);
}

@Override
public void doRender(Entity entity, double x, double y, double z, float f, float f1) {
GL11.glPushMatrix();
GL11.glTranslated(x, y + 0.2, z);

EntityWirelessTracker tracker = (EntityWirelessTracker) entity;
if (tracker.isAttachedToEntity()) {
Vector3 relVec = tracker.getRotatedAttachment();

Vector3 yAxis = new Vector3(0, 1, 0);
Vector3 axis = relVec.copy().crossProduct(yAxis);
double angle = -(relVec.angle(yAxis) * todeg);

GL11.glTranslated(-x, -y - 0.2, -z); // undo translation

Vector3 pos = new Vector3(
tracker.attachedEntity.lastTickPosX
+ (tracker.attachedEntity.posX - tracker.attachedEntity.lastTickPosX) * f1,
tracker.attachedEntity.lastTickPosY
+ (tracker.attachedEntity.posY - tracker.attachedEntity.lastTickPosY) * f1
+ tracker.attachedEntity.height / 2
- tracker.attachedEntity.yOffset
- tracker.height,
tracker.attachedEntity.lastTickPosZ
+ (tracker.attachedEntity.posZ - tracker.attachedEntity.lastTickPosZ) * f1);

pos.add(relVec).add(-RenderManager.renderPosX, -RenderManager.renderPosY, -RenderManager.renderPosZ);
Vector3 relVec = tracker.getRotatedAttachment();

GL11.glTranslated(pos.x, pos.y, pos.z);
final double posX = tracker.attachedEntity.lastTickPosX
+ (tracker.attachedEntity.posX - tracker.attachedEntity.lastTickPosX) * f1
+ relVec.x
- RenderManager.renderPosX;
final double posY = tracker.attachedEntity.lastTickPosY
+ (tracker.attachedEntity.posY - tracker.attachedEntity.lastTickPosY) * f1
+ tracker.attachedEntity.height / 2
- tracker.attachedEntity.yOffset
- tracker.height
+ relVec.y
- RenderManager.renderPosY;
final double posZ = tracker.attachedEntity.lastTickPosZ
+ (tracker.attachedEntity.posZ - tracker.attachedEntity.lastTickPosZ) * f1
+ relVec.z
- RenderManager.renderPosZ;

GL11.glTranslated(posX, posY, posZ);

final double axisX = -relVec.z;
final double axisY = 0;
final double axisZ = relVec.x;
// leaving this code commented for understand,
// but it can be simplified to what is below for speed
// Vector3 yAxis = new Vector3(0, 1, 0);
// double angle = -(relVec.angle(yAxis) * todeg);
final double angle = -(Math.acos(relVec.normalize().y) * todeg);
GL11.glRotatef((float) angle, (float) axisX, (float) axisY, (float) axisZ);

GL11.glRotatef((float) angle, (float) axis.x, (float) axis.y, (float) axis.z);
} else if (tracker.item) {
GL11.glTranslated(x, y + 0.2, z);
double bob = sin(ClientUtils.getRenderTime() / 10) * 0.1;
double rotate = ClientUtils.getRenderTime() / 20 * todeg;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
package codechicken.wirelessredstone.core;

import java.util.Iterator;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.Entity;

import org.lwjgl.opengl.GL11;

import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.RenderUtils;
import codechicken.lib.vec.Vector3;
import codechicken.wirelessredstone.core.WirelessBolt.Segment;

public class RenderWirelessBolt {

private static Vector3 getRelativeViewVector(Vector3 pos) {
Entity renderentity = Minecraft.getMinecraft().renderViewEntity;
return new Vector3(
(float) renderentity.posX - pos.x,
(float) renderentity.posY + renderentity.getEyeHeight() - pos.y,
(float) renderentity.posZ - pos.z);
}

public static void render(float frame, Entity entity) {
GL11.glPushMatrix();
RenderUtils.translateToWorldCoords(entity, frame);
Expand All @@ -33,43 +21,28 @@ public static void render(float frame, Entity entity) {
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

final CCRenderState state = CCRenderState.instance();
state.reset();
state.setBrightness(0xF000F0);
state.changeTexture("wrcbe_core:textures/lightning_glowstone.png");
state.startDrawing(7);
for (WirelessBolt bolt : WirelessBolt.clientboltlist) renderBolt(
bolt,
frame,
ActiveRenderInfo.rotationX,
ActiveRenderInfo.rotationXZ,
ActiveRenderInfo.rotationZ,
ActiveRenderInfo.rotationXY,
0);
state.draw();

state.changeTexture("wrcbe_core:textures/lightning_redstone.png");
state.startDrawing(7);
for (WirelessBolt bolt : WirelessBolt.clientboltlist) renderBolt(
bolt,
frame,
ActiveRenderInfo.rotationX,
ActiveRenderInfo.rotationXZ,
ActiveRenderInfo.rotationZ,
ActiveRenderInfo.rotationXY,
1);
state.draw();
state.resetInstance();
state.setBrightnessInstance(0xF000F0);
CCRenderState.changeTexture("wrcbe_core:textures/lightning_glowstone.png");
state.startDrawingInstance(7);
for (WirelessBolt bolt : WirelessBolt.clientboltlist) renderBolt(bolt, 0);
state.drawInstance();

CCRenderState.changeTexture("wrcbe_core:textures/lightning_redstone.png");
state.startDrawingInstance(7);
for (WirelessBolt bolt : WirelessBolt.clientboltlist) renderBolt(bolt, 1);
state.drawInstance();

GL11.glDisable(GL11.GL_BLEND);
GL11.glDepthMask(true);

GL11.glPopMatrix();
}

private static void renderBolt(WirelessBolt bolt, float partialframe, float cosyaw, float cospitch, float sinyaw,
float cossinpitch, int pass) {
private static void renderBolt(WirelessBolt bolt, int pass) {
Tessellator t = Tessellator.instance;
float boltage = bolt.particleAge < 0 ? 0 : (float) bolt.particleAge / (float) bolt.particleMaxAge;
float mainalpha = 1;
float mainalpha;
if (pass == 0) mainalpha = (1 - boltage) * 0.4F;
else mainalpha = 1 - boltage * 0.5F;

Expand All @@ -78,48 +51,102 @@ private static void renderBolt(WirelessBolt bolt, float partialframe, float cosy
* bolt.numsegments0);
int renderend = (int) ((bolt.particleAge + expandTime) / (float) expandTime * bolt.numsegments0);

for (Iterator<Segment> iterator = bolt.segments.iterator(); iterator.hasNext();) {
Segment rendersegment = iterator.next();

for (Segment rendersegment : bolt.segments) {
if (rendersegment.segmentno < renderstart || rendersegment.segmentno > renderend) continue;

Vector3 playervec = getRelativeViewVector(rendersegment.startpoint.point).negate();

double width = 0.025F * (playervec.mag() / 5 + 1) * (1 + rendersegment.light) * 0.5F;

Vector3 diff1 = playervec.copy().crossProduct(rendersegment.prevdiff).normalize()
.multiply(width / rendersegment.sinprev);
Vector3 diff2 = playervec.copy().crossProduct(rendersegment.nextdiff).normalize()
.multiply(width / rendersegment.sinnext);

Vector3 startvec = rendersegment.startpoint.point;
Vector3 endvec = rendersegment.endpoint.point;
Entity viewEntity = Minecraft.getMinecraft().renderViewEntity;
double startX = rendersegment.startpoint.point.x;
double startY = rendersegment.startpoint.point.y;
double startZ = rendersegment.startpoint.point.z;

double playerX = viewEntity.posX - startX;
double playerY = viewEntity.posY + viewEntity.getEyeHeight() - startY;
double playerZ = viewEntity.posZ - startZ;

double playerMag = Math.sqrt(playerX * playerX + playerY * playerY + playerZ * playerZ);
double width = 0.025F * (playerMag / 5 + 1) * (1 + rendersegment.light) * 0.5F;

double prevDiffX = rendersegment.prevdiff.x;
double prevDiffY = rendersegment.prevdiff.y;
double prevDiffZ = rendersegment.prevdiff.z;

double nextDiffX = rendersegment.nextdiff.x;
double nextDiffY = rendersegment.nextdiff.y;
double nextDiffZ = rendersegment.nextdiff.z;

double crossPrevX = playerY * prevDiffZ - playerZ * prevDiffY;
double crossPrevY = playerZ * prevDiffX - playerX * prevDiffZ;
double crossPrevZ = playerX * prevDiffY - playerY * prevDiffX;
double crossPrevMag = Math
.sqrt(crossPrevX * crossPrevX + crossPrevY * crossPrevY + crossPrevZ * crossPrevZ);
crossPrevX /= crossPrevMag;
crossPrevY /= crossPrevMag;
crossPrevZ /= crossPrevMag;

crossPrevX *= width / rendersegment.sinprev;
crossPrevY *= width / rendersegment.sinprev;
crossPrevZ *= width / rendersegment.sinprev;

double crossNextX = playerY * nextDiffZ - playerZ * nextDiffY;
double crossNextY = playerZ * nextDiffX - playerX * nextDiffZ;
double crossNextZ = playerX * nextDiffY - playerY * nextDiffX;
double crossNextMag = Math
.sqrt(crossNextX * crossNextX + crossNextY * crossNextY + crossNextZ * crossNextZ);
crossNextX /= crossNextMag;
crossNextY /= crossNextMag;
crossNextZ /= crossNextMag;

crossNextX *= width / rendersegment.sinnext;
crossNextY *= width / rendersegment.sinnext;
crossNextZ *= width / rendersegment.sinnext;

double endX = rendersegment.endpoint.point.x;
double endY = rendersegment.endpoint.point.y;
double endZ = rendersegment.endpoint.point.z;

t.setColorRGBA_F(1, 1, 1, mainalpha * rendersegment.light);

t.addVertexWithUV(endvec.x - diff2.x, endvec.y - diff2.y, endvec.z - diff2.z, 0.5, 0);
t.addVertexWithUV(startvec.x - diff1.x, startvec.y - diff1.y, startvec.z - diff1.z, 0.5, 0);
t.addVertexWithUV(startvec.x + diff1.x, startvec.y + diff1.y, startvec.z + diff1.z, 0.5, 1);
t.addVertexWithUV(endvec.x + diff2.x, endvec.y + diff2.y, endvec.z + diff2.z, 0.5, 1);
t.addVertexWithUV(endX - crossNextX, endY - crossNextY, endZ - crossNextZ, 0.5, 0);
t.addVertexWithUV(startX - crossPrevX, startY - crossPrevY, startZ - crossPrevZ, 0.5, 0);
t.addVertexWithUV(startX + crossPrevX, startY + crossPrevY, startZ + crossPrevZ, 0.5, 1);
t.addVertexWithUV(endX + crossNextX, endY + crossNextY, endZ + crossNextZ, 0.5, 1);

if (rendersegment.next == null) {
Vector3 roundend = rendersegment.endpoint.point.copy()
.add(rendersegment.diff.copy().normalize().multiply(width));

t.addVertexWithUV(roundend.x - diff2.x, roundend.y - diff2.y, roundend.z - diff2.z, 0, 0);
t.addVertexWithUV(endvec.x - diff2.x, endvec.y - diff2.y, endvec.z - diff2.z, 0.5, 0);
t.addVertexWithUV(endvec.x + diff2.x, endvec.y + diff2.y, endvec.z + diff2.z, 0.5, 1);
t.addVertexWithUV(roundend.x + diff2.x, roundend.y + diff2.y, roundend.z + diff2.z, 0, 1);
double diffX = rendersegment.diff.x;
double diffY = rendersegment.diff.y;
double diffZ = rendersegment.diff.z;
double diffMag = Math.sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ);
diffX /= diffMag;
diffY /= diffMag;
diffZ /= diffMag;

double roundEndX = endX + diffX * width;
double roundEndY = endY + diffY * width;
double roundEndZ = endZ + diffZ * width;

t.addVertexWithUV(roundEndX - crossNextX, roundEndY - crossNextY, roundEndZ - crossNextZ, 0, 0);
t.addVertexWithUV(endX - crossNextX, endY - crossNextY, endZ - crossNextZ, 0.5, 0);
t.addVertexWithUV(endX + crossNextX, endY + crossNextY, endZ + crossNextZ, 0.5, 1);
t.addVertexWithUV(roundEndX + crossNextX, roundEndY + crossNextY, roundEndZ + crossNextZ, 0, 1);
}

if (rendersegment.prev == null) {
Vector3 roundend = rendersegment.startpoint.point.copy()
.subtract(rendersegment.diff.copy().normalize().multiply(width));

t.addVertexWithUV(startvec.x - diff1.x, startvec.y - diff1.y, startvec.z - diff1.z, 0.5, 0);
t.addVertexWithUV(roundend.x - diff1.x, roundend.y - diff1.y, roundend.z - diff1.z, 0, 0);
t.addVertexWithUV(roundend.x + diff1.x, roundend.y + diff1.y, roundend.z + diff1.z, 0, 1);
t.addVertexWithUV(startvec.x + diff1.x, startvec.y + diff1.y, startvec.z + diff1.z, 0.5, 1);
double diffX = rendersegment.diff.x;
double diffY = rendersegment.diff.y;
double diffZ = rendersegment.diff.z;
double diffMag = Math.sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ);
diffX /= diffMag;
diffY /= diffMag;
diffZ /= diffMag;

double roundEndX = startX - diffX * width;
double roundEndY = startY - diffY * width;
double roundEndZ = startZ - diffZ * width;

t.addVertexWithUV(startX - crossPrevX, startY - crossPrevY, startZ - crossPrevZ, 0.5, 0);
t.addVertexWithUV(roundEndX - crossPrevX, roundEndY - crossPrevY, roundEndZ - crossPrevZ, 0, 0);
t.addVertexWithUV(roundEndX + crossPrevX, roundEndY + crossPrevY, roundEndZ + crossPrevZ, 0, 1);
t.addVertexWithUV(startX + crossPrevX, startY + crossPrevY, startZ + crossPrevZ, 0.5, 1);
}
}
}
Expand Down
Loading