mirror of
https://github.com/GTNewHorizons/CodeChickenCore.git
synced 2026-11-18 03:25:09 +00:00
Initial Commit
This commit is contained in:
1 parent
397135f687
commit
0f32dd8620
82 files changed
+10693
No files matched your search
@@ -0,0 +1,968 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
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
|
||||
{
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Each pixel corresponds to one unit of position when generating the model
|
||||
* @param i Vertex index to start generating at
|
||||
* @param x1 The minX bound of the box
|
||||
* @param y1 The minY bound of the box
|
||||
* @param z1 The minZ bound of the box
|
||||
* @param w The width of the box
|
||||
* @param h The height of the box
|
||||
* @param d The depth of the box
|
||||
* @param tx The distance of the top left corner of the texture map from the left in pixels
|
||||
* @param ty The distance of the top left corner of the texture map from the top in pixels
|
||||
* @param tw The width of the texture in pixels
|
||||
* @param th The height of the texture in pixels
|
||||
* @param f The scale of the model, pixels per block, normally 16
|
||||
* @return The generated model
|
||||
*/
|
||||
public CCModel generateBox(int i, double x1, double y1, double z1, double w, double h, double d, double tx, double ty, double tw, double th, double f)
|
||||
{
|
||||
double u1, v1, u2, v2;
|
||||
double x2 = x1+w;
|
||||
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;
|
||||
verts[i++] = new Vertex5(x1, y1, z2, u1, v2);
|
||||
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;
|
||||
verts[i++] = new Vertex5(x2, y2, z2, u2, v2);
|
||||
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;
|
||||
verts[i++] = new Vertex5(x1, y2, z1, u2, v1);
|
||||
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;
|
||||
verts[i++] = new Vertex5(x1, y2, z2, u1, v1);
|
||||
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;
|
||||
verts[i++] = new Vertex5(x1, y2, z2, u2, v1);
|
||||
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;
|
||||
verts[i++] = new Vertex5(x2, y1, z2, u1, v2);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a box, uv mapped to be the same as a minecraft block with the same bounds
|
||||
* @param i The vertex index to start generating at
|
||||
* @param bounds The bounds of the block, 0 to 1
|
||||
* @return The generated model. When rendering an icon will need to be supplied for the UV transformation.
|
||||
*/
|
||||
public CCModel generateBlock(int i, Cuboid6 bounds)
|
||||
{
|
||||
return generateBlock(i, bounds.min.x, bounds.min.y, bounds.min.z, bounds.max.x, bounds.max.y, bounds.max.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a box, uv mapped to be the same as a minecraft block with the same bounds
|
||||
* @param i The vertex index to start generating at
|
||||
* @param x1 minX
|
||||
* @param y1 minY
|
||||
* @param z1 minZ
|
||||
* @param x2 maxX
|
||||
* @param y2 maxY
|
||||
* @param z2 maxZ
|
||||
* @return The generated model. When rendering an icon will need to be supplied for the UV transformation.
|
||||
*/
|
||||
public CCModel generateBlock(int i, double x1, double y1, double z1, double x2, double y2, double z2)
|
||||
{
|
||||
double u1, v1, u2, v2;
|
||||
|
||||
//bottom face
|
||||
u1 = x1; v1 = z1;
|
||||
u2 = x2; v2 = z2;
|
||||
verts[i++] = new Vertex5(x1, y1, z2, u1, v2);
|
||||
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 = x1+2; v1 = z1;
|
||||
u2 = x2+2; v2 = z2;
|
||||
verts[i++] = new Vertex5(x2, y2, z2, u2, v2);
|
||||
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);
|
||||
|
||||
//east face
|
||||
u1 = x1+4; v1 = 1-y2;
|
||||
u2 = x2+4; v2 = 1-y1;
|
||||
verts[i++] = new Vertex5(x1, y1, z1, u1, v2);
|
||||
verts[i++] = new Vertex5(x1, y2, z1, u1, v1);
|
||||
verts[i++] = new Vertex5(x2, y2, z1, u2, v1);
|
||||
verts[i++] = new Vertex5(x2, y1, z1, u2, v2);
|
||||
|
||||
//west face
|
||||
u1 = x1+6; v1 = 1-y2;
|
||||
u2 = x2+6; v2 = 1-y1;
|
||||
verts[i++] = new Vertex5(x2, y1, z2, u2, v2);
|
||||
verts[i++] = new Vertex5(x2, y2, z2, u2, v1);
|
||||
verts[i++] = new Vertex5(x1, y2, z2, u1, v1);
|
||||
verts[i++] = new Vertex5(x1, y1, z2, u1, v2);
|
||||
|
||||
//north face
|
||||
u1 = z1+8; v1 = 1-y2;
|
||||
u2 = z2+8; v2 = 1-y1;
|
||||
verts[i++] = new Vertex5(x1, y1, z2, u2, v2);
|
||||
verts[i++] = new Vertex5(x1, y2, z2, u2, v1);
|
||||
verts[i++] = new Vertex5(x1, y2, z1, u1, v1);
|
||||
verts[i++] = new Vertex5(x1, y1, z1, u1, v2);
|
||||
|
||||
//south face
|
||||
u1 = z1+10; v1 = 1-y2;
|
||||
u2 = z2+10; v2 = 1-y1;
|
||||
verts[i++] = new Vertex5(x2, y1, z1, u1, v2);
|
||||
verts[i++] = new Vertex5(x2, y2, z1, u1, v1);
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the normals of all faces in the model.
|
||||
* Uses the cross product of the vectors along 2 sides of the face
|
||||
* @param start The first vertex to generate normals for
|
||||
* @param length The number of vertices to generate normals for. Note this must be a multiple of 3 for triangles or 4 for quads
|
||||
* @return The model
|
||||
*/
|
||||
public CCModel computeNormals(int start, int length)
|
||||
{
|
||||
if(length%vp != 0 || start%vp != 0)
|
||||
throw new IllegalArgumentException("Cannot generate normals across polygons");
|
||||
|
||||
if(normals == null)
|
||||
normals = new Vector3[verts.length];
|
||||
|
||||
for(int k = 0; k < length; k+=vp)
|
||||
{
|
||||
int i = k + start;
|
||||
Vector3 diff1 = verts[i+1].vec.copy().subtract(verts[i].vec);
|
||||
Vector3 diff2 = verts[i+vp-1].vec.copy().subtract(verts[i].vec);
|
||||
normals[i] = diff1.crossProduct(diff2).normalize();
|
||||
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.
|
||||
*/
|
||||
public CCRBModel withMCLighting()
|
||||
{
|
||||
return new CCRBModel(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Averages all normals at the same position to produce a smooth lighting effect.
|
||||
* @return The model
|
||||
*/
|
||||
public CCModel smoothNormals()
|
||||
{
|
||||
ArrayList<PositionNormalEntry> map = new ArrayList<PositionNormalEntry>();
|
||||
nextvert: for(int k = 0; k < verts.length; k++)
|
||||
{
|
||||
Vector3 vec = verts[k].vec;
|
||||
for(PositionNormalEntry e : map)
|
||||
if(e.positionEqual(vec))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public CCModel apply(Transformation t)
|
||||
{
|
||||
for(int k = 0; k < verts.length; k++)
|
||||
t.apply(verts[k].vec);
|
||||
|
||||
if(normals != null)
|
||||
for(int k = 0; k < normals.length; k++)
|
||||
t.applyN(normals[k]);
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
public void render(double x, double y, double z, IUVTransformation u)
|
||||
{
|
||||
render(new Translation(new Vector3(x, y, z)), u);
|
||||
}
|
||||
|
||||
public void render(Transformation t, double u, double v)
|
||||
{
|
||||
render(t, new UVTranslation(u, v));
|
||||
}
|
||||
|
||||
public void render(Transformation t, IUVTransformation u)
|
||||
{
|
||||
render(t, u, ColourModifier.instance);
|
||||
}
|
||||
|
||||
public void render(Transformation t, IUVTransformation u, IVertexModifier m)
|
||||
{
|
||||
render(0, verts.length, t, u, m);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public void render(int start, int length, Transformation t, IUVTransformation u, IVertexModifier m)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
double[] values = new double[as.length];
|
||||
for(int i = 0; i < as.length; i++)
|
||||
values[i] = Double.parseDouble(as[i]);
|
||||
return values;
|
||||
}
|
||||
|
||||
public static void illegalAssert(boolean b, String err)
|
||||
{
|
||||
if(!b) throw new IllegalArgumentException(err);
|
||||
}
|
||||
|
||||
public static void assertMatch(Matcher m, String s)
|
||||
{
|
||||
m.reset(s);
|
||||
illegalAssert(m.matches(), "Malformed line: "+s);
|
||||
}
|
||||
|
||||
private static final Pattern vertPattern = Pattern.compile("v(?: ([\\d\\.+-]+))+");
|
||||
private static final Pattern uvwPattern = Pattern.compile("vt(?: ([\\d\\.+-]+))+");
|
||||
private static final Pattern normalPattern = Pattern.compile("vn(?: ([\\d\\.+-]+))+");
|
||||
private static final Pattern polyPattern = Pattern.compile("f(?: ((?:\\d*)(?:/\\d*)?(?:/\\d*)?))+");
|
||||
public static final Matcher vertMatcher = vertPattern.matcher("");
|
||||
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
|
||||
* @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
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Map<String, CCModel> parseObjModels(InputStream input, int vertexMode, Transformation coordSystem) throws IOException
|
||||
{
|
||||
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;
|
||||
while((line = reader.readLine()) != null)
|
||||
{
|
||||
line = line.replaceAll("\\s+", " ").trim();
|
||||
if(line.startsWith("#") || line.length() == 0)
|
||||
continue;
|
||||
|
||||
if(line.startsWith("v "))
|
||||
{
|
||||
assertMatch(vertMatcher, line);
|
||||
double[] values = parseDoubles(line.substring(2), " ");
|
||||
illegalAssert(values.length >= 3, "Vertices must have x, y and z components");
|
||||
Vector3 vert = new Vector3(values[0], values[1], values[2]);
|
||||
coordSystem.apply(vert);
|
||||
verts.add(vert);
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("vt "))
|
||||
{
|
||||
assertMatch(uvwMatcher, line);
|
||||
double[] values = parseDoubles(line.substring(3), " ");
|
||||
illegalAssert(values.length >= 2, "Tex Coords must have u, and v components");
|
||||
uvs.add(new Vector3(values[0], 1-values[1], 0));
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("vn "))
|
||||
{
|
||||
assertMatch(normalMatcher, line);
|
||||
double[] values = parseDoubles(line.substring(3), " ");
|
||||
illegalAssert(values.length >= 3, "Normals must have x, y and z components");
|
||||
Vector3 norm = new Vector3(values[0], values[1], values[2]).normalize();
|
||||
coordSystem.applyN(norm);
|
||||
normals.add(norm);
|
||||
continue;
|
||||
}
|
||||
if(line.startsWith("f "))
|
||||
{
|
||||
assertMatch(polyMatcher, line);
|
||||
String[] av = line.substring(2).split(" ");
|
||||
illegalAssert(av.length >= 3, "Polygons must have at least 3 vertices");
|
||||
int[][] polyVerts = new int[av.length][3];
|
||||
for(int i = 0; i < av.length; i++)
|
||||
{
|
||||
String[] as = av[i].split("/");
|
||||
for(int p = 0; p < as.length; p++)
|
||||
if(as[p].length() > 0)
|
||||
polyVerts[i][p] = Integer.parseInt(as[p]);
|
||||
}
|
||||
if(vp == 3)
|
||||
triangulate(polys, polyVerts);
|
||||
else
|
||||
quadulate(polys, polyVerts);
|
||||
}
|
||||
if(line.startsWith("g "))
|
||||
{
|
||||
if(!polys.isEmpty())
|
||||
{
|
||||
modelMap.put(modelName, createModel(verts, uvs, normals, vertexMode, polys));
|
||||
polys.clear();
|
||||
}
|
||||
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++)
|
||||
{
|
||||
polys.add(polyVerts[0]);
|
||||
polys.add(polyVerts[i]);
|
||||
polys.add(polyVerts[i-1]);
|
||||
}
|
||||
}
|
||||
|
||||
public static void quadulate(List<int[]> polys, int[][] polyVerts)
|
||||
{
|
||||
if(polyVerts.length == 4)
|
||||
{
|
||||
polys.add(polyVerts[0]);
|
||||
polys.add(polyVerts[3]);
|
||||
polys.add(polyVerts[2]);
|
||||
polys.add(polyVerts[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i = 2; i < polyVerts.length; i++)
|
||||
{
|
||||
polys.add(polyVerts[0]);
|
||||
polys.add(polyVerts[i]);
|
||||
polys.add(polyVerts[i-1]);
|
||||
polys.add(polyVerts[i-1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses vertices, texture coords, normals and polygons from a WaveFront Obj file
|
||||
* @param s The name of the obj resource
|
||||
* @return A map of group names to models
|
||||
*/
|
||||
public static Map<String, CCModel> parseObjModels(ResourceLocation res)
|
||||
{
|
||||
return parseObjModels(res, 4, null);
|
||||
}
|
||||
/**
|
||||
* Parses vertices, texture coords, normals and polygons from a WaveFront Obj file
|
||||
* @param res The resource for the obj file
|
||||
* @param coordSystem The cooridnate system transformation to apply
|
||||
* @return A map of group names to models
|
||||
*/
|
||||
public static Map<String, CCModel> parseObjModels(ResourceLocation res, Transformation coordSystem)
|
||||
{
|
||||
try
|
||||
{
|
||||
return parseObjModels(
|
||||
Minecraft.getMinecraft().func_110442_L().func_110536_a(res).func_110527_b(),
|
||||
4, coordSystem);
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
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 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
|
||||
*/
|
||||
public static Map<String, CCModel> parseObjModels(ResourceLocation res, int vertexMode, Transformation coordSystem)
|
||||
{
|
||||
try
|
||||
{
|
||||
return parseObjModels(
|
||||
Minecraft.getMinecraft().func_110442_L().func_110536_a(res).func_110527_b(),
|
||||
vertexMode, coordSystem);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
throw new RuntimeException("failed to load model: "+res, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static CCModel createModel(List<Vector3> verts, List<Vector3> uvs, List<Vector3> normals, int vertexMode, List<int[]> polys)
|
||||
{
|
||||
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()];
|
||||
|
||||
for(int i = 0; i < polys.size(); i++)
|
||||
{
|
||||
int[] ai = polys.get(i);
|
||||
Vector3 vert = verts.get(ai[0]-1).copy();
|
||||
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();
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Brings the UV coordinates of each face closer to the center UV by d.
|
||||
* Useful for fixing texture seams
|
||||
*/
|
||||
public CCModel shrinkUVs(double d)
|
||||
{
|
||||
for(int k = 0; k < verts.length; k+=vp)
|
||||
{
|
||||
UV uv = new UV();
|
||||
for(int i = 0; i < vp; i++)
|
||||
{
|
||||
uv.add(verts[k+i].uv);
|
||||
}
|
||||
uv.mul(1D/vp);
|
||||
for(int i = 0; i < vp; i++)
|
||||
{
|
||||
Vertex5 vert = verts[k+i];
|
||||
vert.uv.u += vert.uv.u < uv.u ? d : -d;
|
||||
vert.uv.v += vert.uv.v < uv.v ? d : -d;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param side1 The side of this model
|
||||
* @param side2 The side of the new model
|
||||
* @param point The point to rotate around
|
||||
* @return A copy of this model rotated to the appropriate side
|
||||
*/
|
||||
public CCModel sidedCopy(int side1, int side2, Vector3 point)
|
||||
{
|
||||
CCModel model = newModel(vertexMode, verts.length);
|
||||
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)
|
||||
{
|
||||
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];
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate models rotated to the other 5 sides of the block
|
||||
* @param models An array of 6 models
|
||||
* @param side The side of this model
|
||||
* @param point The rotation point
|
||||
*/
|
||||
public static void generateSidedModels(CCModel[] models, int side, Vector3 point)
|
||||
{
|
||||
for(int s = 0; s < 6; s++)
|
||||
{
|
||||
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
|
||||
* @param side The side of this model
|
||||
* @param point The rotation point
|
||||
*/
|
||||
public static void generateSidedModelsH(CCModel[] models, int side, Vector3 point)
|
||||
{
|
||||
for(int s = 2; s < 6; s++)
|
||||
{
|
||||
if(s == side)
|
||||
continue;
|
||||
|
||||
models[s] = models[side].sidedCopy(side, s, point);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates copies of faces with clockwise vertices
|
||||
* @return The model
|
||||
*/
|
||||
public CCModel generateBackface(int srcpos, int destpos, int length)
|
||||
{
|
||||
if(srcpos+length > destpos)
|
||||
throw new IllegalArgumentException("Overlapping src and dest arrays");
|
||||
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++)
|
||||
{
|
||||
int b = (i/vp)*vp;
|
||||
int d = i%vp;
|
||||
int di = destpos+b+o[d][1];
|
||||
int si = srcpos+b+o[d][0];
|
||||
verts[di] = new Vertex5(verts[si]);
|
||||
if(normals != null && normals[si] != null)
|
||||
normals[di] = normals[si].copy().negate();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates sided copies of vertices into this model.
|
||||
* Assumes that your model has been generated at vertex side*(numVerts/6)
|
||||
*/
|
||||
public CCModel generateSidedParts(int side, Vector3 point)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates sided copies of vertices into this model.
|
||||
* Assumes that your model has been generated at vertex side*(numVerts/4)
|
||||
*/
|
||||
public CCModel generateSidedPartsH(int side, Vector3 point)
|
||||
{
|
||||
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)
|
||||
continue;
|
||||
|
||||
generateSidedPart(side, s, point, length*(side-2), length*(s-2), length);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a sided copy of verts into this model
|
||||
*/
|
||||
public CCModel generateSidedPart(int side1, int side2, Vector3 point, int srcpos, int destpos, int length)
|
||||
{
|
||||
return generateRotatedPart(new TransformationList(sideRotations[side1].inverse(), sideRotations[side2]), point, srcpos, destpos, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a rotated copy of verts into this model
|
||||
*/
|
||||
public CCModel generateRotatedPart(Transformation t, Vector3 point, int srcpos, int destpos, int length)
|
||||
{
|
||||
for(int k = 0; k < length; k++)
|
||||
{
|
||||
verts[destpos+k] = verts[srcpos+k].copy();
|
||||
verts[destpos+k].vec
|
||||
.subtract(point)
|
||||
.apply(t)
|
||||
.add(point);
|
||||
}
|
||||
|
||||
if(normals != null)
|
||||
for(int k = 0; k < length; k++)
|
||||
normals[destpos+k] = normals[srcpos+k].copy().apply(t);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public static CCModel combine(Collection<CCModel> models)
|
||||
{
|
||||
if(models.isEmpty())
|
||||
return null;
|
||||
|
||||
int numVerts = 0;
|
||||
int vertexMode = -1;
|
||||
for(CCModel model : models)
|
||||
{
|
||||
if(vertexMode == -1)
|
||||
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)
|
||||
{
|
||||
copy(model, 0, c_model, i, model.verts.length);
|
||||
i+=model.verts.length;
|
||||
}
|
||||
|
||||
return c_model;
|
||||
}
|
||||
|
||||
public CCModel twoFacedCopy()
|
||||
{
|
||||
CCModel model = newModel(vertexMode, verts.length*2);
|
||||
copy(this, 0, model, 0, verts.length);
|
||||
model.generateBackface(0, verts.length, verts.length);
|
||||
return model;
|
||||
}
|
||||
|
||||
public CCModel copy()
|
||||
{
|
||||
CCModel model = newModel(vertexMode, verts.length);
|
||||
copy(this, 0, model, 0, verts.length);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The average of all vertices, for bones.
|
||||
*/
|
||||
public Vector3 collapse()
|
||||
{
|
||||
Vector3 v = new Vector3();
|
||||
for(Vertex5 vert : verts)
|
||||
v.add(vert.vec);
|
||||
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];
|
||||
switch(findSide(normal))
|
||||
{
|
||||
case 0:
|
||||
vert.vec.y += offsets.min.y;
|
||||
break;
|
||||
case 1:
|
||||
vert.vec.y += offsets.max.y;
|
||||
break;
|
||||
case 2:
|
||||
vert.vec.z += offsets.min.z;
|
||||
break;
|
||||
case 3:
|
||||
vert.vec.z += offsets.max.z;
|
||||
break;
|
||||
case 4:
|
||||
vert.vec.x += offsets.min.x;
|
||||
break;
|
||||
case 5:
|
||||
vert.vec.x += offsets.max.x;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public static int findSide(Vector3 normal)
|
||||
{
|
||||
if(normal.y <=-0.99) return 0;
|
||||
if(normal.y >= 0.99) return 1;
|
||||
if(normal.z <=-0.99) return 2;
|
||||
if(normal.z >= 0.99) return 3;
|
||||
if(normal.x <=-0.99) return 4;
|
||||
if(normal.x >= 0.99) return 5;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A Cuboid6 containing all the verts in this model
|
||||
*/
|
||||
public Cuboid6 bounds()
|
||||
{
|
||||
Vector3 vec1 = verts[0].vec;
|
||||
Cuboid6 c = new Cuboid6(vec1.copy(), vec1.copy());
|
||||
for(int i = 1; i < verts.length; i++)
|
||||
c.enclose(verts[i].vec);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.vec.Matrix4;
|
||||
import codechicken.lib.vec.Quat;
|
||||
import codechicken.lib.vec.Rotation;
|
||||
import codechicken.lib.vec.Scale;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
|
||||
import static codechicken.lib.math.MathHelper.phi;
|
||||
|
||||
public class CCModelLibrary
|
||||
{
|
||||
public static CCModel icosahedron4;
|
||||
public static CCModel icosahedron7;
|
||||
|
||||
private static int i;
|
||||
|
||||
static
|
||||
{
|
||||
generateIcosahedron();
|
||||
}
|
||||
|
||||
private static void generateIcosahedron()
|
||||
{
|
||||
Vector3[] verts = new Vector3[12];
|
||||
|
||||
verts[0] = new Vector3(-1, phi, 0);
|
||||
verts[1] = new Vector3( 1, phi, 0);
|
||||
verts[2] = new Vector3( 1,-phi, 0);
|
||||
verts[3] = new Vector3(-1,-phi, 0);
|
||||
|
||||
verts[4] = new Vector3(0,-1, phi);
|
||||
verts[5] = new Vector3(0, 1, phi);
|
||||
verts[6] = new Vector3(0, 1,-phi);
|
||||
verts[7] = new Vector3(0,-1,-phi);
|
||||
|
||||
verts[8] = new Vector3( phi, 0,-1);
|
||||
verts[9] = new Vector3( phi, 0, 1);
|
||||
verts[10] = new Vector3(-phi, 0, 1);
|
||||
verts[11] = new Vector3(-phi, 0,-1);
|
||||
|
||||
Quat quat = Quat.aroundAxis(0, 0, 1, Math.atan(1/phi));
|
||||
for(Vector3 vec : verts)
|
||||
quat.rotate(vec);
|
||||
|
||||
icosahedron4 = CCModel.newModel(4, 60);
|
||||
icosahedron7 = CCModel.newModel(7, 80);
|
||||
|
||||
i = 0;
|
||||
//top
|
||||
addIcosahedronTriangle(verts[1], 0.5, 0, verts[0], 0, 0.25, verts[5], 1, 0.25);
|
||||
addIcosahedronTriangle(verts[1], 0.5, 0, verts[5], 0, 0.25, verts[9], 1, 0.25);
|
||||
addIcosahedronTriangle(verts[1], 0.5, 0, verts[9], 0, 0.25, verts[8], 1, 0.25);
|
||||
addIcosahedronTriangle(verts[1], 0.5, 0, verts[8], 0, 0.25, verts[6], 1, 0.25);
|
||||
addIcosahedronTriangle(verts[1], 0.5, 0, verts[6], 0, 0.25, verts[0], 1, 0.25);
|
||||
//centre 1vert top
|
||||
addIcosahedronTriangle(verts[0], 0.5, 0.25, verts[11],0, 0.75, verts[10],1, 0.75);
|
||||
addIcosahedronTriangle(verts[5], 0.5, 0.25, verts[10],0, 0.75, verts[4], 1, 0.75);
|
||||
addIcosahedronTriangle(verts[9], 0.5, 0.25, verts[4], 0, 0.75, verts[2], 1, 0.75);
|
||||
addIcosahedronTriangle(verts[8], 0.5, 0.25, verts[2], 0, 0.75, verts[7], 1, 0.75);
|
||||
addIcosahedronTriangle(verts[6], 0.5, 0.25, verts[7], 0, 0.75, verts[11],1, 0.75);
|
||||
//centre 1vert bottom
|
||||
addIcosahedronTriangle(verts[2], 0.5, 0.75, verts[8], 0, 0.25, verts[9], 1, 0.25);
|
||||
addIcosahedronTriangle(verts[7], 0.5, 0.75, verts[6], 0, 0.25, verts[8], 1, 0.25);
|
||||
addIcosahedronTriangle(verts[11],0.5, 0.75, verts[0], 0, 0.25, verts[6], 1, 0.25);
|
||||
addIcosahedronTriangle(verts[10],0.5, 0.75, verts[5], 0, 0.25, verts[0], 1, 0.25);
|
||||
addIcosahedronTriangle(verts[4], 0.5, 0.75, verts[9], 0, 0.25, verts[5], 1, 0.25);
|
||||
//bottom
|
||||
addIcosahedronTriangle(verts[3], 0.5, 1, verts[2], 0, 0.75, verts[4], 1, 0.75);
|
||||
addIcosahedronTriangle(verts[3], 0.5, 1, verts[7], 0, 0.75, verts[2], 1, 0.75);
|
||||
addIcosahedronTriangle(verts[3], 0.5, 1, verts[11],0, 0.75, verts[7], 1, 0.75);
|
||||
addIcosahedronTriangle(verts[3], 0.5, 1, verts[10],0, 0.75, verts[11],1, 0.75);
|
||||
addIcosahedronTriangle(verts[3], 0.5, 1, verts[4], 0, 0.75, verts[10],1, 0.75);
|
||||
|
||||
icosahedron4.computeNormals().smoothNormals();
|
||||
icosahedron7.computeNormals().smoothNormals();
|
||||
}
|
||||
|
||||
private static void addIcosahedronTriangle(Vector3 vec1, double u1, double v1,
|
||||
Vector3 vec2, double u2, double v2,
|
||||
Vector3 vec3, double u3, double v3)
|
||||
{
|
||||
icosahedron4.verts[i*3] = icosahedron7.verts[i*4] = new Vertex5(vec1, u1, v1);
|
||||
icosahedron4.verts[i*3+1] = icosahedron7.verts[i*4+1] = new Vertex5(vec2, u2, v2);
|
||||
icosahedron4.verts[i*3+2] = icosahedron7.verts[i*4+2] = icosahedron7.verts[i*4+3] = new Vertex5(vec3, u3, v3);
|
||||
i++;
|
||||
}
|
||||
|
||||
public static Matrix4 getRenderMatrix(Vector3 position, Rotation rotation, double scale)
|
||||
{
|
||||
return new Matrix4().translate(position).apply(new Scale(scale)).apply(rotation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 void vertexColour(int r, int g, int b, int a)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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.func_110577_a(texture);
|
||||
}
|
||||
|
||||
public static void apply()
|
||||
{
|
||||
if(hasBrightness)
|
||||
Tessellator.instance.setBrightness(brightness);
|
||||
if(hasColour)
|
||||
Tessellator.instance.setColorRGBA(colour >> 24 & 0xFF, colour >> 16 & 0xFF, colour >> 8 & 0xFF, colour & 0xFF);
|
||||
}
|
||||
|
||||
public static void reset()
|
||||
{
|
||||
hasBrightness = false;
|
||||
useModelColours = false;
|
||||
hasColour = false;
|
||||
alphaOverride = false;
|
||||
}
|
||||
|
||||
public static void startDrawing(int i)
|
||||
{
|
||||
Tessellator.instance.startDrawing(i);
|
||||
apply();
|
||||
}
|
||||
|
||||
public static void draw()
|
||||
{
|
||||
Tessellator.instance.draw();
|
||||
}
|
||||
|
||||
public static void pullLightmap()
|
||||
{
|
||||
setBrightness((int)OpenGlHelper.lastBrightnessY << 16 | (int)OpenGlHelper.lastBrightnessX);
|
||||
}
|
||||
|
||||
public static void applyBrightnessTexCoords()
|
||||
{
|
||||
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, brightness&0xFFFF, brightness>>>16);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.vec.Cuboid6;
|
||||
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.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)
|
||||
{
|
||||
super(world, x, y, z, dx, dy, dz);
|
||||
particleIcon = icon;
|
||||
particleGravity = 1;
|
||||
particleRed = particleGreen = particleBlue = 0.6F;
|
||||
particleScale /= 2.0F;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFXLayer()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* copy pasted from EntityDiggingFX
|
||||
*/
|
||||
@Override
|
||||
public void renderParticle(Tessellator par1Tessellator, float par2, float par3, float par4, float par5, float par6, float par7)
|
||||
{
|
||||
float f6 = (particleTextureIndexX + particleTextureJitterX / 4.0F) / 16.0F;
|
||||
float f7 = f6 + 0.015609375F;
|
||||
float f8 = (particleTextureIndexY + particleTextureJitterY / 4.0F) / 16.0F;
|
||||
float f9 = f8 + 0.015609375F;
|
||||
float f10 = 0.1F * particleScale;
|
||||
|
||||
if (particleIcon != null)
|
||||
{
|
||||
f6 = particleIcon.getInterpolatedU(particleTextureJitterX / 4.0F * 16.0F);
|
||||
f7 = particleIcon.getInterpolatedU((particleTextureJitterX + 1.0F) / 4.0F * 16.0F);
|
||||
f8 = particleIcon.getInterpolatedV(particleTextureJitterY / 4.0F * 16.0F);
|
||||
f9 = particleIcon.getInterpolatedV((particleTextureJitterY + 1.0F) / 4.0F * 16.0F);
|
||||
}
|
||||
|
||||
float f11 = (float)(prevPosX + (posX - prevPosX) * par2 - interpPosX);
|
||||
float f12 = (float)(prevPosY + (posY - prevPosY) * par2 - interpPosY);
|
||||
float f13 = (float)(prevPosZ + (posZ - prevPosZ) * par2 - interpPosZ);
|
||||
float f14 = 1.0F;
|
||||
par1Tessellator.setColorOpaque_F(f14 * particleRed, f14 * particleGreen, f14 * particleBlue);
|
||||
par1Tessellator.addVertexWithUV(f11 - par3 * f10 - par6 * f10, f12 - par4 * f10, f13 - par5 * f10 - par7 * f10, f6, f9);
|
||||
par1Tessellator.addVertexWithUV(f11 - par3 * f10 + par6 * f10, f12 + par4 * f10, f13 - par5 * f10 + par7 * f10, f6, f8);
|
||||
par1Tessellator.addVertexWithUV(f11 + par3 * f10 + par6 * f10, f12 + par4 * f10, f13 + par5 * f10 + par7 * f10, f7, f8);
|
||||
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)
|
||||
{
|
||||
float border = 0.1F;
|
||||
Vector3 diff = bounds.max.copy().subtract(bounds.min).add(-2*border);
|
||||
diff.x*=world.rand.nextDouble();
|
||||
diff.y*=world.rand.nextDouble();
|
||||
diff.z*=world.rand.nextDouble();
|
||||
Vector3 pos = diff.add(bounds.min).add(border);
|
||||
|
||||
if (side == 0)
|
||||
diff.y = bounds.min.y - border;
|
||||
if (side == 1)
|
||||
diff.y = bounds.max.y + border;
|
||||
if (side == 2)
|
||||
diff.z = bounds.min.z - border;
|
||||
if (side == 3)
|
||||
diff.z = bounds.max.z + border;
|
||||
if (side == 4)
|
||||
diff.x = bounds.min.x - border;
|
||||
if (side == 5)
|
||||
diff.x = bounds.max.x + border;
|
||||
|
||||
effectRenderer.addEffect(
|
||||
new EntityDigIconFX(world, pos.x, pos.y, pos.z, 0, 0, 0, icon)
|
||||
.multiplyVelocity(0.2F).multipleParticleScaleBy(0.6F));
|
||||
}
|
||||
|
||||
public static void addBlockDestroyEffects(World world, Cuboid6 bounds, Icon[] icons, EffectRenderer effectRenderer)
|
||||
{
|
||||
Vector3 diff = bounds.max.copy().subtract(bounds.min);
|
||||
Vector3 center = bounds.min.copy().add(bounds.max).multiply(0.5);
|
||||
Vector3 density = diff.copy().multiply(4);
|
||||
density.x = Math.ceil(density.x);
|
||||
density.y = Math.ceil(density.y);
|
||||
density.z = Math.ceil(density.z);
|
||||
|
||||
for (int i = 0; i < density.x; ++i)
|
||||
for (int j = 0; j < density.y; ++j)
|
||||
for (int k = 0; k < density.z; ++k)
|
||||
{
|
||||
double x = bounds.min.x+(i+0.5)*diff.x/density.x;
|
||||
double y = bounds.min.y+(j+0.5)*diff.y/density.y;
|
||||
double z = bounds.min.z+(k+0.5)*diff.z/density.z;
|
||||
effectRenderer.addEffect(
|
||||
new EntityDigIconFX(world, x, y, z, x-center.x, y-center.y, z-center.z, icons[world.rand.nextInt(icons.length)]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public class FontUtils
|
||||
{
|
||||
public static FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
|
||||
|
||||
public static void drawCenteredString(String s, int xCenter, int y, int colour)
|
||||
{
|
||||
fontRenderer.drawString(s, xCenter-fontRenderer.getStringWidth(s)/2, y, colour);
|
||||
}
|
||||
|
||||
public static void drawRightString(String s, int xRight, int y, int colour)
|
||||
{
|
||||
fontRenderer.drawString(s, xRight-fontRenderer.getStringWidth(s), y, colour);
|
||||
}
|
||||
|
||||
public static final String[] prefixes = new String[]{"K", "M", "G"};
|
||||
public static void drawItemQuantity(int x, int y, ItemStack item, String quantity, int mode)
|
||||
{
|
||||
if(item == null || (quantity == null && item.stackSize <= 1))
|
||||
return;
|
||||
|
||||
if(quantity == null)
|
||||
{
|
||||
switch(mode)
|
||||
{
|
||||
case 2:
|
||||
int q = item.stackSize;
|
||||
String postfix = "";
|
||||
for(int p = 0; p < 3 && q > 1000; p++)
|
||||
{
|
||||
q/=1000;
|
||||
postfix = prefixes[p];
|
||||
}
|
||||
quantity = Integer.toString(q)+postfix;
|
||||
case 1:
|
||||
quantity = "";
|
||||
if(item.stackSize/64 > 0)
|
||||
quantity+=item.stackSize/64 + "s";
|
||||
if(item.stackSize%64 > 0)
|
||||
quantity+=item.stackSize%64;
|
||||
break;
|
||||
default:
|
||||
quantity = Integer.toString(item.stackSize);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
double scale = quantity.length() > 2 ? 0.5 : 1;
|
||||
double sheight = 8*scale;
|
||||
double swidth = fontRenderer.getStringWidth(quantity)*scale;
|
||||
|
||||
GL11.glDisable(GL11.GL_LIGHTING);
|
||||
GL11.glDisable(GL11.GL_DEPTH_TEST);
|
||||
GL11.glPushMatrix();
|
||||
GL11.glTranslated(x+16-swidth, y+16-sheight, 0);
|
||||
GL11.glScaled(scale, scale, 1);
|
||||
fontRenderer.drawStringWithShadow(quantity, 0, 0, 0xFFFFFF);
|
||||
GL11.glPopMatrix();
|
||||
GL11.glEnable(GL11.GL_LIGHTING);
|
||||
GL11.glEnable(GL11.GL_DEPTH_TEST);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
public interface IFaceRenderer
|
||||
{
|
||||
public void renderFace(Vertex5[] face, int side);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
public interface IUVTransformation
|
||||
{
|
||||
public void transform(UV texcoord);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
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();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
public class ManagedTextureFX extends TextureFX
|
||||
{
|
||||
public boolean changed;
|
||||
|
||||
public ManagedTextureFX(int size, String name)
|
||||
{
|
||||
super(size, name);
|
||||
imageData = new int[size*size];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup()
|
||||
{
|
||||
}
|
||||
|
||||
public void setData(int[] data)
|
||||
{
|
||||
System.arraycopy(data, 0, imageData, 0, imageData.length);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changed()
|
||||
{
|
||||
boolean r = changed;
|
||||
changed = false;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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 length, int index)
|
||||
{
|
||||
for(int k = start; k < length; k++)
|
||||
{
|
||||
UV uv = m.verts[k].uv;
|
||||
uv.u = uv.u%2+index*2;
|
||||
uv.v %= 2;
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.resources.ResourceManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
public class PlaceholderTexture extends TextureAtlasSprite
|
||||
{
|
||||
protected PlaceholderTexture(String par1)
|
||||
{
|
||||
super(par1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load(ResourceManager manager, ResourceLocation location)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,435 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import static net.minecraftforge.client.IItemRenderer.ItemRenderType.ENTITY;
|
||||
import static net.minecraftforge.client.IItemRenderer.ItemRendererHelper.BLOCK_3D;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import codechicken.lib.vec.Cuboid6;
|
||||
import codechicken.lib.vec.Transformation;
|
||||
import codechicken.lib.vec.Rotation;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
import net.minecraft.util.AxisAlignedBB;
|
||||
import net.minecraft.util.Icon;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.client.renderer.RenderBlocks;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.client.renderer.entity.RenderItem;
|
||||
import net.minecraft.client.renderer.entity.RenderManager;
|
||||
import net.minecraftforge.client.IItemRenderer;
|
||||
import net.minecraftforge.client.MinecraftForgeClient;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
public class RenderUtils
|
||||
{
|
||||
static Vector3[] vectors = new Vector3[8];
|
||||
static RenderItem uniformRenderItem = new RenderItem()
|
||||
{
|
||||
public boolean shouldBob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
static EntityItem entityItem;
|
||||
|
||||
static
|
||||
{
|
||||
for(int i = 0; i < vectors.length; i++)
|
||||
vectors[i] = new Vector3();
|
||||
|
||||
uniformRenderItem.setRenderManager(RenderManager.instance);
|
||||
|
||||
entityItem = new EntityItem(null);
|
||||
entityItem.hoverStart = 0;
|
||||
}
|
||||
|
||||
public static void renderLiquidQuad(Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4, Icon icon, double res)
|
||||
{
|
||||
double u1 = icon.getMinU();
|
||||
double du = icon.getMaxU()-icon.getMinU();
|
||||
double v2 = icon.getMaxV();
|
||||
double dv = icon.getMaxV()-icon.getMinV();
|
||||
|
||||
Vector3 wide = vectors[0].set(point4).subtract(point1);
|
||||
Vector3 high = vectors[1].set(point1).subtract(point2);
|
||||
Tessellator t = Tessellator.instance;
|
||||
|
||||
double wlen = wide.mag();
|
||||
double hlen = high.mag();
|
||||
|
||||
double x = 0;
|
||||
while(x < wlen)
|
||||
{
|
||||
double rx = wlen - x;
|
||||
if(rx > res)
|
||||
rx = res;
|
||||
|
||||
double y = 0;
|
||||
while(y < hlen)
|
||||
{
|
||||
double ry = hlen-y;
|
||||
if(ry > res)
|
||||
ry = res;
|
||||
|
||||
Vector3 dx1 = vectors[2].set(wide).multiply(x/wlen);
|
||||
Vector3 dx2 = vectors[3].set(wide).multiply((x+rx)/wlen);
|
||||
Vector3 dy1 = vectors[4].set(high).multiply(y/hlen);
|
||||
Vector3 dy2 = vectors[5].set(high).multiply((y+ry)/hlen);
|
||||
|
||||
t.addVertexWithUV(point2.x+dx1.x+dy2.x, point2.y+dx1.y+dy2.y, point2.z+dx1.z+dy2.z, u1, v2-ry/res*dv);
|
||||
t.addVertexWithUV(point2.x+dx1.x+dy1.x, point2.y+dx1.y+dy1.y, point2.z+dx1.z+dy1.z, u1, v2);
|
||||
t.addVertexWithUV(point2.x+dx2.x+dy1.x, point2.y+dx2.y+dy1.y, point2.z+dx2.z+dy1.z, u1+rx/res*du, v2);
|
||||
t.addVertexWithUV(point2.x+dx2.x+dy2.x, point2.y+dx2.y+dy2.y, point2.z+dx2.z+dy2.z, u1+rx/res*du, v2-ry/res*dv);
|
||||
|
||||
y+=ry;
|
||||
}
|
||||
|
||||
x+=rx;
|
||||
}
|
||||
}
|
||||
|
||||
public static void translateToWorldCoords(Entity entity, float frame)
|
||||
{
|
||||
double interpPosX = entity.lastTickPosX + (entity.posX - entity.lastTickPosX) * frame;
|
||||
double interpPosY = entity.lastTickPosY + (entity.posY - entity.lastTickPosY) * frame;
|
||||
double interpPosZ = entity.lastTickPosZ + (entity.posZ - entity.lastTickPosZ) * frame;
|
||||
|
||||
GL11.glTranslated(-interpPosX, -interpPosY, -interpPosZ);
|
||||
}
|
||||
|
||||
public static void drawOutlinedBoundingBox(AxisAlignedBB par1AxisAlignedBB)
|
||||
{
|
||||
Tessellator var2 = Tessellator.instance;
|
||||
var2.startDrawing(3);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.minY, par1AxisAlignedBB.minZ);
|
||||
var2.addVertex(par1AxisAlignedBB.maxX, par1AxisAlignedBB.minY, par1AxisAlignedBB.minZ);
|
||||
var2.addVertex(par1AxisAlignedBB.maxX, par1AxisAlignedBB.minY, par1AxisAlignedBB.maxZ);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.minY, par1AxisAlignedBB.maxZ);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.minY, par1AxisAlignedBB.minZ);
|
||||
var2.draw();
|
||||
var2.startDrawing(3);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.minZ);
|
||||
var2.addVertex(par1AxisAlignedBB.maxX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.minZ);
|
||||
var2.addVertex(par1AxisAlignedBB.maxX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.maxZ);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.maxZ);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.minZ);
|
||||
var2.draw();
|
||||
var2.startDrawing(1);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.minY, par1AxisAlignedBB.minZ);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.minZ);
|
||||
var2.addVertex(par1AxisAlignedBB.maxX, par1AxisAlignedBB.minY, par1AxisAlignedBB.minZ);
|
||||
var2.addVertex(par1AxisAlignedBB.maxX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.minZ);
|
||||
var2.addVertex(par1AxisAlignedBB.maxX, par1AxisAlignedBB.minY, par1AxisAlignedBB.maxZ);
|
||||
var2.addVertex(par1AxisAlignedBB.maxX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.maxZ);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.minY, par1AxisAlignedBB.maxZ);
|
||||
var2.addVertex(par1AxisAlignedBB.minX, par1AxisAlignedBB.maxY, par1AxisAlignedBB.maxZ);
|
||||
var2.draw();
|
||||
}
|
||||
|
||||
public static void renderLiquidCuboid(Cuboid6 bound, Icon tex, double res)
|
||||
{
|
||||
renderLiquidQuad(//bottom
|
||||
new Vector3(bound.min.x, bound.min.y, bound.min.z),
|
||||
new Vector3(bound.max.x, bound.min.y, bound.min.z),
|
||||
new Vector3(bound.max.x, bound.min.y, bound.max.z),
|
||||
new Vector3(bound.min.x, bound.min.y, bound.max.z),
|
||||
tex, res);
|
||||
renderLiquidQuad(//top
|
||||
new Vector3(bound.min.x, bound.max.y, bound.min.z),
|
||||
new Vector3(bound.min.x, bound.max.y, bound.max.z),
|
||||
new Vector3(bound.max.x, bound.max.y, bound.max.z),
|
||||
new Vector3(bound.max.x, bound.max.y, bound.min.z),
|
||||
tex, res);
|
||||
renderLiquidQuad(//-x
|
||||
new Vector3(bound.min.x, bound.max.y, bound.min.z),
|
||||
new Vector3(bound.min.x, bound.min.y, bound.min.z),
|
||||
new Vector3(bound.min.x, bound.min.y, bound.max.z),
|
||||
new Vector3(bound.min.x, bound.max.y, bound.max.z),
|
||||
tex, res);
|
||||
renderLiquidQuad(//+x
|
||||
new Vector3(bound.max.x, bound.max.y, bound.max.z),
|
||||
new Vector3(bound.max.x, bound.min.y, bound.max.z),
|
||||
new Vector3(bound.max.x, bound.min.y, bound.min.z),
|
||||
new Vector3(bound.max.x, bound.max.y, bound.min.z),
|
||||
tex, res);
|
||||
renderLiquidQuad(//-z
|
||||
new Vector3(bound.max.x, bound.max.y, bound.min.z),
|
||||
new Vector3(bound.max.x, bound.min.y, bound.min.z),
|
||||
new Vector3(bound.min.x, bound.min.y, bound.min.z),
|
||||
new Vector3(bound.min.x, bound.max.y, bound.min.z),
|
||||
tex, res);
|
||||
renderLiquidQuad(//+z
|
||||
new Vector3(bound.min.x, bound.max.y, bound.max.z),
|
||||
new Vector3(bound.min.x, bound.min.y, bound.max.z),
|
||||
new Vector3(bound.max.x, bound.min.y, bound.max.z),
|
||||
new Vector3(bound.max.x, bound.max.y, bound.max.z),
|
||||
tex, res);
|
||||
}
|
||||
|
||||
public static void renderBlockOverlaySide(int x, int y, int z, int side, double tx1, double tx2, double ty1, double ty2)
|
||||
{
|
||||
double[] points = new double[]{x - 0.009, x + 1.009, y - 0.009, y + 1.009, z - 0.009, z + 1.009};
|
||||
|
||||
Tessellator tessellator = Tessellator.instance;
|
||||
switch(side)
|
||||
{
|
||||
case 0:
|
||||
tessellator.addVertexWithUV(points[0], points[2], points[4], tx1, ty1);
|
||||
tessellator.addVertexWithUV(points[1], points[2], points[4], tx2, ty1);
|
||||
tessellator.addVertexWithUV(points[1], points[2], points[5], tx2, ty2);
|
||||
tessellator.addVertexWithUV(points[0], points[2], points[5], tx1, ty2);
|
||||
break;
|
||||
case 1:
|
||||
tessellator.addVertexWithUV(points[1], points[3], points[4], tx2, ty1);
|
||||
tessellator.addVertexWithUV(points[0], points[3], points[4], tx1, ty1);
|
||||
tessellator.addVertexWithUV(points[0], points[3], points[5], tx1, ty2);
|
||||
tessellator.addVertexWithUV(points[1], points[3], points[5], tx2, ty2);
|
||||
break;
|
||||
case 2:
|
||||
tessellator.addVertexWithUV(points[0], points[3], points[4], tx2, ty1);
|
||||
tessellator.addVertexWithUV(points[1], points[3], points[4], tx1, ty1);
|
||||
tessellator.addVertexWithUV(points[1], points[2], points[4], tx1, ty2);
|
||||
tessellator.addVertexWithUV(points[0], points[2], points[4], tx2, ty2);
|
||||
break;
|
||||
case 3:
|
||||
tessellator.addVertexWithUV(points[1], points[3], points[5], tx2, ty1);
|
||||
tessellator.addVertexWithUV(points[0], points[3], points[5], tx1, ty1);
|
||||
tessellator.addVertexWithUV(points[0], points[2], points[5], tx1, ty2);
|
||||
tessellator.addVertexWithUV(points[1], points[2], points[5], tx2, ty2);
|
||||
break;
|
||||
case 4:
|
||||
tessellator.addVertexWithUV(points[0], points[3], points[5], tx2, ty1);
|
||||
tessellator.addVertexWithUV(points[0], points[3], points[4], tx1, ty1);
|
||||
tessellator.addVertexWithUV(points[0], points[2], points[4], tx1, ty2);
|
||||
tessellator.addVertexWithUV(points[0], points[2], points[5], tx2, ty2);
|
||||
break;
|
||||
case 5:
|
||||
tessellator.addVertexWithUV(points[1], points[3], points[4], tx2, ty1);
|
||||
tessellator.addVertexWithUV(points[1], points[3], points[5], tx1, ty1);
|
||||
tessellator.addVertexWithUV(points[1], points[2], points[5], tx1, ty2);
|
||||
tessellator.addVertexWithUV(points[1], points[2], points[4], tx2, ty2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean shouldRenderLiquid(FluidStack stack)
|
||||
{
|
||||
return stack.amount > 0 && stack.getFluid() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param stack The fluid stack to render
|
||||
* @return The icon of the fluid
|
||||
*/
|
||||
public static Icon prepareFluidRender(FluidStack stack, int alpha)
|
||||
{
|
||||
GL11.glDisable(GL11.GL_LIGHTING);
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
Fluid fluid = stack.getFluid();
|
||||
CCRenderState.setColour(fluid.getColor(stack)<<8|alpha);
|
||||
TextureUtils.bindAtlas(fluid.getSpriteNumber());
|
||||
return fluid.getIcon(stack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-enables lighting and disables blending.
|
||||
*/
|
||||
public static void postFluidRender()
|
||||
{
|
||||
GL11.glEnable(GL11.GL_LIGHTING);
|
||||
GL11.glDisable(GL11.GL_BLEND);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a fluid within a bounding box.
|
||||
* If the fluid is a liquid it will render as a normal tank with height equal to density/bound.height.
|
||||
* If the fluid is a gas, it will render the full box with an alpha equal to density.
|
||||
* Warning, bound will be mutated if the fluid is a liquid
|
||||
* @param stack The fluid to render.
|
||||
* @param bound The box within which the fluid is contained.
|
||||
* @param density The volume of fluid / the capacity of the tank. For gases this determines the alpha, for liquids this determines the height.
|
||||
* @param res The resolution to render at.
|
||||
*/
|
||||
public static void renderLiquidCuboid(FluidStack stack, Cuboid6 bound, double density, double res)
|
||||
{
|
||||
if(!shouldRenderLiquid(stack))
|
||||
return;
|
||||
|
||||
int alpha = 255;
|
||||
if(stack.getFluid().isGaseous())
|
||||
alpha = (int) (density*255);
|
||||
else
|
||||
bound.max.y = bound.min.y+(bound.max.y-bound.min.y)*density;
|
||||
|
||||
Icon tex = prepareFluidRender(stack, alpha);
|
||||
CCRenderState.startDrawing(7);
|
||||
renderLiquidCuboid(bound, tex, res);
|
||||
CCRenderState.draw();
|
||||
postFluidRender();
|
||||
}
|
||||
|
||||
public static void renderItemUniform(ItemStack item)
|
||||
{
|
||||
IItemRenderer customRenderer = MinecraftForgeClient.getItemRenderer(item, ENTITY);
|
||||
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()))
|
||||
{
|
||||
int renderType = Block.blocksList[item.itemID].getRenderType();
|
||||
larger = !(renderType == 1 || renderType == 19 || renderType == 12 || renderType == 2);
|
||||
}
|
||||
else if(is3D)
|
||||
{
|
||||
larger = true;
|
||||
}
|
||||
|
||||
double d = 2;
|
||||
double d1 = 1/d;
|
||||
if(larger)
|
||||
GL11.glScaled(d, d, d);
|
||||
|
||||
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
|
||||
|
||||
entityItem.setEntityItemStack(item);
|
||||
uniformRenderItem.doRenderItem(entityItem, 0, larger ? 0.09 : 0.06, 0, 0, 0);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import static org.lwjgl.opengl.ARBShaderObjects.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.lwjgl.opengl.ARBShaderObjects;
|
||||
import org.lwjgl.opengl.ARBVertexShader;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.vector.Matrix4f;
|
||||
|
||||
public class ShaderProgram
|
||||
{
|
||||
int programID;
|
||||
|
||||
public ShaderProgram()
|
||||
{
|
||||
programID = glCreateProgramObjectARB();
|
||||
if(programID == 0)
|
||||
throw new RuntimeException("Unable to allocate shader program object.");
|
||||
}
|
||||
|
||||
public void attach(int shaderType, String resource)
|
||||
{
|
||||
InputStream stream = ShaderProgram.class.getResourceAsStream(resource);
|
||||
if(stream == null)
|
||||
throw new RuntimeException("Unable to locate resource: "+resource);
|
||||
|
||||
attach(shaderType, stream);
|
||||
}
|
||||
|
||||
public void use()
|
||||
{
|
||||
glUseProgramObjectARB(programID);
|
||||
}
|
||||
|
||||
public static void restore()
|
||||
{
|
||||
glUseProgramObjectARB(0);
|
||||
}
|
||||
|
||||
public void link()
|
||||
{
|
||||
glLinkProgramARB(programID);
|
||||
if(glGetObjectParameteriARB(programID, GL_OBJECT_LINK_STATUS_ARB) == GL11.GL_FALSE)
|
||||
throw new RuntimeException("Error linking program: "+getInfoLog(programID));
|
||||
|
||||
glValidateProgramARB(programID);
|
||||
if(glGetObjectParameteriARB(programID, GL_OBJECT_VALIDATE_STATUS_ARB) == GL11.GL_FALSE)
|
||||
throw new RuntimeException("Error validating program: "+getInfoLog(programID));
|
||||
|
||||
use();
|
||||
onLink();
|
||||
restore();
|
||||
}
|
||||
|
||||
public void attach(int shaderType, InputStream stream)
|
||||
{
|
||||
if(stream == null)
|
||||
throw new RuntimeException("Invalid shader inputstream");
|
||||
|
||||
int shaderID = 0;
|
||||
try
|
||||
{
|
||||
shaderID = glCreateShaderObjectARB(shaderType);
|
||||
if(shaderID == 0)
|
||||
throw new RuntimeException("Unable to allocate shader object.");
|
||||
|
||||
try
|
||||
{
|
||||
glShaderSourceARB(shaderID, asString(stream));
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
throw new RuntimeException("Error reading inputstream.", e);
|
||||
}
|
||||
|
||||
glCompileShaderARB(shaderID);
|
||||
if(glGetObjectParameteriARB(shaderID, GL_OBJECT_COMPILE_STATUS_ARB) == GL11.GL_FALSE)
|
||||
throw new RuntimeException("Error compiling shader: "+getInfoLog(shaderID));
|
||||
|
||||
glAttachObjectARB(programID, shaderID);
|
||||
}
|
||||
catch(RuntimeException e)
|
||||
{
|
||||
glDeleteObjectARB(shaderID);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static String asString(InputStream stream) throws IOException
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
BufferedReader bin = new BufferedReader(new InputStreamReader(stream));
|
||||
String line;
|
||||
while((line = bin.readLine()) != null)
|
||||
sb.append(line).append('\n');
|
||||
stream.close();
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String getInfoLog(int shaderID)
|
||||
{
|
||||
return glGetInfoLogARB(shaderID, glGetObjectParameteriARB(shaderID, GL_OBJECT_INFO_LOG_LENGTH_ARB));
|
||||
}
|
||||
|
||||
public int getUniformLoc(String name)
|
||||
{
|
||||
return ARBShaderObjects.glGetUniformLocationARB(programID, name);
|
||||
}
|
||||
|
||||
public int getAttribLoc(String name)
|
||||
{
|
||||
return ARBVertexShader.glGetAttribLocationARB(programID, name);
|
||||
}
|
||||
|
||||
public void uniformTexture(String name, int textureIndex)
|
||||
{
|
||||
ARBShaderObjects.glUniform1iARB(getUniformLoc(name), textureIndex);
|
||||
}
|
||||
|
||||
public void onLink()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void glVertexAttributeMat4(int loc, Matrix4f matrix)
|
||||
{
|
||||
ARBVertexShader.glVertexAttrib4fARB(loc , matrix.m00, matrix.m01, matrix.m02, matrix.m03);
|
||||
ARBVertexShader.glVertexAttrib4fARB(loc+1, matrix.m10, matrix.m11, matrix.m12, matrix.m13);
|
||||
ARBVertexShader.glVertexAttrib4fARB(loc+2, matrix.m20, matrix.m21, matrix.m22, matrix.m23);
|
||||
ARBVertexShader.glVertexAttrib4fARB(loc+3, matrix.m30, matrix.m31, matrix.m32, matrix.m33);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import codechicken.lib.render.TextureUtils.IIconRegister;
|
||||
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.TextureMap;
|
||||
import net.minecraft.util.Icon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
public class SpriteSheetManager
|
||||
{
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static class SpriteSheet implements IIconRegister
|
||||
{
|
||||
private int tilesX;
|
||||
private int tilesY;
|
||||
private ArrayList<Integer> newSprites = new ArrayList<Integer>();
|
||||
private TextureSpecial[] sprites;
|
||||
private ResourceLocation resource;
|
||||
private TextureDataHolder texture;
|
||||
private int spriteWidth;
|
||||
private int spriteHeight;
|
||||
|
||||
public int atlasIndex;
|
||||
|
||||
private SpriteSheet(int tilesX, int tilesY, ResourceLocation textureFile)
|
||||
{
|
||||
this.tilesX = tilesX;
|
||||
this.tilesY = tilesY;
|
||||
this.resource = textureFile;
|
||||
sprites = new TextureSpecial[tilesX*tilesY];
|
||||
}
|
||||
|
||||
public void requestIndicies(int... indicies)
|
||||
{
|
||||
for(int i : indicies)
|
||||
setupSprite(i);
|
||||
}
|
||||
|
||||
public void registerIcons(IconRegister register)
|
||||
{
|
||||
TextureMap textureMap = (TextureMap)register;
|
||||
|
||||
if(TextureUtils.refreshTexture(textureMap, resource.func_110623_a()))
|
||||
{
|
||||
reloadTexture();
|
||||
for(int i = 0; i < sprites.length; i++)
|
||||
if(sprites[i] != null)
|
||||
textureMap.setTextureEntry(sprites[i].getIconName(), sprites[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i : newSprites)
|
||||
textureMap.setTextureEntry(sprites[i].getIconName(), sprites[i]);
|
||||
}
|
||||
newSprites.clear();
|
||||
}
|
||||
|
||||
public TextureSpecial setupSprite(int i)
|
||||
{
|
||||
if(sprites[i] == null)
|
||||
{
|
||||
String name = resource+"_"+i;
|
||||
sprites[i] = new TextureSpecial(name).baseFromSheet(this, i);
|
||||
newSprites.add(i);
|
||||
}
|
||||
return sprites[i];
|
||||
}
|
||||
|
||||
private void reloadTexture()
|
||||
{
|
||||
texture = TextureUtils.loadTexture(resource);
|
||||
spriteWidth = texture.width/tilesX;
|
||||
spriteHeight = texture.height/tilesY;
|
||||
}
|
||||
|
||||
public Icon getSprite(int index)
|
||||
{
|
||||
Icon i = sprites[index];
|
||||
if(i == null)
|
||||
throw new IllegalArgumentException("Sprite at index: "+index+" from texture file "+resource+" was not preloaded.");
|
||||
return i;
|
||||
}
|
||||
|
||||
public TextureDataHolder createSprite(int spriteIndex)
|
||||
{
|
||||
int sx = spriteIndex%tilesX;
|
||||
int sy = spriteIndex/tilesX;
|
||||
TextureDataHolder sprite = new TextureDataHolder(spriteWidth, spriteHeight);
|
||||
TextureUtils.copySubImg(texture.data, texture.width, sx*spriteWidth, sy*spriteHeight,
|
||||
spriteWidth, spriteHeight,
|
||||
sprite.data, spriteWidth, 0, 0);
|
||||
return sprite;
|
||||
}
|
||||
|
||||
public int spriteWidth()
|
||||
{
|
||||
return spriteWidth;
|
||||
}
|
||||
|
||||
public int spriteHeight()
|
||||
{
|
||||
return spriteHeight;
|
||||
}
|
||||
|
||||
public TextureSpecial func_110577_aFX(int i, TextureFX textureFX)
|
||||
{
|
||||
return setupSprite(i).addTextureFX(textureFX);
|
||||
}
|
||||
|
||||
public SpriteSheet selfRegister(int atlas)
|
||||
{
|
||||
TextureUtils.addIconRegistrar(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int atlasIndex()
|
||||
{
|
||||
return atlasIndex;
|
||||
}
|
||||
}
|
||||
|
||||
private static HashMap<String, SpriteSheet> spriteSheets = new HashMap<String, SpriteSheet>();
|
||||
|
||||
public static SpriteSheet getSheet(ResourceLocation resource)
|
||||
{
|
||||
return getSheet(16, 16, resource);
|
||||
}
|
||||
|
||||
public static SpriteSheet getSheet(int tilesX, int tilesY, ResourceLocation resource)
|
||||
{
|
||||
SpriteSheet sheet = spriteSheets.get(resource.toString());
|
||||
if(sheet == null)
|
||||
spriteSheets.put(resource.toString(), sheet = new SpriteSheet(tilesX, tilesY, resource));
|
||||
return sheet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class TextureDataHolder
|
||||
{
|
||||
public int width;
|
||||
public int height;
|
||||
public int[] data;
|
||||
|
||||
public TextureDataHolder(int width, int height)
|
||||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
data = new int[width*height];
|
||||
}
|
||||
|
||||
public TextureDataHolder(int[] data, int width)
|
||||
{
|
||||
this.data = data;
|
||||
this.width = width;
|
||||
height = data.length/width;
|
||||
}
|
||||
|
||||
public TextureDataHolder(BufferedImage img)
|
||||
{
|
||||
this(img.getWidth(), img.getHeight());
|
||||
img.getRGB(0, 0, width, height, data, 0, width);
|
||||
}
|
||||
|
||||
public TextureDataHolder copyData()
|
||||
{
|
||||
int[] copy = new int[data.length];
|
||||
System.arraycopy(data, 0, copy, 0, data.length);
|
||||
data = copy;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.render.SpriteSheetManager.SpriteSheet;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class TextureFX
|
||||
{
|
||||
public int[] imageData;
|
||||
public int tileSizeBase = 16;
|
||||
public int tileSizeSquare = 256;
|
||||
public int tileSizeMask = 15;
|
||||
public int tileSizeSquareMask = 255;
|
||||
|
||||
public boolean anaglyphEnabled;
|
||||
public TextureSpecial texture;
|
||||
|
||||
public TextureFX(int spriteIndex, SpriteSheet sheet)
|
||||
{
|
||||
texture = sheet.func_110577_aFX(spriteIndex, this);
|
||||
}
|
||||
|
||||
public TextureFX(int size, String name)
|
||||
{
|
||||
texture = new TextureSpecial(name).blank(size).selfRegister().addTextureFX(this);
|
||||
}
|
||||
|
||||
public TextureFX setAtlas(int index)
|
||||
{
|
||||
texture.atlasIndex = index;
|
||||
return this;
|
||||
}
|
||||
|
||||
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+")");
|
||||
|
||||
tileSizeBase = width;
|
||||
tileSizeSquare = tileSizeBase * tileSizeBase;
|
||||
tileSizeMask = tileSizeBase - 1;
|
||||
tileSizeSquareMask = tileSizeSquare - 1;
|
||||
setup();
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
anaglyphEnabled = Minecraft.getMinecraft().gameSettings.anaglyph;
|
||||
onTick();
|
||||
}
|
||||
|
||||
public void onTick()
|
||||
{
|
||||
}
|
||||
|
||||
public boolean changed()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import codechicken.lib.render.SpriteSheetManager.SpriteSheet;
|
||||
import codechicken.lib.render.TextureUtils.IIconRegister;
|
||||
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.TextureAtlasSprite;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.client.renderer.texture.TextureUtil;
|
||||
import net.minecraft.client.resources.ResourceManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class TextureSpecial extends TextureAtlasSprite implements IIconRegister
|
||||
{
|
||||
//sprite sheet fields
|
||||
private int spriteIndex;
|
||||
private SpriteSheet spriteSheet;
|
||||
|
||||
//textureFX fields
|
||||
private TextureFX textureFX;
|
||||
|
||||
private int blankSize = -1;
|
||||
|
||||
private ArrayList<TextureDataHolder> baseTextures;
|
||||
|
||||
private boolean selfRegister;
|
||||
public int atlasIndex;
|
||||
|
||||
protected TextureSpecial(String par1)
|
||||
{
|
||||
super(par1);
|
||||
}
|
||||
|
||||
public TextureSpecial addTexture(TextureDataHolder t)
|
||||
{
|
||||
if(baseTextures == null)
|
||||
baseTextures = new ArrayList<TextureDataHolder>();
|
||||
baseTextures.add(t);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextureSpecial baseFromSheet(SpriteSheet spriteSheet, int spriteIndex)
|
||||
{
|
||||
this.spriteSheet = spriteSheet;
|
||||
this.spriteIndex = spriteIndex;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextureSpecial addTextureFX(TextureFX fx)
|
||||
{
|
||||
textureFX = fx;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void func_110971_a(int sheetWidth, int sheetHeight, int originX, int originY, boolean rotated)
|
||||
{
|
||||
super.func_110971_a(sheetWidth, sheetHeight, originX, originY, rotated);
|
||||
if(textureFX != null)
|
||||
textureFX.onTextureDimensionsUpdate(field_130223_c, field_130224_d);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateAnimation()
|
||||
{
|
||||
if(textureFX != null)
|
||||
{
|
||||
textureFX.update();
|
||||
if(textureFX.changed())
|
||||
TextureUtil.func_110998_a(textureFX.imageData, field_130223_c, field_130224_d, field_110975_c, field_110974_d, false, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load(ResourceManager manager, ResourceLocation location)
|
||||
{
|
||||
if(baseTextures != null)
|
||||
{
|
||||
for(TextureDataHolder tex : baseTextures)
|
||||
{
|
||||
field_110976_a.add(tex.data);
|
||||
field_130223_c = tex.width;
|
||||
field_130224_d = tex.height;
|
||||
}
|
||||
}
|
||||
|
||||
if(spriteSheet != null)
|
||||
{
|
||||
TextureDataHolder tex = spriteSheet.createSprite(spriteIndex);
|
||||
field_130223_c = tex.width;
|
||||
field_130224_d = tex.height;
|
||||
field_110976_a.add(tex.data);
|
||||
}
|
||||
|
||||
if(blankSize > 0)
|
||||
{
|
||||
field_130223_c = field_130224_d = blankSize;
|
||||
field_110976_a.add(new int[blankSize*blankSize]);
|
||||
}
|
||||
|
||||
if(field_110976_a.isEmpty())
|
||||
throw new RuntimeException("No base frame for texture: "+getIconName());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean func_130098_m()
|
||||
{
|
||||
return textureFX != null || super.func_130098_m();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int func_110970_k()
|
||||
{
|
||||
if(textureFX != null)
|
||||
return 1;
|
||||
|
||||
return super.func_110970_k();
|
||||
}
|
||||
|
||||
public TextureSpecial blank(int size)
|
||||
{
|
||||
blankSize = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextureSpecial selfRegister()
|
||||
{
|
||||
selfRegister = true;
|
||||
TextureUtils.addIconRegistrar(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerIcons(IconRegister register)
|
||||
{
|
||||
if(selfRegister)
|
||||
((TextureMap)register).setTextureEntry(getIconName(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int atlasIndex()
|
||||
{
|
||||
return atlasIndex;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL12;
|
||||
|
||||
import codechicken.lib.colour.Colour;
|
||||
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.TextureMap;
|
||||
import net.minecraft.util.Icon;
|
||||
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 void registerIcons(IconRegister register);
|
||||
public int atlasIndex();
|
||||
}
|
||||
|
||||
static
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.register(new TextureUtils());
|
||||
}
|
||||
|
||||
private static ArrayList<IIconRegister> iconRegistrars = new ArrayList<TextureUtils.IIconRegister>();
|
||||
|
||||
public static void addIconRegistrar(IIconRegister registrar)
|
||||
{
|
||||
iconRegistrars.add(registrar);
|
||||
}
|
||||
|
||||
@ForgeSubscribe
|
||||
public void textureLoad(TextureStitchEvent.Pre event)
|
||||
{
|
||||
for(IIconRegister reg : iconRegistrars)
|
||||
if(reg.atlasIndex() == event.map.textureType)
|
||||
reg.registerIcons(event.map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return an array of ARGB pixel data
|
||||
*/
|
||||
public static int[] loadTextureData(ResourceLocation resource)
|
||||
{
|
||||
return loadTexture(resource).data;
|
||||
}
|
||||
|
||||
public static Colour[] loadTextureColours(ResourceLocation resource)
|
||||
{
|
||||
int[] idata = loadTextureData(resource);
|
||||
Colour[] data = new Colour[idata.length];
|
||||
for(int i = 0; i < data.length; i++)
|
||||
data[i] = new ColourARGB(idata[i]);
|
||||
return data;
|
||||
}
|
||||
|
||||
public static InputStream getTextureResource(ResourceLocation textureFile) throws IOException
|
||||
{
|
||||
return Minecraft.getMinecraft().func_110442_L().func_110536_a(textureFile).func_110527_b();
|
||||
}
|
||||
|
||||
public static BufferedImage loadBufferedImage(ResourceLocation textureFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
return loadBufferedImage(getTextureResource(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
|
||||
{
|
||||
BufferedImage img = ImageIO.read(in);
|
||||
in.close();
|
||||
return img;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
toTex[tp] = fromTex[fp];
|
||||
}
|
||||
}
|
||||
|
||||
public static void bindAtlas(int atlasIndex)
|
||||
{
|
||||
engine().func_110577_a(atlasIndex == 0 ? TextureMap.field_110575_b : TextureMap.field_110576_c);
|
||||
}
|
||||
|
||||
public static Icon getBlankIcon(int size, IconRegister 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)
|
||||
{
|
||||
TextureMap textureMap = (TextureMap) iconRegister;
|
||||
Icon 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case GL12.GL_TEXTURE_3D:
|
||||
GL11.glTexParameteri(target, GL12.GL_TEXTURE_WRAP_R, wrap);
|
||||
case GL11.GL_TEXTURE_2D:
|
||||
GL11.glTexParameteri(target, GL11.GL_TEXTURE_WRAP_T, wrap);
|
||||
case GL11.GL_TEXTURE_1D:
|
||||
GL11.glTexParameteri(target, GL11.GL_TEXTURE_WRAP_S, wrap);
|
||||
}
|
||||
}
|
||||
|
||||
public static TextureDataHolder loadTexture(ResourceLocation resource)
|
||||
{
|
||||
BufferedImage img = loadBufferedImage(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)
|
||||
{
|
||||
map.setTextureEntry(name, new PlaceholderTexture(name));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
public class UVScale implements IUVTransformation
|
||||
{
|
||||
double su;
|
||||
double sv;
|
||||
|
||||
public UVScale(double scaleu, double scalev)
|
||||
{
|
||||
su = scaleu;
|
||||
sv = scalev;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(UV uv)
|
||||
{
|
||||
uv.u*=su;
|
||||
uv.v*=sv;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import codechicken.lib.vec.Vector3;
|
||||
|
||||
public class Vertex5
|
||||
{
|
||||
public Vector3 vec;
|
||||
public UV uv;
|
||||
|
||||
public Vertex5()
|
||||
{
|
||||
this(new Vector3(), new UV());
|
||||
}
|
||||
|
||||
public Vertex5(Vector3 vert, UV uv)
|
||||
{
|
||||
this.vec = vert;
|
||||
this.uv = uv;
|
||||
}
|
||||
|
||||
public Vertex5(Vector3 vert, double u, double v)
|
||||
{
|
||||
this(vert, new UV(u, v));
|
||||
}
|
||||
|
||||
public Vertex5(double x, double y, double z, double u, double v)
|
||||
{
|
||||
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);
|
||||
uv.set(u, v);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vertex5(Vertex5 vertex5)
|
||||
{
|
||||
this(vertex5.vec.copy(), vertex5.uv.copy());
|
||||
}
|
||||
|
||||
public Vertex5 copy()
|
||||
{
|
||||
return new Vertex5(this);
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "Vertex: ("+new BigDecimal(vec.x, cont)+", "+new BigDecimal(vec.y, cont)+", "+new BigDecimal(vec.z, cont)+") ("+new BigDecimal(uv.u, cont)+", "+new BigDecimal(uv.v, cont)+")";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user