mirror of
https://github.com/GTNewHorizons/CodeChickenCore.git
synced 2026-11-16 10:24:41 +00:00
Initial 1.7 commit
This commit is contained in:
1 parent
34154fc011
commit
ca675b5f31
72 files changed
+2967
-3335
No files matched your search
@@ -0,0 +1,126 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.lighting.LC;
|
||||
import codechicken.lib.vec.Cuboid6;
|
||||
|
||||
public class BlockRenderer
|
||||
{
|
||||
public static class BlockFace implements CCRenderState.IVertexSource
|
||||
{
|
||||
public Vertex5[] verts = new Vertex5[]{new Vertex5(), new Vertex5(), new Vertex5(), new Vertex5()};
|
||||
public LC[] lightCoords = new LC[]{new LC(), new LC(), new LC(), new LC()};
|
||||
public boolean lcComputed = false;
|
||||
public int side;
|
||||
|
||||
@Override
|
||||
public Vertex5[] getVertices() {
|
||||
return verts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getAttributes(CCRenderState.VertexAttribute<T> attr) {
|
||||
return attr == CCRenderState.lightCoordAttrib && lcComputed ? (T)lightCoords : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAttribute(CCRenderState.VertexAttribute<?> attr) {
|
||||
return attr == CCRenderState.sideAttrib || attr == CCRenderState.lightCoordAttrib && lcComputed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareVertex() {
|
||||
CCRenderState.side = side;
|
||||
}
|
||||
|
||||
public BlockFace computeLightCoords() {
|
||||
if(!lcComputed) {
|
||||
for (int i = 0; i < 4; i++)
|
||||
lightCoords[i].compute(verts[i].vec, side);
|
||||
lcComputed = true;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public BlockFace loadCuboidFace(Cuboid6 c, int side) {
|
||||
double x1 = c.min.x;
|
||||
double x2 = c.max.x;
|
||||
double y1 = c.min.y;
|
||||
double y2 = c.max.y;
|
||||
double z1 = c.min.z;
|
||||
double z2 = c.max.z;
|
||||
double u1; double u2; double v1; double v2;
|
||||
this.side = side;
|
||||
lcComputed = false;
|
||||
|
||||
switch(side) {
|
||||
case 0:
|
||||
u1 = x1; v1 = z1;
|
||||
u2 = x2; v2 = z2;
|
||||
verts[0].set(x1, y1, z2, u1, v2, 0);
|
||||
verts[1].set(x1, y1, z1, u1, v1, 0);
|
||||
verts[2].set(x2, y1, z1, u2, v1, 0);
|
||||
verts[3].set(x2, y1, z2, u2, v2, 0);
|
||||
break;
|
||||
case 1:
|
||||
u1 = x1; v1 = z1;
|
||||
u2 = x2; v2 = z2;
|
||||
verts[0].set(x2, y2, z2, u2, v2, 1);
|
||||
verts[1].set(x2, y2, z1, u2, v1, 1);
|
||||
verts[2].set(x1, y2, z1, u1, v1, 1);
|
||||
verts[3].set(x1, y2, z2, u1, v2, 1);
|
||||
break;
|
||||
case 2:
|
||||
u1 = 1-x1; v1 = 1-y2;
|
||||
u2 = 1-x2; v2 = 1-y1;
|
||||
verts[0].set(x1, y1, z1, u1, v2, 2);
|
||||
verts[1].set(x1, y2, z1, u1, v1, 2);
|
||||
verts[2].set(x2, y2, z1, u2, v1, 2);
|
||||
verts[3].set(x2, y1, z1, u2, v2, 2);
|
||||
break;
|
||||
case 3:
|
||||
u1 = x1; v1 = 1-y2;
|
||||
u2 = x2; v2 = 1-y1;
|
||||
verts[0].set(x2, y1, z2, u2, v2, 3);
|
||||
verts[1].set(x2, y2, z2, u2, v1, 3);
|
||||
verts[2].set(x1, y2, z2, u1, v1, 3);
|
||||
verts[3].set(x1, y1, z2, u1, v2, 3);
|
||||
break;
|
||||
case 4:
|
||||
u1 = z1; v1 = 1-y2;
|
||||
u2 = z2; v2 = 1-y1;
|
||||
verts[0].set(x1, y1, z2, u2, v2, 4);
|
||||
verts[1].set(x1, y2, z2, u2, v1, 4);
|
||||
verts[2].set(x1, y2, z1, u1, v1, 4);
|
||||
verts[3].set(x1, y1, z1, u1, v2, 4);
|
||||
break;
|
||||
case 5:
|
||||
u1 = 1-z1; v1 = 1-y2;
|
||||
u2 = 1-z2; v2 = 1-y1;
|
||||
verts[0].set(x2, y1, z1, u1, v2, 5);
|
||||
verts[1].set(x2, y2, z1, u1, v1, 5);
|
||||
verts[2].set(x2, y2, z2, u2, v1, 5);
|
||||
verts[3].set(x2, y1, z2, u2, v2, 5);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private static BlockFace face = new BlockFace();
|
||||
|
||||
/**
|
||||
* Renders faces of a cuboid with texture coordinates mapped to match a standard minecraft block
|
||||
* @param bounds The bounding cuboid to render
|
||||
* @param sideMask A mask of faces not to render
|
||||
*/
|
||||
public static void renderCuboid(Cuboid6 bounds, int sideMask) {
|
||||
if(sideMask == 0x3F)
|
||||
return;
|
||||
|
||||
CCRenderState.setModel(face);
|
||||
for(int s = 0; s < 6; s++)
|
||||
if((sideMask & 1<<s) == 0) {
|
||||
face.loadCuboidFace(bounds, s);
|
||||
CCRenderState.render();
|
||||
}
|
||||
}
|
||||
}
|
||||
+195
-206
@@ -11,59 +11,96 @@ import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import codechicken.lib.lighting.LC;
|
||||
import codechicken.lib.render.uv.UV;
|
||||
import codechicken.lib.render.uv.UVTransformation;
|
||||
import codechicken.lib.render.uv.UVTranslation;
|
||||
import codechicken.lib.util.Copyable;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import codechicken.lib.lighting.CCRBModel;
|
||||
import codechicken.lib.lighting.LightModel;
|
||||
import codechicken.lib.vec.Cuboid6;
|
||||
import codechicken.lib.vec.RedundantTransformation;
|
||||
import codechicken.lib.vec.Transformation;
|
||||
import codechicken.lib.vec.TransformationList;
|
||||
import codechicken.lib.vec.Translation;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
|
||||
import static codechicken.lib.vec.Rotation.*;
|
||||
|
||||
public class CCModel
|
||||
public class CCModel implements CCRenderState.IVertexSource, Copyable<CCModel>
|
||||
{
|
||||
private static class PositionNormalEntry
|
||||
{
|
||||
{
|
||||
public Vector3 pos;
|
||||
public LinkedList<Vector3> normals = new LinkedList<Vector3>();
|
||||
|
||||
|
||||
public PositionNormalEntry(Vector3 position)
|
||||
{
|
||||
pos = position;
|
||||
}
|
||||
|
||||
|
||||
public boolean positionEqual(Vector3 v)
|
||||
{
|
||||
return pos.x == v.x && pos.y == v.y && pos.z == v.z;
|
||||
}
|
||||
|
||||
|
||||
public PositionNormalEntry addNormal(Vector3 normal)
|
||||
{
|
||||
normals.add(normal);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public final int vertexMode;
|
||||
public final int vp;
|
||||
public Vertex5[] verts;
|
||||
public Vector3[] normals;
|
||||
public int[] colours;
|
||||
|
||||
public ArrayList<Object> attributes = new ArrayList<Object>();
|
||||
|
||||
protected CCModel(int vertexMode)
|
||||
{
|
||||
if(vertexMode != 7 && vertexMode != 4)
|
||||
throw new IllegalArgumentException("Models must be GL_QUADS or GL_TRIANGLES");
|
||||
|
||||
|
||||
this.vertexMode = vertexMode;
|
||||
vp = vertexMode == 7 ? 4 : 3;
|
||||
}
|
||||
|
||||
|
||||
public Vector3[] normals() {
|
||||
return getAttributes(CCRenderState.normalAttrib);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vertex5[] getVertices() {
|
||||
return verts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getAttributes(CCRenderState.VertexAttribute<T> attr) {
|
||||
if(attr.attributeIndex < attributes.size())
|
||||
return (T) attributes.get(attr.attributeIndex);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAttribute(CCRenderState.VertexAttribute<?> attrib) {
|
||||
return attrib.attributeIndex < attributes.size() && attributes.get(attrib.attributeIndex) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareVertex() {
|
||||
}
|
||||
|
||||
public <T> T getOrAllocate(CCRenderState.VertexAttribute<T> attrib) {
|
||||
T array = getAttributes(attrib);
|
||||
if(array == null) {
|
||||
while(attributes.size() <= attrib.attributeIndex)
|
||||
attributes.add(null);
|
||||
attributes.set(attrib.attributeIndex, array = attrib.newArray(verts.length));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Each pixel corresponds to one unit of position when generating the model
|
||||
* @param i Vertex index to start generating at
|
||||
@@ -87,7 +124,7 @@ public class CCModel
|
||||
double y2 = y1+h;
|
||||
double z2 = z1+d;
|
||||
x1 /= f; x2 /= f; y1 /= f; y2 /= f; z1 /= f; z2 /= f;
|
||||
|
||||
|
||||
//bottom face
|
||||
u1 = (tx + d + w) / tw; v1 = (ty + d) / th;
|
||||
u2 = (tx + d*2 + w) / tw; v2 = ty / th;
|
||||
@@ -95,7 +132,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x1, y1, z1, u1, v1);
|
||||
verts[i++] = new Vertex5(x2, y1, z1, u2, v1);
|
||||
verts[i++] = new Vertex5(x2, y1, z2, u2, v2);
|
||||
|
||||
|
||||
//top face
|
||||
u1 = (tx + d) / tw; v1 = (ty + d) / th;
|
||||
u2 = (tx + d + w) / tw; v2 = ty / th;
|
||||
@@ -103,7 +140,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x2, y2, z1, u2, v1);
|
||||
verts[i++] = new Vertex5(x1, y2, z1, u1, v1);
|
||||
verts[i++] = new Vertex5(x1, y2, z2, u1, v2);
|
||||
|
||||
|
||||
//front face
|
||||
u1 = (tx + d + w) / tw; v1 = (ty + d) / th;
|
||||
u2 = (tx + d) / tw; v2 = (ty + d + h) / th;
|
||||
@@ -111,7 +148,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x2, y2, z1, u1, v1);
|
||||
verts[i++] = new Vertex5(x2, y1, z1, u1, v2);
|
||||
verts[i++] = new Vertex5(x1, y1, z1, u2, v2);
|
||||
|
||||
|
||||
//back face
|
||||
u1 = (tx + d*2 + w*2) / tw; v1 = (ty + d) / th;
|
||||
u2 = (tx + d*2 + w) / tw; v2 = (ty + d + h) / th;
|
||||
@@ -119,7 +156,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x1, y1, z2, u1, v2);
|
||||
verts[i++] = new Vertex5(x2, y1, z2, u2, v2);
|
||||
verts[i++] = new Vertex5(x2, y2, z2, u2, v1);
|
||||
|
||||
|
||||
//left face
|
||||
u1 = (tx + d) / tw; v1 = (ty + d) / th;
|
||||
u2 = (tx) / tw; v2 = (ty + d + h) / th;
|
||||
@@ -127,7 +164,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x1, y2, z1, u1, v1);
|
||||
verts[i++] = new Vertex5(x1, y1, z1, u1, v2);
|
||||
verts[i++] = new Vertex5(x1, y1, z2, u2, v2);
|
||||
|
||||
|
||||
//right face
|
||||
u1 = (tx + d*2 + w) / tw; v1 = (ty + d) / th;
|
||||
u2 = (tx + d + w) / tw; v2 = (ty + d + h) / th;
|
||||
@@ -135,7 +172,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x2, y1, z1, u2, v2);
|
||||
verts[i++] = new Vertex5(x2, y2, z1, u2, v1);
|
||||
verts[i++] = new Vertex5(x2, y2, z2, u1, v1);
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -149,7 +186,7 @@ public class CCModel
|
||||
{
|
||||
return generateBlock(i, bounds, 0);
|
||||
}
|
||||
|
||||
|
||||
public CCModel generateBlock(int i, Cuboid6 bounds, int mask)
|
||||
{
|
||||
return generateBlock(i, bounds.min.x, bounds.min.y, bounds.min.z, bounds.max.x, bounds.max.y, bounds.max.z, mask);
|
||||
@@ -175,7 +212,7 @@ public class CCModel
|
||||
public CCModel generateBlock(int i, double x1, double y1, double z1, double x2, double y2, double z2, int mask)
|
||||
{
|
||||
double u1, v1, u2, v2;
|
||||
|
||||
|
||||
if((mask&1) == 0) {//bottom face
|
||||
u1 = x1; v1 = z1;
|
||||
u2 = x2; v2 = z2;
|
||||
@@ -184,7 +221,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x2, y1, z1, u2, v1);
|
||||
verts[i++] = new Vertex5(x2, y1, z2, u2, v2);
|
||||
}
|
||||
|
||||
|
||||
if((mask&2) == 0) {//top face
|
||||
u1 = x1+2; v1 = z1;
|
||||
u2 = x2+2; v2 = z2;
|
||||
@@ -193,7 +230,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x1, y2, z1, u1, v1);
|
||||
verts[i++] = new Vertex5(x1, y2, z2, u1, v2);
|
||||
}
|
||||
|
||||
|
||||
if((mask&4) == 0) {//east face
|
||||
u1 = 1-x1+4; v1 = 1-y2;
|
||||
u2 = 1-x2+4; v2 = 1-y1;
|
||||
@@ -202,7 +239,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x2, y2, z1, u2, v1);
|
||||
verts[i++] = new Vertex5(x2, y1, z1, u2, v2);
|
||||
}
|
||||
|
||||
|
||||
if((mask&8) == 0) {//west face
|
||||
u1 = x1+6; v1 = 1-y2;
|
||||
u2 = x2+6; v2 = 1-y1;
|
||||
@@ -211,7 +248,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x1, y2, z2, u1, v1);
|
||||
verts[i++] = new Vertex5(x1, y1, z2, u1, v2);
|
||||
}
|
||||
|
||||
|
||||
if((mask&0x10) == 0) {//north face
|
||||
u1 = z1+8; v1 = 1-y2;
|
||||
u2 = z2+8; v2 = 1-y1;
|
||||
@@ -220,7 +257,7 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x1, y2, z1, u1, v1);
|
||||
verts[i++] = new Vertex5(x1, y1, z1, u1, v2);
|
||||
}
|
||||
|
||||
|
||||
if((mask&0x20) == 0) {//south face
|
||||
u1 = 1-z1+10; v1 = 1-y2;
|
||||
u2 = 1-z2+10; v2 = 1-y1;
|
||||
@@ -229,10 +266,10 @@ public class CCModel
|
||||
verts[i++] = new Vertex5(x2, y2, z2, u2, v1);
|
||||
verts[i++] = new Vertex5(x2, y1, z2, u2, v2);
|
||||
}
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public CCModel computeNormals()
|
||||
{
|
||||
return computeNormals(0, verts.length);
|
||||
@@ -249,10 +286,8 @@ public class CCModel
|
||||
{
|
||||
if(length%vp != 0 || start%vp != 0)
|
||||
throw new IllegalArgumentException("Cannot generate normals across polygons");
|
||||
|
||||
if(normals == null)
|
||||
normals = new Vector3[verts.length];
|
||||
|
||||
|
||||
Vector3[] normals = getOrAllocate(CCRenderState.normalAttrib);
|
||||
for(int k = 0; k < length; k+=vp)
|
||||
{
|
||||
int i = k + start;
|
||||
@@ -262,44 +297,47 @@ public class CCModel
|
||||
for(int d = 1; d < vp; d++)
|
||||
normals[i+d] = normals[i].copy();
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes lighting using the normals and the colour 0xFFFFFF.
|
||||
* Per vert colouring will be added when needed.
|
||||
* Make sure you have generated your normals on the model first.
|
||||
* If you rotate your model after this, the lighting will no longer be valid
|
||||
* @param light The light model to calculate
|
||||
* @return The model
|
||||
*/
|
||||
public CCModel computeLighting(LightModel light)
|
||||
{
|
||||
if(colours == null)
|
||||
setColour(-1);
|
||||
for(int k = 0; k < verts.length; k++)
|
||||
colours[k] = light.apply(colours[k], normals[k]);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CCModel setColour(int c)
|
||||
{
|
||||
if(colours == null)
|
||||
colours = new int[verts.length];
|
||||
for(int k = 0; k < verts.length; k++)
|
||||
colours[k] = c;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning, only use this if you NEED to be identical to MC's light model, it's hideous.
|
||||
* Computes lighting using the normals add a light model
|
||||
* If the model is rotated, the lighting will no longer be valid
|
||||
* @return The model
|
||||
*/
|
||||
public CCRBModel withMCLighting()
|
||||
public CCModel computeLighting(LightModel light)
|
||||
{
|
||||
return new CCRBModel(this);
|
||||
Vector3[] normals = normals();
|
||||
int[] colours = getAttributes(CCRenderState.colourAttrib);
|
||||
if(colours == null) {
|
||||
setColour(-1);
|
||||
colours = getAttributes(CCRenderState.colourAttrib);
|
||||
}
|
||||
for(int k = 0; k < verts.length; k++)
|
||||
colours[k] = light.apply(colours[k], normals[k]);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public CCModel setColour(int c)
|
||||
{
|
||||
int[] colours = getOrAllocate(CCRenderState.colourAttrib);
|
||||
Arrays.fill(colours, c);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the minecraft lighting coordinates for use with a LightMatrix
|
||||
* @return The model
|
||||
*/
|
||||
public CCModel computeLightCoords() {
|
||||
LC[] lcs = getOrAllocate(CCRenderState.lightCoordAttrib);
|
||||
Vector3[] normals = normals();
|
||||
for(int i = 0; i < verts.length; i++)
|
||||
lcs[i] = new LC().compute(verts[i].vec, normals[i]);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Averages all normals at the same position to produce a smooth lighting effect.
|
||||
* @return The model
|
||||
@@ -307,6 +345,7 @@ public class CCModel
|
||||
public CCModel smoothNormals()
|
||||
{
|
||||
ArrayList<PositionNormalEntry> map = new ArrayList<PositionNormalEntry>();
|
||||
Vector3[] normals = normals();
|
||||
nextvert: for(int k = 0; k < verts.length; k++)
|
||||
{
|
||||
Vector3 vec = verts[k].vec;
|
||||
@@ -316,24 +355,24 @@ public class CCModel
|
||||
e.addNormal(normals[k]);
|
||||
continue nextvert;
|
||||
}
|
||||
|
||||
|
||||
map.add(new PositionNormalEntry(vec).addNormal(normals[k]));
|
||||
}
|
||||
|
||||
|
||||
for(PositionNormalEntry e : map)
|
||||
{
|
||||
if(e.normals.size() <= 1)
|
||||
continue;
|
||||
|
||||
|
||||
Vector3 new_n = new Vector3();
|
||||
for(Vector3 n : e.normals)
|
||||
new_n.add(n);
|
||||
|
||||
|
||||
new_n.normalize();
|
||||
for(Vector3 n : e.normals)
|
||||
n.set(new_n);
|
||||
}
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -341,134 +380,93 @@ public class CCModel
|
||||
{
|
||||
for(int k = 0; k < verts.length; k++)
|
||||
verts[k].apply(t);
|
||||
|
||||
|
||||
Vector3[] normals = normals();
|
||||
if(normals != null)
|
||||
for(int k = 0; k < normals.length; k++)
|
||||
t.applyN(normals[k]);
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public CCModel apply(IUVTransformation uvt)
|
||||
public CCModel apply(UVTransformation uvt)
|
||||
{
|
||||
for(int k = 0; k < verts.length; k++)
|
||||
verts[k].apply(uvt);
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public CCModel expand(int extraVerts)
|
||||
{
|
||||
int newLen = verts.length+extraVerts;
|
||||
verts = Arrays.copyOf(verts, newLen);
|
||||
if(normals != null)
|
||||
normals = Arrays.copyOf(normals, newLen);
|
||||
if(colours != null)
|
||||
colours = Arrays.copyOf(colours, newLen);
|
||||
for(int i = 0; i < attributes.size(); i++)
|
||||
if(attributes.get(i) != null)
|
||||
attributes.set(i, CCRenderState.copyOf((CCRenderState.VertexAttribute)CCRenderState.getAttribute(i), attributes.get(i), newLen));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public void render()
|
||||
{
|
||||
render(0, verts.length, null, null, null);
|
||||
}
|
||||
|
||||
|
||||
public void render(double x, double y, double z, double u, double v)
|
||||
{
|
||||
render(new Translation(new Vector3(x, y, z)), new UVTranslation(u, v));
|
||||
render(new Vector3(x, y, z).translation(), new UVTranslation(u, v));
|
||||
}
|
||||
|
||||
public void render(double x, double y, double z, IUVTransformation u)
|
||||
public void render(double x, double y, double z, UVTransformation u)
|
||||
{
|
||||
render(new Translation(new Vector3(x, y, z)), u);
|
||||
render(new Vector3(x, y, z).translation(), u);
|
||||
}
|
||||
|
||||
public void render(Transformation t, double u, double v)
|
||||
{
|
||||
render(t, new UVTranslation(u, v));
|
||||
}
|
||||
|
||||
public void render(Transformation t, IUVTransformation u)
|
||||
|
||||
public void render(CCRenderState.IVertexOperation... ops)
|
||||
{
|
||||
render(t, u, ColourModifier.instance);
|
||||
}
|
||||
|
||||
public void render(Transformation t, IUVTransformation u, IVertexModifier m)
|
||||
{
|
||||
render(0, verts.length, t, u, m);
|
||||
render(0, verts.length, ops);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders vertices start through start+length-1 of the model
|
||||
* @param start The first vertex to render
|
||||
* @param length The number of vertices to render
|
||||
* @param t The transformation to apply to the mat
|
||||
* @param u The u texture offset
|
||||
* @param v The v texture offset
|
||||
* @param start The first vertex index to render
|
||||
* @param end The vertex index to render until
|
||||
* @param ops Operations to apply
|
||||
*/
|
||||
public void render(int start, int length, Transformation t, IUVTransformation u, IVertexModifier m)
|
||||
public void render(int start, int end, CCRenderState.IVertexOperation... ops)
|
||||
{
|
||||
boolean drawNormal = CCRenderState.useNormals() && normals != null;
|
||||
boolean computeNormal = drawNormal || m != null && m.needsNormals();
|
||||
Vector3 normal = new Vector3();
|
||||
Vertex5 vert;
|
||||
Vector3 vec = new Vector3();
|
||||
UV uv = new UV();
|
||||
Tessellator tess = Tessellator.instance;
|
||||
for(int k = 0; k < length; k++)
|
||||
{
|
||||
int i = start+k;
|
||||
if(computeNormal)
|
||||
{
|
||||
if(t != null)
|
||||
t.applyN(normal.set(normals[i]));
|
||||
else
|
||||
normal = normals[i];
|
||||
|
||||
if(drawNormal)
|
||||
tess.setNormal((float)normal.x, (float)normal.y, (float)normal.z);
|
||||
}
|
||||
|
||||
vert = verts[i];
|
||||
if(t != null)
|
||||
t.apply(vec.set(vert.vec));
|
||||
else
|
||||
vec = vert.vec;
|
||||
|
||||
if(u != null)
|
||||
u.transform(uv.set(vert.uv));
|
||||
else
|
||||
uv = vert.uv;
|
||||
|
||||
if(m != null)
|
||||
m.applyModifiers(this, tess, vec, uv, normal, i);
|
||||
|
||||
tess.addVertexWithUV(vec.x, vec.y, vec.z, uv.u, uv.v);
|
||||
}
|
||||
CCRenderState.setPipeline(this, start, end, ops);
|
||||
CCRenderState.render();
|
||||
}
|
||||
|
||||
|
||||
public static CCModel quadModel(int numVerts)
|
||||
{
|
||||
return newModel(7, numVerts);
|
||||
}
|
||||
|
||||
|
||||
public static CCModel triModel(int numVerts)
|
||||
{
|
||||
return newModel(4, numVerts);
|
||||
}
|
||||
|
||||
|
||||
public static CCModel newModel(int vertexMode, int numVerts)
|
||||
{
|
||||
CCModel model = newModel(vertexMode);
|
||||
model.verts = new Vertex5[numVerts];
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
public static CCModel newModel(int vertexMode)
|
||||
{
|
||||
return new CCModel(vertexMode);
|
||||
}
|
||||
|
||||
|
||||
public static double[] parseDoubles(String s, String token)
|
||||
{
|
||||
String[] as = s.split(token);
|
||||
@@ -482,7 +480,7 @@ public class CCModel
|
||||
{
|
||||
if(!b) throw new IllegalArgumentException(err);
|
||||
}
|
||||
|
||||
|
||||
public static void assertMatch(Matcher m, String s)
|
||||
{
|
||||
m.reset(s);
|
||||
@@ -497,7 +495,7 @@ public class CCModel
|
||||
public static final Matcher uvwMatcher = uvwPattern.matcher("");
|
||||
public static final Matcher normalMatcher = normalPattern.matcher("");
|
||||
public static final Matcher polyMatcher = polyPattern.matcher("");
|
||||
|
||||
|
||||
/**
|
||||
* Parses vertices, texture coords, normals and polygons from a WaveFront Obj file
|
||||
* @param input An input stream to a obj file
|
||||
@@ -511,14 +509,14 @@ public class CCModel
|
||||
if(coordSystem == null)
|
||||
coordSystem = new RedundantTransformation();
|
||||
int vp = vertexMode == 7 ? 4 : 3;
|
||||
|
||||
|
||||
HashMap<String, CCModel> modelMap = new HashMap<String, CCModel>();
|
||||
ArrayList<Vector3> verts = new ArrayList<Vector3>();
|
||||
ArrayList<Vector3> uvs = new ArrayList<Vector3>();
|
||||
ArrayList<Vector3> normals = new ArrayList<Vector3>();
|
||||
ArrayList<int[]> polys = new ArrayList<int[]>();
|
||||
String modelName = "unnamed";
|
||||
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
|
||||
|
||||
String line;
|
||||
@@ -527,7 +525,7 @@ public class CCModel
|
||||
line = line.replaceAll("\\s+", " ").trim();
|
||||
if(line.startsWith("#") || line.length() == 0)
|
||||
continue;
|
||||
|
||||
|
||||
if(line.startsWith("v "))
|
||||
{
|
||||
assertMatch(vertMatcher, line);
|
||||
@@ -584,13 +582,13 @@ public class CCModel
|
||||
modelName = line.substring(2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(!polys.isEmpty())
|
||||
modelMap.put(modelName, createModel(verts, uvs, normals, vertexMode, polys));
|
||||
|
||||
|
||||
return modelMap;
|
||||
}
|
||||
|
||||
|
||||
public static void triangulate(List<int[]> polys, int[][] polyVerts)
|
||||
{
|
||||
for(int i = 2; i < polyVerts.length; i++)
|
||||
@@ -600,7 +598,7 @@ public class CCModel
|
||||
polys.add(polyVerts[i-1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void quadulate(List<int[]> polys, int[][] polyVerts)
|
||||
{
|
||||
if(polyVerts.length == 4)
|
||||
@@ -624,7 +622,7 @@ public class CCModel
|
||||
|
||||
/**
|
||||
* Parses vertices, texture coords, normals and polygons from a WaveFront Obj file
|
||||
* @param s The name of the obj resource
|
||||
* @param res The resource for the obj file
|
||||
* @return A map of group names to models
|
||||
*/
|
||||
public static Map<String, CCModel> parseObjModels(ResourceLocation res)
|
||||
@@ -642,7 +640,7 @@ public class CCModel
|
||||
try
|
||||
{
|
||||
return parseObjModels(
|
||||
Minecraft.getMinecraft().getResourceManager().getResource(res).getInputStream(),
|
||||
Minecraft.getMinecraft().getResourceManager().getResource(res).getInputStream(),
|
||||
4, coordSystem);
|
||||
}
|
||||
catch(IOException e)
|
||||
@@ -650,10 +648,10 @@ public class CCModel
|
||||
throw new RuntimeException("failed to load model: "+res, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses vertices, texture coords, normals and polygons from a WaveFront Obj file
|
||||
* @param s The name of the obj resource
|
||||
* @param res The resource for the obj file
|
||||
* @param vertexMode The vertex mode to create the model for (GL_TRIANGLES or GL_QUADS)
|
||||
* @param coordSystem The cooridnate system transformation to apply
|
||||
* @return A map of group names to models
|
||||
@@ -663,7 +661,7 @@ public class CCModel
|
||||
try
|
||||
{
|
||||
return parseObjModels(
|
||||
Minecraft.getMinecraft().getResourceManager().getResource(res).getInputStream(),
|
||||
Minecraft.getMinecraft().getResourceManager().getResource(res).getInputStream(),
|
||||
vertexMode, coordSystem);
|
||||
}
|
||||
catch(Exception e)
|
||||
@@ -677,12 +675,12 @@ public class CCModel
|
||||
int vp = vertexMode == 7 ? 4 : 3;
|
||||
if(polys.size() < vp || polys.size()%vp != 0)
|
||||
throw new IllegalArgumentException("Invalid number of vertices for model: "+polys.size());
|
||||
|
||||
|
||||
boolean hasNormals = polys.get(0)[2] > 0;
|
||||
CCModel model = CCModel.newModel(vertexMode, polys.size());
|
||||
if(hasNormals)
|
||||
model.normals = new Vector3[polys.size()];
|
||||
|
||||
model.getOrAllocate(CCRenderState.normalAttrib);
|
||||
|
||||
for(int i = 0; i < polys.size(); i++)
|
||||
{
|
||||
int[] ai = polys.get(i);
|
||||
@@ -690,12 +688,12 @@ public class CCModel
|
||||
Vector3 uv = ai[1] <= 0 ? new Vector3() : uvs.get(ai[1]-1).copy();
|
||||
if(ai[2] > 0 != hasNormals)
|
||||
throw new IllegalArgumentException("Normals are an all or nothing deal here.");
|
||||
|
||||
|
||||
model.verts[i] = new Vertex5(vert, uv.x, uv.y);
|
||||
if(hasNormals)
|
||||
model.normals[i] = normals.get(ai[2]-1).copy();
|
||||
model.normals()[i] = normals.get(ai[2]-1).copy();
|
||||
}
|
||||
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
@@ -726,7 +724,7 @@ public class CCModel
|
||||
int vStart = verts.size();
|
||||
int uStart = uvs.size();
|
||||
int nStart = normals.size();
|
||||
boolean hasNormals = m.normals != null;
|
||||
boolean hasNormals = m.normals() != null;
|
||||
polys.clear();
|
||||
|
||||
for(int i = 0; i < m.verts.length; i++) {
|
||||
@@ -734,7 +732,7 @@ public class CCModel
|
||||
ia[0] = addIndex(verts, m.verts[i].vec);
|
||||
ia[1] = addIndex(uvs, m.verts[i].uv);
|
||||
if(hasNormals)
|
||||
ia[2] = addIndex(normals, m.normals[i]);
|
||||
ia[2] = addIndex(normals, m.normals()[i]);
|
||||
polys.add(ia);
|
||||
}
|
||||
|
||||
@@ -788,7 +786,7 @@ public class CCModel
|
||||
{
|
||||
uv.add(verts[k+i].uv);
|
||||
}
|
||||
uv.mul(1D/vp);
|
||||
uv.multiply(1D/vp);
|
||||
for(int i = 0; i < vp; i++)
|
||||
{
|
||||
Vertex5 vert = verts[k+i];
|
||||
@@ -798,7 +796,7 @@ public class CCModel
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param side1 The side of this model
|
||||
* @param side2 The side of the new model
|
||||
@@ -811,30 +809,18 @@ public class CCModel
|
||||
copy(this, 0, model, 0, model.verts.length);
|
||||
return model.apply(new TransformationList(sideRotations[side1].inverse(), sideRotations[side2]).at(point));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Copies length vertices and normals
|
||||
*/
|
||||
public static void copy(CCModel src, int srcpos, CCModel dest, int destpos, int length)
|
||||
public static void copy(CCModel src, int srcpos, CCModel dst, int destpos, int length)
|
||||
{
|
||||
for(int k = 0; k < length; k++)
|
||||
dest.verts[destpos+k] = src.verts[srcpos+k].copy();
|
||||
|
||||
if(src.normals != null)
|
||||
{
|
||||
if(dest.normals == null)
|
||||
dest.normals = new Vector3[dest.verts.length];
|
||||
dst.verts[destpos+k] = src.verts[srcpos+k].copy();
|
||||
|
||||
for(int k = 0; k < length; k++)
|
||||
dest.normals[destpos+k] = src.normals[srcpos+k].copy();
|
||||
}
|
||||
|
||||
if(src.colours != null)
|
||||
{
|
||||
if(dest.colours == null)
|
||||
dest.colours = new int[dest.verts.length];
|
||||
System.arraycopy(src.colours, srcpos, dest.colours, destpos, length);
|
||||
}
|
||||
for(int i = 0; i < src.attributes.size(); i++)
|
||||
if(src.attributes.get(i) != null)
|
||||
CCRenderState.arrayCopy(src.attributes.get(i), srcpos, dst.getOrAllocate(CCRenderState.getAttribute(i)), destpos, length);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -849,11 +835,11 @@ public class CCModel
|
||||
{
|
||||
if(s == side)
|
||||
continue;
|
||||
|
||||
|
||||
models[s] = models[side].sidedCopy(side, s, point);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate models rotated to the other 3 horizontal of the block
|
||||
* @param models An array of 4 models
|
||||
@@ -870,12 +856,12 @@ public class CCModel
|
||||
models[s] = models[side].sidedCopy(side, s, point);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public CCModel backfacedCopy()
|
||||
{
|
||||
return generateBackface(this, 0, copy(), 0, verts.length);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates copies of faces with clockwise vertices
|
||||
* @return The model
|
||||
@@ -885,7 +871,7 @@ public class CCModel
|
||||
int vp = src.vp;
|
||||
if(srcpos%vp != 0 || destpos%vp != 0 || length%vp != 0)
|
||||
throw new IllegalArgumentException("Vertices do not align with polygons");
|
||||
|
||||
|
||||
int[][] o = new int[][]{{0, 0}, {1, vp-1}, {2, vp-2}, {3, vp-3}};
|
||||
for(int i = 0; i < length; i++)
|
||||
{
|
||||
@@ -894,10 +880,12 @@ public class CCModel
|
||||
int di = destpos+b+o[d][1];
|
||||
int si = srcpos+b+o[d][0];
|
||||
dst.verts[di] = src.verts[si].copy();
|
||||
if(src.normals != null && src.normals[si] != null)
|
||||
dst.normals[di] = src.normals[si].copy().negate();
|
||||
if(src.colours != null)
|
||||
dst.colours[di] = src.colours[si];
|
||||
for(int a = 0; a < src.attributes.size(); a++)
|
||||
if(src.attributes.get(a) != null)
|
||||
CCRenderState.arrayCopy(src.attributes.get(a), si, dst.getOrAllocate(CCRenderState.getAttribute(a)), di, 1);
|
||||
|
||||
if(dst.normals() != null && dst.normals()[di] != null)
|
||||
dst.normals()[di].negate();
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
@@ -911,15 +899,15 @@ public class CCModel
|
||||
if(verts.length%(6*vp) != 0)
|
||||
throw new IllegalArgumentException("Invalid number of vertices for sided part generation");
|
||||
int length = verts.length/6;
|
||||
|
||||
|
||||
for(int s = 0; s < 6; s++)
|
||||
{
|
||||
if(s == side)
|
||||
continue;
|
||||
|
||||
|
||||
generateSidedPart(side, s, point, length*side, length*s, length);
|
||||
}
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -932,7 +920,7 @@ public class CCModel
|
||||
if(verts.length%(4*vp) != 0)
|
||||
throw new IllegalArgumentException("Invalid number of vertices for sided part generation");
|
||||
int length = verts.length/4;
|
||||
|
||||
|
||||
for(int s = 2; s < 6; s++)
|
||||
{
|
||||
if(s == side)
|
||||
@@ -940,7 +928,7 @@ public class CCModel
|
||||
|
||||
generateSidedPart(side, s, point, length*(side-2), length*(s-2), length);
|
||||
}
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -962,13 +950,14 @@ public class CCModel
|
||||
verts[destpos+k] = verts[srcpos+k].copy();
|
||||
verts[destpos+k].vec.apply(t);
|
||||
}
|
||||
|
||||
|
||||
Vector3[] normals = normals();
|
||||
if(normals != null)
|
||||
for(int k = 0; k < length; k++) {
|
||||
normals[destpos+k] = normals[srcpos+k].copy();
|
||||
t.applyN(normals[destpos+k]);
|
||||
}
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -976,7 +965,7 @@ public class CCModel
|
||||
{
|
||||
if(models.isEmpty())
|
||||
return null;
|
||||
|
||||
|
||||
int numVerts = 0;
|
||||
int vertexMode = -1;
|
||||
for(CCModel model : models)
|
||||
@@ -985,10 +974,10 @@ public class CCModel
|
||||
vertexMode = model.vertexMode;
|
||||
if(vertexMode != model.vertexMode)
|
||||
throw new IllegalArgumentException("Cannot combine models with different vertex modes");
|
||||
|
||||
|
||||
numVerts+=model.verts.length;
|
||||
}
|
||||
|
||||
|
||||
CCModel c_model = newModel(vertexMode, numVerts);
|
||||
int i = 0;
|
||||
for(CCModel model : models)
|
||||
@@ -996,7 +985,7 @@ public class CCModel
|
||||
copy(model, 0, c_model, i, model.verts.length);
|
||||
i+=model.verts.length;
|
||||
}
|
||||
|
||||
|
||||
return c_model;
|
||||
}
|
||||
|
||||
@@ -1013,7 +1002,7 @@ public class CCModel
|
||||
copy(this, 0, model, 0, verts.length);
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return The average of all vertices, for bones.
|
||||
*/
|
||||
@@ -1025,13 +1014,13 @@ public class CCModel
|
||||
v.multiply(1/(double)verts.length);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
public CCModel zOffset(Cuboid6 offsets)
|
||||
{
|
||||
for(int k = 0; k < verts.length; k++)
|
||||
{
|
||||
Vertex5 vert = verts[k];
|
||||
Vector3 normal = normals[k];
|
||||
Vector3 normal = normals()[k];
|
||||
switch(findSide(normal))
|
||||
{
|
||||
case 0:
|
||||
@@ -1056,7 +1045,7 @@ public class CCModel
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public static int findSide(Vector3 normal)
|
||||
{
|
||||
if(normal.y <=-0.99) return 0;
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import codechicken.lib.render.CCRenderState.IVertexOperation;
|
||||
import codechicken.lib.render.CCRenderState.VertexAttribute;
|
||||
|
||||
public class CCRenderPipeline
|
||||
{
|
||||
public class PipelineBuilder
|
||||
{
|
||||
public PipelineBuilder add(IVertexOperation op) {
|
||||
ops.add(op);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PipelineBuilder add(IVertexOperation... ops) {
|
||||
for(int i = 0; i < ops.length; i++)
|
||||
CCRenderPipeline.this.ops.add(ops[i]);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void build() {
|
||||
rebuild();
|
||||
}
|
||||
|
||||
public void render() {
|
||||
rebuild();
|
||||
CCRenderState.render();
|
||||
}
|
||||
}
|
||||
|
||||
private class PipelineNode
|
||||
{
|
||||
public ArrayList<PipelineNode> deps = new ArrayList<PipelineNode>();
|
||||
public IVertexOperation op;
|
||||
|
||||
public void add() {
|
||||
if(op == null)
|
||||
return;
|
||||
|
||||
for(int i = 0; i < deps.size(); i++)
|
||||
deps.get(i).add();
|
||||
deps.clear();
|
||||
sorted.add(op);
|
||||
op = null;
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList<VertexAttribute> attribs = new ArrayList<VertexAttribute>();
|
||||
private ArrayList<IVertexOperation> ops = new ArrayList<IVertexOperation>();
|
||||
private ArrayList<PipelineNode> nodes = new ArrayList<PipelineNode>();
|
||||
private ArrayList<IVertexOperation> sorted = new ArrayList<IVertexOperation>();
|
||||
private PipelineNode loading;
|
||||
private PipelineBuilder builder = new PipelineBuilder();
|
||||
|
||||
public void setPipeline(IVertexOperation... ops) {
|
||||
this.ops.clear();
|
||||
for(int i = 0; i < ops.length; i++)
|
||||
this.ops.add(ops[i]);
|
||||
rebuild();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
ops.clear();
|
||||
unbuild();
|
||||
}
|
||||
|
||||
private void unbuild() {
|
||||
for(int i = 0; i < attribs.size(); i++)
|
||||
attribs.get(i).active = false;
|
||||
attribs.clear();
|
||||
sorted.clear();
|
||||
}
|
||||
|
||||
public void rebuild() {
|
||||
if(ops.isEmpty() || CCRenderState.model == null)
|
||||
return;
|
||||
|
||||
//ensure enough nodes for all ops
|
||||
while(nodes.size() < CCRenderState.operationCount())
|
||||
nodes.add(new PipelineNode());
|
||||
unbuild();
|
||||
|
||||
if(CCRenderState.useNormals)
|
||||
addAttribute(CCRenderState.normalAttrib);
|
||||
if(CCRenderState.baseColour != -1 || CCRenderState.alphaOverride >= 0)
|
||||
addAttribute(CCRenderState.colourAttrib);
|
||||
else if(CCRenderState.hasColour)
|
||||
CCRenderState.setColour(-1);
|
||||
|
||||
for(int i = 0; i < ops.size(); i++) {
|
||||
IVertexOperation op = ops.get(i);
|
||||
loading = nodes.get(op.operationID());
|
||||
boolean loaded = op.load();
|
||||
if(loaded)
|
||||
loading.op = op;
|
||||
|
||||
if(op instanceof VertexAttribute)
|
||||
if(loaded)
|
||||
attribs.add((VertexAttribute)op);
|
||||
else
|
||||
((VertexAttribute)op).active = false;
|
||||
}
|
||||
|
||||
for(int i = 0; i < nodes.size(); i++)
|
||||
nodes.get(i).add();
|
||||
}
|
||||
|
||||
public void addRequirement(int opRef) {
|
||||
loading.deps.add(nodes.get(opRef));
|
||||
}
|
||||
|
||||
public void addDependency(VertexAttribute attrib) {
|
||||
loading.deps.add(nodes.get(attrib.operationID()));
|
||||
addAttribute(attrib);
|
||||
}
|
||||
|
||||
public void addAttribute(VertexAttribute attrib) {
|
||||
if(!attrib.active) {
|
||||
ops.add(attrib);
|
||||
attrib.active = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void operate() {
|
||||
for(int i = 0; i < sorted.size(); i++)
|
||||
sorted.get(i).operate();
|
||||
}
|
||||
|
||||
public PipelineBuilder builder() {
|
||||
ops.clear();
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -1,147 +1,383 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.colour.ColourRGBA;
|
||||
import net.minecraft.block.Block;
|
||||
import codechicken.lib.lighting.LC;
|
||||
import codechicken.lib.lighting.LightMatrix;
|
||||
import codechicken.lib.util.Copyable;
|
||||
import codechicken.lib.vec.Rotation;
|
||||
import codechicken.lib.vec.Transformation;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.OpenGlHelper;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The core of the CodeChickenLib render system.
|
||||
* Rendering operations are written to avoid object allocations by reusing static variables.
|
||||
*/
|
||||
public class CCRenderState
|
||||
{
|
||||
private static boolean useNormals;
|
||||
private static boolean hasBrightness;
|
||||
private static int brightness;
|
||||
private static boolean useModelColours;
|
||||
private static int colour;
|
||||
private static boolean hasColour;
|
||||
private static int alpha;
|
||||
private static boolean alphaOverride;
|
||||
|
||||
public static void useNormals(boolean b)
|
||||
{
|
||||
useNormals = b;
|
||||
}
|
||||
|
||||
public static boolean useNormals()
|
||||
{
|
||||
return useNormals;
|
||||
}
|
||||
|
||||
public static void useModelColours(boolean b)
|
||||
{
|
||||
useModelColours = b;
|
||||
{
|
||||
private static int nextOperationIndex;
|
||||
|
||||
public static int registerOperation() {
|
||||
return nextOperationIndex++;
|
||||
}
|
||||
|
||||
public static boolean useModelColours()
|
||||
{
|
||||
return useModelColours;
|
||||
}
|
||||
|
||||
public static void setAlpha(int a)
|
||||
{
|
||||
alpha = a;
|
||||
alphaOverride = true;
|
||||
}
|
||||
|
||||
public static void clearAlphaOverride()
|
||||
{
|
||||
alphaOverride = false;
|
||||
}
|
||||
|
||||
public static void vertexColour(int c)
|
||||
{
|
||||
if(alphaOverride)
|
||||
setColour(c&0xFFFFFF00|alpha);
|
||||
else
|
||||
setColour(c);
|
||||
public static int operationCount() {
|
||||
return nextOperationIndex;
|
||||
}
|
||||
|
||||
public static void vertexColour(int r, int g, int b, int a)
|
||||
/**
|
||||
* Represents an operation to be run for each vertex that operates on and modifies the current state
|
||||
*/
|
||||
public static interface IVertexOperation
|
||||
{
|
||||
if(alphaOverride)
|
||||
Tessellator.instance.setColorRGBA(r, g, b, alpha);
|
||||
else
|
||||
Tessellator.instance.setColorRGBA(r, g, b, a);
|
||||
}
|
||||
|
||||
public static void setBrightness(IBlockAccess world, int x, int y, int z)
|
||||
{
|
||||
Block block = Block.blocksList[world.getBlockId(x, y, z)];
|
||||
setBrightness(block == null ? world.getLightBrightnessForSkyBlocks(x, y, z, 0) : block.getMixedBrightnessForBlock(world, x, y, z));
|
||||
setColour(0xFFFFFFFF);
|
||||
/**
|
||||
* Load any required references and add dependencies to the pipeline based on the current model (may be null)
|
||||
* Return false if this operation is redundant in the pipeline with the given model
|
||||
*/
|
||||
public boolean load();
|
||||
|
||||
/**
|
||||
* Perform the operation on the current render state
|
||||
*/
|
||||
public void operate();
|
||||
|
||||
/**
|
||||
* Get the unique id representing this type of operation. Duplicate operation IDs within the pipeline may have unexpected results.
|
||||
* ID shoulld be obtained from CCRenderState.registerOperation() and stored in a static variable
|
||||
*/
|
||||
public int operationID();
|
||||
}
|
||||
|
||||
public static void setBrightness(int b)
|
||||
{
|
||||
hasBrightness = true;
|
||||
Tessellator.instance.setBrightness(brightness = b);
|
||||
}
|
||||
|
||||
public static void setColourOpaque(int c)
|
||||
{
|
||||
setColour(c << 8 | 0xFF);
|
||||
}
|
||||
|
||||
//RGBA
|
||||
public static void setColour(int c)
|
||||
{
|
||||
hasColour = true;
|
||||
colour = c;
|
||||
Tessellator.instance.setColorRGBA(colour >> 24 & 0xFF, colour >> 16 & 0xFF, colour >> 8 & 0xFF, colour & 0xFF);
|
||||
}
|
||||
|
||||
public static void changeTexture(String texture)
|
||||
{
|
||||
changeTexture(new ResourceLocation(texture));
|
||||
}
|
||||
|
||||
public static void changeTexture(ResourceLocation texture)
|
||||
{
|
||||
Minecraft.getMinecraft().renderEngine.bindTexture(texture);
|
||||
private static ArrayList<VertexAttribute<?>> vertexAttributes = new ArrayList<VertexAttribute<?>>();
|
||||
private static int registerVertexAttribute(VertexAttribute<?> attr) {
|
||||
vertexAttributes.add(attr);
|
||||
return vertexAttributes.size()-1;
|
||||
}
|
||||
|
||||
public static void apply()
|
||||
public static VertexAttribute<?> getAttribute(int index) {
|
||||
return vertexAttributes.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Management class for a vertex attrute such as colour, normal etc
|
||||
* This class should handle the loading of the attrute from an array provided by IVertexSource.getAttributes or the computation of this attrute from others
|
||||
* @param <T> The array type for this attrute eg. int[], Vector3[]
|
||||
*/
|
||||
public static abstract class VertexAttribute<T> implements IVertexOperation
|
||||
{
|
||||
public final int attributeIndex = registerVertexAttribute(this);
|
||||
private final int operationIndex = registerOperation();
|
||||
/**
|
||||
* Set to true when the attrute is part of the pipeline. Should only be managed by CCRenderState when constructing the pipeline
|
||||
*/
|
||||
public boolean active = false;
|
||||
|
||||
/**
|
||||
* Construct a new array for storage of vertex attrutes in a model
|
||||
*/
|
||||
public abstract T newArray(int length);
|
||||
|
||||
@Override
|
||||
public int operationID() {
|
||||
return operationIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public static void arrayCopy(Object src, int srcPos, Object dst, int destPos, int length) {
|
||||
System.arraycopy(src, srcPos, dst, destPos, length);
|
||||
if(dst instanceof Copyable[]) {
|
||||
Object[] oa = (Object[])dst;
|
||||
Copyable<Object>[] c = (Copyable[])dst;
|
||||
for(int i = destPos; i < destPos+length; i++)
|
||||
if(c[i] != null)
|
||||
oa[i] = c[i].copy();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T copyOf(VertexAttribute<T> attr, T src, int length) {
|
||||
T dst = attr.newArray(length);
|
||||
arrayCopy(src, 0, dst, 0, length);
|
||||
return dst;
|
||||
}
|
||||
|
||||
public static interface IVertexSource
|
||||
{
|
||||
public Vertex5[] getVertices();
|
||||
|
||||
/**
|
||||
* Gets an array of vertex attrutes
|
||||
* @param attr The vertex attrute to get
|
||||
* @param <T> The attrute array type
|
||||
* @return An array, or null if not computed
|
||||
*/
|
||||
public <T> T getAttributes(VertexAttribute<T> attr);
|
||||
|
||||
/**
|
||||
* @return True if the specified attrute is provided by this model, either by returning an array from getAttributes or by setting the state in prepareVertex
|
||||
*/
|
||||
public boolean hasAttribute(VertexAttribute<?> attr);
|
||||
|
||||
/**
|
||||
* Callback to set CCRenderState for a vertex before the pipeline runs
|
||||
*/
|
||||
public void prepareVertex();
|
||||
}
|
||||
|
||||
public static VertexAttribute<Vector3[]> normalAttrib = new VertexAttribute<Vector3[]>() {
|
||||
private Vector3[] normalRef;
|
||||
|
||||
@Override
|
||||
public Vector3[] newArray(int length) {
|
||||
return new Vector3[length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
normalRef = model.getAttributes(this);
|
||||
if(model.hasAttribute(this))
|
||||
return normalRef != null;
|
||||
|
||||
if(model.hasAttribute(sideAttrib)) {
|
||||
pipeline.addDependency(sideAttrib);
|
||||
return true;
|
||||
}
|
||||
throw new IllegalStateException("Normals requested but neither normal or side attrutes are provided by the model");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
if(normalRef != null)
|
||||
setNormal(normalRef[vertexIndex]);
|
||||
else
|
||||
setNormal(Rotation.axes[side]);
|
||||
}
|
||||
};
|
||||
public static VertexAttribute<int[]> colourAttrib = new VertexAttribute<int[]>() {
|
||||
private int[] colourRef;
|
||||
|
||||
@Override
|
||||
public int[] newArray(int length) {
|
||||
return new int[length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
colourRef = model.getAttributes(this);
|
||||
return colourRef != null || !model.hasAttribute(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
if(colourRef != null)
|
||||
setColour(ColourRGBA.multiply(baseColour, colourRef[vertexIndex]));
|
||||
else
|
||||
setColour(baseColour);
|
||||
}
|
||||
};
|
||||
public static VertexAttribute<int[]> sideAttrib = new VertexAttribute<int[]>() {
|
||||
private int[] sideRef;
|
||||
|
||||
@Override
|
||||
public int[] newArray(int length) {
|
||||
return new int[length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
sideRef = model.getAttributes(this);
|
||||
if(model.hasAttribute(this))
|
||||
return sideRef != null;
|
||||
|
||||
pipeline.addDependency(normalAttrib);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
if(sideRef != null)
|
||||
side = sideRef[vertexIndex];
|
||||
else
|
||||
side = CCModel.findSide(normal);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Uses the position of the lightmatrix to compute LC if not provided
|
||||
*/
|
||||
public static VertexAttribute<LC[]> lightCoordAttrib = new VertexAttribute<LC[]>() {
|
||||
private LC[] lcRef;
|
||||
private Vector3 vec = new Vector3();//for computation
|
||||
private Vector3 pos = new Vector3();
|
||||
|
||||
@Override
|
||||
public LC[] newArray(int length) {
|
||||
return new LC[length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
lcRef = model.getAttributes(this);
|
||||
if(model.hasAttribute(this))
|
||||
return lcRef != null;
|
||||
|
||||
pos.set(lightMatrix.pos.x, lightMatrix.pos.y, lightMatrix.pos.z);
|
||||
pipeline.addDependency(sideAttrib);
|
||||
pipeline.addRequirement(Transformation.operationIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
if(lcRef != null)
|
||||
lc.set(lcRef[vertexIndex]);
|
||||
else
|
||||
lc.compute(vec.set(vert.vec).sub(pos), side);
|
||||
}
|
||||
};
|
||||
|
||||
//pipeline state
|
||||
public static IVertexSource model;
|
||||
public static int firstVertexIndex;
|
||||
public static int lastVertexIndex;
|
||||
public static int vertexIndex;
|
||||
public static CCRenderPipeline pipeline = new CCRenderPipeline();
|
||||
|
||||
//context
|
||||
public static int baseColour;
|
||||
public static int alphaOverride;
|
||||
public static boolean useNormals;
|
||||
public static LightMatrix lightMatrix = new LightMatrix();
|
||||
|
||||
//vertex outputs
|
||||
public static Vertex5 vert = new Vertex5();
|
||||
public static boolean hasNormal;
|
||||
public static Vector3 normal = new Vector3();
|
||||
public static boolean hasColour;
|
||||
public static int colour;
|
||||
public static boolean hasBrightness;
|
||||
public static int brightness;
|
||||
|
||||
//attrute storage
|
||||
public static int side;
|
||||
public static LC lc = new LC();
|
||||
|
||||
public static void reset() {
|
||||
model = null;
|
||||
pipeline.reset();
|
||||
hasNormal = hasColour = hasBrightness = false;
|
||||
baseColour = alphaOverride = -1;
|
||||
}
|
||||
|
||||
public static void setPipeline(IVertexOperation... ops) {
|
||||
pipeline.setPipeline(ops);
|
||||
}
|
||||
|
||||
public static void setPipeline(IVertexSource model, int start, int end, IVertexOperation... ops) {
|
||||
pipeline.reset();
|
||||
setModel(model, start, end);
|
||||
pipeline.setPipeline(ops);
|
||||
}
|
||||
|
||||
public static void bindModel(IVertexSource model) {
|
||||
if(CCRenderState.model != model) {
|
||||
CCRenderState.model = model;
|
||||
pipeline.rebuild();
|
||||
}
|
||||
}
|
||||
|
||||
public static void setModel(IVertexSource source) {
|
||||
setModel(source, 0, source.getVertices().length);
|
||||
}
|
||||
|
||||
public static void setModel(IVertexSource source, int start, int end) {
|
||||
bindModel(source);
|
||||
firstVertexIndex = start;
|
||||
lastVertexIndex = end;
|
||||
}
|
||||
|
||||
public static void render(IVertexOperation... ops) {
|
||||
setPipeline(ops);
|
||||
render();
|
||||
}
|
||||
|
||||
public static void render() {
|
||||
Vertex5[] verts = model.getVertices();
|
||||
for(vertexIndex = firstVertexIndex; vertexIndex < lastVertexIndex; vertexIndex++) {
|
||||
model.prepareVertex();
|
||||
vert.set(verts[vertexIndex]);
|
||||
runPipeline();
|
||||
writeVert();
|
||||
}
|
||||
}
|
||||
|
||||
public static void runPipeline() {
|
||||
pipeline.operate();
|
||||
}
|
||||
|
||||
public static void writeVert() {
|
||||
if(hasNormal)
|
||||
Tessellator.instance.setNormal((float)normal.x, (float)normal.y, (float)normal.z);
|
||||
if(hasColour)
|
||||
Tessellator.instance.setColorRGBA(colour>>>24, colour>>16 & 0xFF, colour>>8 & 0xFF, alphaOverride >= 0 ? alphaOverride : colour & 0xFF);
|
||||
if(hasBrightness)
|
||||
Tessellator.instance.setBrightness(brightness);
|
||||
if(hasColour)
|
||||
Tessellator.instance.setColorRGBA(colour >> 24 & 0xFF, colour >> 16 & 0xFF, colour >> 8 & 0xFF, colour & 0xFF);
|
||||
Tessellator.instance.addVertexWithUV(vert.vec.x, vert.vec.y, vert.vec.z, vert.uv.u, vert.uv.v);
|
||||
}
|
||||
|
||||
public static void reset()
|
||||
{
|
||||
hasBrightness = false;
|
||||
useModelColours = false;
|
||||
hasColour = false;
|
||||
alphaOverride = false;
|
||||
useNormals = false;
|
||||
public static void setNormal(double x, double y, double z) {
|
||||
hasNormal = true;
|
||||
normal.set(x, y, z);
|
||||
}
|
||||
|
||||
public static void startDrawing(int i)
|
||||
{
|
||||
Tessellator.instance.startDrawing(i);
|
||||
apply();
|
||||
public static void setNormal(Vector3 n) {
|
||||
hasNormal = true;
|
||||
normal.set(n);
|
||||
}
|
||||
|
||||
public static void draw()
|
||||
{
|
||||
Tessellator.instance.draw();
|
||||
public static void setColour(int c) {
|
||||
hasColour = true;
|
||||
colour = c;
|
||||
}
|
||||
|
||||
public static void pullLightmap()
|
||||
{
|
||||
public static void setBrightness(int b) {
|
||||
hasBrightness = true;
|
||||
brightness = b;
|
||||
}
|
||||
|
||||
public static void setBrightness(IBlockAccess world, int x, int y, int z) {
|
||||
setBrightness(world.getBlock(x, y, z).getMixedBrightnessForBlock(world, x, y, z));
|
||||
}
|
||||
|
||||
public static void pullLightmap() {
|
||||
setBrightness((int)OpenGlHelper.lastBrightnessY << 16 | (int)OpenGlHelper.lastBrightnessX);
|
||||
}
|
||||
|
||||
public static void applyBrightnessTexCoords()
|
||||
{
|
||||
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, brightness&0xFFFF, brightness>>>16);
|
||||
public static void changeTexture(String texture) {
|
||||
changeTexture(new ResourceLocation(texture));
|
||||
}
|
||||
|
||||
public static void glColour()
|
||||
{
|
||||
new ColourRGBA(colour).glColour();
|
||||
public static void changeTexture(ResourceLocation texture) {
|
||||
Minecraft.getMinecraft().renderEngine.bindTexture(texture);
|
||||
}
|
||||
|
||||
public static void startDrawing() {
|
||||
startDrawing(7);
|
||||
}
|
||||
|
||||
private static void startDrawing(int mode) {
|
||||
Tessellator.instance.startDrawing(mode);
|
||||
if(hasColour)
|
||||
Tessellator.instance.setColorRGBA(colour>>>24, colour>>16 & 0xFF, colour>>8 & 0xFF, alphaOverride >= 0 ? alphaOverride : colour & 0xFF);
|
||||
if(hasBrightness)
|
||||
Tessellator.instance.setBrightness(brightness);
|
||||
}
|
||||
|
||||
public static void draw() {
|
||||
Tessellator.instance.draw();
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
|
||||
public class ColourModifier implements IVertexModifier
|
||||
{
|
||||
public static final ColourModifier instance = new ColourModifier();
|
||||
|
||||
@Override
|
||||
public void applyModifiers(CCModel m, Tessellator tess, Vector3 vec, UV uv, Vector3 normal, int i)
|
||||
{
|
||||
if(CCRenderState.useModelColours() && m != null && m.colours != null)
|
||||
CCRenderState.vertexColour(m.colours[i]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsNormals()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,39 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import codechicken.lib.colour.Colour;
|
||||
import codechicken.lib.colour.ColourRGBA;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
|
||||
public class ColourMultiplier implements IVertexModifier
|
||||
public class ColourMultiplier implements CCRenderState.IVertexOperation
|
||||
{
|
||||
public Colour colour;
|
||||
|
||||
public ColourMultiplier(Colour colour)
|
||||
{
|
||||
private static ColourMultiplier instance = new ColourMultiplier(-1);
|
||||
|
||||
public static ColourMultiplier instance(int colour) {
|
||||
instance.colour = colour;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static final int operationIndex = CCRenderState.registerOperation();
|
||||
public int colour;
|
||||
|
||||
public ColourMultiplier(int colour) {
|
||||
this.colour = colour;
|
||||
}
|
||||
|
||||
public ColourMultiplier(int colour)
|
||||
{
|
||||
this(new ColourRGBA(colour));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void applyModifiers(CCModel m, Tessellator tess, Vector3 vec, UV uv, Vector3 normal, int i)
|
||||
{
|
||||
if(CCRenderState.useModelColours() && m != null && m.colours != null)
|
||||
CCRenderState.vertexColour(new ColourRGBA(m.colours[i]).multiply(colour).rgba());
|
||||
public boolean load() {
|
||||
if(colour == -1)
|
||||
return false;
|
||||
|
||||
CCRenderState.pipeline.addDependency(CCRenderState.colourAttrib);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsNormals()
|
||||
{
|
||||
return false;
|
||||
public void operate() {
|
||||
CCRenderState.setColour(ColourRGBA.multiply(CCRenderState.colour, colour));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int operationID() {
|
||||
return operationIndex;
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,12 @@ import codechicken.lib.vec.Vector3;
|
||||
import net.minecraft.client.particle.EffectRenderer;
|
||||
import net.minecraft.client.particle.EntityFX;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.util.Icon;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class EntityDigIconFX extends EntityFX
|
||||
{
|
||||
public EntityDigIconFX(World world, double x, double y, double z, double dx, double dy, double dz, Icon icon)
|
||||
public EntityDigIconFX(World world, double x, double y, double z, double dx, double dy, double dz, IIcon icon)
|
||||
{
|
||||
super(world, x, y, z, dx, dy, dz);
|
||||
particleIcon = icon;
|
||||
@@ -72,7 +72,7 @@ public class EntityDigIconFX extends EntityFX
|
||||
par1Tessellator.addVertexWithUV(f11 + par3 * f10 - par6 * f10, f12 - par4 * f10, f13 + par5 * f10 - par7 * f10, f7, f9);
|
||||
}
|
||||
|
||||
public static void addBlockHitEffects(World world, Cuboid6 bounds, int side, Icon icon, EffectRenderer effectRenderer)
|
||||
public static void addBlockHitEffects(World world, Cuboid6 bounds, int side, IIcon icon, EffectRenderer effectRenderer)
|
||||
{
|
||||
float border = 0.1F;
|
||||
Vector3 diff = bounds.max.copy().subtract(bounds.min).add(-2*border);
|
||||
@@ -99,7 +99,7 @@ public class EntityDigIconFX extends EntityFX
|
||||
.multiplyVelocity(0.2F).multipleParticleScaleBy(0.6F));
|
||||
}
|
||||
|
||||
public static void addBlockDestroyEffects(World world, Cuboid6 bounds, Icon[] icons, EffectRenderer effectRenderer)
|
||||
public static void addBlockDestroyEffects(World world, Cuboid6 bounds, IIcon[] icons, EffectRenderer effectRenderer)
|
||||
{
|
||||
Vector3 diff = bounds.max.copy().subtract(bounds.min);
|
||||
Vector3 center = bounds.min.copy().add(bounds.max).multiply(0.5);
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
public interface IUVTransformation
|
||||
{
|
||||
public void transform(UV texcoord);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
|
||||
public interface IVertexModifier
|
||||
{
|
||||
public void applyModifiers(CCModel m, Tessellator tess, Vector3 vec, UV uv, Vector3 normal, int i);
|
||||
|
||||
public boolean needsNormals();
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.util.Icon;
|
||||
|
||||
public class IconTransformation implements IUVTransformation
|
||||
{
|
||||
public Icon icon;
|
||||
|
||||
public IconTransformation(Icon icon)
|
||||
{
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(UV texcoord)
|
||||
{
|
||||
texcoord.u = icon.getInterpolatedU(texcoord.u%2*16);
|
||||
texcoord.v = icon.getInterpolatedV(texcoord.v%2*16);
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.util.Icon;
|
||||
|
||||
/**
|
||||
* Icon index is specified as (int)u>>1
|
||||
*/
|
||||
public class MultiIconTransformation implements IUVTransformation
|
||||
{
|
||||
public Icon[] icons;
|
||||
|
||||
public MultiIconTransformation(Icon... icons)
|
||||
{
|
||||
this.icons = icons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(UV texcoord)
|
||||
{
|
||||
int i = (int)texcoord.u>>1;
|
||||
Icon icon = icons[i%icons.length];
|
||||
texcoord.u = icon.getInterpolatedU(texcoord.u%2*16);
|
||||
texcoord.v = icon.getInterpolatedV(texcoord.v%2*16);
|
||||
}
|
||||
|
||||
public static CCModel setIconIndex(CCModel m, int index)
|
||||
{
|
||||
return setIconIndex(m, 0, m.verts.length, index);
|
||||
}
|
||||
|
||||
public static CCModel setIconIndex(CCModel m, int start, int end, int index)
|
||||
{
|
||||
for(int k = start; k < end; k++)
|
||||
{
|
||||
UV uv = m.verts[k].uv;
|
||||
uv.u = uv.u%2+index*2;
|
||||
uv.v %= 2;
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.resources.ResourceManager;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
public class PlaceholderTexture extends TextureAtlasSprite
|
||||
@@ -12,7 +12,7 @@ public class PlaceholderTexture extends TextureAtlasSprite
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load(ResourceManager manager, ResourceLocation location)
|
||||
public boolean load(IResourceManager manager, ResourceLocation location)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.render.uv.UV;
|
||||
import codechicken.lib.render.uv.UVScale;
|
||||
import codechicken.lib.vec.*;
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.IconRegister;
|
||||
import net.minecraft.util.Icon;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
@@ -638,7 +640,7 @@ public class QBImporter
|
||||
return map.get(key).m;
|
||||
}
|
||||
|
||||
public Icon getIcon(String key, IconRegister r, String iconName) {
|
||||
public IIcon getIcon(String key, IIconRegister r, String iconName) {
|
||||
int img = map.get(key).img;
|
||||
if(icons[img] != null && !iconName.equals(icons[img]))
|
||||
throw new IllegalArgumentException("Attempted to get a previously registered icon by a different name: "+icons[img]+", "+iconName);
|
||||
|
||||
@@ -3,6 +3,8 @@ package codechicken.lib.render;
|
||||
import static net.minecraftforge.client.IItemRenderer.ItemRenderType.ENTITY;
|
||||
import static net.minecraftforge.client.IItemRenderer.ItemRendererHelper.BLOCK_3D;
|
||||
|
||||
import codechicken.lib.render.uv.UV;
|
||||
import codechicken.lib.render.uv.UVTransformation;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import codechicken.lib.vec.Cuboid6;
|
||||
@@ -10,7 +12,7 @@ import codechicken.lib.vec.Rectangle4i;
|
||||
import codechicken.lib.vec.Transformation;
|
||||
import codechicken.lib.vec.Rotation;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
import net.minecraft.util.Icon;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
@@ -48,7 +50,7 @@ public class RenderUtils
|
||||
entityItem.hoverStart = 0;
|
||||
}
|
||||
|
||||
public static void renderFluidQuad(Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4, Icon icon, double res)
|
||||
public static void renderFluidQuad(Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4, IIcon icon, double res)
|
||||
{
|
||||
renderFluidQuad(point2, vectors[0].set(point4).subtract(point1), vectors[1].set(point1).subtract(point2), icon, res);
|
||||
}
|
||||
@@ -60,7 +62,7 @@ public class RenderUtils
|
||||
* @param high The left side of the quad
|
||||
* @param res Units per icon
|
||||
*/
|
||||
public static void renderFluidQuad(Vector3 base, Vector3 wide, Vector3 high, Icon icon, double res)
|
||||
public static void renderFluidQuad(Vector3 base, Vector3 wide, Vector3 high, IIcon icon, double res)
|
||||
{
|
||||
Tessellator t = Tessellator.instance;
|
||||
|
||||
@@ -141,7 +143,7 @@ public class RenderUtils
|
||||
var2.draw();
|
||||
}
|
||||
|
||||
public static void renderFluidCuboid(Cuboid6 bound, Icon tex, double res)
|
||||
public static void renderFluidCuboid(Cuboid6 bound, IIcon tex, double res)
|
||||
{
|
||||
renderFluidQuad(//bottom
|
||||
new Vector3(bound.min.x, bound.min.y, bound.min.z),
|
||||
@@ -236,7 +238,7 @@ public class RenderUtils
|
||||
* @param stack The fluid stack to render
|
||||
* @return The icon of the fluid
|
||||
*/
|
||||
public static Icon prepareFluidRender(FluidStack stack, int alpha)
|
||||
public static IIcon prepareFluidRender(FluidStack stack, int alpha)
|
||||
{
|
||||
GL11.glDisable(GL11.GL_LIGHTING);
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
@@ -283,8 +285,8 @@ public class RenderUtils
|
||||
else
|
||||
bound.max.y = bound.min.y+(bound.max.y-bound.min.y)*density;
|
||||
|
||||
Icon tex = prepareFluidRender(stack, alpha);
|
||||
CCRenderState.startDrawing(7);
|
||||
IIcon tex = prepareFluidRender(stack, alpha);
|
||||
CCRenderState.startDrawing();
|
||||
renderFluidCuboid(bound, tex, res);
|
||||
CCRenderState.draw();
|
||||
postFluidRender();
|
||||
@@ -305,8 +307,8 @@ public class RenderUtils
|
||||
rect.h = height;
|
||||
}
|
||||
|
||||
Icon tex = prepareFluidRender(stack, alpha);
|
||||
CCRenderState.startDrawing(7);
|
||||
IIcon tex = prepareFluidRender(stack, alpha);
|
||||
CCRenderState.startDrawing();
|
||||
renderFluidQuad(
|
||||
new Vector3(rect.x, rect.y+rect.h, 0),
|
||||
new Vector3(rect.w,0, 0),
|
||||
@@ -333,9 +335,9 @@ public class RenderUtils
|
||||
boolean is3D = customRenderer != null && customRenderer.shouldUseRenderHelper(ENTITY, item, BLOCK_3D);
|
||||
|
||||
boolean larger = false;
|
||||
if (item.getItem() instanceof ItemBlock && RenderBlocks.renderItemIn3d(Block.blocksList[item.itemID].getRenderType()))
|
||||
if (item.getItem() instanceof ItemBlock && RenderBlocks.renderItemIn3d(Block.getBlockFromItem(item.getItem()).getRenderType()))
|
||||
{
|
||||
int renderType = Block.blocksList[item.itemID].getRenderType();
|
||||
int renderType = Block.getBlockFromItem(item.getItem()).getRenderType();
|
||||
larger = !(renderType == 1 || renderType == 19 || renderType == 12 || renderType == 2);
|
||||
}
|
||||
else if(is3D)
|
||||
@@ -351,137 +353,9 @@ public class RenderUtils
|
||||
GL11.glColor4f(1, 1, 1, 1);
|
||||
|
||||
entityItem.setEntityItemStack(item);
|
||||
uniformRenderItem.doRenderItem(entityItem, 0, larger ? 0.09 : 0.06, 0, 0, (float)(spin*9/Math.PI));
|
||||
uniformRenderItem.doRender(entityItem, 0, larger ? 0.09 : 0.06, 0, 0, (float)(spin*9/Math.PI));
|
||||
|
||||
if(larger)
|
||||
GL11.glScaled(d1, d1, d1);
|
||||
}
|
||||
|
||||
private static Vertex5[] face = new Vertex5[]{new Vertex5(), new Vertex5(), new Vertex5(), new Vertex5()};
|
||||
public static void renderBlock(Cuboid6 c, int sideMask, IFaceRenderer r)
|
||||
{
|
||||
double x1 = c.min.x;
|
||||
double x2 = c.max.x;
|
||||
double y1 = c.min.y;
|
||||
double y2 = c.max.y;
|
||||
double z1 = c.min.z;
|
||||
double z2 = c.max.z;
|
||||
double u1 = 0;
|
||||
double u2 = 0;
|
||||
double v1 = 0;
|
||||
double v2 = 0;
|
||||
|
||||
if((sideMask&1) == 0)
|
||||
{
|
||||
u1 = x1; v1 = z1;
|
||||
u2 = x2; v2 = z2;
|
||||
face[0].set(x1, y1, z2, u1, v2);
|
||||
face[1].set(x1, y1, z1, u1, v1);
|
||||
face[2].set(x2, y1, z1, u2, v1);
|
||||
face[3].set(x2, y1, z2, u2, v2);
|
||||
r.renderFace(face, c.min.y > 0 ? 6 : 0);
|
||||
}
|
||||
|
||||
if((sideMask&2) == 0)
|
||||
{
|
||||
u1 = x1+2; v1 = z1;
|
||||
u2 = x2+2; v2 = z2;
|
||||
face[0].set(x2, y2, z2, u2, v2);
|
||||
face[1].set(x2, y2, z1, u2, v1);
|
||||
face[2].set(x1, y2, z1, u1, v1);
|
||||
face[3].set(x1, y2, z2, u1, v2);
|
||||
r.renderFace(face, c.max.y < 1 ? 7 : 1);
|
||||
}
|
||||
|
||||
if((sideMask&4) == 0)
|
||||
{
|
||||
u1 = 1-x1+4; v1 = 1-y2;
|
||||
u2 = 1-x2+4; v2 = 1-y1;
|
||||
face[0].set(x1, y1, z1, u1, v2);
|
||||
face[1].set(x1, y2, z1, u1, v1);
|
||||
face[2].set(x2, y2, z1, u2, v1);
|
||||
face[3].set(x2, y1, z1, u2, v2);
|
||||
r.renderFace(face, c.min.z > 0 ? 8 : 2);
|
||||
}
|
||||
|
||||
if((sideMask&8) == 0)
|
||||
{
|
||||
u1 = x1+6; v1 = 1-y2;
|
||||
u2 = x2+6; v2 = 1-y1;
|
||||
face[0].set(x2, y1, z2, u2, v2);
|
||||
face[1].set(x2, y2, z2, u2, v1);
|
||||
face[2].set(x1, y2, z2, u1, v1);
|
||||
face[3].set(x1, y1, z2, u1, v2);
|
||||
r.renderFace(face, c.max.z < 1 ? 9 : 3);
|
||||
}
|
||||
|
||||
if((sideMask&0x10) == 0)
|
||||
{
|
||||
u1 = z1+8; v1 = 1-y2;
|
||||
u2 = z2+8; v2 = 1-y1;
|
||||
face[0].set(x1, y1, z2, u2, v2);
|
||||
face[1].set(x1, y2, z2, u2, v1);
|
||||
face[2].set(x1, y2, z1, u1, v1);
|
||||
face[3].set(x1, y1, z1, u1, v2);
|
||||
r.renderFace(face, c.min.x > 0 ? 10 : 4);
|
||||
}
|
||||
|
||||
if((sideMask&0x20) == 0)
|
||||
{
|
||||
u1 = 1-z1+10; v1 = 1-y2;
|
||||
u2 = 1-z2+10; v2 = 1-y1;
|
||||
face[0].set(x2, y1, z1, u1, v2);
|
||||
face[1].set(x2, y2, z1, u1, v1);
|
||||
face[2].set(x2, y2, z2, u2, v1);
|
||||
face[3].set(x2, y1, z2, u2, v2);
|
||||
r.renderFace(face, c.max.x < 1 ? 11 : 5);
|
||||
}
|
||||
}
|
||||
|
||||
public static void renderBlock(Cuboid6 bounds, int sideMask, final Transformation t, final IUVTransformation u, final IVertexModifier m)
|
||||
{
|
||||
renderBlock(bounds, sideMask, new IFaceRenderer(){
|
||||
boolean drawNormal = CCRenderState.useNormals();
|
||||
boolean computeNormal = drawNormal || m != null && m.needsNormals();
|
||||
Vector3 normal = new Vector3();
|
||||
Vertex5 vert;
|
||||
Vector3 vec = new Vector3();
|
||||
UV uv = new UV();
|
||||
|
||||
@Override
|
||||
public void renderFace(Vertex5[] face, int side)
|
||||
{
|
||||
Tessellator tess = Tessellator.instance;
|
||||
for(int i = 0; i < face.length; i++)
|
||||
{
|
||||
if(computeNormal)
|
||||
{
|
||||
if(t != null)
|
||||
t.applyN(normal.set(Rotation.axes[side%6]));
|
||||
else
|
||||
normal = Rotation.axes[side%6];
|
||||
|
||||
if(drawNormal)
|
||||
tess.setNormal((float)normal.x, (float)normal.y, (float)normal.z);
|
||||
}
|
||||
|
||||
vert = face[i];
|
||||
if(t != null)
|
||||
t.apply(vec.set(vert.vec));
|
||||
else
|
||||
vec = vert.vec;
|
||||
|
||||
if(u != null)
|
||||
u.transform(uv.set(vert.uv));
|
||||
else
|
||||
uv = vert.uv;
|
||||
|
||||
if(m != null)
|
||||
m.applyModifiers(null, tess, vec, uv, normal, i);
|
||||
|
||||
tess.addVertexWithUV(vec.x, vec.y, vec.z, uv.u, uv.v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,19 +3,19 @@ package codechicken.lib.render;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import codechicken.lib.render.TextureUtils.IIconRegister;
|
||||
import codechicken.lib.render.TextureUtils.IIconSelfRegister;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
import net.minecraft.client.renderer.texture.IconRegister;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.util.Icon;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
public class SpriteSheetManager
|
||||
{
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static class SpriteSheet implements IIconRegister
|
||||
public static class SpriteSheet implements IIconSelfRegister
|
||||
{
|
||||
private int tilesX;
|
||||
private int tilesY;
|
||||
@@ -42,7 +42,7 @@ public class SpriteSheetManager
|
||||
setupSprite(i);
|
||||
}
|
||||
|
||||
public void registerIcons(IconRegister register)
|
||||
public void registerIcons(IIconRegister register)
|
||||
{
|
||||
TextureMap textureMap = (TextureMap)register;
|
||||
|
||||
@@ -79,9 +79,9 @@ public class SpriteSheetManager
|
||||
spriteHeight = texture.height/tilesY;
|
||||
}
|
||||
|
||||
public Icon getSprite(int index)
|
||||
public IIcon getSprite(int index)
|
||||
{
|
||||
Icon i = sprites[index];
|
||||
IIcon i = sprites[index];
|
||||
if(i == null)
|
||||
throw new IllegalArgumentException("Sprite at index: "+index+" from texture file "+resource+" was not preloaded.");
|
||||
return i;
|
||||
|
||||
@@ -13,55 +13,47 @@ public class TextureFX
|
||||
public int tileSizeSquare = 256;
|
||||
public int tileSizeMask = 15;
|
||||
public int tileSizeSquareMask = 255;
|
||||
|
||||
|
||||
public boolean anaglyphEnabled;
|
||||
public TextureSpecial texture;
|
||||
|
||||
public TextureFX(int spriteIndex, SpriteSheet sheet)
|
||||
{
|
||||
public TextureFX(int spriteIndex, SpriteSheet sheet) {
|
||||
texture = sheet.bindTextureFX(spriteIndex, this);
|
||||
}
|
||||
|
||||
public TextureFX(int size, String name)
|
||||
{
|
||||
|
||||
public TextureFX(int size, String name) {
|
||||
texture = new TextureSpecial(name).blank(size).selfRegister().addTextureFX(this);
|
||||
}
|
||||
|
||||
public TextureFX setAtlas(int index)
|
||||
{
|
||||
|
||||
public TextureFX setAtlas(int index) {
|
||||
texture.atlasIndex = index;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setup()
|
||||
{
|
||||
public void setup() {
|
||||
imageData = new int[tileSizeSquare];
|
||||
}
|
||||
|
||||
public void onTextureDimensionsUpdate(int width, int height)
|
||||
{
|
||||
if(width != height)
|
||||
throw new IllegalArgumentException("Non-Square textureFX not supported ("+width+":"+height+")");
|
||||
|
||||
|
||||
public void onTextureDimensionsUpdate(int width, int height) {
|
||||
if (width != height)
|
||||
throw new IllegalArgumentException("Non-Square textureFX not supported (" + width + ":" + height + ")");
|
||||
|
||||
tileSizeBase = width;
|
||||
tileSizeSquare = tileSizeBase * tileSizeBase;
|
||||
tileSizeMask = tileSizeBase - 1;
|
||||
tileSizeSquareMask = tileSizeSquare - 1;
|
||||
setup();
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
|
||||
public void update() {
|
||||
anaglyphEnabled = Minecraft.getMinecraft().gameSettings.anaglyph;
|
||||
onTick();
|
||||
}
|
||||
|
||||
public void onTick()
|
||||
{
|
||||
public void onTick() {
|
||||
}
|
||||
|
||||
public boolean changed()
|
||||
{
|
||||
|
||||
public boolean changed() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,150 +1,148 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import codechicken.lib.render.SpriteSheetManager.SpriteSheet;
|
||||
import codechicken.lib.render.TextureUtils.IIconRegister;
|
||||
import codechicken.lib.render.TextureUtils.IIconSelfRegister;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.client.renderer.texture.IconRegister;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.client.renderer.texture.TextureUtil;
|
||||
import net.minecraft.client.resources.ResourceManager;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.client.settings.GameSettings;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class TextureSpecial extends TextureAtlasSprite implements IIconRegister
|
||||
{
|
||||
public class TextureSpecial extends TextureAtlasSprite implements IIconSelfRegister {
|
||||
//sprite sheet fields
|
||||
private int spriteIndex;
|
||||
private SpriteSheet spriteSheet;
|
||||
|
||||
|
||||
//textureFX fields
|
||||
private TextureFX textureFX;
|
||||
|
||||
private int blankSize = -1;
|
||||
|
||||
private int blankSize = -1;
|
||||
private ArrayList<TextureDataHolder> baseTextures;
|
||||
|
||||
|
||||
private boolean selfRegister;
|
||||
public int atlasIndex;
|
||||
|
||||
protected TextureSpecial(String par1)
|
||||
{
|
||||
|
||||
protected TextureSpecial(String par1) {
|
||||
super(par1);
|
||||
}
|
||||
|
||||
public TextureSpecial addTexture(TextureDataHolder t)
|
||||
{
|
||||
if(baseTextures == null)
|
||||
|
||||
public TextureSpecial addTexture(TextureDataHolder t) {
|
||||
if (baseTextures == null)
|
||||
baseTextures = new ArrayList<TextureDataHolder>();
|
||||
baseTextures.add(t);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextureSpecial baseFromSheet(SpriteSheet spriteSheet, int spriteIndex)
|
||||
{
|
||||
|
||||
public TextureSpecial baseFromSheet(SpriteSheet spriteSheet, int spriteIndex) {
|
||||
this.spriteSheet = spriteSheet;
|
||||
this.spriteIndex = spriteIndex;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextureSpecial addTextureFX(TextureFX fx)
|
||||
{
|
||||
|
||||
public TextureSpecial addTextureFX(TextureFX fx) {
|
||||
textureFX = fx;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void initSprite(int sheetWidth, int sheetHeight, int originX, int originY, boolean rotated)
|
||||
{
|
||||
public void initSprite(int sheetWidth, int sheetHeight, int originX, int originY, boolean rotated) {
|
||||
super.initSprite(sheetWidth, sheetHeight, originX, originY, rotated);
|
||||
if(textureFX != null)
|
||||
if (textureFX != null)
|
||||
textureFX.onTextureDimensionsUpdate(width, height);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateAnimation()
|
||||
{
|
||||
if(textureFX != null)
|
||||
{
|
||||
public void updateAnimation() {
|
||||
if (textureFX != null) {
|
||||
textureFX.update();
|
||||
if(textureFX.changed())
|
||||
TextureUtil.uploadTextureSub(textureFX.imageData, width, height, originX, originY, false, false);
|
||||
if (textureFX.changed())
|
||||
TextureUtil.uploadTextureSub(0, textureFX.imageData, width, height, originX, originY, false, false, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean load(ResourceManager manager, ResourceLocation location)
|
||||
{
|
||||
if(baseTextures != null)
|
||||
{
|
||||
for(TextureDataHolder tex : baseTextures)
|
||||
{
|
||||
framesTextureData.add(tex.data);
|
||||
public boolean hasCustomLoader(IResourceManager manager, ResourceLocation location) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void addFrame(int[] data) {
|
||||
GameSettings settings = Minecraft.getMinecraft().gameSettings;
|
||||
BufferedImage[] images = new BufferedImage[settings.mipmapLevels+1];
|
||||
images[0] = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||
images[0].setRGB(0, 0, width, height, data, 0, width);
|
||||
|
||||
super.loadSprite(images, null, settings.anisotropicFiltering > 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load(IResourceManager manager, ResourceLocation location) {
|
||||
if (baseTextures != null) {
|
||||
for (TextureDataHolder tex : baseTextures) {
|
||||
width = tex.width;
|
||||
height = tex.height;
|
||||
addFrame(tex.data);
|
||||
}
|
||||
}
|
||||
|
||||
if(spriteSheet != null)
|
||||
{
|
||||
|
||||
if (spriteSheet != null) {
|
||||
TextureDataHolder tex = spriteSheet.createSprite(spriteIndex);
|
||||
width = tex.width;
|
||||
height = tex.height;
|
||||
framesTextureData.add(tex.data);
|
||||
addFrame(tex.data);
|
||||
}
|
||||
|
||||
if(blankSize > 0)
|
||||
{
|
||||
|
||||
if (blankSize > 0) {
|
||||
width = height = blankSize;
|
||||
framesTextureData.add(new int[blankSize*blankSize]);
|
||||
addFrame(new int[blankSize * blankSize]);
|
||||
}
|
||||
|
||||
if(framesTextureData.isEmpty())
|
||||
throw new RuntimeException("No base frame for texture: "+getIconName());
|
||||
|
||||
return true;
|
||||
|
||||
if (framesTextureData.isEmpty())
|
||||
throw new RuntimeException("No base frame for texture: " + getIconName());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasAnimationMetadata()
|
||||
{
|
||||
public boolean hasAnimationMetadata() {
|
||||
return textureFX != null || super.hasAnimationMetadata();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getFrameCount()
|
||||
{
|
||||
if(textureFX != null)
|
||||
public int getFrameCount() {
|
||||
if (textureFX != null)
|
||||
return 1;
|
||||
|
||||
|
||||
return super.getFrameCount();
|
||||
}
|
||||
|
||||
public TextureSpecial blank(int size)
|
||||
{
|
||||
public TextureSpecial blank(int size) {
|
||||
blankSize = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextureSpecial selfRegister()
|
||||
{
|
||||
|
||||
public TextureSpecial selfRegister() {
|
||||
selfRegister = true;
|
||||
TextureUtils.addIconRegistrar(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void registerIcons(IconRegister register)
|
||||
{
|
||||
if(selfRegister)
|
||||
((TextureMap)register).setTextureEntry(getIconName(), this);
|
||||
public void registerIcons(IIconRegister register) {
|
||||
if (selfRegister)
|
||||
((TextureMap) register).setTextureEntry(getIconName(), this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int atlasIndex()
|
||||
{
|
||||
public int atlasIndex() {
|
||||
return atlasIndex;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import java.util.ArrayList;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL12;
|
||||
|
||||
@@ -15,138 +16,118 @@ import codechicken.lib.colour.ColourARGB;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.TextureManager;
|
||||
import net.minecraft.client.renderer.texture.IconRegister;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.util.Icon;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.event.TextureStitchEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.ForgeSubscribe;
|
||||
|
||||
public class TextureUtils
|
||||
{
|
||||
public static interface IIconRegister
|
||||
{
|
||||
public static interface IIconSelfRegister
|
||||
{
|
||||
public void registerIcons(IconRegister register);
|
||||
public void registerIcons(IIconRegister register);
|
||||
|
||||
public int atlasIndex();
|
||||
}
|
||||
|
||||
static
|
||||
{
|
||||
|
||||
static {
|
||||
MinecraftForge.EVENT_BUS.register(new TextureUtils());
|
||||
}
|
||||
|
||||
private static ArrayList<IIconRegister> iconRegistrars = new ArrayList<TextureUtils.IIconRegister>();
|
||||
|
||||
public static void addIconRegistrar(IIconRegister registrar)
|
||||
{
|
||||
|
||||
private static ArrayList<IIconSelfRegister> iconRegistrars = new ArrayList<TextureUtils.IIconSelfRegister>();
|
||||
|
||||
public static void addIconRegistrar(IIconSelfRegister registrar) {
|
||||
iconRegistrars.add(registrar);
|
||||
}
|
||||
|
||||
@ForgeSubscribe
|
||||
public void textureLoad(TextureStitchEvent.Pre event)
|
||||
{
|
||||
for(IIconRegister reg : iconRegistrars)
|
||||
if(reg.atlasIndex() == event.map.textureType)
|
||||
|
||||
@SubscribeEvent
|
||||
public void textureLoad(TextureStitchEvent.Pre event) {
|
||||
for (IIconSelfRegister reg : iconRegistrars)
|
||||
if (reg.atlasIndex() == event.map.getTextureType())
|
||||
reg.registerIcons(event.map);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return an array of ARGB pixel data
|
||||
*/
|
||||
public static int[] loadTextureData(ResourceLocation resource)
|
||||
{
|
||||
public static int[] loadTextureData(ResourceLocation resource) {
|
||||
return loadTexture(resource).data;
|
||||
}
|
||||
|
||||
public static Colour[] loadTextureColours(ResourceLocation resource)
|
||||
{
|
||||
public static Colour[] loadTextureColours(ResourceLocation resource) {
|
||||
int[] idata = loadTextureData(resource);
|
||||
Colour[] data = new Colour[idata.length];
|
||||
for(int i = 0; i < data.length; i++)
|
||||
for (int i = 0; i < data.length; i++)
|
||||
data[i] = new ColourARGB(idata[i]);
|
||||
return data;
|
||||
}
|
||||
|
||||
public static InputStream getTextureResource(ResourceLocation textureFile) throws IOException
|
||||
{
|
||||
|
||||
public static InputStream getTextureResource(ResourceLocation textureFile) throws IOException {
|
||||
return Minecraft.getMinecraft().getResourceManager().getResource(textureFile).getInputStream();
|
||||
}
|
||||
|
||||
public static BufferedImage loadBufferedImage(ResourceLocation textureFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
public static BufferedImage loadBufferedImage(ResourceLocation textureFile) {
|
||||
try {
|
||||
return loadBufferedImage(getTextureResource(textureFile));
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
System.err.println("Failed to load texture file: "+textureFile);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to load texture file: " + textureFile);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static BufferedImage loadBufferedImage(InputStream in) throws IOException
|
||||
{
|
||||
public static BufferedImage loadBufferedImage(InputStream in) throws IOException {
|
||||
BufferedImage img = ImageIO.read(in);
|
||||
in.close();
|
||||
return img;
|
||||
}
|
||||
|
||||
public static TextureManager engine()
|
||||
{
|
||||
public static TextureManager engine() {
|
||||
return Minecraft.getMinecraft().renderEngine;
|
||||
}
|
||||
|
||||
public static void copySubImg(int[] fromTex, int fromWidth, int fromX, int fromY, int width, int height, int[] toTex, int toWidth, int toX, int toY)
|
||||
{
|
||||
for(int y = 0; y < height; y++)
|
||||
for(int x = 0; x < width; x++)
|
||||
{
|
||||
int fp = (y+fromY)*fromWidth+x+fromX;
|
||||
int tp = (y+toX)*toWidth+x+toX;
|
||||
public static void copySubImg(int[] fromTex, int fromWidth, int fromX, int fromY, int width, int height, int[] toTex, int toWidth, int toX, int toY) {
|
||||
for (int y = 0; y < height; y++)
|
||||
for (int x = 0; x < width; x++) {
|
||||
int fp = (y + fromY) * fromWidth + x + fromX;
|
||||
int tp = (y + toX) * toWidth + x + toX;
|
||||
|
||||
toTex[tp] = fromTex[fp];
|
||||
}
|
||||
}
|
||||
|
||||
public static void bindAtlas(int atlasIndex)
|
||||
{
|
||||
public static void bindAtlas(int atlasIndex) {
|
||||
engine().bindTexture(atlasIndex == 0 ? TextureMap.locationBlocksTexture : TextureMap.locationItemsTexture);
|
||||
}
|
||||
|
||||
public static Icon getBlankIcon(int size, IconRegister iconRegister)
|
||||
{
|
||||
TextureMap textureMap = (TextureMap)iconRegister;
|
||||
String s = "blank_"+size;
|
||||
if(textureMap.getTextureExtry(s) == null)
|
||||
{
|
||||
|
||||
public static IIcon getBlankIcon(int size, IIconRegister iconRegister) {
|
||||
TextureMap textureMap = (TextureMap) iconRegister;
|
||||
String s = "blank_" + size;
|
||||
if (textureMap.getTextureExtry(s) == null) {
|
||||
TextureSpecial icon = new TextureSpecial(s).blank(size);
|
||||
textureMap.setTextureEntry(s, icon);
|
||||
}
|
||||
return iconRegister.registerIcon(s);
|
||||
}
|
||||
|
||||
public static TextureSpecial getTextureSpecial(IconRegister iconRegister, String name)
|
||||
{
|
||||
|
||||
public static TextureSpecial getTextureSpecial(IIconRegister iconRegister, String name) {
|
||||
TextureMap textureMap = (TextureMap) iconRegister;
|
||||
Icon entry = textureMap.getTextureExtry(name);
|
||||
if(entry != null)
|
||||
throw new IllegalStateException("Texture: "+name+" is already registered");
|
||||
|
||||
IIcon entry = textureMap.getTextureExtry(name);
|
||||
if (entry != null)
|
||||
throw new IllegalStateException("Texture: " + name + " is already registered");
|
||||
|
||||
TextureSpecial icon = new TextureSpecial(name);
|
||||
textureMap.setTextureEntry(name, icon);
|
||||
return icon;
|
||||
}
|
||||
|
||||
public static void prepareTexture(int target, int texture, int min_mag_filter, int wrap)
|
||||
{
|
||||
public static void prepareTexture(int target, int texture, int min_mag_filter, int wrap) {
|
||||
GL11.glBindTexture(target, texture);
|
||||
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, min_mag_filter);
|
||||
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, min_mag_filter);
|
||||
switch(target)
|
||||
{
|
||||
switch (target) {
|
||||
case GL12.GL_TEXTURE_3D:
|
||||
GL11.glTexParameteri(target, GL12.GL_TEXTURE_WRAP_R, wrap);
|
||||
case GL11.GL_TEXTURE_2D:
|
||||
@@ -156,32 +137,28 @@ public class TextureUtils
|
||||
}
|
||||
}
|
||||
|
||||
public static TextureDataHolder loadTexture(ResourceLocation resource)
|
||||
{
|
||||
public static TextureDataHolder loadTexture(ResourceLocation resource) {
|
||||
BufferedImage img = loadBufferedImage(resource);
|
||||
if(img == null)
|
||||
throw new RuntimeException("Texture not found: "+resource);
|
||||
if (img == null)
|
||||
throw new RuntimeException("Texture not found: " + resource);
|
||||
return new TextureDataHolder(img);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses an empty placeholder texture to tell if the map has been reloaded since the last call to refresh texture and the texture with name needs to be reacquired to be valid
|
||||
*/
|
||||
public static boolean refreshTexture(TextureMap map, String name)
|
||||
{
|
||||
if(map.getTextureExtry(name) == null)
|
||||
{
|
||||
public static boolean refreshTexture(TextureMap map, String name) {
|
||||
if (map.getTextureExtry(name) == null) {
|
||||
map.setTextureEntry(name, new PlaceholderTexture(name));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Icon safeIcon(Icon icon)
|
||||
{
|
||||
if(icon == null)
|
||||
icon = ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).getAtlasSprite("missingno");
|
||||
|
||||
public static IIcon safeIcon(IIcon icon) {
|
||||
if (icon == null)
|
||||
icon = ((TextureMap) Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).getAtlasSprite("missingno");
|
||||
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class UV
|
||||
{
|
||||
public double u;
|
||||
public double v;
|
||||
|
||||
public UV()
|
||||
{
|
||||
}
|
||||
|
||||
public UV(double u, double v)
|
||||
{
|
||||
this.u = u;
|
||||
this.v = v;
|
||||
}
|
||||
|
||||
public UV(UV uv)
|
||||
{
|
||||
this(uv.u, uv.v);
|
||||
}
|
||||
|
||||
public UV set(double u, double v)
|
||||
{
|
||||
this.u = u;
|
||||
this.v = v;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UV set(UV uv)
|
||||
{
|
||||
u = uv.u;
|
||||
v = uv.v;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UV copy()
|
||||
{
|
||||
return new UV(this);
|
||||
}
|
||||
|
||||
public UV add(UV uv)
|
||||
{
|
||||
u+=uv.u;
|
||||
v+=uv.v;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UV mul(double d)
|
||||
{
|
||||
u*=d;
|
||||
v*=d;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "UV("+new BigDecimal(u, cont)+", "+new BigDecimal(v, cont)+")";
|
||||
}
|
||||
|
||||
public UV apply(IUVTransformation transform)
|
||||
{
|
||||
transform.transform(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if(!(o instanceof UV))
|
||||
return false;
|
||||
UV uv = (UV)o;
|
||||
return u == uv.u && v == uv.v;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
public class UVScale implements IUVTransformation
|
||||
{
|
||||
double su;
|
||||
double sv;
|
||||
|
||||
public UVScale(double scaleu, double scalev)
|
||||
{
|
||||
su = scaleu;
|
||||
sv = scalev;
|
||||
}
|
||||
|
||||
public UVScale(double d)
|
||||
{
|
||||
this(d, d);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(UV uv)
|
||||
{
|
||||
uv.u*=su;
|
||||
uv.v*=sv;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
public class UVTranslation implements IUVTransformation
|
||||
{
|
||||
public double du;
|
||||
public double dv;
|
||||
|
||||
public UVTranslation(double u, double v)
|
||||
{
|
||||
du = u;
|
||||
dv = v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(UV texcoord)
|
||||
{
|
||||
texcoord.u+=du;
|
||||
texcoord.v+=dv;
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,13 @@ import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import codechicken.lib.render.uv.UV;
|
||||
import codechicken.lib.render.uv.UVTransformation;
|
||||
import codechicken.lib.util.Copyable;
|
||||
import codechicken.lib.vec.Transformation;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
|
||||
public class Vertex5
|
||||
public class Vertex5 implements Copyable<Vertex5>
|
||||
{
|
||||
public Vector3 vec;
|
||||
public UV uv;
|
||||
@@ -32,7 +35,7 @@ public class Vertex5
|
||||
{
|
||||
this(new Vector3(x, y, z), new UV(u, v));
|
||||
}
|
||||
|
||||
|
||||
public Vertex5 set(double x, double y, double z, double u, double v)
|
||||
{
|
||||
vec.set(x, y, z);
|
||||
@@ -40,6 +43,19 @@ public class Vertex5
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vertex5 set(double x, double y, double z, double u, double v, int tex)
|
||||
{
|
||||
vec.set(x, y, z);
|
||||
uv.set(u, v, tex);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vertex5 set(Vertex5 vert) {
|
||||
vec.set(vert.vec);
|
||||
uv.set(vert.uv);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vertex5(Vertex5 vertex5)
|
||||
{
|
||||
this(vertex5.vec.copy(), vertex5.uv.copy());
|
||||
@@ -62,7 +78,7 @@ public class Vertex5
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vertex5 apply(IUVTransformation t)
|
||||
public Vertex5 apply(UVTransformation t)
|
||||
{
|
||||
uv.apply(t);
|
||||
return this;
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.vec.IrreversibleTransformationException;
|
||||
import net.minecraft.util.IIcon;
|
||||
|
||||
public class IconTransformation extends UVTransformation {
|
||||
public IIcon icon;
|
||||
|
||||
public IconTransformation(IIcon icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
uv.u = icon.getInterpolatedU(uv.u * 16);
|
||||
uv.v = icon.getInterpolatedV(uv.v * 16);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
throw new IrreversibleTransformationException(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.vec.IrreversibleTransformationException;
|
||||
import net.minecraft.util.IIcon;
|
||||
|
||||
public class MultiIconTransformation extends UVTransformation {
|
||||
public IIcon[] icons;
|
||||
|
||||
public MultiIconTransformation(IIcon... icons) {
|
||||
this.icons = icons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
IIcon icon = icons[uv.tex % icons.length];
|
||||
uv.u = icon.getInterpolatedU(uv.u * 16);
|
||||
uv.v = icon.getInterpolatedV(uv.v * 16);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
throw new IrreversibleTransformationException(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.util.Copyable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class UV implements Copyable<UV> {
|
||||
public double u;
|
||||
public double v;
|
||||
public int tex;
|
||||
|
||||
public UV() {
|
||||
}
|
||||
|
||||
public UV(double u, double v) {
|
||||
this(u, v, 0);
|
||||
}
|
||||
|
||||
public UV(double u, double v, int tex) {
|
||||
this.u = u;
|
||||
this.v = v;
|
||||
this.tex = tex;
|
||||
}
|
||||
|
||||
public UV(UV uv) {
|
||||
this(uv.u, uv.v, uv.tex);
|
||||
}
|
||||
|
||||
public UV set(double u, double v, int tex) {
|
||||
this.u = u;
|
||||
this.v = v;
|
||||
this.tex = tex;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UV set(double u, double v) {
|
||||
return set(u, v, tex);
|
||||
}
|
||||
|
||||
public UV set(UV uv) {
|
||||
return set(uv.u, uv.v, uv.tex);
|
||||
}
|
||||
|
||||
public UV copy() {
|
||||
return new UV(this);
|
||||
}
|
||||
|
||||
public UV add(UV uv) {
|
||||
u += uv.u;
|
||||
v += uv.v;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UV multiply(double d) {
|
||||
u *= d;
|
||||
v *= d;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "UV(" + new BigDecimal(u, cont) + ", " + new BigDecimal(v, cont) + ")";
|
||||
}
|
||||
|
||||
public UV apply(UVTransformation t) {
|
||||
t.apply(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof UV))
|
||||
return false;
|
||||
UV uv = (UV) o;
|
||||
return u == uv.u && v == uv.v;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.math.MathHelper;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class UVRotation extends UVTransformation
|
||||
{
|
||||
public double angle;
|
||||
|
||||
/**
|
||||
* @param angle The angle to rotate counterclockwise in radians
|
||||
*/
|
||||
public UVRotation(double angle) {
|
||||
this.angle = angle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
double c = MathHelper.cos(angle);
|
||||
double s = MathHelper.sin(angle);
|
||||
double u2 = c*uv.u + s*uv.v;
|
||||
uv.v = - s*uv.u + c*uv.v;
|
||||
uv.u = u2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
return new UVRotation(-angle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation merge(UVTransformation next) {
|
||||
if(next instanceof UVRotation)
|
||||
return new UVRotation(angle+((UVRotation)next).angle);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRedundant() {
|
||||
return MathHelper.between(-1E-5, angle, 1E-5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "UVRotation(" + new BigDecimal(angle, cont) + ")";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class UVScale extends UVTransformation {
|
||||
double su;
|
||||
double sv;
|
||||
|
||||
public UVScale(double scaleu, double scalev) {
|
||||
su = scaleu;
|
||||
sv = scalev;
|
||||
}
|
||||
|
||||
public UVScale(double d) {
|
||||
this(d, d);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
uv.u *= su;
|
||||
uv.v *= sv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
return new UVScale(1 / su, 1 / sv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "UVScale(" + new BigDecimal(su, cont) + ", " + new BigDecimal(sv, cont) + ")";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.render.CCRenderState;
|
||||
import codechicken.lib.vec.ITransformation;
|
||||
|
||||
/**
|
||||
* Abstract supertype for any UV transformation
|
||||
*/
|
||||
public abstract class UVTransformation extends ITransformation<UV, UVTransformation> implements CCRenderState.IVertexOperation
|
||||
{
|
||||
public static final int operationIndex = CCRenderState.registerOperation();
|
||||
|
||||
public UVTransformation at(UV point) {
|
||||
return new UVTransformationList(new UVTranslation(-point.u, -point.v), this, new UVTranslation(point.u, point.v));
|
||||
}
|
||||
|
||||
public UVTransformationList with(UVTransformation t) {
|
||||
return new UVTransformationList(this, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
return !isRedundant();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
apply(CCRenderState.vert.uv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int operationID() {
|
||||
return operationIndex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class UVTransformationList extends UVTransformation
|
||||
{
|
||||
private ArrayList<UVTransformation> transformations = new ArrayList<UVTransformation>();
|
||||
|
||||
public UVTransformationList(UVTransformation... transforms)
|
||||
{
|
||||
for(UVTransformation t : transforms)
|
||||
if(t instanceof UVTransformationList)
|
||||
transformations.addAll(((UVTransformationList)t).transformations);
|
||||
else
|
||||
transformations.add(t);
|
||||
|
||||
compact();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv)
|
||||
{
|
||||
for(int i = 0; i < transformations.size(); i++)
|
||||
transformations.get(i).apply(uv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformationList with(UVTransformation t)
|
||||
{
|
||||
if(t.isRedundant())
|
||||
return this;
|
||||
|
||||
if(t instanceof UVTransformationList)
|
||||
transformations.addAll(((UVTransformationList)t).transformations);
|
||||
else
|
||||
transformations.add(t);
|
||||
|
||||
compact();
|
||||
return this;
|
||||
}
|
||||
|
||||
public UVTransformationList prepend(UVTransformation t)
|
||||
{
|
||||
if(t.isRedundant())
|
||||
return this;
|
||||
|
||||
if(t instanceof UVTransformationList)
|
||||
transformations.addAll(0, ((UVTransformationList)t).transformations);
|
||||
else
|
||||
transformations.add(0, t);
|
||||
|
||||
compact();
|
||||
return this;
|
||||
}
|
||||
|
||||
private void compact() {
|
||||
ArrayList<UVTransformation> newList = new ArrayList<UVTransformation>(transformations.size());
|
||||
Iterator<UVTransformation> iterator = transformations.iterator();
|
||||
UVTransformation prev = null;
|
||||
while(iterator.hasNext()) {
|
||||
UVTransformation t = iterator.next();
|
||||
if(t.isRedundant())
|
||||
continue;
|
||||
|
||||
if(prev != null) {
|
||||
UVTransformation m = prev.merge(t);
|
||||
if(m == null)
|
||||
newList.add(prev);
|
||||
else if(m.isRedundant())
|
||||
t = null;
|
||||
else
|
||||
t = m;
|
||||
}
|
||||
prev = t;
|
||||
}
|
||||
if(prev != null)
|
||||
newList.add(prev);
|
||||
|
||||
if(newList.size() < transformations.size())
|
||||
transformations = newList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRedundant() {
|
||||
return transformations.size() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse()
|
||||
{
|
||||
UVTransformationList rev = new UVTransformationList();
|
||||
for(int i = transformations.size()-1; i >= 0; i--)
|
||||
rev.with(transformations.get(i).inverse());
|
||||
return rev;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String s = "";
|
||||
for(UVTransformation t : transformations)
|
||||
s+="\n"+t.toString();
|
||||
return s.trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.math.MathHelper;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class UVTranslation extends UVTransformation {
|
||||
public double du;
|
||||
public double dv;
|
||||
|
||||
public UVTranslation(double u, double v) {
|
||||
du = u;
|
||||
dv = v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
uv.u += du;
|
||||
uv.v += dv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation at(UV point) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
return new UVTranslation(-du, -dv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation merge(UVTransformation next) {
|
||||
if (next instanceof UVTranslation) {
|
||||
UVTranslation t = (UVTranslation)next;
|
||||
return new UVTranslation(du+t.du, dv+t.dv);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRedundant() {
|
||||
return MathHelper.between(-1E-5, du, 1E-5) && MathHelper.between(-1E-5, dv, 1E-5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "UVTranslation(" + new BigDecimal(du, cont) + ", " + new BigDecimal(dv, cont) + ")";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user