net.minecraft.client.renderer.vertex.VertexFormatElement Java Examples

The following examples show how to use net.minecraft.client.renderer.vertex.VertexFormatElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BWBakedModel.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public BWBakedModel(IBakedModel wrapped, VertexFormat format, Optional<IBlockState> state, long rand) {
	this.wrapped = wrapped;
	this.format = format;
	this.matrix.translate(-0.5, -0.5, -0.5);
	this.blockState = state;
	this.rand = rand;

	if (!format.getElements().stream().anyMatch(VertexFormatElement::isPositionElement))
		return; // VertexFormat doesn't have a position

	Arrays.stream(Direction.values())
		.map(this::getQuads)
		.flatMap(Collection::stream)
		.map(this::quadToFace)
		.forEachOrdered(faces::add);
}
 
Example #2
Source File: BWBakedModel.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public BWBakedModel(@SuppressWarnings("deprecation") net.minecraft.client.resources.model.IBakedModel wrapped, VertexFormat format) {
	this.wrapped = wrapped;
	this.format = format;
	this.matrix.translate(-0.5, -0.5, -0.5);

	if (!((List<VertexFormatElement>) format.getElements()).stream().anyMatch(VertexFormatElement::isPositionElement))
		return; // VertexFormat doesn't have a position

	getGeneralQuads().stream()
		.map(this::quadToFace)
		.forEachOrdered(faces::add);

	Arrays.stream(Direction.VALID_DIRECTIONS)
		.map(this::getFaceQuads)
		.flatMap(Collection::stream)
		.map(this::quadToFace)
		.forEachOrdered(faces::add);
}
 
Example #3
Source File: AdvCCRSConsumer.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void put(int e, float... data) {
    VertexFormat format = getVertexFormat();

    VertexFormatElement fmte = format.getElement(e);
    switch (fmte.getUsage()) {
        case POSITION:
            ccrs.vert.vec.set(data).apply(translation);
            break;
        case UV:
            if (fmte.getIndex() == 0) {
                ccrs.vert.uv.set(data[0], data[1]);
            } else {
                ccrs.brightness = (int) (data[1] * 0xFFFF / 2) << 16 | (int) (data[0] * 0xFFFF / 2);
            }
            break;
        case COLOR:
            ccrs.colour = Colour.packRGBA(data);
            break;
        case NORMAL:
            ccrs.normal.set(data);
            break;
        case PADDING:
            break;
        default:
            throw new UnsupportedOperationException("Generic vertex format element");
    }
    if (e == format.getElementCount() - 1) {
        ccrs.writeVert();
    }
}
 
Example #4
Source File: RenderGlobalSchematic.java    From litematica with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void renderBlockOverlayBuffers(OverlayRenderType type)
{
    this.mc.entityRenderer.enableLightmap();

    if (OpenGlHelper.useVbo())
    {
        GlStateManager.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        GlStateManager.glEnableClientState(GL11.GL_COLOR_ARRAY);
    }

    this.renderContainer.renderBlockOverlays(type);

    if (OpenGlHelper.useVbo())
    {
        for (VertexFormatElement element : DefaultVertexFormats.POSITION_COLOR.getElements())
        {
            VertexFormatElement.EnumUsage usage = element.getUsage();

            switch (usage)
            {
                case POSITION:
                    GlStateManager.glDisableClientState(GL11.GL_VERTEX_ARRAY);
                    break;
                case UV:
                    OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit + element.getIndex());
                    GlStateManager.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
                    OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
                    break;
                case COLOR:
                    GlStateManager.glDisableClientState(GL11.GL_COLOR_ARRAY);
                    GlStateManager.resetColor();
                default:
            }
        }
    }

    this.mc.entityRenderer.disableLightmap();
}
 
Example #5
Source File: VertexDataUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Gets the position for the element 'position' in the elements list for use in LightUtil.pack/unpack for a given format.
 *
 * @param format The format.
 * @return The element position, -1 if it does not exist.
 */
