Jump to content

[1.15] Rendering Blocks/QUADS (GlStateManager, tessellator, bufferBuilder) - Client Side


M1ntcraft3r

Recommended Posts

Hi all,

 

I was working on a custom mod in 1.14 which finds blocks (chests, ores etc.) around the player, adds them to an array and then renders those blocks based on distance to the player.

The mod was working and the ores/chests were colored based on the type of block and alpha was applied based on the distance to the player.

 

However, in 1.15, it no longer works.

The blocks are still found (and the coordinates are corect) but when they are being rendered, it seems to render them in a very odd place even though the coordinates for x, y, z are correct.

When I say an odd place, it's a little difficult to explain:

  • when moving the head of the player, the render remains static
  • when moving around, the render does move, but it does not correspond to the position of the ores/chest
  • sometimes I can have the chest sittin in front of me, but the render is behind me and I have to move back to see it (still it does not correstpond to the location of the chest

I know this is probably a long shot for someone to help me with so little info, maybe I could post screenshots if you think this would help?

 

In 1.14, I used the GlStateManager/Tessellator to render all surfaces as quads, creating a box around the blocks of interest.

I am not actually sure if this is the best way of doing such renders or if there is a much easier way. I spent a lot of time looking online to find ways to render blocks (or surfaces) and this was the only way I found that actually worked reliably.

So please, if you know of a better, more efficient way, please share your thoughts here. I am very new to modding though and I would massively appreciate an example to render a block at 0, 65, 0.

 

I would like to point out that this is not something I am ever planning on releasing, but rather a mod I am using to learm Forge modding...

I started with blocks in the vasinity of the player (based on coordinates) to start off but wanted to get into trying to get this info from loaded chunks etc., however, since this no longer works properly in 1.15, I'm basically stuck where I was when I started the mod.

 

Below is the code I am using in 1.15 and the only changes to 1.14 I had to make was the getPosX(), getPosY() and getPosZ(), previously this was posX(), posY(), posZ().

 

package com.example.examplemod;

import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import org.lwjgl.opengl.GL11;

import java.util.List;

import static java.lang.Math.sqrt;
import static net.minecraft.block.Blocks.*;
import static net.minecraft.block.Blocks.CHEST;
import static net.minecraft.world.biome.Biome.LOGGER;

public class BlockRenderedBox {

    public static void renderBlockOutline(float partialTicks) {
        ClientPlayerEntity playerEntity = Minecraft.getInstance().player;
        double x = playerEntity.lastTickPosX + (playerEntity.getPosX() - playerEntity.lastTickPosX) * partialTicks;
        double y = playerEntity.lastTickPosY + (playerEntity.getPosY() - playerEntity.lastTickPosY) * partialTicks;
        double z = playerEntity.lastTickPosZ + (playerEntity.getPosZ() - playerEntity.lastTickPosZ) * partialTicks;


        if (ExampleMod.RedstoneOres.size() > 0) {
            BlockRenderedBox.render(x,y,z,ExampleMod.RedstoneOres, playerEntity);
        }
        if (ExampleMod.DiamondOres.size() > 0) {
            BlockRenderedBox.render(x,y,z,ExampleMod.DiamondOres, playerEntity);
        }
        if (ExampleMod.GoldOres.size() > 0) {
            BlockRenderedBox.render(x,y,z,ExampleMod.GoldOres, playerEntity);
        }
        if (ExampleMod.Chests.size() > 0) {
            BlockRenderedBox.render(x,y,z,ExampleMod.Chests, playerEntity);
        }
        if (ExampleMod.Heads.size() > 0) {
            BlockRenderedBox.render(x,y,z,ExampleMod.Heads, playerEntity);
        }
        if (ExampleMod.ClayBlocks.size() > 0) {
            BlockRenderedBox.render(x,y,z,ExampleMod.ClayBlocks, playerEntity);
        }
        if (ExampleMod.toRender.size() > 0) {
            BlockRenderedBox.render(x,y,z,ExampleMod.toRender, playerEntity);
        }

    }




    private static void render(double x, double y, double z, List<BlockPos> ore, ClientPlayerEntity player){
        for (int i = 0; i < ore.size(); i++) {
            GlStateManager.pushMatrix();

            Vec3d projectedView = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();
            GlStateManager.translated(-projectedView.x, -projectedView.y, -projectedView.z);

            GlStateManager.enableBlend();
            GlStateManager.disableCull();
            GlStateManager.disableDepthTest();
            GlStateManager.disableTexture();
            GlStateManager.disableLighting();
            GlStateManager.disableAlphaTest();

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

            bufferBuilder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);

            Block block = player.world.getBlockState(ore.get(i)).getBlock();

            double distance_to_ore = sqrt(ore.get(i).distanceSq(player.getPosX(), player.getPosY(), player.getPosZ(), true));
            float alpha = 0.1f;
            if(distance_to_ore >= 30){
                GlStateManager.lineWidth(1);
                alpha = 1f;
            }
            if(distance_to_ore < 30 && distance_to_ore > 20){
                GlStateManager.lineWidth(5);
                alpha = 0.3f;
            }
            if(distance_to_ore > 10 && distance_to_ore < 20){
                GlStateManager.lineWidth(10);
                alpha = 0.2f;
            }
            if(distance_to_ore <= 10){
                GlStateManager.lineWidth(15);
                alpha = 0.1f;
            }

            if(block == REDSTONE_ORE){
                GL11.glColor4f(1f,0f,0f, alpha);
            }
            if(block == GOLD_ORE){
                GL11.glColor4f(0.95f,1f,0f,alpha);
            }
            if(block == DIAMOND_ORE){
                GL11.glColor4f(0f,0.968f,0.968f,alpha);
            }
            if(block == CHEST){
                GL11.glColor4f(0f,0.968f,0.968f,alpha);
            }
            if(block == PLAYER_HEAD){
                GL11.glColor4f(0f,1f,0f,alpha);
            }
            if(block == CLAY){
                GL11.glColor4f(0f,1f,0f,alpha);
            }

            // bottom
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY(), ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY(), ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY(), ore.get(i).getZ() + 1).endVertex();
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY(), ore.get(i).getZ() + 1).endVertex();

            // top
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY()+ 1, ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY()+ 1, ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY()+ 1, ore.get(i).getZ() + 1).endVertex();
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY()+ 1, ore.get(i).getZ() + 1).endVertex();

            // left
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY(), ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY() + 1, ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY() + 1, ore.get(i).getZ() + 1).endVertex();
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY(), ore.get(i).getZ() + 1).endVertex();

            // right
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY(), ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY() + 1, ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY() + 1, ore.get(i).getZ() + 1).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY(), ore.get(i).getZ() + 1).endVertex();

            // front
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY(), ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY(), ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY() + 1, ore.get(i).getZ()).endVertex();
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY() + 1, ore.get(i).getZ()).endVertex();

            // back
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY(), ore.get(i).getZ() + 1).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY(), ore.get(i).getZ() + 1).endVertex();
            bufferBuilder.pos(ore.get(i).getX() + 1, ore.get(i).getY() + 1, ore.get(i).getZ() + 1).endVertex();
            bufferBuilder.pos(ore.get(i).getX(), ore.get(i).getY() + 1, ore.get(i).getZ() + 1).endVertex();


            tessellator.draw();
            GlStateManager.enableCull();
            GlStateManager.enableDepthTest();
            GlStateManager.enableTexture();
            GlStateManager.enableLighting();
            GlStateManager.disableBlend();
            GlStateManager.enableAlphaTest();
            GlStateManager.popMatrix();
        }
    }


}

 

