Skip to content

Commit

Permalink
Torus Object implemented, clean up some inconsistencies, clean up Ren…
Browse files Browse the repository at this point in the history
…derUtil.java, support for rotation and floating number coordinates
  • Loading branch information
SirEndii committed Nov 5, 2024
1 parent 396e6b7 commit 130d91c
Show file tree
Hide file tree
Showing 22 changed files with 569 additions and 271 deletions.
248 changes: 118 additions & 130 deletions src/main/java/de/srendi/advancedperipherals/client/RenderUtil.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderLevelStageEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand Down Expand Up @@ -82,30 +80,25 @@ public static void renderLevelState(RenderLevelStageEvent event) {
BufferUploader.drawWithShader(bufferbuilder.end());
posestack.popPose();

bufferbuilder.begin(VertexFormat.Mode.TRIANGLES, DefaultVertexFormat.POSITION_COLOR_NORMAL);
posestack.pushPose();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL);
colors = EnumColor.LIGHT_PURPLE.getRgb();

blockPos = new BlockPos(0, 10, 2);
blockPos = new BlockPos(0, 10, 0);
posestack.translate(-view.x + blockPos.getX(), -view.y + blockPos.getY(), -view.z + blockPos.getZ());

VoxelShape shape = Block.box(0.0, 0.0, 0.0, 16.0, 18.0, 16.0);
RenderSystem.setShaderColor(colors[0], colors[1], colors[2], 0.6f);

RenderUtil.drawVoxelShape(posestack, bufferbuilder, shape, 0f, 0f, 0f, colors[0], colors[1], colors[2], 0.6f);
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderUtil.drawSphere(posestack, bufferbuilder, 0.5f, 0f, 0f, 0f, 90f, 0f, 0f, colors[0], colors[1], colors[2], 0.6f, 128, 48);

BufferUploader.drawWithShader(bufferbuilder.end());
posestack.popPose();

bufferbuilder.begin(VertexFormat.Mode.TRIANGLES, DefaultVertexFormat.POSITION_COLOR_NORMAL);
bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL);
posestack.pushPose();

blockPos = new BlockPos(0, 10, 0);
colors = EnumColor.DARK_BLUE.getRgb();
blockPos = new BlockPos(6, 10, 0);
posestack.translate(-view.x + blockPos.getX(), -view.y + blockPos.getY(), -view.z + blockPos.getZ());

RenderUtil.drawSphere(posestack, bufferbuilder, 0.5f, 0f, 0f, 0f, colors[0], colors[1], colors[2], 0.6f, 128, 48);
RenderUtil.drawTorus(posestack, bufferbuilder, 0.5f, 0.09f, 0f, 0f, 0f, 0f, 0f, 0f, colors[0], colors[1], colors[2], 0.6f, 300, 32);

