Jump to content

[SOLVED] Import .obj to my mod


kitsushadow

Recommended Posts

Hey, I have a custom model for an item that id like to render. All the tutorials i've found so far use techne. Seeing as i'm a linux user and wine isn't running techne I opted to use blender. I have exported the blender model as an obj file but i have no idea how to create the model class to import the model. I have the Item renderer class built and ready but i need to have the model class obviously to make it work.

 

Any suggestions or links to tutorials that show how to make the java file for an obj would be welcome.

 

Thanks

WORKING CODE

 

package com.kitsu.medievalcraft.renderer;

 

import org.lwjgl.opengl.GL11;

 

//import com.kitsu.medievalcraft.models.Model;

 

 

import com.kitsu.medievalcraft.item.weapon.ItemWoodenShield;

 

import net.minecraft.client.Minecraft;

import net.minecraft.entity.Entity;

import net.minecraft.item.ItemStack;

import net.minecraft.util.ResourceLocation;

import net.minecraftforge.client.IItemRenderer;

import net.minecraftforge.client.model.AdvancedModelLoader;

import net.minecraftforge.client.model.IModelCustom;

 

 

 

public class ItemRenderWoodenShield implements IItemRenderer {

 

public static final ResourceLocation WOODEN_SHIELD = new ResourceLocation("kitsumedievalcraft:models/woodenShieldObjectTriangle.obj");

public static final ResourceLocation woodenshield = new ResourceLocation("kitsumedievalcraft:models/woodenshield.png");

 

public IModelCustom model = AdvancedModelLoader.loadModel(WOODEN_SHIELD);

 

@Override

public boolean handleRenderType(ItemStack item, ItemRenderType type) {

 

switch(type) {

case EQUIPPED: {

return true;

}

case EQUIPPED_FIRST_PERSON: {

return true;

}

 

default: return false;

}

}

 

@Override

public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item,

ItemRendererHelper helper) {

 

return false;

}

 

@Override

public void renderItem(ItemRenderType type, ItemStack item, Object... data) {

 

switch(type) {

 

case EQUIPPED: {

 

GL11.glPushMatrix();

GL11.glScalef(0.875F, 0.875F, 0.875F);

 

//ANGLE, X ROTATE, Y ROTATE, Z ROTATE

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

GL11.glRotatef(10F, 0.0F, 0.0F, -1.0F);

GL11.glRotated(90, 1.0, 0.0, 0.0);

 

GL11.glTranslatef(-0.35F, -1.5F, -0.55F);

 

Minecraft.getMinecraft().renderEngine.bindTexture(woodenshield);

model.renderAll();

 

GL11.glPopMatrix();

} break;

 

case EQUIPPED_FIRST_PERSON: {

if (ItemWoodenShield.woodenShieldInUse == true) {

GL11.glPushMatrix();

GL11.glScalef(1.0F, 1.0F, 1.0F);

 

//ANGLE, X ROTATE, Y ROTATE, Z ROTATE

 

//GL11.glRotatef(0F, 0.0F, 0.0F, 0.0F);

 

GL11.glRotated(-85, 1.0, 0.0, 0.0);

GL11.glRotated(50, 0.0F, 0.0F, 1.0F);

GL11.glRotated(3, 0.0F, 1.0F, 0.0F);

 

GL11.glTranslatef(0.9F, -0.9F, 0.9F);

//GL11.glTranslatef(-0.35F, -1.5F, -0.55F);

 

Minecraft.getMinecraft().renderEngine.bindTexture(woodenshield);

model.renderAll();

 

GL11.glPopMatrix();

}

} break;

 

default:

break;

}

}

 

 

}

 

Link to comment
Share on other sites

Think i got the model stuff in the right places but the model is not rendering. Here is what i have so far.

 

Item Renderer

 

 

package com.kitsu.medievalcraft.renderer;

 

import org.lwjgl.opengl.GL11;

 

import com.kitsu.medievalcraft.models.Model;

 

import net.minecraft.client.Minecraft;

import net.minecraft.entity.Entity;

import net.minecraft.item.ItemStack;

import net.minecraft.util.ResourceLocation;

import net.minecraftforge.client.IItemRenderer;

 

 

 

