TheTrollguy_ Posted May 2, 2017 Posted May 2, 2017 Well hello there I'm back with rendering-related questions. My main goal is to render my block that I'm about to place in the position it would be placed if I pressed right-click. Here's an example: say I'm looking in the distance, no block outlines are rendered. But, if I'm looking at e.g. dirt (which is near me), I would see it's bounding box (the black outline). If I were to place my block, it would be placed in front of that dirt block. What I want to do is to render my own block in that place (with a certain level of transparency), without actually placing it. I hope I explained it well enough for you to understand. Quote
V0idWa1k3r Posted May 2, 2017 Posted May 2, 2017 You will need to use one of forge's render events, like RenderWorldLastEvent. You can get the block the player is looking at with the World::rayTraceBlocks method. I believe it's HitResult returns you the BlockPos of the block the player is looking at with EnumFacing of the side the player is looking at. Your rendering position would then be the raytraceresult position offset by that facing. To render a blockstate in the world you can look at RenderFallingBlock class. There might be some issues with transparency but in theory you can just enable blend with appropriate source and destination factors and either use gl's color method or manually override vertex color data in the vertexbuffer you are rendering your block with using VertexBuffer::putColor. Just do not forget to properly offset your rendering, or you will be the third person in a row with the rendered object 'following' the player/not rendering 1 Quote
Draco18s Posted May 2, 2017 Posted May 2, 2017 DrawBlockHighlightEvent would be more appropriate. 2 Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
V0idWa1k3r Posted May 2, 2017 Posted May 2, 2017 Yes, you are right, I misunderstood the question a little bit 1 Quote
TheTrollguy_ Posted May 4, 2017 Author Posted May 4, 2017 I did everything you said, but the only thing I get is the black bounding box that you look at don't get rendered, instead, gray lines that form triangles on the block's face renders. The block itself is never shown. @SubscribeEvent public void highlightGhostBlock(DrawBlockHighlightEvent event) { EntityPlayer player = event.getPlayer(); BlockPos position = event.getTarget().getBlockPos().offset(player.getHorizontalFacing()); ItemStack stack = player.getHeldItemMainhand(); if (stack.getItem() instanceof BSMItemBlock) { IBlockState stateToRender = ((BSMItemBlock)stack.getItem()).getDetails(player.world, position, player, 0, 0, 0, event.getTarget().sideHit).getSecondElement(); Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE); GlStateManager.pushMatrix(); GlStateManager.disableLighting(); GlStateManager.enableAlpha(); //GlStateManager.alphaFunc(0, 0); Tessellator tessellator = Tessellator.getInstance(); VertexBuffer vertexbuffer = tessellator.getBuffer(); position.add(0.5, 0, 0.5); vertexbuffer.begin(7, DefaultVertexFormats.BLOCK); BlockPos blockpos = new BlockPos(position.getX(), position.getY(), position.getZ()); GlStateManager.translate(position.getX(), position.getY(), position.getZ()); BlockRendererDispatcher blockrendererdispatcher = Minecraft.getMinecraft().getBlockRendererDispatcher(); blockrendererdispatcher.getBlockModelRenderer().renderModel(event.getPlayer().world, blockrendererdispatcher.getModelForState(stateToRender), stateToRender, blockpos, vertexbuffer, false, MathHelper.getPositionRandom(position)); tessellator.draw(); GlStateManager.enableLighting(); GlStateManager.popMatrix(); } } Quote
V0idWa1k3r Posted May 4, 2017 Posted May 4, 2017 (edited) And... congratulations! You are the third person in a row who forgot about applying correct translations before rendering! With correct translations applied I get the following from your code: Also if you want your block to be transparent you need to setup GL's blend too, not only alpha Edited May 4, 2017 by V0idWa1k3r 2 Quote
TheTrollguy_ Posted May 4, 2017 Author Posted May 4, 2017 @V0idWa1k3r hahahahh well third time's a charm, it all works now. Just a little optimization on the go and It's done. Thanks :) Quote
Recommended Posts
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.