codechicken.lib.model.CachedFormat

Here are the examples of the java api codechicken.lib.model.CachedFormat taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

6 Examples 7

19 Source : QuadTransformer.java
with GNU Lesser General Public License v2.1
from TheCBProject

/**
 * Base clreplaced for a simple QuadTransformer.
 * Operates on BakedQuads.
 * QuadTransformers can be piped into each other at no performance penalty.
 *
 * @author covers1624
 */
public abstract clreplaced QuadTransformer implements IVertexConsumer, ISmartVertexConsumer, IPipelineConsumer {

    protected final Quad quad;

    protected CachedFormat format;

    protected IVertexConsumer consumer;

    /**
     * Used for the BakedPipeline.
     */
    protected QuadTransformer() {
        quad = new Quad();
    }

    public QuadTransformer(IVertexConsumer consumer) {
        this(consumer.getVertexFormat(), consumer);
    }

    public QuadTransformer(VertexFormat format, IVertexConsumer consumer) {
        this(CachedFormat.lookup(format), consumer);
    }

    public QuadTransformer(CachedFormat format, IVertexConsumer consumer) {
        this.format = format;
        this.consumer = consumer;
        quad = new Quad(format);
    }

    @Override
    @OverridingMethodsMustInvokeSuper
    public void reset(CachedFormat format) {
        this.format = format;
        quad.reset(format);
    }

    @Override
    public void setParent(IVertexConsumer parent) {
        consumer = parent;
    }

    @Override
    @OverridingMethodsMustInvokeSuper
    public void setInputQuad(Quad quad) {
        if (consumer instanceof IPipelineConsumer) {
            ((IPipelineConsumer) consumer).setInputQuad(quad);
        }
    }

    // @formatter:off
    @Override
    public VertexFormat getVertexFormat() {
        return format.format;
    }

    @Override
    public void setQuadTint(int tint) {
        quad.setQuadTint(tint);
    }

    @Override
    public void setQuadOrientation(Direction orientation) {
        quad.setQuadOrientation(orientation);
    }

    @Override
    public void setApplyDiffuseLighting(boolean diffuse) {
        quad.setApplyDiffuseLighting(diffuse);
    }

    @Override
    public void setTexture(TextureAtlreplacedprite texture) {
        quad.setTexture(texture);
    }

    // @formatter:on
    @Override
    public void put(int element, float... data) {
        quad.put(element, data);
        if (quad.full) {
            onFull();
        }
    }

    @Override
    public void put(Quad quad) {
        this.quad.put(quad);
        onFull();
    }

    /**
     * Called to transform the vertices.
     *
     * @return If the transformer should pipe the quad.
     */
    public abstract boolean transform();

    public void onFull() {
        if (transform()) {
            quad.pipe(consumer);
        }
    }

    // Should be small enough.
    private final static double EPSILON = 0.00001;

    public static boolean epsComp(float a, float b) {
        if (a == b) {
            return true;
        } else {
            return Math.abs(a - b) < EPSILON;
        }
    }
}

18 Source : BakedPipeline.java
with GNU Lesser General Public License v2.1
from TheCBProject

/**
 * Used to reset the pipeline for the next quad.
 * MUST be called between quads.
 *
 * @param format The format.
 */
public void reset(CachedFormat format) {
    unpacker.reset(format);
    for (PipelineElement<?> element : elements) {
        element.reset(format);
    }
    first = null;
}

17 Source : QuadReInterpolator.java
with GNU Lesser General Public License v2.1
from TheCBProject

@Override
public void reset(CachedFormat format) {
    super.reset(format);
    interpCache.reset(format);
}

16 Source : QuadTransformer.java
with GNU Lesser General Public License v2.1
from TheCBProject

@Override
@OverridingMethodsMustInvokeSuper
public void reset(CachedFormat format) {
    this.format = format;
    quad.reset(format);
}

16 Source : AbstractBakedPropertiesModel.java
with GNU Lesser General Public License v2.1
from TheCBProject

protected boolean checkDepth(BakedQuad quad, Vector3 hit, Direction hitFace) {
    int[] quadData = quad.getVertexData();
    CachedFormat format = CachedFormat.lookup(DefaultVertexFormats.BLOCK);
    Vector3 posVec = new Vector3();
    float[] pos = new float[4];
    for (int v = 0; v < 4; v++) {
        LightUtil.unpack(quadData, pos, format.format, v, format.positionIndex);
        posVec.add(pos[0], pos[1], pos[2]);
    }
    posVec.divide(4);
    double diff = 0;
    switch(hitFace.getAxis()) {
        case X:
            diff = Math.abs(hit.x - posVec.x);
            break;
        case Y:
            diff = Math.abs(hit.y - posVec.y);
            break;
        case Z:
            diff = Math.abs(hit.z - posVec.z);
            break;
    }
    return !(diff > 0.01);
}

13 Source : CCRenderState.java
with GNU Lesser General Public License v2.1
from TheCBProject

