Skip to content

Commit

Permalink
[#515] Implemented another rendering object, a circle
Browse files Browse the repository at this point in the history
Implemented a system to register rendering object types for network sync and rendering on the client
  • Loading branch information
SirEndii committed Aug 4, 2024
1 parent 5148b85 commit 558cc96
Show file tree
Hide file tree
Showing 15 changed files with 369 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.srendi.advancedperipherals.client.screens.SaddleTurtleScreen;
import de.srendi.advancedperipherals.client.screens.SmartGlassesScreen;
import de.srendi.advancedperipherals.client.smartglasses.OverlayModuleOverlay;
import de.srendi.advancedperipherals.client.smartglasses.OverlayObjectHolder;
import de.srendi.advancedperipherals.common.setup.APBlockEntityTypes;
import de.srendi.advancedperipherals.common.setup.APContainerTypes;
import de.srendi.advancedperipherals.common.setup.CCRegistration;
Expand Down Expand Up @@ -58,6 +59,7 @@ public static void onClientSetup(FMLClientSetupEvent event) {
ComputerCraftAPIClient.registerTurtleUpgradeModeller(CCRegistration.WEAK_TURTLE.get(), new MetaTurtleUpgradeModeller<>());

ItemPropertiesRegistry.register();
OverlayObjectHolder.registerDecodeObjects();
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Matrix4f;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.RenderableObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraftforge.client.gui.overlay.ForgeGui;
import net.minecraftforge.client.gui.overlay.IGuiOverlay;
Expand All @@ -20,22 +20,8 @@ public class OverlayModuleOverlay implements IGuiOverlay {
public void render(ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
poseStack.pushPose();

// Just a simple quads renderer to test the syncing to the client, will evolve while we implement more renderable objects
for (RenderableObject object : OverlayObjectHolder.getObjects()) {
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;

RenderSystem.setShader(GameRenderer::getPositionColorShader);
BufferBuilder bufferbuilder = Tesselator.getInstance().getBuilder();
Matrix4f matrix = poseStack.last().pose();
bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_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();
BufferUploader.drawWithShader(bufferbuilder.end());
object.getRenderObject().render(object, gui, poseStack, partialTick, screenWidth, screenHeight);
}
poseStack.popPose();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package de.srendi.advancedperipherals.client.smartglasses;

import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.RenderableObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.ObjectDecodeRegistry;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Circle;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Panel;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -31,4 +34,9 @@ public static void removeObject(String id) {
public static void clear() {
objects.clear();
}

public static void registerDecodeObjects() {
ObjectDecodeRegistry.register(Panel.ID, Panel::decode);
ObjectDecodeRegistry.register(Circle.ID, Circle::decode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.srendi.advancedperipherals.client.smartglasses.objects;

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.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Matrix4f;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Circle;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraftforge.client.gui.overlay.ForgeGui;

public class CircleRenderer implements IObjectRenderer {

@Override
public void render(RenderableObject object, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
Circle circle = (Circle) object;

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;

drawCircle(poseStack, circle.x, circle.y, circle.radius, 120, red, green, blue, alpha);
}

public void drawCircle(PoseStack poseStack, float cx, float cy, float r, int numSegments, float red, float green, float blue, float alpha) {
RenderSystem.setShader(GameRenderer::getPositionColorShader);
BufferBuilder bufferbuilder = Tesselator.getInstance().getBuilder();

Matrix4f matrix = poseStack.last().pose();
bufferbuilder.begin(VertexFormat.Mode.TRIANGLE_FAN, DefaultVertexFormat.POSITION_COLOR);

bufferbuilder.vertex(matrix, cx, cy, 1f).color(red, green, blue, alpha).endVertex();

double angle = Math.PI * 2 / numSegments;

for (int i = 0; i <= numSegments; i++) {
double x = cx + r * Math.cos(i * angle);
double y = cy + r * Math.sin(i * angle);

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

BufferUploader.drawWithShader(bufferbuilder.end());
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.srendi.advancedperipherals.client.smartglasses.objects;

import com.mojang.blaze3d.vertex.PoseStack;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraftforge.client.gui.overlay.ForgeGui;

public interface IObjectRenderer {

void render(RenderableObject object, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.srendi.advancedperipherals.client.smartglasses.objects;

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.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Matrix4f;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraftforge.client.gui.overlay.ForgeGui;

public class PanelRenderer implements IObjectRenderer {

@Override
public void render(RenderableObject object, ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
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;

RenderSystem.setShader(GameRenderer::getPositionColorShader);
BufferBuilder bufferbuilder = Tesselator.getInstance().getBuilder();
Matrix4f matrix = poseStack.last().pose();
bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_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();
BufferUploader.drawWithShader(bufferbuilder.end());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import de.srendi.advancedperipherals.client.smartglasses.OverlayObjectHolder;
import de.srendi.advancedperipherals.common.network.base.IPacket;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.RenderableObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.ObjectDecodeRegistry;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;

Expand All @@ -25,6 +26,8 @@ public void encode(FriendlyByteBuf buffer) {
}

public static RenderableObjectSyncPacket decode(FriendlyByteBuf buffer) {
return new RenderableObjectSyncPacket(RenderableObject.decode(buffer));
int id = buffer.readInt();
RenderableObject decodedObject = ObjectDecodeRegistry.getObject(id, buffer);
return new RenderableObjectSyncPacket(decodedObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.srendi.advancedperipherals.common.smartglasses.modules.overlay;

import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import net.minecraft.network.FriendlyByteBuf;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class ObjectDecodeRegistry {

private static final Map<Integer, Function<FriendlyByteBuf, RenderableObject>> objectRegistry = new HashMap<>();

public static RenderableObject getObject(int id, FriendlyByteBuf buf) {
if (objectRegistry.containsKey(id)) {
return objectRegistry.get(id).apply(buf);
}
return null;
}

public static void register(int id, Function<FriendlyByteBuf, RenderableObject> function) {
objectRegistry.put(id, function);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess;
import de.srendi.advancedperipherals.common.smartglasses.modules.IModule;
import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleFunctions;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Circle;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.Panel;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import de.srendi.advancedperipherals.common.util.Pair;
import net.minecraft.client.Minecraft;

public class OverlayGlassesFunctions implements IModuleFunctions {

Expand All @@ -34,6 +38,16 @@ public final MethodResult createPanel(String id, IArguments arguments) throws Lu
return MethodResult.of(success.getLeft(), "SUCCESS");
}

@LuaFunction
public final MethodResult createCircle(String id, IArguments arguments) throws LuaException {
Circle circle = new Circle(id, overlayModule, arguments);
Pair<RenderableObject, Boolean> success = overlayModule.addObject(circle);
if(!success.getRight())
return MethodResult.of(success.getLeft(), IModule.ErrorConstants.ALREADY_EXISTS);

return MethodResult.of(success.getLeft(), "SUCCESS");
}

@LuaFunction
public final MethodResult getObjects(IArguments arguments) {
return MethodResult.of((Object) overlayModule.getObjects().stream().map(OverlayObject::getId).toArray(String[]::new));
Expand All @@ -49,4 +63,10 @@ public final MethodResult clear() {
return MethodResult.of(overlayModule.clear());
}

// TODO: This will crash on dedicated servers
@LuaFunction
public final MethodResult getSize() {
return MethodResult.of(Minecraft.getInstance().getWindow().getWidth(), Minecraft.getInstance().getWindow().getHeight());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess;
import de.srendi.advancedperipherals.common.smartglasses.modules.IModule;
import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleFunctions;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.RenderableObject;
import de.srendi.advancedperipherals.common.util.Pair;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.network.NetworkEvent;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.logging.log4j.Level;

Expand All @@ -20,7 +19,6 @@
import java.util.UUID;
import java.util.stream.Collectors;


public abstract class OverlayObject {

@BooleanProperty
Expand Down Expand Up @@ -175,10 +173,6 @@ public Object castValueToFieldType(Field field, Object value) {
return value;
}

protected void handle(NetworkEvent.Context context) {

}

public void encode(FriendlyByteBuf buffer) {
buffer.writeUtf(id);
Entity entity = module.getAccess().getEntity();
Expand Down

This file was deleted.

Loading

0 comments on commit 558cc96

Please sign in to comment.