Jump to content

[solved]How do you Rotate custom modeled block


Kakarotvg

Recommended Posts

I am wondering how to rotate my custom modeled block depending on the direction you placed it.

 

block code

 

 

package kakarotvg.omega.tileentity;

 

import kakarotvg.omega.CreativetabHandler;

import kakarotvg.omega.Reference;

import kakarotvg.omega.entity.TileEntityComputerEntity;

import net.minecraft.block.BlockContainer;

import net.minecraft.block.material.Material;

import net.minecraft.client.renderer.texture.IconRegister;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.item.ItemStack;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public class TileEntityComputer extends BlockContainer {

 

    //Treat it like a normal block here. The Block Bounds are a good idea - the first three are X Y and Z of the botton-left corner,

    //And the second three are the top-right corner.

    public TileEntityComputer(int id) {

        super(id, Material.iron);

        this.setCreativeTab(CreativetabHandler.vgtab);

    }

 

    //Make sure you set this as your TileEntity class relevant for the block!

    @Override

    public TileEntity createNewTileEntity(World world) {

        return new TileEntityComputerEntity();

    }

 

    @Override

    public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase player, ItemStack par6ItemStack) {

        int dir = MathHelper.floor_double((double) ((player.rotationYaw * 4F) / 360F) + 0.5D) & 3;

        world.setBlockMetadataWithNotify(x, y, z, dir, 0);

 

    }

 

    //You don't want the normal render type, or it wont render properly.

    @Override

    public int getRenderType() {

        return -1;

    }

 

    //It's not an opaque cube, so you need this.

    @Override

    public boolean isOpaqueCube() {

        return false;

    }

 

    //It's not a normal block, so you need this too.

    public boolean renderAsNormalBlock() {

        return false;

    }

 

    @SideOnly(Side.CLIENT)

    @Override

    public void registerIcons(IconRegister register) {

        this.blockIcon = register.registerIcon(Reference.MOD_ID + ":" + (this.getUnlocalizedName().substring(5)));

    }

 

}

 

 

 

 

renderer code

 

 

package kakarotvg.omega.render;

 

import kakarotvg.omega.Reference;

import kakarotvg.omega.model.ModelComputer;

import net.minecraft.block.Block;

import net.minecraft.client.Minecraft;

import net.minecraft.client.renderer.OpenGlHelper;

import net.minecraft.client.renderer.Tessellator;

import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;

import net.minecraft.entity.Entity;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.ResourceLocation;

import net.minecraft.world.World;

 

import org.lwjgl.opengl.GL11;

 

public class TileEntityComputerRenderer extends TileEntitySpecialRenderer {

 

    //The model of your block

    public final ModelComputer model;

    private static final ResourceLocation resourceloc = new ResourceLocation(Reference.MOD_ID + ":" + "textures/tileentity/computer.png");

 

    public TileEntityComputerRenderer() {

        this.model = new ModelComputer();

    }

 

    private void adjustRotatePivotViaMeta(World world, int x, int y, int z) {

        int meta = world.getBlockMetadata(x, y, z);

        GL11.glPushMatrix();

        GL11.glRotatef(meta * (-90), 0.0F, 0.0F, 1.0F);

        GL11.glPopMatrix();

    }

 

    @Override

    public void renderTileEntityAt(TileEntity te, double x, double y, double z, float scale) {

        //The PushMatrix tells the renderer to "start" doing something.

        GL11.glPushMatrix();

        //This is setting the initial location.

        GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);

        Minecraft.getMinecraft().renderEngine.func_110577_a(resourceloc);

        //This is the texture of your block. It's pathed to be the same place as your other blocks here.

        //This rotation part is very important! Without it, your model will render upside-down! And for some reason you DO need PushMatrix again!                     

        GL11.glPushMatrix();

        GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F);

        //A reference to your Model file. Again, very important.

        this.model.render((Entity) null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);

        //Tell it to stop rendering for both the PushMatrix's

        GL11.glPopMatrix();

        GL11.glPopMatrix();

    }

 

    //Set the lighting stuff, so it changes it's brightness properly.     

    private void adjustLightFixture(World world, int i, int j, int k, Block block) {

        Tessellator tess = Tessellator.instance;

        float brightness = block.getBlockBrightness(world, i, j, k);

        int skyLight = world.getLightBrightnessForSkyBlocks(i, j, k, 0);

        int modulousModifier = skyLight % 65536;

        int divModifier = skyLight / 65536;

        tess.setColorOpaque_F(brightness, brightness, brightness);

        OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, (float) modulousModifier, divModifier);

 

    }

}

 

 

 

if (You.likescoding == false){
      You.goaway;
}

Link to comment
Share on other sites

methinks cpw's ironchests is always a good place to start with this:

 

https://github.com/cpw/ironchest

 

in fact, it's generally a good place to start for any new release of forge i find, as it's sortof a "sandbox" for any new api shenanigans...

 

 

 

edit: i look like a right git now you've added the code since i posted the above, i thought you simply didn't know where to start :)

Link to comment
Share on other sites

this hurts so much xD

 

 

public class TileEntityComputerRenderer extends TileEntitySpecialRenderer {

 

    //The model of your block

    public final ModelComputer model;

    private static final ResourceLocation resourceloc = new ResourceLocation(Reference.MOD_ID + ":" + "textures/tileentity/computer.png");

 

    public TileEntityComputerRenderer() {

        this.model = new ModelComputer();

    }

 

    private void adjustRotatePivotViaMeta(World world, int x, int y, int z) {//hydro: never called ??? why does it exists

        int meta = world.getBlockMetadata(x, y, z);

      //*facepalm*

      GL11.glPushMatrix();

        GL11.glRotatef(meta * (-90), 0.0F, 0.0F, 1.0F);

        GL11.glPopMatrix();

    }

 