/**
 * The core of the CodeChickenLib render system.
 * Where possible replacedign a local var of CCRenderState to avoid millions of calls to instance();
 * Uses a ThreadLocal system to replacedign each thread their own CCRenderState so we can use it in Mulreplacedhreaded chunk batching.
 * TODO, proper piping of BakedQuads and CCBakedQuads.
 */
public clreplaced CCRenderState {

    private static final ThreadLocal<CCRenderState> instances = ThreadLocal.withInitial(CCRenderState::new);

    // Each attrib needs to be replacedigned in this order to have a valid operation index.
    public final VertexAttribute<Vector3[]> normalAttrib = new NormalAttribute();

    public final VertexAttribute<int[]> colourAttrib = new ColourAttribute();

    public final VertexAttribute<int[]> lightingAttrib = new LightingAttribute();

    public final VertexAttribute<int[]> sideAttrib = new SideAttribute();

    public final VertexAttribute<LC[]> lightCoordAttrib = new LightCoordAttribute();

    // pipeline state
    public IVertexSource model;

    public int firstVertexIndex;

    public int lastVertexIndex;

    public int vertexIndex;

    public CCRenderPipeline pipeline;

    public IVertexBuilder r;

    public VertexFormat fmt;

    public CachedFormat cFmt;

    // context
    /**
     * The base color, multiplied by the {@link ColourAttribute} from the bound model if present otherwise used as-is.
     */
    public int baseColour;

    /**
     * An override for the alpha colour component.
     */
    public int alphaOverride;

    /**
     * Lets the {@link LightMatrix} or {@link PlanarLightModel} know if this {@link CCRenderState} should compute lighting.
     */
    public boolean computeLighting;

    /**
     * A standard {@link LightMatrix} instance to be shared on this pipeline.
     */
    public LightMatrix lightMatrix = new LightMatrix();

    // vertex outputs
    public final Vertex5 vert = new Vertex5();

    public final Vector3 normal = new Vector3();

    public int colour;

    public int brightness;

    public int overlay;

    // attribute storage
    public int side;

    public LC lc = new LC();

    public TextureAtlreplacedprite sprite;

    private CCRenderState() {
        pipeline = new CCRenderPipeline(this);
    }

    public static CCRenderState instance() {
        return instances.get();
    }

    /**
     * Bind this {@link CCRenderState} instance to the {@link Tessellator} buffer
     * and prepare to start drawing vertices for the given <code>mode</code> and {@link VertexFormat}.
     *
     * @param mode   The GL integer mode. E.g: GL_QUADS, GL_TRIANGLES, and so on.
     * @param format The {@link VertexFormat}.
     * @return The {@link BufferBuilder} instance from {@link Tessellator}.
     */
    public BufferBuilder startDrawing(int mode, VertexFormat format) {
        BufferBuilder r = Tessellator.getInstance().getBuffer();
        r.begin(mode, format);
        bind(r);
        return r;
    }

    /**
     * Bind this {@link CCRenderState} instance to the given {@link BufferBuilder}
     * and prepare to start drawing vertices for the given <code>mode</code> and {@link VertexFormat}.
     *
     * @param mode   The GL integer mode. E.g: GL_QUADS, GL_TRIANGLES, and so on.
     * @param format The {@link VertexFormat}.
     * @param buffer The {@link BufferBuilder} to bind to.
     * @return The same {@link BufferBuilder} that was preplaceded in.
     */
    public BufferBuilder startDrawing(int mode, VertexFormat format, BufferBuilder buffer) {
        buffer.begin(mode, format);
        bind(buffer);
        return buffer;
    }

    /**
     * Bind this {@link CCRenderState} instance to the given {@link BufferBuilder}.
     *
     * @param r The {@link BufferBuilder}.
     */
    public void bind(BufferBuilder r) {
        bind(r, r.getVertexFormat());
    }

    /**
     * Bind this {@link CCRenderState} to the given {@link IVertexBuilder} and {@link VertexFormat}.
     *
     * @param consumer The {@link IVertexBuilder} to bind to.
     * @param format   The {@link VertexFormat} of the {@link IVertexBuilder}.
     */
    public void bind(IVertexBuilder consumer, VertexFormat format) {
        r = consumer;
        fmt = format;
        cFmt = CachedFormat.lookup(format);
    }

    /**
     * Bind this {@link CCRenderState} to the given {@link RenderType}.
     *
     * @param renderType The {@link RenderType} to bind to.
     * @param getter     The {@link IRenderTypeBuffer} instance.
     */
    public void bind(RenderType renderType, IRenderTypeBuffer getter) {
        bind(getter.getBuffer(renderType), renderType.getVertexFormat());
    }

    /**
     * Bind this {@link CCRenderState} to the given {@link RenderType}, applying
     * the given MatrixStack.
     *
     * @param renderType The {@link RenderType} to bind to.
     * @param getter     The {@link IRenderTypeBuffer} instance.
     * @param mStack     The {@link MatrixStack} to apply.
     */
    public void bind(RenderType renderType, IRenderTypeBuffer getter, MatrixStack mStack) {
        bind(new TransformingVertexBuilder(getter.getBuffer(renderType), mStack), renderType.getVertexFormat());
    }

    /**
     * Bind this {@link CCRenderState} to the given {@link RenderType}, applying
     * the given MatrixStack.
     *
     * @param renderType The {@link RenderType} to bind to.
     * @param getter     The {@link IRenderTypeBuffer} instance.
     * @param mat        The {@link Matrix4} to apply.
     */
    public void bind(RenderType renderType, IRenderTypeBuffer getter, Matrix4 mat) {
        bind(new TransformingVertexBuilder(getter.getBuffer(renderType), mat), renderType.getVertexFormat());
    }

    /**
     * Resets this {@link CCRenderState} instance's pipeline and internal state.
     */
    public void reset() {
        model = null;
        pipeline.reset();
        computeLighting = true;
        colour = baseColour = alphaOverride = -1;
    }

    public void preRenderWorld(IBlockDisplayReader world, BlockPos pos) {
        this.reset();
        this.colour = 0xFFFFFFFF;
        this.setBrightness(world, pos);
    }

    public void setPipeline(IVertexOperation... ops) {
        pipeline.setPipeline(ops);
    }

    public void setPipeline(IVertexSource model, int start, int end, IVertexOperation... ops) {
        pipeline.reset();
        setModel(model, start, end);
        pipeline.setPipeline(ops);
    }

    public void bindModel(IVertexSource model) {
        if (this.model != model) {
            this.model = model;
            pipeline.rebuild();
        }
    }

    public void setModel(IVertexSource source) {
        setModel(source, 0, source.getVertices().length);
    }

    public void setModel(IVertexSource source, int start, int end) {
        bindModel(source);
        setVertexRange(start, end);
    }

    public void setVertexRange(int start, int end) {
        firstVertexIndex = start;
        lastVertexIndex = end;
    }

    public void render(IVertexOperation... ops) {
        setPipeline(ops);
        render();
    }

    public void render() {
        Vertex5[] verts = model.getVertices();
        for (vertexIndex = firstVertexIndex; vertexIndex < lastVertexIndex; vertexIndex++) {
            model.prepareVertex(this);
            vert.set(verts[vertexIndex]);
            runPipeline();
            writeVert();
        }
    }

    public void runPipeline() {
        pipeline.operate();
    }

    public void writeVert() {
        if (r instanceof ISpriteAwareVertexBuilder) {
            ((ISpriteAwareVertexBuilder) r).sprite(sprite);
        }
        ImmutableList<VertexFormatElement> elements = fmt.getElements();
        for (int e = 0; e < elements.size(); e++) {
            VertexFormatElement fmte = elements.get(e);
            switch(fmte.getUsage()) {
                case POSITION:
                    r.pos(vert.vec.x, vert.vec.y, vert.vec.z);
                    break;
                case UV:
                    int idx = fmte.getIndex();
                    switch(idx) {
                        case 0:
                            r.tex((float) vert.uv.u, (float) vert.uv.v);
                            break;
                        case 1:
                            r.overlay(overlay);
                            break;
                        case 2:
                            r.lightmap(brightness);
                            break;
                    }
                    break;
                case COLOR:
                    if (r instanceof BufferBuilder && ((BufferBuilder) r).defaultColor) {
                        // -_- replaced mojang..
                        ((BufferBuilder) r).nextVertexFormatIndex();
                    } else {
                        r.color(colour >>> 24, colour >> 16 & 0xFF, colour >> 8 & 0xFF, alphaOverride >= 0 ? alphaOverride : colour & 0xFF);
                    }
                    break;
                case NORMAL:
                    r.normal((float) normal.x, (float) normal.y, (float) normal.z);
                    break;
                case PADDING:
                    break;
                default:
                    throw new UnsupportedOperationException("Generic vertex format element");
            }
        }
        r.endVertex();
    }

    @Deprecated
    public void pushColour() {
        GlStateManager.color4f((colour >>> 24) / 255F, (colour >> 16 & 0xFF) / 255F, (colour >> 8 & 0xFF) / 255F, (alphaOverride >= 0 ? alphaOverride : colour & 0xFF) / 255F);
    }

    public void setBrightness(IBlockDisplayReader world, BlockPos pos) {
        brightness = WorldRenderer.getPackedLightmapCoords(world, world.getBlockState(pos), pos);
    }

    public void setBrightness(Enreplacedy enreplacedy, float frameDelta) {
        brightness = Minecraft.getInstance().getRenderManager().getPackedLight(enreplacedy, frameDelta);
    }

    public void setFluidColour(FluidStack fluidStack) {
        setFluidColour(fluidStack, 0xFF);
    }

    public void setFluidColour(FluidStack fluidStack, int alpha) {
        this.baseColour = fluidStack.getFluid().getAttributes().getColor(fluidStack) << 8 | alpha;
    }

    public void setColour(Colour colour) {
        this.colour = colour.rgba();
    }

    public ColourRGBA getColour() {
        return new ColourRGBA(colour);
    }

    public IVertexBuilder getConsumer() {
        return r;
    }

    public VertexFormat getVertexFormat() {
        return fmt;
    }

    public void draw() {
        Tessellator.getInstance().draw();
    }
}