public static int getPositionElement(VertexFormat format) {
    List<VertexFormatElement> elements = format.getElements();
    for (int e = 0; e < elements.size(); e++) {
        if (elements.get(e).isPositionElement()) {
            return e;
        }
    }
    return -1;
}
 
Example #6
Source File: VertexDataUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Gets the position for the element 'normal' in the elements list for use in LightUtil.pack/unpack for a given format.
 *
 * @param format The format.
 * @return The element position, -1 if it does not exist.
 */
public static int getNormalElement(VertexFormat format) {
    List<VertexFormatElement> elements = format.getElements();
    for (int e = 0; e < elements.size(); e++) {
        if (elements.get(e).getUsage() == Usage.NORMAL) {
            return e;
        }
    }
    return -1;
}
 
Example #7
Source File: VertexDataUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Gets the position for the element 'uv' in the elements list for use in LightUtil.pack/unpack for a given format.
 *
 * @param format The format.
 * @return The element position, -1 if it does not exist.
 */
public static int getUVElement(VertexFormat format) {
    List<VertexFormatElement> elements = format.getElements();
    for (int e = 0; e < elements.size(); e++) {
        if (elements.get(e).getUsage() == Usage.UV && elements.get(e).getIndex() == 0) {
            return e;
        }
    }
    return -1;
}
 
Example #8
Source File: VertexDataUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Gets the position for the element provided in the elements list for use in LightUtil.pack/unpack for a given format.
 *
 * @param format  The format.
 * @param element THe element to get.
 * @return The element position, -1 if it does not exist.
 */
public static int getElement(VertexFormat format, VertexFormatElement element) {
    List<VertexFormatElement> elements = format.getElements();
    for (int e = 0; e < elements.size(); e++) {
        if (elements.get(e).equals(element)) {
            return e;
        }
    }
    return -1;
}
 
Example #9
Source File: VertexDataUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void fullyPackQuads(int[] packedData, float[][][] unpackedData, VertexFormat format) {
    List<VertexFormatElement> elements = format.getElements();
    for (int e = 0; e < elements.size(); e++) {
        for (int v = 0; v < 4; v++) {
            LightUtil.pack(unpackedData[v][e], packedData, format, v, e);
        }
    }
}
 
Example #10
Source File: VertexDataUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void fullyUnPackQuads(int[] packedData, float[][][] unpackedData, VertexFormat format) {
    List<VertexFormatElement> elements = format.getElements();
    for (int e = 0; e < elements.size(); e++) {
        for (int v = 0; v < 4; v++) {
            LightUtil.unpack(packedData, unpackedData[v][e], format, v, e);
        }
    }
}
 
Example #11
Source File: VertexDataUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static float[][] unpackElements(int[] packed, VertexFormat format, VertexFormatElement element) {
    float[][] data = new float[4][4];
    int e = getElement(format, element);
    for (int v = 0; v < 4; v++) {
        LightUtil.unpack(packed, data[v], format, v, e);
    }
    return data;
}
 
Example #12
Source File: RenderGlobalSchematic.java    From litematica with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void renderBlockLayer(BlockRenderLayer layer)
{
    this.mc.entityRenderer.enableLightmap();

    if (OpenGlHelper.useVbo())
    {
        GlStateManager.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
        GlStateManager.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
        OpenGlHelper.setClientActiveTexture(OpenGlHelper.lightmapTexUnit);
        GlStateManager.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
        OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
        GlStateManager.glEnableClientState(GL11.GL_COLOR_ARRAY);
    }

    this.renderContainer.renderChunkLayer(layer);

    if (OpenGlHelper.useVbo())
    {
        for (VertexFormatElement vertexformatelement : DefaultVertexFormats.BLOCK.getElements())
        {
            VertexFormatElement.EnumUsage vertexformatelement$enumusage = vertexformatelement.getUsage();
            int index = vertexformatelement.getIndex();

            switch (vertexformatelement$enumusage)
            {
                case POSITION:
                    GlStateManager.glDisableClientState(GL11.GL_VERTEX_ARRAY);
                    break;
                case UV:
                    OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit + index);
                    GlStateManager.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
                    OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
                    break;
                case COLOR:
                    GlStateManager.glDisableClientState(GL11.GL_COLOR_ARRAY);
                    GlStateManager.resetColor();
                default:
            }
        }
    }

    this.mc.entityRenderer.disableLightmap();
}
 