public class ItemRenderWoodenShield implements IItemRenderer {

 

protected Model model;

private ResourceLocation woodenshield = new ResourceLocation("kitsumedievalcraft:models/woodenshield.png");

 

public ItemRenderWoodenShield() {

model = new Model();

}

 

 

 

@Override

public boolean handleRenderType(ItemStack item, ItemRenderType type) {

 

switch(type) {

case EQUIPPED: return true;

default: return false;

}

 

}

 

@Override

public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item,

ItemRendererHelper helper) {

 

return false;

}

 

@Override

public void renderItem(ItemRenderType type, ItemStack item, Object... data) {

 

switch(type) {

case EQUIPPED: {

GL11.glPushMatrix();

float scale = 1.5F;

Minecraft.getMinecraft().renderEngine.bindTexture(woodenshield);

GL11.glScalef(scale, scale, scale);

model.render();

 

 

 

GL11.glPopMatrix();

}

default:

break;

}

}

 

 

}

 

 

 

Item WoodenShield

 

 

package com.kitsu.medievalcraft.item.weapon;

 

import com.google.common.collect.HashMultimap;

import com.google.common.collect.Multimap;

import com.kitsu.medievalcraft.CustomTab;

import com.kitsu.medievalcraft.Main;

 

import net.minecraft.entity.Entity;

import net.minecraft.entity.SharedMonsterAttributes;

import net.minecraft.entity.ai.attributes.AttributeModifier;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.init.Blocks;

import net.minecraft.init.Items;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.item.ItemSword;

import net.minecraft.potion.Potion;

import net.minecraft.potion.PotionEffect;

import net.minecraft.world.World;

 

public class ItemWoodenShield extends ItemSword {

 

public static boolean woodenShieldInUse;

 

public ItemWoodenShield(String unlocalizedName, ToolMaterial mat) {

super(mat);

this.setUnlocalizedName(unlocalizedName);

//this.setTextureName(Main.MODID + ":" + unlocalizedName);

setCreativeTab(CustomTab.MedievalCraftTab);

setMaxStackSize(1);

setMaxDamage(mat.getMaxUses());

setFull3D().isRepairable();

}

 

@Override

public int getItemEnchantability () {

return 0;

 

}

 

@Override

public ItemStack onItemRightClick (ItemStack stack, World world, EntityPlayer player) {

 

 

woodenShieldInUse = true;

 

return super.onItemRightClick(stack, world, player);

}

 

    @Override

    public boolean getIsRepairable(ItemStack item, ItemStack repair)

    {

        return Item.getItemFromBlock(Blocks.planks) == repair.getItem() ? true : false;

    }

 

 

 

}

 

 

 

Model Class

 

 

package com.kitsu.medievalcraft.models;

 

import net.minecraft.entity.Entity;

import net.minecraft.util.ResourceLocation;

import net.minecraftforge.client.model.AdvancedModelLoader;

import net.minecraftforge.client.model.IModelCustom;

 

public class Model {

 

public static Model instance = new Model();

 

private IModelCustom woodenShield;

 

public static final ResourceLocation WOODEN_SHIELD = new ResourceLocation("kitsumedievalcraft:models/woodenShieldObject.obj");

 

 

public Model() {

woodenShield = AdvancedModelLoader.loadModel(WOODEN_SHIELD);

}

 

public void render() {

woodenShield.renderAll();

}

 

 

 

}

 

 

 

 

Client Proxy

 

 

package com.kitsu.medievalcraft;

 

import net.minecraft.client.renderer.entity.RenderSnowball;

import net.minecraft.world.World;

import net.minecraftforge.client.IItemRenderer;

import net.minecraftforge.client.MinecraftForgeClient;

 

import com.kitsu.medievalcraft.entity.EntityShit;

import com.kitsu.medievalcraft.item.ModItems;

import com.kitsu.medievalcraft.renderer.ItemRenderWoodenShield;

 

import cpw.mods.fml.client.FMLClientHandler;

import cpw.mods.fml.client.registry.RenderingRegistry;

 

public class ClientProxy extends CommonProxy {

 

    @Override

    public void registerRenderer() {

   

   

   

    }

   

