lyonlancer5.pfsteel.client.render.RenderDarkShield.java Source code

Java tutorial

Introduction

Here is the source code for lyonlancer5.pfsteel.client.render.RenderDarkShield.java

Source

/* Copyright (c) 2015 Lance Selga <lyonecro55@gmail.com>
 * 
 * This work is free. You can redistribute it and/or modify it under the
 * terms of the Do What The Fuck You Want To Public License, Version 2,
 * as published by Sam Hocevar. See the COPYING file for more details.
 */
package lyonlancer5.pfsteel.client.render;

import lyonlancer5.pfsteel.entity.EntityDarkShield;
import lyonlancer5.pfsteel.util.ModVersion;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class RenderDarkShield extends Render {

    private static final ResourceLocation darkTex = new ResourceLocation(ModVersion.modid,
            "textures/models/dark.png");

    @Override
    public void doRender(Entity entity, double x, double y, double z, float yaw, float pitch) {
        renderDarkMain((EntityDarkShield) entity, x, y, z, yaw, pitch);
    }

    protected void renderDarkMain(EntityDarkShield shield, double x, double y, double z, float yaw, float pitch) {
        GL11.glDisable(GL11.GL_LIGHTING);

        GL11.glPushMatrix();
        GL11.glDisable(GL11.GL_CULL_FACE);
        if (RenderManager.instance.getDistanceToCamera(shield.posX, shield.posY, shield.posZ) < 36.0D) {
            GL11.glDepthFunc(GL11.GL_ALWAYS);
        } else {
            GL11.glDepthMask(false);
        }
        GL11.glTranslatef((float) x, (float) y, (float) z);
        GL11.glRotatef(-renderManager.playerViewY, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(renderManager.playerViewX, 1.0F, 0.0F, 0.0F);

        Tessellator tessellator = Tessellator.instance;
        GL11.glDepthMask(false);
        GL11.glEnable(GL11.GL_BLEND);

        GL11.glBlendFunc(GL11.GL_ZERO, GL11.GL_ONE_MINUS_SRC_COLOR);
        GL11.glTranslatef(0.0F, 1.3F, 0.0F);
        this.bindTexture(darkTex);
        renderDark(tessellator, 12.0D, 12.0F, 0.0D, 1.0F, 0);
        GL11.glDisable(GL11.GL_BLEND);

        GL11.glDepthFunc(GL11.GL_LEQUAL);
        GL11.glEnable(GL11.GL_CULL_FACE);
        GL11.glDepthMask(true);
        GL11.glPopMatrix();

        GL11.glEnable(GL11.GL_LIGHTING);
    }

    protected void renderDark(Tessellator tessellator, double length, float width, double zPos, float alpha,
            int time) {

        float maxWidth = (float) width / 2.0F;

        int zAngleDivNum = 18;
        double angleZ = 0F;
        double angleSpanZ = Math.PI * 2.0D / (double) zAngleDivNum;

        int zDivNum = 9;
        zPos = Math.sin(-Math.PI / 2.0D) * maxWidth;
        double zPosOld = zPos;

        float xPos = 0F;
        float yPos = 0F;
        float xPos2 = 0F;
        float yPos2 = 0F;

        float xPosOld = xPos;
        float yPosOld = yPos;
        float xPos2Old = xPos2;
        float yPos2Old = yPos2;

        float angle = -(float) Math.PI / 2.0F;
        float angleSpan = (float) Math.PI / (float) (zDivNum);
        angle += angleSpan;

        float widthOld = 0.0F;

        for (int j = 0; j < zDivNum; j++) {
            zPos = Math.sin(angle) * maxWidth;
            width = (float) Math.cos(angle) * maxWidth;

            xPos = width;
            yPos = 0F;
            angleZ = 0F;
            xPosOld = (float) Math.cos(angleZ) * width;
            yPosOld = (float) Math.sin(angleZ) * width;
            xPos2Old = (float) Math.cos(angleZ) * widthOld;
            yPos2Old = (float) Math.sin(angleZ) * widthOld;

            angleZ = angleSpanZ;

            for (int i = 1; i <= zAngleDivNum; i++) {
                xPos = (float) Math.cos(angleZ) * width;
                yPos = (float) Math.sin(angleZ) * width;
                xPos2 = (float) Math.cos(angleZ) * widthOld;
                yPos2 = (float) Math.sin(angleZ) * widthOld;

                tessellator.startDrawingQuads();
                tessellator.setColorRGBA_F(1.0F, 1.0F, 1.0F, alpha);
                tessellator.setNormal(0.0F, 1.0F, 0.0F);
                tessellator.addVertexWithUV(xPos, yPos, zPos, 1.0F, 0.0F);
                tessellator.addVertexWithUV(xPosOld, yPosOld, zPos, 0.0F, 0.0F);
                tessellator.addVertexWithUV(xPos2Old, yPos2Old, zPosOld, 0.0F, 1.0F);
                tessellator.addVertexWithUV(xPos2, yPos2, zPosOld, 1.0F, 1.0F);

                tessellator.draw();

                xPosOld = xPos;
                yPosOld = yPos;
                xPos2Old = xPos2;
                yPos2Old = yPos2;
                angleZ += angleSpanZ;

            }
            zPosOld = zPos;
            angle += angleSpan;
            widthOld = width;

        }
    }

    @Override
    protected ResourceLocation getEntityTexture(Entity entity) {
        return getEntityTexture((EntityDarkShield) entity);
    }

    protected ResourceLocation getEntityTexture(EntityDarkShield entity) {
        return darkTex;
    }

}