Example #13
Source File: FastBlockModelRenderer.java    From Valkyrien-Skies with Apache License 2.0 4 votes vote down vote up
public static void renderVertexBuffer(VertexBuffer vertexBuffer) {
    // Check if optifine shaders are currently loaded.
    final boolean areOptifineShadersEnabled = GibsModelRegistry.isOptifineShadersEnabled();

    GlStateManager.pushMatrix();
    GlStateManager.resetColor();

    GlStateManager.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
    GlStateManager.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
    OpenGlHelper.setClientActiveTexture(OpenGlHelper.lightmapTexUnit);
    GlStateManager.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
    OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
    GlStateManager.glEnableClientState(GL11.GL_COLOR_ARRAY);

    // Extra OpenGL states that must be enabled when shaders are enabled.
    if (areOptifineShadersEnabled) {
        GL11.glEnableClientState(32885);
        GL20.glEnableVertexAttribArray(11);
        GL20.glEnableVertexAttribArray(12);
        GL20.glEnableVertexAttribArray(10);
    }

    GlStateManager.pushMatrix();
    vertexBuffer.bindBuffer();

    // Even more OpenGL states that must be enabled when shaders are enabled.
    if (areOptifineShadersEnabled) {
        int vertexSizeI = 14;
        GL11.glVertexPointer(3, 5126, 56, 0L);
        GL11.glColorPointer(4, 5121, 56, 12L);
        GL11.glTexCoordPointer(2, 5126, 56, 16L);
        OpenGlHelper.setClientActiveTexture(OpenGlHelper.lightmapTexUnit);
        GL11.glTexCoordPointer(2, 5122, 56, 24L);
        OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
        GL11.glNormalPointer(5120, 56, 28L);
        GL20.glVertexAttribPointer(11, 2, 5126, false, 56, 32L);
        GL20.glVertexAttribPointer(12, 4, 5122, false, 56, 40L);
        GL20.glVertexAttribPointer(10, 3, 5122, false, 56, 48L);
    } else {

        GlStateManager.glVertexPointer(3, 5126, 28, 0);
        GlStateManager.glColorPointer(4, 5121, 28, 12);
        GlStateManager.glTexCoordPointer(2, 5126, 28, 16);
        OpenGlHelper.setClientActiveTexture(OpenGlHelper.lightmapTexUnit);
        GlStateManager.glTexCoordPointer(2, 5122, 28, 24);
        OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
    }

    vertexBuffer.drawArrays(7);
    GlStateManager.popMatrix();
    vertexBuffer.unbindBuffer();
    GlStateManager.resetColor();

    for (VertexFormatElement vertexformatelement : DefaultVertexFormats.BLOCK.getElements()) {
        VertexFormatElement.EnumUsage vertexformatelement$enumusage = vertexformatelement
            .getUsage();
        int i = vertexformatelement.getIndex();

        switch (vertexformatelement$enumusage) {
            case POSITION:
                GlStateManager.glDisableClientState(32884);
                break;
            case UV:
                OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit + i);
                GlStateManager.glDisableClientState(32888);
                OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
                break;
            case COLOR:
                GlStateManager.glDisableClientState(32886);
                GlStateManager.resetColor();
        }
    }

    OpenGlHelper.glBindBuffer(OpenGlHelper.GL_ARRAY_BUFFER, 0);

    // Finally disable some of those extra OpenGL states that were be enabled due to shaders.
    if (areOptifineShadersEnabled) {
        GL11.glDisableClientState(32885);
        GL20.glDisableVertexAttribArray(11);
        GL20.glDisableVertexAttribArray(12);
        GL20.glDisableVertexAttribArray(10);
    }

    GlStateManager.resetColor();
    GlStateManager.popMatrix();
}
 
Example #14
Source File: CachedFormat.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Caches the vertex format element indexes for efficiency.
 *
 * @param format The format.
 */
public CachedFormat(VertexFormat format) {
    this.format = format;
    List<VertexFormatElement> elements = format.getElements();
    elementCount = elements.size();
    for (int i = 0; i < elementCount; i++) {
        VertexFormatElement element = elements.get(i);
        switch (element.getUsage()) {
            case POSITION:
                if (hasPosition) {
                    throw new IllegalStateException("Found 2 position elements..");
                }
                hasPosition = true;
                positionIndex = i;
                break;
            case NORMAL:
                if (hasNormal) {
                    throw new IllegalStateException("Found 2 normal elements..");
                }
                hasNormal = true;
                normalIndex = i;
                break;
            case COLOR:
                if (hasColor) {
                    throw new IllegalStateException("Found 2 color elements..");
                }
                hasColor = true;
                colorIndex = i;
                break;
            case UV:
                switch (element.getIndex()) {
                    case 0:
                        if (hasUV) {
                            throw new IllegalStateException("Found 2 UV elements..");
                        }
                        hasUV = true;
                        uvIndex = i;
                        break;
                    case 1:
                        if (hasOverlay) {
                            throw new IllegalStateException("Found 2 Overlay elements..");
                        }
                        hasOverlay = true;
                        overlayIndex = i;
                        break;
                    case 2:
                        if (hasLightMap) {
                            throw new IllegalStateException("Found 2 LightMap elements..");
                        }
                        hasLightMap = true;
                        lightMapIndex = i;
                        break;
                }
                break;
        }
    }
}
 
Example #15
Source File: BWBakedModel.java    From NOVA-Core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Face quadToFace(BakedQuad quad) {
	Face face = new Face();
	final VertexFormat format = quad.getFormat();

	int[] data = quad.getVertexData();
	Optional<TextureAtlasSprite> texture = Optional.ofNullable(quad.getSprite() == null ? wrapped.getParticleTexture() : quad.getSprite());

	final Optional<VertexFormatElement> posElement = ((Collection<VertexFormatElement>)format.getElements()).stream()
		.filter(VertexFormatElement::isPositionElement)
		.findFirst();

	final Optional<VertexFormatElement> uvElement = ((Collection<VertexFormatElement>)format.getElements()).stream()
		.filter(vfe -> vfe.getUsage() == VertexFormatElement.EnumUsage.UV)
		.findFirst();

	face.texture = texture
		.filter(t -> uvElement.isPresent())
		.map(TextureAtlasSprite::getIconName)
		.map(ResourceLocation::new)
		.map(AssetConverter.instance()::toNovaTexture);

	// `VertexFormat` offsets are for a `ByteBuffer`
	// `data` is an int array, so we convert it

	// TODO: support offsets which are not divisible by four
	final int posOffset = posElement.map(VertexFormatElement::getIndex).map(i -> i / 4).orElse(-1);
	final int uvOffset = uvElement.map(VertexFormatElement::getIndex).map(i -> i / 4).orElse(-1);
	final int colorOffset = format.hasColor() ? (format.getColorOffset() / 4) : -1;
	final int normalOffset = format.hasNormal() ? (format.getNormalOffset() / 4) : -1;

	for (int i = 0; i < data.length; i += 7) {
		Vector3D pos = posElement.isPresent() ? new Vector3D(
			Float.intBitsToFloat(data[i + posOffset]),
			Float.intBitsToFloat(data[i + posOffset + 1]),
			Float.intBitsToFloat(data[i + posOffset + 2])) : Vector3D.ZERO;

		Vector2D uv = uvElement.isPresent() ? new Vector2D(
			deinterpolateU(Float.intBitsToFloat(data[i + uvOffset]), texture),
			deinterpolateV(Float.intBitsToFloat(data[i + uvOffset + 1]), texture)) : Vector2D.ZERO;

		Vertex vertex = new Vertex(pos, uv);
		if (format.hasColor()) {
			vertex.color = Color.argb(data[i + colorOffset]);
		}

		Optional<Vector3D> normal = Optional.empty();
		if (format.hasNormal()) {
			int mergedNormal = data[i + normalOffset];
			if (mergedNormal != 0)
				normal = Optional.of(new Vector3D(((byte)(mergedNormal & 0xFF)) / 127D,
					((byte)((mergedNormal >> 8) & 0xFF)) / 127D,
					((byte)((mergedNormal >> 16) & 0xFF)) / 127D));
		}

		if (format.hasNormal())
			vertex.normal = normal;
		face.drawVertex(vertex);
	}
	face.normal = Vector3DUtil.calculateNormal(face);
	return face;
}
 
