Jump to content

GL11 Drawing cube in wrong location


AntiRix

Recommended Posts

Hi,

 

I'm trying to draw a cube in the world. It's being rendered, but in the wrong position. When the player moves, so does the cube, even if the translation part of the following code is commented out. There must be an offset I'm missing to get the positioning correct.

 

@SubscribeEvent
	public void onRenderWorldLast(RenderWorldLastEvent event)
	{
	    double x_fix = mc.player.lastTickPosX + (mc.player.posX - mc.player.lastTickPosX) * event.getPartialTicks();
	    double y_fix = mc.player.lastTickPosY + (mc.player.posY - mc.player.lastTickPosY) * event.getPartialTicks();
	    double z_fix = mc.player.lastTickPosZ + (mc.player.posZ - mc.player.lastTickPosZ) * event.getPartialTicks();
	    GL11.glPushMatrix();
	    
	    GL11.glTranslated(-x_fix, -y_fix, -z_fix);
	    GL11.glDisable(GL11.GL_DEPTH_TEST);
	    GL11.glDisable(GL11.GL_LIGHTING);
	    GL11.glEnable(GL11.GL_BLEND);
	    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
	    GL11.glDisable(GL11.GL_TEXTURE_2D);
	    
	    for (BlockPos pos : blocks)
	    {
	    	int x = pos.getX();
	    	int y = pos.getY();
	    	int z = pos.getZ();
	    	
	    	GL11.glColor4f(1.0F, 0.0F, 0.0F, 0.5F);
	    	box(new AxisAlignedBB(x, y, z, x + 1, y + 1, z + 1));
	    	//GL11.glColor4f(1.0F, 1.0F, 1.0F, 0.75F);
	    	//lines(new AxisAlignedBB(x, z, y, x + 1, z + 1, y + 1));
	    	GL11.glColor4f(1F, 1F, 1F, 1F);
	    }
	    
	    GL11.glEnable(GL11.GL_TEXTURE_2D);
	    GL11.glDisable(GL11.GL_BLEND);
	    GL11.glEnable(GL11.GL_LIGHTING);
	    GL11.glEnable(GL11.GL_DEPTH_TEST);
	    GL11.glPopMatrix();
	}
	
	public void box(AxisAlignedBB aabb)
	{
        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder worldrenderer = tessellator.getBuffer();

        // top
        worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);
        worldrenderer.pos(aabb.maxX, aabb.maxY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.maxY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.maxY, aabb.maxZ).endVertex();
        worldrenderer.pos(aabb.maxX, aabb.maxY, aabb.maxZ).endVertex();
        tessellator.draw();

        // bottom
        worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);
        worldrenderer.pos(aabb.maxX, aabb.minY, aabb.maxZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.minY, aabb.maxZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.minY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.maxX, aabb.minY, aabb.minZ).endVertex();
        tessellator.draw();

        // north
        worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);
        worldrenderer.pos(aabb.maxX, aabb.minY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.minY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.maxY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.maxX, aabb.maxY, aabb.minZ).endVertex();
        tessellator.draw();
        
        // south
        worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);
        worldrenderer.pos(aabb.maxX, aabb.maxY, aabb.maxZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.maxY, aabb.maxZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.minY, aabb.maxZ).endVertex();
        worldrenderer.pos(aabb.maxX, aabb.minY, aabb.maxZ).endVertex();
        tessellator.draw();
        
        // west
        worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);
        worldrenderer.pos(aabb.minX, aabb.maxY, aabb.maxZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.maxY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.minY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.minX, aabb.minY, aabb.maxZ).endVertex();
        tessellator.draw();
        
        // east
        worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);
        worldrenderer.pos(aabb.maxX, aabb.minY, aabb.maxZ).endVertex();
        worldrenderer.pos(aabb.maxX, aabb.minY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.maxX, aabb.maxY, aabb.minZ).endVertex();
        worldrenderer.pos(aabb.maxX, aabb.maxY, aabb.maxZ).endVertex();
        tessellator.draw();
    }

 

Edited by AntiRix
Link to comment
Share on other sites

33 minutes ago, AntiRix said:

There must be an offset I'm missing to get the positioning correct.

You have to offset based on the players position because that's where 0,0,0 is considered to be.

 

OP is already doing it.

Edited by Animefan8888
Can't see what is blatently in front of myself.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

2 minutes ago, AntiRix said:

Isn't that already what the code is doing?

Also don't use GL calls directly switch it all to the GLStateManager variant.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

25 minutes ago, AntiRix said:

GL11

Don't use GL11 directly, use GlStateManager otherwise you are screwing up minecraft's state machine.

 

26 minutes ago, AntiRix said:

double x_fix = mc.player.lastTickPosX + (mc.player.posX - mc.player.lastTickPosX) * event.getPartialTicks();

Well, what if the player isn't the camera? What if the player spectates as a zombie? Use Minecraft#getRenderViewEntity instead.

 

28 minutes ago, AntiRix said:

public void box(AxisAlignedBB aabb)

Any reason you are rendering your box in 6 passes? Do it in one. Draw calls are expensive.

 

6 minutes ago, Animefan8888 said:

You have to offset based on the players position because that's where 0,0,0 is considered to be.

That's what the OP is doing:

29 minutes ago, AntiRix said:

double x_fix = mc.player.lastTickPosX + (mc.player.posX - mc.player.lastTickPosX) * event.getPartialTicks();

 double y_fix = mc.player.lastTickPosY + (mc.player.posY - mc.player.lastTickPosY) * event.getPartialTicks();

double z_fix = mc.player.lastTickPosZ + (mc.player.posZ - mc.player.lastTickPosZ) * event.getPartialTicks(); GL11.glPushMatrix();

GL11.glTranslated(-x_fix, -y_fix, -z_fix);

If you translate by -camerapos then you get the world's 0,0,0 origin.

 

I am actually unable to replicate the issue with the code provided, the cube renders in the correct spot for me...

Link to comment
Share on other sites

Ah, a silly mistake with how I was storing the block positions to be rendered.

 

BlockPos.getAllInBoxMutable(centre.add(-radius, -radius, -radius), centre.add(radius, radius, radius)).forEach(pos ->
        {
        	Block block = world.getBlockState(pos).getBlock();
        	String material = block.getLocalizedName();
        	
        	if (!material.startsWith("tile.skull")) return;
        	
        	//TileEntitySkull skull = (TileEntitySkull)world.getTileEntity(pos);
        	blocks.add(pos);
        	mc.player.sendMessage(new TextComponentString(pos.toString()));
        });

 

Changing the method to getAllInBox fixed it. I have no clue why because the output in chat was the same for both. That means the position in the list was being altered by the time the cube was rendered.

 

After a bit of trial and error, I've also managed to make that other code efficient and it's working nicely now

Edited by AntiRix
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.



×
×
  • Create New...

Important Information

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