Skip to content

Instantly share code, notes, and snippets.

@CptSpaceToaster
Created July 27, 2014 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CptSpaceToaster/a192fe010e3bd1e6e6c4 to your computer and use it in GitHub Desktop.
Save CptSpaceToaster/a192fe010e3bd1e6e6c4 to your computer and use it in GitHub Desktop.
package net.minecraft.client.renderer;
import coloredlightscore.src.helper.CLTessellatorHelper;
import java.nio.ByteOrder;
import java.util.Comparator;
import java.util.PriorityQueue;
import net.minecraft.client.util.QuadComparator;
import net.minecraft.client.shader.TesselatorVertexState;
import org.lwjgl.opengl.GL11;
import java.nio.ShortBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ByteBuffer;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import coloredlightscore.src.types.CLTessellatorInterface;
@SideOnly(Side.CLIENT)
public class Tessellator implements CLTessellatorInterface
{
private static int nativeBufferSize;
private static int trivertsInBuffer;
public static boolean renderingWorldRenderer;
public boolean defaultTexture;
private int rawBufferSize;
public int textureID;
private static ByteBuffer byteBuffer;
private static IntBuffer intBuffer;
private static FloatBuffer floatBuffer;
private static ShortBuffer shortBuffer;
public int[] rawBuffer;
public int vertexCount;
public double textureU;
public double textureV;
public int brightness;
public int color;
public boolean hasColor;
public boolean hasTexture;
public boolean hasBrightness;
public boolean hasNormals;
public int rawBufferIndex;
public int addedVertices;
private boolean isColorDisabled;
public int drawMode;
public double xOffset;
public double yOffset;
public double zOffset;
public int normal;
public static final Tessellator instance;
public boolean isDrawing;
private int bufferSize;
private static final String __OBFID = "CL_00000960";
private Tessellator(final int p_i1250_1_) {
super();
this.defaultTexture = false;
this.rawBufferSize = 0;
this.textureID = 0;
}
public Tessellator() {
super();
this.defaultTexture = false;
this.rawBufferSize = 0;
this.textureID = 0;
}
public int draw() {
if (!this.isDrawing) {
throw new IllegalStateException("Not tesselating!");
}
this.isDrawing = false;
int offs = 0;
while (offs < this.vertexCount) {
final int vtc = Math.min(this.vertexCount - offs, Tessellator.nativeBufferSize / 40);
Tessellator.intBuffer.clear();
Tessellator.intBuffer.put(this.rawBuffer, offs * 10, vtc * 10);
Tessellator.byteBuffer.position(0);
Tessellator.byteBuffer.limit(vtc * 40);
offs += vtc;
if (this.hasTexture) {
Tessellator.floatBuffer.position(3);
GL11.glTexCoordPointer(2, 40, Tessellator.floatBuffer);
GL11.glEnableClientState(32888);
}
if (this.hasBrightness) {
OpenGlHelper.setClientActiveTexture(OpenGlHelper.lightmapTexUnit);
Tessellator.shortBuffer.position(14);
GL11.glTexCoordPointer(2, 40, Tessellator.shortBuffer);
OpenGlHelper.setClientActiveTexture(33986);
Tessellator.shortBuffer.position(15);
GL11.glTexCoordPointer(3, 40, Tessellator.shortBuffer);
GL11.glEnableClientState(32888);
OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
}
if (this.hasColor) {
Tessellator.byteBuffer.position(20);
GL11.glColorPointer(4, true, 40, Tessellator.byteBuffer);
GL11.glEnableClientState(32886);
}
if (this.hasNormals) {
Tessellator.byteBuffer.position(24);
GL11.glNormalPointer(40, Tessellator.byteBuffer);
GL11.glEnableClientState(32885);
}
Tessellator.floatBuffer.position(0);
GL11.glVertexPointer(3, 40, Tessellator.floatBuffer);
GL11.glEnableClientState(32884);
GL11.glDrawArrays(this.drawMode, 0, vtc);
GL11.glDisableClientState(32884);
if (this.hasTexture) {
GL11.glDisableClientState(32888);
}
if (this.hasBrightness) {
OpenGlHelper.setClientActiveTexture(OpenGlHelper.lightmapTexUnit);
GL11.glDisableClientState(32888);
OpenGlHelper.setClientActiveTexture(OpenGlHelper.defaultTexUnit);
}
if (this.hasColor) {
GL11.glDisableClientState(32886);
}
if (this.hasNormals) {
GL11.glDisableClientState(32885);
}
}
if (this.rawBufferSize > 131072 && this.rawBufferIndex < this.rawBufferSize << 3) {
this.rawBufferSize = 65536;
this.rawBuffer = new int[this.rawBufferSize];
}
final int i = this.rawBufferIndex * 4;
this.reset();
return i;
}
public TesselatorVertexState getVertexState(final float p_147564_1_, final float p_147564_2_, final float p_147564_3_) {
final int[] aint = new int[this.rawBufferIndex];
final PriorityQueue priorityqueue = new PriorityQueue(this.rawBufferIndex, new QuadComparator(this.rawBuffer, p_147564_1_ + (float)this.xOffset, p_147564_2_ + (float)this.yOffset, p_147564_3_ + (float)this.zOffset));
final byte b0 = 40;
for (int i = 0; i < this.rawBufferIndex; i += b0) {
priorityqueue.add(i);
}
int i = 0;
while (!priorityqueue.isEmpty()) {
final int j = (int)priorityqueue.remove();
for (int k = 0; k < b0; ++k) {
aint[i + k] = this.rawBuffer[j + k];
}
i += b0;
}
System.arraycopy(aint, 0, this.rawBuffer, 0, aint.length);
return new TesselatorVertexState(aint, this.rawBufferIndex, this.vertexCount, this.hasTexture, this.hasBrightness, this.hasNormals, this.hasColor);
}
public void setVertexState(final TesselatorVertexState p_147565_1_) {
while (p_147565_1_.getRawBuffer().length > this.rawBufferSize && this.rawBufferSize > 0) {
this.rawBufferSize <<= 1;
}
if (this.rawBufferSize > this.rawBuffer.length) {
this.rawBuffer = new int[this.rawBufferSize];
}
System.arraycopy(p_147565_1_.getRawBuffer(), 0, this.rawBuffer, 0, p_147565_1_.getRawBuffer().length);
this.rawBufferIndex = p_147565_1_.getRawBufferIndex();
this.vertexCount = p_147565_1_.getVertexCount();
this.hasTexture = p_147565_1_.getHasTexture();
this.hasBrightness = p_147565_1_.getHasBrightness();
this.hasColor = p_147565_1_.getHasColor();
this.hasNormals = p_147565_1_.getHasNormals();
}
private void reset() {
this.vertexCount = 0;
Tessellator.byteBuffer.clear();
this.rawBufferIndex = 0;
this.addedVertices = 0;
}
public void startDrawingQuads() {
this.startDrawing(7);
}
public void startDrawing(final int p_78371_1_) {
if (this.isDrawing) {
throw new IllegalStateException("Already tesselating!");
}
this.isDrawing = true;
this.reset();
this.drawMode = p_78371_1_;
this.hasNormals = false;
this.hasColor = false;
this.hasTexture = false;
this.hasBrightness = false;
this.isColorDisabled = false;
}
public void setTextureUV(final double p_78385_1_, final double p_78385_3_) {
this.hasTexture = true;
this.textureU = p_78385_1_;
this.textureV = p_78385_3_;
}
public void setBrightness(final int p_78380_1_) {
this.hasBrightness = true;
this.brightness = p_78380_1_;
}
public void setColorOpaque_F(final float p_78386_1_, final float p_78386_2_, final float p_78386_3_) {
this.setColorOpaque((int)(p_78386_1_ * 255.0f), (int)(p_78386_2_ * 255.0f), (int)(p_78386_3_ * 255.0f));
}
public void setColorRGBA_F(final float p_78369_1_, final float p_78369_2_, final float p_78369_3_, final float p_78369_4_) {
this.setColorRGBA((int)(p_78369_1_ * 255.0f), (int)(p_78369_2_ * 255.0f), (int)(p_78369_3_ * 255.0f), (int)(p_78369_4_ * 255.0f));
}
public void setColorOpaque(final int p_78376_1_, final int p_78376_2_, final int p_78376_3_) {
this.setColorRGBA(p_78376_1_, p_78376_2_, p_78376_3_, 255);
}
public void setColorRGBA(int p_78370_1_, int p_78370_2_, int p_78370_3_, int p_78370_4_) {
if (!this.isColorDisabled) {
if (p_78370_1_ > 255) {
p_78370_1_ = 255;
}
if (p_78370_2_ > 255) {
p_78370_2_ = 255;
}
if (p_78370_3_ > 255) {
p_78370_3_ = 255;
}
if (p_78370_4_ > 255) {
p_78370_4_ = 255;
}
if (p_78370_1_ < 0) {
p_78370_1_ = 0;
}
if (p_78370_2_ < 0) {
p_78370_2_ = 0;
}
if (p_78370_3_ < 0) {
p_78370_3_ = 0;
}
if (p_78370_4_ < 0) {
p_78370_4_ = 0;
}
this.hasColor = true;
if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
this.color = (p_78370_4_ << 24 | p_78370_3_ << 16 | p_78370_2_ << 8 | p_78370_1_);
}
else {
this.color = (p_78370_1_ << 24 | p_78370_2_ << 16 | p_78370_3_ << 8 | p_78370_4_);
}
}
}
public void func_154352_a(final byte p_154352_1_, final byte p_154352_2_, final byte p_154352_3_) {
this.setColorOpaque(p_154352_1_ & 0xFF, p_154352_2_ & 0xFF, p_154352_3_ & 0xFF);
}
public void addVertexWithUV(final double p_78374_1_, final double p_78374_3_, final double p_78374_5_, final double p_78374_7_, final double p_78374_9_) {
this.setTextureUV(p_78374_7_, p_78374_9_);
this.addVertex(p_78374_1_, p_78374_3_, p_78374_5_);
}
public void addVertex(final double par1, final double par2, final double par3) {
CLTessellatorHelper.addVertex(this, par1, par2, par3);
}
public void setColorOpaque_I(final int p_78378_1_) {
final int j = p_78378_1_ >> 16 & 0xFF;
final int k = p_78378_1_ >> 8 & 0xFF;
final int l = p_78378_1_ & 0xFF;
this.setColorOpaque(j, k, l);
}
public void setColorRGBA_I(final int p_78384_1_, final int p_78384_2_) {
final int k = p_78384_1_ >> 16 & 0xFF;
final int l = p_78384_1_ >> 8 & 0xFF;
final int i1 = p_78384_1_ & 0xFF;
this.setColorRGBA(k, l, i1, p_78384_2_);
}
public void disableColor() {
this.isColorDisabled = true;
}
public void setNormal(final float p_78375_1_, final float p_78375_2_, final float p_78375_3_) {
this.hasNormals = true;
final byte b0 = (byte)(p_78375_1_ * 127.0f);
final byte b = (byte)(p_78375_2_ * 127.0f);
final byte b2 = (byte)(p_78375_3_ * 127.0f);
this.normal = ((b0 & 0xFF) | (b & 0xFF) << 8 | (b2 & 0xFF) << 16);
}
public void setTranslation(final double p_78373_1_, final double p_78373_3_, final double p_78373_5_) {
this.xOffset = p_78373_1_;
this.yOffset = p_78373_3_;
this.zOffset = p_78373_5_;
}
public void addTranslation(final float p_78372_1_, final float p_78372_2_, final float p_78372_3_) {
this.xOffset += p_78372_1_;
this.yOffset += p_78372_2_;
this.zOffset += p_78372_3_;
}
static {
Tessellator.nativeBufferSize = 2097152;
Tessellator.trivertsInBuffer = Tessellator.nativeBufferSize / 48 * 6;
Tessellator.renderingWorldRenderer = false;
Tessellator.byteBuffer = GLAllocation.createDirectByteBuffer(Tessellator.nativeBufferSize * 4);
Tessellator.intBuffer = Tessellator.byteBuffer.asIntBuffer();
Tessellator.floatBuffer = Tessellator.byteBuffer.asFloatBuffer();
Tessellator.shortBuffer = Tessellator.byteBuffer.asShortBuffer();
instance = new Tessellator(2097152);
Tessellator.instance.defaultTexture = true;
}
@Override
public int getRawBufferSize() {
return this.rawBufferSize;
}
@Override
public void setRawBufferSize(final int rawBufferSize) {
this.rawBufferSize = rawBufferSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment