Jump to content

[1.16.3] Rendering a selection box


MemeMan

Recommended Posts

I've been trying to render a selection box using an AxisAlignedBB by using the method in WorldRenderer as an example. The first problem I have is that lines that are behind blocks are being rendered in front of the blocks like in the image. The second problem is that the selection block seems to disappear when I break a block or sprint. Third problem is that as far as I can tell, the box should have red lines but they are still black. These are the methods that I'm using: 

 

// In Event Handler
    @SubscribeEvent
    public static void onRenderWorldEvent(RenderWorldLastEvent e) {
        final AxisAlignedBB test = new AxisAlignedBB(0,0,0,1,1,1);
        Renderer.drawBoundingBoxAtBlockPos(e.getMatrixStack(), test, 1.0F, 0.0F, 0.0F, 1.0F, new BlockPos(0,64,0));
    }

// In Renderer Class
    public static void drawBoundingBoxAtBlockPos(MatrixStack matrixStackIn, AxisAlignedBB aabbIn, float red, float green, float blue, float alpha, BlockPos pos) {
        Vector3d cam = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();
        double camX = cam.x, camY = cam.y, camZ = cam.z;

        matrixStackIn.push();
        RenderSystem.lineWidth(2.0F);
        matrixStackIn.translate(pos.getX() - camX, pos.getY() - camY, pos.getZ() - camZ);
        drawBoundingBox(matrixStackIn, aabbIn.minX, aabbIn.minY, aabbIn.minZ, aabbIn.maxX, aabbIn.maxY, aabbIn.maxZ, red, green, blue, alpha, red, green, blue);
        matrixStackIn.pop();
    }

    private static void drawBoundingBox(MatrixStack matrixStackIn, double minX, double minY, double minZ, double maxX, double maxY, double maxZ, float red, float green, float blue, float alpha, float red2, float green2, float blue2) {
        Matrix4f matrix4f = matrixStackIn.getLast().getMatrix();
        float f = (float)minX;
        float f1 = (float)minY;
        float f2 = (float)minZ;
        float f3 = (float)maxX;
        float f4 = (float)maxY;
        float f5 = (float)maxZ;

        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder bufferIn = tessellator.getBuffer();

        bufferIn.begin(1, DefaultVertexFormats.POSITION_COLOR);
        bufferIn.pos(matrix4f, f, f1, f2).color(red, green2, blue2, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f1, f2).color(red, green2, blue2, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f1, f2).color(red2, green, blue2, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f4, f2).color(red2, green, blue2, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f1, f2).color(red2, green2, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f1, f5).color(red2, green2, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f1, f2).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f4, f2).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f4, f2).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f4, f2).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f4, f2).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f4, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f4, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f1, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f1, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f1, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f1, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f1, f2).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f, f4, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f4, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f1, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f4, f5).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f4, f2).color(red, green, blue, alpha).endVertex();
        bufferIn.pos(matrix4f, f3, f4, f5).color(red, green, blue, alpha).endVertex();
        Tessellator.getInstance().draw();
    }

 

spacer.png

 

Any help would be very appreciated.

Edited by MemeMan
Link to comment
Share on other sites

How can I apply the code you posted to using it within the RenderWorldLast event as in the DrawBlockHighlight, you get the RenderTypeBuffer and the ActiveRenderInfo for the ProjectedView for the offsets. The problem I'm currently having is that when rendering with RenderWorldLast, sprinting or going into third person displaces the box.

Edited by MemeMan
Link to comment
Share on other sites

Ah.  It sounds like your projection matrix is being messed up.

 

I had a look at the vanilla code and found these lines

      this.mc.getProfiler().endStartSection("forge_render_last");
      net.minecraftforge.client.ForgeHooksClient.dispatchRenderLast(this.mc.worldRenderer, matrixStackIn, partialTicks, matrix4f, finishTimeNano);
      this.mc.getProfiler().endStartSection("hand");
      if (this.renderHand) {
         RenderSystem.clear(256, Minecraft.IS_RUNNING_ON_MAC);
         this.renderHand(matrixStackIn, activerenderinfo, partialTicks);
      }

   private void renderHand(MatrixStack matrixStackIn, ActiveRenderInfo activeRenderInfoIn, float partialTicks) {
      if (!this.debugView) {
         this.resetProjectionMatrix(this.getProjectionMatrix(activeRenderInfoIn, partialTicks, false));

which makes me think that you might need to add this to the start of your  RenderWorldLast event:

 

this.resetProjectionMatrix(this.getProjectionMatrix(activeRenderInfoIn, partialTicks, false));

 

Disclaimer: I haven't tried it yet myself so no guarantee it will work.

 

-TGG

Link to comment
Share on other sites

Are there any problems you can see with this: 

// In Event Handler
	@SubscribeEvent
    public static void onRenderWorldEvent(RenderWorldLastEvent e) {
        final GameRenderer gameRenderer = Minecraft.getInstance().gameRenderer;
        gameRenderer.resetProjectionMatrix(e.getProjectionMatrix());

        final AxisAlignedBB test = new AxisAlignedBB(0,0,0,1,1,1);
        Renderer.drawBoundingBoxAtBlockPos(e.getMatrixStack(), test, 1.0F, 0.0F, 0.0F, 1.0F, new BlockPos(0,64,0));
    }

// In Renderer
    public static void drawBoundingBoxAtBlockPos(MatrixStack matrixStackIn, AxisAlignedBB aabbIn, float red, float green, float blue, float alpha, BlockPos pos) {
        Vector3d cam = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();

        double camX = cam.getX(), camY = cam.getY(), camZ = cam.getZ();

        matrixStackIn.push();

        GL11.glDisable(GL11.GL_DEPTH_TEST);
        drawShapeOutline(matrixStackIn, VoxelShapes.create(aabbIn), pos.getX() - camX, pos.getY() - camY, pos.getZ() - camZ, red, green, blue, alpha);
        GL11.glEnable(GL11.GL_DEPTH_TEST);

        matrixStackIn.pop();
    }

    private static void drawShapeOutline(MatrixStack matrixStack, VoxelShape voxelShape, double originX, double originY, double originZ, float red, float green, float blue, float alpha) {
        Matrix4f matrix4f = matrixStack.getLast().getMatrix();

        IRenderTypeBuffer.Impl renderTypeBuffer = Minecraft.getInstance().getRenderTypeBuffers().getBufferSource();
        IVertexBuilder bufferIn = renderTypeBuffer.getBuffer(RenderType.getLines());

        voxelShape.forEachEdge((x0, y0, z0, x1, y1, z1) -> {
            bufferIn.pos(matrix4f, (float) (x0 + originX), (float) (y0 + originY), (float) (z0 + originZ)).color(red, green, blue, alpha).endVertex();
            bufferIn.pos(matrix4f, (float) (x1 + originX), (float) (y1 + originY), (float) (z1 + originZ)).color(red, green, blue, alpha).endVertex();
        });

        renderTypeBuffer.finish(RenderType.getLines());
    }

As far as I can tell it renders properly however I tried using some of the RenderSystem methods like depth and they didn't work.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.