    @Override

    public void renderTileEntityAt(TileEntity te, double x, double y, double z, float scale) {

        //The PushMatrix tells the renderer to "start" doing something. // hydro: correction, tell openGL to "save current matrix transform".

        GL11.glPushMatrix();

        //This is setting the initial location.//

        GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);

        Minecraft.getMinecraft().renderEngine.func_110577_a(resourceloc);

        //This is the texture of your block. It's pathed to be the same place as your other blocks here.

        //This rotation part is very important! Without it, your model will render upside-down! And for some reason you DO need PushMatrix again!//the reason is because if you dont push you will not see your model, it will be renderered but very far from where you expect it to be

        GL11.glPushMatrix();

        GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F);

        //A reference to your Model file. Again, very important.

        this.model.render((Entity) null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);

        //Tell it to stop rendering for both the PushMatrix's//hydro: tell openGL to restore saved matrix transform has nothing to do with rendering

        GL11.glPopMatrix();

        GL11.glPopMatrix();

    }

 

    //Set the lighting stuff, so it changes it's brightness properly.     

    private void adjustLightFixture(World world, int i, int j, int k, Block block) {//hydro:copy pasted never called ? ....

        Tessellator tess = Tessellator.instance;

        float brightness = block.getBlockBrightness(world, i, j, k);

        int skyLight = world.getLightBrightnessForSkyBlocks(i, j, k, 0);

        int modulousModifier = skyLight % 65536;

        int divModifier = skyLight / 65536;

        tess.setColorOpaque_F(brightness, brightness, brightness);

        OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, (float) modulousModifier, divModifier);

 

    }

}

 

and now clean version:

 

 

public class TileEntityComputerRenderer extends TileEntitySpecialRenderer {

 

    //The model of your block

    public final ModelComputer model;

    private static final ResourceLocation resourceloc = new ResourceLocation(Reference.MOD_ID + ":" + "textures/tileentity/computer.png");

 

    public TileEntityComputerRenderer() {

        this.model = new ModelComputer();

    }

 

    private void adjustRotatePivotViaMeta(World world, int x, int y, int z) {

        int meta = world.getBlockMetadata(x, y, z);

        GL11.glRotatef(meta * (-90), 0.0F, 0.0F, 1.0F);

    }

 

    @Override

    public void renderTileEntityAt(TileEntity te, double x, double y, double z, float scale) {

        GL11.glPushMatrix();

        GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);

        Minecraft.getMinecraft().renderEngine.func_110577_a(resourceloc);

        GL11.glPushMatrix();

        adjustRotatePivotViaMeta(te.worldObj, te.xCoord, te.yCoord, te.zCoord);//rotation 1

        GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F);

        //adjustRotatePivotViaMeta(te.worldObj, te.xCoord, te.yCoord, te.zCoord);//comment line "rotation 1" and uncomment this line if it doesnt work.

        model.render((Entity) null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);

        GL11.glPopMatrix();

        GL11.glPopMatrix();

    }

}

 

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

What you had me do kept rendering upside down, but this got it working.

 

 

 

package kakarotvg.omega.render;

 

import kakarotvg.omega.Reference;

import kakarotvg.omega.model.ModelComputer;

import net.minecraft.block.Block;

import net.minecraft.client.Minecraft;

import net.minecraft.client.renderer.OpenGlHelper;

import net.minecraft.client.renderer.Tessellator;

import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;

import net.minecraft.entity.Entity;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.ResourceLocation;

import net.minecraft.world.World;

 

import org.lwjgl.opengl.GL11;

 

public class TileEntityComputerRenderer extends TileEntitySpecialRenderer {

 

    //The model of your block

    public final ModelComputer model;

    private static final ResourceLocation resourceloc = new ResourceLocation(Reference.MOD_ID + ":" + "textures/tileentity/computer.png");

 

    public TileEntityComputerRenderer() {

        this.model = new ModelComputer();

    }

 

    @Override

    public void renderTileEntityAt(TileEntity te, double x, double y, double z, float scale) {

        //The PushMatrix tells the renderer to "start" doing something.

        GL11.glPushMatrix();

        //This is setting the initial location.

        GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);

        //This is the texture of your block. It's pathed to be the same place as your other blocks here.

        //This rotation part is very important! Without it, your model will render upside-down! And for some reason you DO need PushMatrix again!                     

        GL11.glPushMatrix();

        GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F);

 

        adjustLightFixture(te.worldObj, te.xCoord, te.yCoord, te.zCoord, te.blockType);

        //A reference to your Model file. Again, very important.

        //Tell it to stop rendering for both the PushMatrix's

        GL11.glPopMatrix();

        GL11.glPopMatrix();

    }

 

    //Set the lighting stuff, so it changes it's brightness properly.     

    private void adjustLightFixture(World world, int i, int j, int k, Block block) {

        Tessellator tess = Tessellator.instance;

        float brightness = block.getBlockBrightness(world, i, j, k);

        int skyLight = world.getLightBrightnessForSkyBlocks(i, j, k, 0);

        int modulousModifier = skyLight % 65536;

        int divModifier = skyLight / 65536;

        tess.setColorOpaque_F(brightness, brightness, brightness);

        OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, (float) modulousModifier, divModifier);

 

        int dir = world.getBlockMetadata(i, j, k);

 

        GL11.glPushMatrix();

        //This line actually rotates the renderer.

        GL11.glRotatef(dir * (90F), 0F, 1F, 0F);

        Minecraft.getMinecraft().renderEngine.func_110577_a(resourceloc);

        this.model.render((Entity) null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);

        /*

        * Place your rendering code here.

        */

 

        GL11.glPopMatrix();

 

    }

}

 

 

 

if (You.likescoding == false){
      You.goaway;
}

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.