    public void registerItemRenderers() {

    MinecraftForgeClient.registerItemRenderer(ModItems.woodenShield, (IItemRenderer)new ItemRenderWoodenShield());

    }

   

    @Override

    public Object getClient() {

    return FMLClientHandler.instance().getClient();

    }

 

    @Override

    public World getClientWorld() {

    return FMLClientHandler.instance().getClient().theWorld;

    }

   

}

 

 

Link to comment
Share on other sites

To load an obj model you need an IModelCustom, and a ResourceLocation. You don't need a model class, that just over complicates things. You just put these in the ItemRenderer. You only need a model class if you are using techne, and also, not needed, but if you are using tessellators too.

 


public final ResourceLocation modelResource = new ResourceLocation("modid:yourModelLocation/yourModel.obj");
public final ResourceLocation texture = new ResourceLocation("modid:yourTextureLocation/yourTexture.png");

public IModelCustom model = AdvancedModelLoader.loadModel(modelResource);

public void renderItem(ItemRenderType type, ItemStack stack, Object... data) {
        GL11.glPushMatrix();
        Minecraft.getMinecraft().renderEngine.bindTexture(this.texture); //Not 100% sure if I got this right, but you bing the texture here.
        //Do all your GL11 rotations, scales, and translaters here...
        this.model.renderAll(); //Finally render the model
        GL11.glPopMatrix();
}

 

Hope this helped!

 

Also please note that your model HAS to be triangulated. If your model isn't triangulated, you will get a crash saying ModelFormatException.

I ask complicated questions, and apparently like to write really long detailed posts. But I also help others when I can.

Link to comment
Share on other sites

Okay, for that crash, there is a NullPointerException on line 19 of your model class. always make sure something is not null if there is the possibility for it to be. Unfortunately, I can't look at the code you put on bitbucket, I am on a school pc, and that website is blocked due to a security error. May you put the code in "code" and "/code" (with square brackets [ ]) annotations? that way I can see what you have changed.

 

As I said though, you don't need a model class, put it in the ItemRenderer.

I ask complicated questions, and apparently like to write really long detailed posts. But I also help others when I can.

Link to comment
Share on other sites

ok i tried what u suggested im getting a ModelFormatException error now

I did triangulate it.

Error parsing entry ('o Cylinder.002_Cylinder.003', line 4)

 

ItemRenderer Class

 

package com.kitsu.medievalcraft.renderer;

 

import org.lwjgl.opengl.GL11;

 

//import com.kitsu.medievalcraft.models.Model;

 

import net.minecraft.client.Minecraft;

import net.minecraft.entity.Entity;

import net.minecraft.item.ItemStack;

import net.minecraft.util.ResourceLocation;

import net.minecraftforge.client.IItemRenderer;

import net.minecraftforge.client.model.AdvancedModelLoader;

import net.minecraftforge.client.model.IModelCustom;

 

 

 

public class ItemRenderWoodenShield implements IItemRenderer {

 

//protected Model model;

 

/*public ItemRenderWoodenShield() {

model = new Model();

}*/

 

 

public static final ResourceLocation WOODEN_SHIELD = new ResourceLocation("kitsumedievalcraft:models/woodenShieldObject.obj");

public static final ResourceLocation woodenshield = new ResourceLocation("kitsumedievalcraft:models/woodenshield.png");

 

public IModelCustom model = AdvancedModelLoader.loadModel(WOODEN_SHIELD);

 

@Override

public boolean handleRenderType(ItemStack item, ItemRenderType type) {

 

switch(type) {

case EQUIPPED: return true;

//EQUIPPED_FIRST_PERSON

default: return false;

}

 

}

 

@Override

public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item,

ItemRendererHelper helper) {

 

return false;

}

 

@Override

public void renderItem(ItemRenderType type, ItemStack item, Object... data) {

 

switch(type) {

case EQUIPPED: {

 

GL11.glPushMatrix();

 

Minecraft.getMinecraft().renderEngine.bindTexture(woodenshield);

this.model.renderAll();

 

GL11.glPopMatrix();

}

default:

break;

}

}

 

 

}

 

 

Error

http://pastebin.com/0i0BgURQ

 

 