Example #16
Source File: BWBakedModel.java    From NOVA-Core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public Face quadToFace(BakedQuad quad) {
	Face face = new Face();
	final VertexFormat format = this.format; // 1.11.2 stores VertexFormat on a per-quad basis.

	int[] data = quad.getVertexData();
	Optional<TextureAtlasSprite> texture = Optional.ofNullable(wrapped.getTexture());

	final Optional<VertexFormatElement> posElement = ((Collection<VertexFormatElement>)format.getElements()).stream()
		.filter(VertexFormatElement::isPositionElement)
		.findFirst();

	final Optional<VertexFormatElement> uvElement = ((Collection<VertexFormatElement>)format.getElements()).stream()
		.filter(vfe -> vfe.getUsage() == VertexFormatElement.EnumUsage.UV)
		.findFirst();

	face.texture = texture
		.filter(t -> uvElement.isPresent())
		.map(TextureAtlasSprite::getIconName)
		.map(ResourceLocation::new)
		.map(AssetConverter.instance()::toNovaTexture);

	// `VertexFormat` offsets are for a `ByteBuffer`
	// `data` is an int array, so we convert the offsets

	// TODO: support offsets which are not divisible by four
	final int posOffset = posElement.map(VertexFormatElement::getOffset).map(i -> i / 4).orElse(-1);
	final int uvOffset = uvElement.map(VertexFormatElement::getOffset).map(i -> i / 4).orElse(-1);
	final int colorOffset = format.hasColor() ? (format.getColorOffset() / 4) : -1;
	final int normalOffset = format.hasNormal() ? (format.getNormalOffset() / 4) : -1;

	for (int i = 0; i < data.length; i += 7) {
		Vector3D pos = posElement.isPresent() ? new Vector3D(
			Float.intBitsToFloat(data[i + posOffset]),
			Float.intBitsToFloat(data[i + posOffset + 1]),
			Float.intBitsToFloat(data[i + posOffset + 2])) : Vector3D.ZERO;

		Vector2D uv = uvElement.isPresent() ? new Vector2D(
			deinterpolateU(Float.intBitsToFloat(data[i + uvOffset]), texture),
			deinterpolateV(Float.intBitsToFloat(data[i + uvOffset + 1]), texture)) : Vector2D.ZERO;

		Vertex vertex = new Vertex(pos, uv);
		if (format.hasColor()) {
			if (DefaultVertexFormats.BLOCK.equals(format))
				vertex.color = Color.argb(data[i + colorOffset]);
			else
				vertex.color = Color.rgba(data[i + colorOffset]);
		}

		Optional<Vector3D> normal = Optional.empty();
		if (format.hasNormal()) {
			int mergedNormal = data[i + normalOffset];
			if (mergedNormal != 0)
				normal = Optional.of(new Vector3D(((byte)(mergedNormal & 0xFF)) / 127D,
					((byte)((mergedNormal >> 8) & 0xFF)) / 127D,
					((byte)((mergedNormal >> 16) & 0xFF)) / 127D));
		}

		if (format.hasNormal())
			vertex.normal = normal;
		face.drawVertex(vertex);
	}
	face.normal = Vector3DUtil.calculateNormal(face);
	return face;
}