Any help is much appreciated!

 

Thanks in advance

 

Tim

Edited by M1ntcraft3r
Link to comment
Share on other sites

I actually just solved this problem with a very similar mod to yours here.

 

Shortened Version

If you are doing this from RenderWorldLastEvent, it no longer maintains the player's head rotation. To fix this, you need to rotate the matrix so it is correct.

 

ActiveRenderInfo renderInfo = Minecraft.getInstance().gameRenderer.getActiveRenderInfo();
Vec3d projectedView = renderInfo.getProjectedView();
GlStateManager.rotatef(renderInfo.getPitch(), 1, 0, 0); // Fixes camera rotation.
GlStateManager.rotatef(renderInfo.getYaw() + 180, 0, 1, 0); // Fixes camera rotation.
GlStateManager.translated(-projectedView.x, -projectedView.y, -projectedView.z);

 

Edited by xChris6041x
Link to comment
Share on other sites

32 minutes ago, xChris6041x said:

I actually just solved this problem with a very similar mod to yours here.

 

Shortened Version

If you are doing this from RenderWorldLastEvent, it no longer maintains the player's head rotation. To fix this, you need to rotate the matrix so it is correct.

 


ActiveRenderInfo renderInfo = Minecraft.getInstance().gameRenderer.getActiveRenderInfo();
Vec3d projectedView = renderInfo.getProjectedView();
GlStateManager.rotatef(renderInfo.getPitch(), 1, 0, 0); // Fixes camera rotation.
GlStateManager.rotatef(renderInfo.getYaw() + 180, 0, 1, 0); // Fixes camera rotation.
GlStateManager.translated(-projectedView.x, -projectedView.y, -projectedView.z);

 

Amazing!

I had a quick check and it seems to track it again!

 

Thanks so much for your reply mate!

Link to comment
Share on other sites

52 minutes ago, xChris6041x said:

I actually just solved this problem with a very similar mod to yours here.

 

Shortened Version

If you are doing this from RenderWorldLastEvent, it no longer maintains the player's head rotation. To fix this, you need to rotate the matrix so it is correct.

 