Link to comment
Share on other sites

Well, if it isn't rendering, you either haven't bound the texture properly, or it is out of the players FOV, if you go into third person mode, can you see it? if not, the texture is not done properly, if so, you just need to use GL11 to translate it into the players view, and from there, you need to figure out the rotations and translations as no two models have the same translations/rotations.

I ask complicated questions, and apparently like to write really long detailed posts. But I also help others when I can.

Link to comment
Share on other sites

Well, if it isn't rendering, you either haven't bound the texture properly, or it is out of the players FOV, if you go into third person mode, can you see it? if not, the texture is not done properly, if so, you just need to use GL11 to translate it into the players view, and from there, you need to figure out the rotations and translations as no two models have the same translations/rotations.

 

 

Got It all working, thnx a lot for ur help

 

spoiler is broken again so

package com.kitsu.medievalcraft.renderer;

 

import org.lwjgl.opengl.GL11;

 

//import com.kitsu.medievalcraft.models.Model;

 

 

import com.kitsu.medievalcraft.item.weapon.ItemWoodenShield;

 

import net.minecraft.client.Minecraft;

import net.minecraft.entity.Entity;

import net.minecraft.item.ItemStack;

import net.minecraft.util.ResourceLocation;

import net.minecraftforge.client.IItemRenderer;

import net.minecraftforge.client.model.AdvancedModelLoader;

import net.minecraftforge.client.model.IModelCustom;

 

 

 

public class ItemRenderWoodenShield implements IItemRenderer {

 

public static final ResourceLocation WOODEN_SHIELD = new ResourceLocation("kitsumedievalcraft:models/woodenShieldObjectTriangle.obj");

public static final ResourceLocation woodenshield = new ResourceLocation("kitsumedievalcraft:models/woodenshield.png");

 

public IModelCustom model = AdvancedModelLoader.loadModel(WOODEN_SHIELD);

 

@Override

public boolean handleRenderType(ItemStack item, ItemRenderType type) {

 

switch(type) {

case EQUIPPED: {

return true;

}

case EQUIPPED_FIRST_PERSON: {

return true;

}

 

default: return false;

}

}

 

@Override

public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item,

ItemRendererHelper helper) {

 

return false;

}

 

@Override

public void renderItem(ItemRenderType type, ItemStack item, Object... data) {

 

switch(type) {

 

case EQUIPPED: {

 

GL11.glPushMatrix();

GL11.glScalef(0.875F, 0.875F, 0.875F);

 

//ANGLE, X ROTATE, Y ROTATE, Z ROTATE

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

GL11.glRotatef(10F, 0.0F, 0.0F, -1.0F);

//GL11.glRotatef(30F, 0.0F, 0.0F, 1.0F);

GL11.glRotated(90, 1.0, 0.0, 0.0);

 

GL11.glTranslatef(-0.35F, -1.5F, -0.55F);

 

Minecraft.getMinecraft().renderEngine.bindTexture(woodenshield);

model.renderAll();

 

GL11.glPopMatrix();

} break;

 

case EQUIPPED_FIRST_PERSON: {

if (ItemWoodenShield.woodenShieldInUse == true) {

GL11.glPushMatrix();

GL11.glScalef(1.0F, 1.0F, 1.0F);

 

//ANGLE, X ROTATE, Y ROTATE, Z ROTATE

//GL11.glRotatef(90F, 0.0F, 1.0F, 0.0F);

GL11.glRotatef(0F, 0.0F, 0.0F, 0.0F);

//GL11.glRotatef(30F, 0.0F, 0.0F, 1.0F);

 

 

GL11.glRotated(-85, 1.0, 0.0, 0.0);

GL11.glRotated(50, 0.0F, 0.0F, 1.0F);

GL11.glRotated(3, 0.0F, 1.0F, 0.0F);

 

GL11.glTranslatef(0.9F, -0.9F, 0.9F);

//GL11.glTranslatef(-0.35F, -1.5F, -0.55F);

 

Minecraft.getMinecraft().renderEngine.bindTexture(woodenshield);

model.renderAll();

 

GL11.glPopMatrix();

}

} break;

 

default:

break;

}

}

 

 

}

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.