Search in sources :

Example 1 with VertexBuffer

use of net.minecraft.client.renderer.vertex.VertexBuffer in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class GibsModelRegistry method renderGibsModel.

/**
 * Note this method is very unfinished, and really is only confirmed to work on obj models.
 */
public static void renderGibsModel(String name, int brightness) {
    if (!NAMES_TO_RESOURCE_LOCATION.containsKey(name)) {
        throw new IllegalArgumentException("No registed gibs model with the name " + name + "!");
    }
    // If we don't have an IBakedModel for this gib then make one.
    if (!NAMES_TO_BAKED_MODELS.containsKey(name)) {
        ResourceLocation location = NAMES_TO_RESOURCE_LOCATION.get(name);
        try {
            IModel model = ModelLoaderRegistry.getModel(location).process(FLIP_UV_CUSTOM_DATA);
            IBakedModel bakedModel = model.bake(model.getDefaultState(), DefaultVertexFormats.ITEM, ModelLoader.defaultTextureGetter());
            NAMES_TO_BAKED_MODELS.put(name, bakedModel);
        } catch (Exception e) {
            System.err.println("No model found for: " + name);
            e.printStackTrace();
            throw new IllegalStateException();
        }
    }
    // Then if we don't have a BufferBuilder.State for this gib then make one.
    if (!NAMES_TO_BUFFER_STATES.containsKey(name)) {
        FastBlockModelRenderer.VERTEX_BUILDER.begin(7, DefaultVertexFormats.BLOCK);
        BlockRendererDispatcher blockrendererdispatcher = Minecraft.getMinecraft().getBlockRendererDispatcher();
        IBakedModel modelFromState = NAMES_TO_BAKED_MODELS.get(name);
        blockrendererdispatcher.getBlockModelRenderer().renderModel(Minecraft.getMinecraft().world, modelFromState, Blocks.AIR.getDefaultState(), offsetPos, FastBlockModelRenderer.VERTEX_BUILDER, false, 0);
        BufferBuilder.State bufferState = FastBlockModelRenderer.VERTEX_BUILDER.getVertexState();
        FastBlockModelRenderer.VERTEX_BUILDER.finishDrawing();
        FastBlockModelRenderer.VERTEX_BUILDER.reset();
        NAMES_TO_BUFFER_STATES.put(name, bufferState);
    }
    // Then if we don't have a brightness to VertexBuffer map for this gib then make one.
    if (!NAMES_AND_BRIGHTNESS_TO_VERTEX_BUFFER.containsKey(name)) {
        NAMES_AND_BRIGHTNESS_TO_VERTEX_BUFFER.put(name, new HashMap<Integer, VertexBuffer>());
    }
    // Then if the brightness to VertexBuffer map doesn't have a VertexBuffer for the given brightness then make one.
    if (!NAMES_AND_BRIGHTNESS_TO_VERTEX_BUFFER.get(name).containsKey(brightness)) {
        BufferBuilder.State bufferState = NAMES_TO_BUFFER_STATES.get(name);
        FastBlockModelRenderer.VERTEX_BUILDER.setTranslation(0, 0, 0);
        FastBlockModelRenderer.VERTEX_BUILDER.begin(7, DefaultVertexFormats.BLOCK);
        FastBlockModelRenderer.VERTEX_BUILDER.setVertexState(bufferState);
        // This code adjusts the brightness of the model rendered.
        int j = FastBlockModelRenderer.VERTEX_BUILDER.vertexFormat.getSize() >> 2;
        int cont = FastBlockModelRenderer.VERTEX_BUILDER.getVertexCount();
        int offsetUV = FastBlockModelRenderer.VERTEX_BUILDER.vertexFormat.getUvOffsetById(1) / 4;
        int bufferNextSize = FastBlockModelRenderer.VERTEX_BUILDER.vertexFormat.getIntegerSize();
        // Set the brightness manually, so that we don't have to create a new IBakedModel for each brightness.
        for (int contont = 0; contont < cont; contont += 4) {
            try {
                int i = (contont) * bufferNextSize + offsetUV;
                FastBlockModelRenderer.VERTEX_BUILDER.rawIntBuffer.put(i, brightness);
                FastBlockModelRenderer.VERTEX_BUILDER.rawIntBuffer.put(i + j, brightness);
                FastBlockModelRenderer.VERTEX_BUILDER.rawIntBuffer.put(i + j * 2, brightness);
                FastBlockModelRenderer.VERTEX_BUILDER.rawIntBuffer.put(i + j * 3, brightness);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        VertexBuffer gibVertexBuffer = new VertexBuffer(DefaultVertexFormats.BLOCK);
        // Now that the VERTEX_BUILDER has been filled with all the render data, we must
        // upload it to the gpu.
        // The VERTEX_UPLOADER copies the state of the VERTEX_BUILDER to
        // blockVertexBuffer, and then uploads it to the gpu.
        FastBlockModelRenderer.VERTEX_BUILDER.finishDrawing();
        FastBlockModelRenderer.VERTEX_BUILDER.reset();
        // Copy the data over from the BufferBuilder into the VertexBuffer, and then
        // upload that data to the gpu memory.
        gibVertexBuffer.bufferData(FastBlockModelRenderer.VERTEX_BUILDER.getByteBuffer());
        NAMES_AND_BRIGHTNESS_TO_VERTEX_BUFFER.get(name).put(brightness, gibVertexBuffer);
    }
    // Finally, once past all these checks, we can render it.
    GlStateManager.pushMatrix();
    GlStateManager.translate(-offsetPos.getX(), -offsetPos.getY(), -offsetPos.getZ());
    FastBlockModelRenderer.renderVertexBuffer(NAMES_AND_BRIGHTNESS_TO_VERTEX_BUFFER.get(name).get(brightness));
    GlStateManager.popMatrix();
}
Also used : IModel(net.minecraftforge.client.model.IModel) VertexBuffer(net.minecraft.client.renderer.vertex.VertexBuffer) BufferBuilder(net.minecraft.client.renderer.BufferBuilder) ResourceLocation(net.minecraft.util.ResourceLocation) IBakedModel(net.minecraft.client.renderer.block.model.IBakedModel) BlockRendererDispatcher(net.minecraft.client.renderer.BlockRendererDispatcher)

Example 2 with VertexBuffer

use of net.minecraft.client.renderer.vertex.VertexBuffer in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class FastBlockModelRenderer method renderBlockModelHighQualityHighRam.

private static void renderBlockModelHighQualityHighRam(Tessellator tessellator, World world, IBlockState blockstateToRender, int brightness) {
    if (!blockstateToVertexData.containsKey(blockstateToRender)) {
        generateRenderDataFor(world, blockstateToRender);
    }
    // there isn't one we will create it, then render.
    if (!blockstateBrightnessToVertexBuffer.containsKey(blockstateToRender)) {
        blockstateBrightnessToVertexBuffer.put(blockstateToRender, new HashMap<Integer, VertexBuffer>());
    }
    if (!blockstateBrightnessToVertexBuffer.get(blockstateToRender).containsKey(brightness)) {
        // We have to create the VertexBuffer
        BufferBuilder.State bufferBuilderState = blockstateToVertexData.get(blockstateToRender);
        VERTEX_BUILDER.setTranslation(0, 0, 0);
        VERTEX_BUILDER.begin(7, DefaultVertexFormats.BLOCK);
        VERTEX_BUILDER.setVertexState(bufferBuilderState);
        // This code adjusts the brightness of the model rendered.
        int j = VERTEX_BUILDER.vertexFormat.getSize() >> 2;
        int cont = VERTEX_BUILDER.getVertexCount();
        int offsetUV = VERTEX_BUILDER.vertexFormat.getUvOffsetById(1) / 4;
        int bufferNextSize = VERTEX_BUILDER.vertexFormat.getIntegerSize();
        for (int contont = 0; contont < cont; contont += 4) {
            try {
                int i = (contont) * bufferNextSize + offsetUV;
                VERTEX_BUILDER.rawIntBuffer.put(i, brightness);
                VERTEX_BUILDER.rawIntBuffer.put(i + j, brightness);
                VERTEX_BUILDER.rawIntBuffer.put(i + j * 2, brightness);
                VERTEX_BUILDER.rawIntBuffer.put(i + j * 3, brightness);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        VertexBuffer blockVertexBuffer = new VertexBuffer(DefaultVertexFormats.BLOCK);
        // Now that the VERTEX_BUILDER has been filled with all the render data, we must
        // upload it to the gpu.
        // The VERTEX_UPLOADER copies the state of the VERTEX_BUILDER to
        // blockVertexBuffer, and then uploads it to the gpu.
        VERTEX_BUILDER.finishDrawing();
        VERTEX_BUILDER.reset();
        blockVertexBuffer.bufferData(VERTEX_BUILDER.getByteBuffer());
        // Put the VertexBuffer for that data into the Map for future rendering.
        blockstateBrightnessToVertexBuffer.get(blockstateToRender).put(brightness, blockVertexBuffer);
    }
    // Just to test the look of the State in case I ever need to.
    if (false) {
        BufferBuilder.State bufferBuilderState = blockstateToVertexData.get(blockstateToRender);
        tessellator.getBuffer().begin(7, DefaultVertexFormats.BLOCK);
        tessellator.getBuffer().setVertexState(bufferBuilderState);
        tessellator.getBuffer().finishDrawing();
        tessellator.draw();
    }
    renderVertexBuffer(blockstateBrightnessToVertexBuffer.get(blockstateToRender).get(brightness));
}
Also used : VertexBuffer(net.minecraft.client.renderer.vertex.VertexBuffer) BufferBuilder(net.minecraft.client.renderer.BufferBuilder)

Aggregations

BufferBuilder (net.minecraft.client.renderer.BufferBuilder)2 VertexBuffer (net.minecraft.client.renderer.vertex.VertexBuffer)2 BlockRendererDispatcher (net.minecraft.client.renderer.BlockRendererDispatcher)1 IBakedModel (net.minecraft.client.renderer.block.model.IBakedModel)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 IModel (net.minecraftforge.client.model.IModel)1