package shadersmod.client;

import net.minecraft.client.model.PositionTextureVertex;
import net.minecraft.client.model.TexturedQuad;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.Vec3d;

public class STexturedQuad {

    public static void draw(TexturedQuad quad, BufferBuilder renderer, float scale) {
        Vec3d vec01 = quad.vertexPositions[1].vector3D.subtractReverse(quad.vertexPositions[0].vector3D);
        Vec3d vec21 = quad.vertexPositions[1].vector3D.subtractReverse(quad.vertexPositions[2].vector3D);
        Vec3d vecNormal = vec21.crossProduct(vec01).normalize();
        float fx = (float)vecNormal.xCoord;
        float fy = (float)vecNormal.yCoord;
        float fz = (float)vecNormal.zCoord;

        if (Dummy.get_texturedQuad_invertNormal(quad))
        {
            fx = -fx;
            fy = -fy;
            fz = -fz;
        }

        renderer.begin(7, DefaultVertexFormats.OLDMODEL_POSITION_TEX_NORMAL);

        for (int i = 0; i < 4; ++i)
        {
            PositionTextureVertex positiontexturevertex = quad.vertexPositions[i];
            renderer
            .pos(positiontexturevertex.vector3D.xCoord * (double)scale, positiontexturevertex.vector3D.yCoord * (double)scale, positiontexturevertex.vector3D.zCoord * (double)scale)
            .tex((double)positiontexturevertex.texturePositionX, (double)positiontexturevertex.texturePositionY)
            .normal(fx, fy, fz)
            .endVertex();
        }
        SVertexBuffer.calcNormal(renderer, 0);

        Tessellator.getInstance().draw();
    }

}