ActiveRenderInfo renderInfo = Minecraft.getInstance().gameRenderer.getActiveRenderInfo();
Vec3d projectedView = renderInfo.getProjectedView();
GlStateManager.rotatef(renderInfo.getPitch(), 1, 0, 0); // Fixes camera rotation.
GlStateManager.rotatef(renderInfo.getYaw() + 180, 0, 1, 0); // Fixes camera rotation.
GlStateManager.translated(-projectedView.x, -projectedView.y, -projectedView.z);

 

Interestingly it renders even over the players hand but I think I read something about that cuz its the RenderWorldLastEvent

Link to comment
Share on other sites

1 hour ago, xChris6041x said:

That is interesting, it doesn't do that on my mod. I tweaked mine to try and find the cause, but no luck.

Thanks for checking..

I'll have to search the web again cuz I know someone had a similar issue and if I recall correctly, there was a solution.

Hopefully I'll find it.

 

It is odd thought that you're not seeing it.

Anyway I'll post an update over the next few days!

 

Thanks again for your help Chris

Link to comment
Share on other sites

On 2/23/2020 at 4:51 AM, TheGreyGhost said:

Howdy

I haven't tried solving this problem myself, yet, but my research has found a relevant clue perhaps-?

Forge 1.15 update

 

You might need to make use of the event.getMatrix().

 

-TGG

Thanks Ghost!

This actually looks like what I need and also what Chris seemed to have tried in his thread (to some extend).

 

Unfortunately I have not been able to get it to work but it looks like the IRenderTypeBuffer is the new way to go and thats how it should be done, rather than the Tessellator, BufferBuilder and the GlStateManager!

 

If someone has a basic method which plots a few quads at a given location, that would be great.

 

I spend a few hours at the weekend trying to get this to work but unforunately I got nowhere. I also don't want to paste my old code which plotts all the quads but still doesn't have the players hand ocrrected becasue this seems to be the outdated way to do it anyway.

 

Thanks in advance

Link to comment
Share on other sites

On 2/22/2020 at 8:32 PM, xChris6041x said:

That is interesting, it doesn't do that on my mod. I tweaked mine to try and find the cause, but no luck.

So Chris, I think I have sort of worked it out... If I draw a single quad, the player hand reamains rendered but if I loop over an array, it disappears.

The way I am doing it is:

 

        ActiveRenderInfo renderInfo = Minecraft.getInstance().gameRenderer.getActiveRenderInfo();
        Vec3d projectedView = renderInfo.getProjectedView();

        GlStateManager.rotatef(renderInfo.getPitch(), 1, 0, 0); // Fixes camera rotation.
        GlStateManager.rotatef(renderInfo.getYaw() + 180, 0, 1, 0); // Fixes camera rotation.
        GlStateManager.translated(-projectedView.x, -projectedView.y, -projectedView.z);

 

then for each box, I am calling:

            GlStateManager.pushMatrix();
            GlStateManager.enableBlend();
            GlStateManager.disableCull();
            GlStateManager.disableDepthTest();
            GlStateManager.disableTexture();
            GlStateManager.disableLighting();
            GlStateManager.disableAlphaTest();
            Tessellator tessellator = Tessellator.getInstance();
            BufferBuilder bufferBuilder = tessellator.getBuffer();

            bufferBuilder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);
            Block block = player.world.getBlockState(oresBlockPosList.get(oreBlockPos)).getBlock();


            // bottom
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();

            // top
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY()+ 1, oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY()+ 1, oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY()+ 1, oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY()+ 1, oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();

            // left
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY() + 1, oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY() + 1, oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();

            // right
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY() + 1, oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY() + 1, oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();

            // front
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY() + 1, oresBlockPosList.get(oreBlockPos).getZ()).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY() + 1, oresBlockPosList.get(oreBlockPos).getZ()).endVertex();

            // back
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY(), oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX() + 1, oresBlockPosList.get(oreBlockPos).getY() + 1, oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();
            bufferBuilder.pos(oresBlockPosList.get(oreBlockPos).getX(), oresBlockPosList.get(oreBlockPos).getY() + 1, oresBlockPosList.get(oreBlockPos).getZ() + 1).endVertex();

            // Tessellator has to come before the rest or some of the GlStateManager stuff
            tessellator.draw();
            GlStateManager.enableCull();
            GlStateManager.enableDepthTest();
            GlStateManager.enableTexture();
            GlStateManager.enableLighting();
            GlStateManager.disableBlend();
            GlStateManager.enableAlphaTest();
            GlStateManager.popMatrix();

 

Basically I am not sure if I need to 

1) Push and Pop the matrix for each bufferBuilder?

2) Draw tessellator every time

3) Create a new BufferBuilder for each block (6 quads)

 

Also, if you worked out how to use the RenderSystem and the matrixStack, please share it with me, it'd be much appreachiated.

 

Thanks

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.