Java tutorial
/* * This file is part of ProjectX. * Copyright (c) 2015 - 2018, KitsuneAlex, All rights reserved. * * ProjectX is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ProjectX is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with ProjectX. If not, see <http://www.gnu.org/licenses/lgpl>. */ package de.kitsunealex.projectx.client.render.block; import codechicken.lib.render.CCModel; import codechicken.lib.render.CCRenderState; import codechicken.lib.render.buffer.BakingVertexBuffer; import codechicken.lib.vec.Translation; import codechicken.lib.vec.Vector3; import codechicken.lib.vec.uv.IconTransformation; import de.kitsunealex.projectx.client.IAnimationHandler; import de.kitsunealex.silverfish.block.IBlockTextureProvider; import de.kitsunealex.silverfish.client.render.ModelCache; import de.kitsunealex.silverfish.client.render.SimpleBakedModel; import de.kitsunealex.silverfish.util.ClientUtils; import de.kitsunealex.silverfish.util.RenderUtils; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import org.lwjgl.opengl.GL11; @SideOnly(Side.CLIENT) public class RenderHelper { protected void renderBreakingAnimation(CCModel[] model, BlockPos pos, TextureAtlasSprite texture, BufferBuilder buffer) { CCRenderState renderState = CCRenderState.instance(); renderState.reset(); renderState.bind(buffer); for (int i = 0; i < model.length; i++) { model[i].copy().apply(new Translation(Vector3.fromBlockPos(pos))).render(renderState, new IconTransformation(texture)); } } protected void renderAnimation(CCModel[] model, IBlockAccess world, BlockPos pos, IBlockState state, BufferBuilder buffer, CCRenderState renderState) { IAnimationHandler handler = (IAnimationHandler) state.getBlock(); renderState.reset(); renderState.bind(buffer); for (int i = 0; i < model.length; i++) { for (int j = 0; j < 6; j++) { if (state.shouldSideBeRendered(world, pos, EnumFacing.getFront(j))) { int index = i * 100 + j; renderState.baseColour = handler.getAnimationColor(world, pos, state, index); renderState.brightness = handler.getAnimationBrightness(world, pos, state, index); TextureAtlasSprite texture = handler.getAnimationTexture(world, pos, state, index); model[i].copy().apply(new Translation(Vector3.fromBlockPos(pos))).render(renderState, j * 4, j * 4 + 4, new IconTransformation(texture)); } } } } protected boolean renderDefaultBlock(CCModel[] model, IBlockAccess world, BlockPos pos, IBlockState state, BufferBuilder buffer, CCRenderState renderState) { String key = ClientUtils.getRenderKey(world, pos, state); if (!ModelCache.contains(key)) { IBlockTextureProvider provider = (IBlockTextureProvider) state.getBlock(); BakingVertexBuffer parentBuffer = BakingVertexBuffer.create(); parentBuffer.begin(GL11.GL_QUADS, DefaultVertexFormats.ITEM); renderState.reset(); renderState.bind(parentBuffer); for (int i = 0; i < model.length; i++) { for (int j = 0; j < 6; j++) { renderState.baseColour = ClientUtils.getColorMultiplier(world, pos, state, j, i); TextureAtlasSprite texture = ClientUtils.getTexture(world, pos, state, j, i); model[i].render(renderState, j * 4, j * 4 + 4, new IconTransformation(texture)); } } parentBuffer.finishDrawing(); ModelCache.put(key, new SimpleBakedModel(parentBuffer.bake())); } return RenderUtils.renderModel(ModelCache.get(key), world, pos, state, buffer); } protected void renderAnimationInventory(CCModel[] model, ItemStack stack, BufferBuilder buffer, CCRenderState renderState) { IAnimationHandler handler = (IAnimationHandler) Block.getBlockFromItem(stack.getItem()); GlStateManager.pushMatrix(); int[] mipmapData = disableMipmap(); GlStateManager.disableLighting(); buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.ITEM); renderState.reset(); renderState.bind(buffer); for (int i = 0; i < model.length; i++) { for (int j = 0; j < 6; j++) { int index = i * 100 + j; renderState.baseColour = handler.getAnimationColor(stack, index); renderState.brightness = handler.getAnimationBrightness(stack, index); renderState.pushLightmap(); TextureAtlasSprite texture = handler.getAnimationTexture(stack, index); model[i].render(renderState, j * 4, j * 4 + 4, new IconTransformation(texture)); } } Tessellator.getInstance().draw(); GlStateManager.enableLighting(); enableMipmap(mipmapData); GlStateManager.popMatrix(); } protected void renderDefaultBlockInventory(CCModel[] model, ItemStack stack, BufferBuilder buffer, CCRenderState renderState) { GlStateManager.pushMatrix(); buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.ITEM); renderState.reset(); renderState.bind(buffer); for (int i = 0; i < model.length; i++) { for (int j = 0; j < 6; j++) { renderState.baseColour = ClientUtils.getColorMultiplier(stack, j, i); TextureAtlasSprite texture = ClientUtils.getTexture(stack, j, i); model[i].render(renderState, j * 4, j * 4 + 4, new IconTransformation(texture)); } } Tessellator.getInstance().draw(); GlStateManager.popMatrix(); } protected void renderDefaultBlockInventory(CCModel[] model, ItemStack stack, BufferBuilder buffer, CCRenderState renderState, int lastBrightness) { GlStateManager.pushMatrix(); buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.ITEM); renderState.reset(); renderState.bind(buffer); renderState.brightness = lastBrightness; renderState.pushLightmap(); for (int i = 0; i < model.length; i++) { for (int j = 0; j < 6; j++) { renderState.baseColour = ClientUtils.getColorMultiplier(stack, j, i); TextureAtlasSprite texture = ClientUtils.getTexture(stack, j, i); model[i].render(renderState, j * 4, j * 4 + 4, new IconTransformation(texture)); } } Tessellator.getInstance().draw(); GlStateManager.popMatrix(); } protected int[] disableMipmap() { int min = GL11.glGetTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER); int mag = GL11.glGetTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); return new int[] { min, mag }; } protected void enableMipmap(int[] data) { GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, data[0]); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, data[1]); } }