BufferUploader.drawWithShader(bufferbuilder.end());
posestack.popPose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BlockObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BoxObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.SphereObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.TorusObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.CircleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.ItemObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RectangleObject;
Expand Down Expand Up @@ -52,5 +53,6 @@ public static void registerDecodeObjects() {
ObjectDecodeRegistry.register(BoxObject.TYPE_ID, BoxObject::decode);
ObjectDecodeRegistry.register(BlockObject.TYPE_ID, BlockObject::decode);
ObjectDecodeRegistry.register(SphereObject.TYPE_ID, SphereObject::decode);
ObjectDecodeRegistry.register(TorusObject.TYPE_ID, TorusObject::decode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Quaternion;
import de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BlockObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject;
import de.srendi.advancedperipherals.common.util.RegistryUtil;
Expand All @@ -25,33 +27,34 @@ public class BlockRenderer implements IThreeDObjectRenderer {
public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder) {
poseStack.pushPose();

for (ThreeDimensionalObject renderableObject : batch) {
for (ThreeDimensionalObject obj : batch) {
poseStack.pushPose();
onPreRender(renderableObject);
onPreRender(obj);

bufferBuilder.begin(RenderType.solid().mode(), DefaultVertexFormat.BLOCK);

BlockObject block = (BlockObject) renderableObject;
BlockObject block = (BlockObject) obj;

BlockPos blockPos = new BlockPos(renderableObject.getX(), renderableObject.getY(), renderableObject.getZ());

poseStack.translate(-view.x + blockPos.getX(), -view.y + blockPos.getY(), -view.z + blockPos.getZ());
float alpha = renderableObject.opacity;
float red = (float) (renderableObject.color >> 16 & 255) / 255.0F;
float green = (float) (renderableObject.color >> 8 & 255) / 255.0F;
float blue = (float) (renderableObject.color & 255) / 255.0F;
poseStack.translate(-view.x + block.getX(), -view.y + block.getY(), -view.z + block.getZ());
poseStack.mulPose(new Quaternion(block.xRot, block.yRot, block.zRot, true));
poseStack.translate(-0.5f, -0.5f, -0.5f);
float alpha = block.opacity;
float red = RenderUtil.getRed(block.color);
float green = RenderUtil.getGreen(block.color);
float blue = RenderUtil.getBlue(block.color);

RenderSystem.setShader(GameRenderer::getBlockShader);
RenderSystem.setShaderColor(red, green, blue, alpha);

Block blockToRender = RegistryUtil.getRegistryEntry(block.block, ForgeRegistries.BLOCKS);
BlockPos blockPos = new BlockPos(obj.getX(), obj.getY(), obj.getZ());

if (blockToRender != null)
Minecraft.getInstance().getBlockRenderer().renderBatched(blockToRender.defaultBlockState(), blockPos, event.getCamera().getEntity().level, poseStack, bufferBuilder, false, event.getCamera().getEntity().level.random);

poseStack.popPose();
BufferUploader.drawWithShader(bufferBuilder.end());
onPostRender(renderableObject);
onPostRender(obj);
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexFormat;
import de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BoxObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.world.phys.Vec3;
Expand All @@ -20,21 +21,23 @@ public class BoxRenderer implements IThreeDObjectRenderer {
public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder) {
poseStack.pushPose();

for (ThreeDimensionalObject renderableObject : batch) {
for (ThreeDimensionalObject obj : batch) {
poseStack.pushPose();
onPreRender(renderableObject);
onPreRender(obj);
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL);

BoxObject box = (BoxObject) obj;

RenderSystem.setShader(GameRenderer::getPositionColorShader);
float alpha = renderableObject.opacity;
float red = (float) (renderableObject.color >> 16 & 255) / 255.0F;
float green = (float) (renderableObject.color >> 8 & 255) / 255.0F;
float blue = (float) (renderableObject.color & 255) / 255.0F;
float alpha = box.opacity;
float red = RenderUtil.getRed(box.color);
float green = RenderUtil.getGreen(box.color);
float blue = RenderUtil.getBlue(box.color);

poseStack.translate(-view.x + renderableObject.getX(), -view.y + renderableObject.getY(), -view.z + renderableObject.getZ());
RenderUtil.drawBox(poseStack, bufferBuilder, red, green, blue, alpha, renderableObject.getMaxX(), renderableObject.getMaxY(), renderableObject.getMaxX());
poseStack.translate(-view.x + box.getX(), -view.y + box.getY(), -view.z + box.getZ());
RenderUtil.drawBox(poseStack, bufferBuilder, red, green, blue, alpha, box.x, box.y, box.z, obj.xRot, obj.yRot, obj.zRot, obj.getMaxX(), obj.getMaxY(), obj.getMaxX());
BufferUploader.drawWithShader(bufferBuilder.end());
onPostRender(renderableObject);
onPostRender(obj);

poseStack.popPose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ public class SphereRenderer implements IThreeDObjectRenderer {
public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder) {
poseStack.pushPose();

for (ThreeDimensionalObject renderableObject : batch) {
for (ThreeDimensionalObject obj : batch) {
poseStack.pushPose();
onPreRender(renderableObject);
onPreRender(obj);
bufferBuilder.begin(VertexFormat.Mode.TRIANGLES, DefaultVertexFormat.POSITION_COLOR_NORMAL);

SphereObject sphere = (SphereObject) renderableObject;
SphereObject sphere = (SphereObject) obj;

RenderSystem.setShader(GameRenderer::getPositionColorShader);
float alpha = renderableObject.opacity;
float red = (float) (renderableObject.color >> 16 & 255) / 255.0F;
float green = (float) (renderableObject.color >> 8 & 255) / 255.0F;
float blue = (float) (renderableObject.color & 255) / 255.0F;
float alpha = sphere.opacity;
float red = RenderUtil.getRed(sphere.color);
float green = RenderUtil.getRed(sphere.color);
float blue = RenderUtil.getRed(sphere.color);

poseStack.translate(-view.x + renderableObject.getX(), -view.y + renderableObject.getY(), -view.z + renderableObject.getZ());
RenderUtil.drawSphere(poseStack, bufferBuilder, 1f, 0f, 0f, 0f, red, green, blue, alpha, sphere.sectors, sphere.stacks);
poseStack.translate(-view.x, -view.y, -view.z);
RenderUtil.drawSphere(poseStack, bufferBuilder, sphere.radius, sphere.x, sphere.y, sphere.z, sphere.xRot, sphere.yRot, sphere.zRot, red, green, blue, alpha, sphere.sectors, sphere.stacks);
BufferUploader.drawWithShader(bufferBuilder.end());
onPostRender(renderableObject);
onPostRender(obj);

poseStack.popPose();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package de.srendi.advancedperipherals.client.smartglasses.objects.threedim;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexFormat;
import de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.TorusObject;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderLevelStageEvent;

import java.util.List;

public class TorusRenderer implements IThreeDObjectRenderer {

@Override
public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder) {
poseStack.pushPose();

for (ThreeDimensionalObject obj : batch) {
poseStack.pushPose();
onPreRender(obj);
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL);

TorusObject torus = (TorusObject) obj;

RenderSystem.setShader(GameRenderer::getPositionColorShader);
float alpha = torus.opacity;
float red = RenderUtil.getRed(torus.color);
float green = RenderUtil.getGreen(torus.color);
float blue = RenderUtil.getBlue(torus.color);

poseStack.translate(-view.x + torus.x, -view.y + torus.y, -view.z + torus.z);
RenderUtil.drawTorus(poseStack, bufferBuilder, torus.majorRadius, torus.minorRadius, 0, 0, 0, torus.xRot, torus.yRot, torus.zRot, red, green, blue, alpha, torus.rings, torus.sides);
BufferUploader.drawWithShader(bufferBuilder.end());
onPostRender(obj);

poseStack.popPose();
}

poseStack.popPose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Matrix4f;
import de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.CircleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RenderableObject;
import net.minecraft.client.renderer.GameRenderer;
Expand All @@ -19,14 +20,14 @@ public class CircleRenderer implements ITwoDObjectRenderer {

@Override
public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
for (RenderableObject object : objects) {
for (RenderableObject obj : objects) {

CircleObject circle = (CircleObject) object;
CircleObject circle = (CircleObject) obj;

float alpha = object.opacity;
float red = (float) (object.color >> 16 & 255) / 255.0F;
float green = (float) (object.color >> 8 & 255) / 255.0F;
float blue = (float) (object.color & 255) / 255.0F;
float alpha = circle.opacity;
float red = RenderUtil.getRed(circle.color);
float green = RenderUtil.getGreen(circle.color);
float blue = RenderUtil.getBlue(circle.color);

drawCircle(poseStack, circle.x, circle.y, circle.radius, 120, red, green, blue, alpha);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class ItemRenderer implements ITwoDObjectRenderer {
public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
Minecraft minecraft = Minecraft.getInstance();

for (RenderableObject object : objects) {
Item renderItem = RegistryUtil.getRegistryEntry(((ItemObject) object).item, ForgeRegistries.ITEMS);
for (RenderableObject obj : objects) {
Item renderItem = RegistryUtil.getRegistryEntry(((ItemObject) obj).item, ForgeRegistries.ITEMS);
if (renderItem == null)
continue;
minecraft.getItemRenderer().renderGuiItem(new ItemStack(renderItem), object.x, object.y);
minecraft.getItemRenderer().renderGuiItem(new ItemStack(renderItem), (int) obj.x, (int) obj.y);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Matrix4f;
import de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RenderableObject;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraftforge.client.gui.overlay.ForgeGui;
Expand All @@ -23,19 +24,18 @@ public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack
Matrix4f matrix = poseStack.last().pose();
bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);

for (RenderableObject object : objects) {
float alpha = object.opacity;
float red = (float) (object.color >> 16 & 255) / 255.0F;
float green = (float) (object.color >> 8 & 255) / 255.0F;
float blue = (float) (object.color & 255) / 255.0F;
for (RenderableObject obj : objects) {
float alpha = obj.opacity;
float red = RenderUtil.getRed(obj.color);
float green = RenderUtil.getGreen(obj.color);
float blue = RenderUtil.getBlue(obj.color);

bufferbuilder.vertex(matrix, (float) object.x, (float) object.maxY, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, (float) object.maxX, (float) object.maxY, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, (float) object.maxX, (float) object.y, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, (float) object.x, (float) object.y, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, obj.x, obj.maxY, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, obj.maxX, obj.maxY, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, obj.maxX, obj.y, 0f).color(red, green, blue, alpha).endVertex();
bufferbuilder.vertex(matrix, obj.x, obj.y, 0f).color(red, green, blue, alpha).endVertex();
}

BufferUploader.drawWithShader(bufferbuilder.end());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class TextRenderer implements ITwoDObjectRenderer {
public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {

Minecraft minecraft = Minecraft.getInstance();
for (RenderableObject object : objects) {
TextObject text = (TextObject) object;
for (RenderableObject obj : objects) {
TextObject text = (TextObject) obj;
poseStack.scale(text.fontSize, text.fontSize, 1);
if (text.shadow) {
minecraft.font.drawShadow(poseStack, text.content, text.x / text.fontSize, text.y / text.fontSize, text.color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BlockObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BoxObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.SphereObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.TorusObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.CircleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.ItemObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RectangleObject;
Expand Down Expand Up @@ -82,6 +83,14 @@ public final MethodResult createSphere(IArguments arguments) throws LuaException
return MethodResult.of(object, "SUCCESS");
}

@LuaFunction
public final MethodResult createTorus(IArguments arguments) throws LuaException {
TorusObject block = new TorusObject(overlayModule, arguments);
RenderableObject object = overlayModule.addObject(block);

return MethodResult.of(object, "SUCCESS");
}

@LuaFunction
public final MethodResult getObject(IArguments arguments) throws LuaException {
int id = arguments.getInt(0);
Expand Down
Loading

0 comments on commit 130d91c

Please sign in to comment.