mirror of
https://github.com/GTNewHorizons/CodeChickenCore.git
synced 2026-11-17 11:05:24 +00:00
Cleaned up directory layout
This commit is contained in:
1 parent
32fa471c44
commit
0a14b64e12
105 files changed
+1
-35
No files matched your search
@@ -0,0 +1,126 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.lighting.LC;
|
||||
import codechicken.lib.vec.Cuboid6;
|
||||
|
||||
public class BlockRenderer
|
||||
{
|
||||
public static class BlockFace implements CCRenderState.IVertexSource
|
||||
{
|
||||
public Vertex5[] verts = new Vertex5[]{new Vertex5(), new Vertex5(), new Vertex5(), new Vertex5()};
|
||||
public LC[] lightCoords = new LC[]{new LC(), new LC(), new LC(), new LC()};
|
||||
public boolean lcComputed = false;
|
||||
public int side;
|
||||
|
||||
@Override
|
||||
public Vertex5[] getVertices() {
|
||||
return verts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getAttributes(CCRenderState.VertexAttribute<T> attr) {
|
||||
return attr == CCRenderState.lightCoordAttrib && lcComputed ? (T)lightCoords : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAttribute(CCRenderState.VertexAttribute<?> attr) {
|
||||
return attr == CCRenderState.sideAttrib || attr == CCRenderState.lightCoordAttrib && lcComputed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareVertex() {
|
||||
CCRenderState.side = side;
|
||||
}
|
||||
|
||||
public BlockFace computeLightCoords() {
|
||||
if(!lcComputed) {
|
||||
for (int i = 0; i < 4; i++)
|
||||
lightCoords[i].compute(verts[i].vec, side);
|
||||
lcComputed = true;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public BlockFace loadCuboidFace(Cuboid6 c, int side) {
|
||||
double x1 = c.min.x;
|
||||
double x2 = c.max.x;
|
||||
double y1 = c.min.y;
|
||||
double y2 = c.max.y;
|
||||
double z1 = c.min.z;
|
||||
double z2 = c.max.z;
|
||||
double u1; double u2; double v1; double v2;
|
||||
this.side = side;
|
||||
lcComputed = false;
|
||||
|
||||
switch(side) {
|
||||
case 0:
|
||||
u1 = x1; v1 = z1;
|
||||
u2 = x2; v2 = z2;
|
||||
verts[0].set(x1, y1, z2, u1, v2, 0);
|
||||
verts[1].set(x1, y1, z1, u1, v1, 0);
|
||||
verts[2].set(x2, y1, z1, u2, v1, 0);
|
||||
verts[3].set(x2, y1, z2, u2, v2, 0);
|
||||
break;
|
||||
case 1:
|
||||
u1 = x1; v1 = z1;
|
||||
u2 = x2; v2 = z2;
|
||||
verts[0].set(x2, y2, z2, u2, v2, 1);
|
||||
verts[1].set(x2, y2, z1, u2, v1, 1);
|
||||
verts[2].set(x1, y2, z1, u1, v1, 1);
|
||||
verts[3].set(x1, y2, z2, u1, v2, 1);
|
||||
break;
|
||||
case 2:
|
||||
u1 = 1-x1; v1 = 1-y2;
|
||||
u2 = 1-x2; v2 = 1-y1;
|
||||
verts[0].set(x1, y1, z1, u1, v2, 2);
|
||||
verts[1].set(x1, y2, z1, u1, v1, 2);
|
||||
verts[2].set(x2, y2, z1, u2, v1, 2);
|
||||
verts[3].set(x2, y1, z1, u2, v2, 2);
|
||||
break;
|
||||
case 3:
|
||||
u1 = x1; v1 = 1-y2;
|
||||
u2 = x2; v2 = 1-y1;
|
||||
verts[0].set(x2, y1, z2, u2, v2, 3);
|
||||
verts[1].set(x2, y2, z2, u2, v1, 3);
|
||||
verts[2].set(x1, y2, z2, u1, v1, 3);
|
||||
verts[3].set(x1, y1, z2, u1, v2, 3);
|
||||
break;
|
||||
case 4:
|
||||
u1 = z1; v1 = 1-y2;
|
||||
u2 = z2; v2 = 1-y1;
|
||||
verts[0].set(x1, y1, z2, u2, v2, 4);
|
||||
verts[1].set(x1, y2, z2, u2, v1, 4);
|
||||
verts[2].set(x1, y2, z1, u1, v1, 4);
|
||||
verts[3].set(x1, y1, z1, u1, v2, 4);
|
||||
break;
|
||||
case 5:
|
||||
u1 = 1-z1; v1 = 1-y2;
|
||||
u2 = 1-z2; v2 = 1-y1;
|
||||
verts[0].set(x2, y1, z1, u1, v2, 5);
|
||||
verts[1].set(x2, y2, z1, u1, v1, 5);
|
||||
verts[2].set(x2, y2, z2, u2, v1, 5);
|
||||
verts[3].set(x2, y1, z2, u2, v2, 5);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private static BlockFace face = new BlockFace();
|
||||
|
||||
/**
|
||||
* Renders faces of a cuboid with texture coordinates mapped to match a standard minecraft block
|
||||
* @param bounds The bounding cuboid to render
|
||||
* @param sideMask A mask of faces not to render
|
||||
*/
|
||||
public static void renderCuboid(Cuboid6 bounds, int sideMask) {
|
||||
if(sideMask == 0x3F)
|
||||
return;
|
||||
|
||||
CCRenderState.setModel(face);
|
||||
for(int s = 0; s < 6; s++)
|
||||
if((sideMask & 1<<s) == 0) {
|
||||
face.loadCuboidFace(bounds, s);
|
||||
CCRenderState.render();
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,89 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.vec.*;
|
||||
|
||||
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,135 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.render.CCRenderState.IVertexOperation;
|
||||
import codechicken.lib.render.CCRenderState.VertexAttribute;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CCRenderPipeline
|
||||
{
|
||||
public class PipelineBuilder
|
||||
{
|
||||
public PipelineBuilder add(IVertexOperation op) {
|
||||
ops.add(op);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PipelineBuilder add(IVertexOperation... ops) {
|
||||
for(int i = 0; i < ops.length; i++)
|
||||
CCRenderPipeline.this.ops.add(ops[i]);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void build() {
|
||||
rebuild();
|
||||
}
|
||||
|
||||
public void render() {
|
||||
rebuild();
|
||||
CCRenderState.render();
|
||||
}
|
||||
}
|
||||
|
||||
private class PipelineNode
|
||||
{
|
||||
public ArrayList<PipelineNode> deps = new ArrayList<PipelineNode>();
|
||||
public IVertexOperation op;
|
||||
|
||||
public void add() {
|
||||
if(op == null)
|
||||
return;
|
||||
|
||||
for(int i = 0; i < deps.size(); i++)
|
||||
deps.get(i).add();
|
||||
deps.clear();
|
||||
sorted.add(op);
|
||||
op = null;
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList<VertexAttribute> attribs = new ArrayList<VertexAttribute>();
|
||||
private ArrayList<IVertexOperation> ops = new ArrayList<IVertexOperation>();
|
||||
private ArrayList<PipelineNode> nodes = new ArrayList<PipelineNode>();
|
||||
private ArrayList<IVertexOperation> sorted = new ArrayList<IVertexOperation>();
|
||||
private PipelineNode loading;
|
||||
private PipelineBuilder builder = new PipelineBuilder();
|
||||
|
||||
public void setPipeline(IVertexOperation... ops) {
|
||||
this.ops.clear();
|
||||
for(int i = 0; i < ops.length; i++)
|
||||
this.ops.add(ops[i]);
|
||||
rebuild();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
ops.clear();
|
||||
unbuild();
|
||||
}
|
||||
|
||||
private void unbuild() {
|
||||
for(int i = 0; i < attribs.size(); i++)
|
||||
attribs.get(i).active = false;
|
||||
attribs.clear();
|
||||
sorted.clear();
|
||||
}
|
||||
|
||||
public void rebuild() {
|
||||
if(ops.isEmpty() || CCRenderState.model == null)
|
||||
return;
|
||||
|
||||
//ensure enough nodes for all ops
|
||||
while(nodes.size() < CCRenderState.operationCount())
|
||||
nodes.add(new PipelineNode());
|
||||
unbuild();
|
||||
|
||||
if(CCRenderState.useNormals)
|
||||
addAttribute(CCRenderState.normalAttrib);
|
||||
if(CCRenderState.baseColour != -1 || CCRenderState.alphaOverride >= 0)
|
||||
addAttribute(CCRenderState.colourAttrib);
|
||||
else if(CCRenderState.hasColour)
|
||||
CCRenderState.setColour(-1);
|
||||
|
||||
for(int i = 0; i < ops.size(); i++) {
|
||||
IVertexOperation op = ops.get(i);
|
||||
loading = nodes.get(op.operationID());
|
||||
boolean loaded = op.load();
|
||||
if(loaded)
|
||||
loading.op = op;
|
||||
|
||||
if(op instanceof VertexAttribute)
|
||||
if(loaded)
|
||||
attribs.add((VertexAttribute)op);
|
||||
else
|
||||
((VertexAttribute)op).active = false;
|
||||
}
|
||||
|
||||
for(int i = 0; i < nodes.size(); i++)
|
||||
nodes.get(i).add();
|
||||
}
|
||||
|
||||
public void addRequirement(int opRef) {
|
||||
loading.deps.add(nodes.get(opRef));
|
||||
}
|
||||
|
||||
public void addDependency(VertexAttribute attrib) {
|
||||
loading.deps.add(nodes.get(attrib.operationID()));
|
||||
addAttribute(attrib);
|
||||
}
|
||||
|
||||
public void addAttribute(VertexAttribute attrib) {
|
||||
if(!attrib.active) {
|
||||
ops.add(attrib);
|
||||
attrib.active = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void operate() {
|
||||
for(int i = 0; i < sorted.size(); i++)
|
||||
sorted.get(i).operate();
|
||||
}
|
||||
|
||||
public PipelineBuilder builder() {
|
||||
ops.clear();
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,383 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.colour.ColourRGBA;
|
||||
import codechicken.lib.lighting.LC;
|
||||
import codechicken.lib.lighting.LightMatrix;
|
||||
import codechicken.lib.util.Copyable;
|
||||
import codechicken.lib.vec.Rotation;
|
||||
import codechicken.lib.vec.Transformation;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.OpenGlHelper;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The core of the CodeChickenLib render system.
|
||||
* Rendering operations are written to avoid object allocations by reusing static variables.
|
||||
*/
|
||||
public class CCRenderState
|
||||
{
|
||||
private static int nextOperationIndex;
|
||||
|
||||
public static int registerOperation() {
|
||||
return nextOperationIndex++;
|
||||
}
|
||||
|
||||
public static int operationCount() {
|
||||
return nextOperationIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents an operation to be run for each vertex that operates on and modifies the current state
|
||||
*/
|
||||
public static interface IVertexOperation
|
||||
{
|
||||
/**
|
||||
* Load any required references and add dependencies to the pipeline based on the current model (may be null)
|
||||
* Return false if this operation is redundant in the pipeline with the given model
|
||||
*/
|
||||
public boolean load();
|
||||
|
||||
/**
|
||||
* Perform the operation on the current render state
|
||||
*/
|
||||
public void operate();
|
||||
|
||||
/**
|
||||
* Get the unique id representing this type of operation. Duplicate operation IDs within the pipeline may have unexpected results.
|
||||
* ID shoulld be obtained from CCRenderState.registerOperation() and stored in a static variable
|
||||
*/
|
||||
public int operationID();
|
||||
}
|
||||
|
||||
private static ArrayList<VertexAttribute<?>> vertexAttributes = new ArrayList<VertexAttribute<?>>();
|
||||
private static int registerVertexAttribute(VertexAttribute<?> attr) {
|
||||
vertexAttributes.add(attr);
|
||||
return vertexAttributes.size()-1;
|
||||
}
|
||||
|
||||
public static VertexAttribute<?> getAttribute(int index) {
|
||||
return vertexAttributes.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Management class for a vertex attrute such as colour, normal etc
|
||||
* This class should handle the loading of the attrute from an array provided by IVertexSource.getAttributes or the computation of this attrute from others
|
||||
* @param <T> The array type for this attrute eg. int[], Vector3[]
|
||||
*/
|
||||
public static abstract class VertexAttribute<T> implements IVertexOperation
|
||||
{
|
||||
public final int attributeIndex = registerVertexAttribute(this);
|
||||
private final int operationIndex = registerOperation();
|
||||
/**
|
||||
* Set to true when the attrute is part of the pipeline. Should only be managed by CCRenderState when constructing the pipeline
|
||||
*/
|
||||
public boolean active = false;
|
||||
|
||||
/**
|
||||
* Construct a new array for storage of vertex attrutes in a model
|
||||
*/
|
||||
public abstract T newArray(int length);
|
||||
|
||||
@Override
|
||||
public int operationID() {
|
||||
return operationIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public static void arrayCopy(Object src, int srcPos, Object dst, int destPos, int length) {
|
||||
System.arraycopy(src, srcPos, dst, destPos, length);
|
||||
if(dst instanceof Copyable[]) {
|
||||
Object[] oa = (Object[])dst;
|
||||
Copyable<Object>[] c = (Copyable[])dst;
|
||||
for(int i = destPos; i < destPos+length; i++)
|
||||
if(c[i] != null)
|
||||
oa[i] = c[i].copy();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T copyOf(VertexAttribute<T> attr, T src, int length) {
|
||||
T dst = attr.newArray(length);
|
||||
arrayCopy(src, 0, dst, 0, length);
|
||||
return dst;
|
||||
}
|
||||
|
||||
public static interface IVertexSource
|
||||
{
|
||||
public Vertex5[] getVertices();
|
||||
|
||||
/**
|
||||
* Gets an array of vertex attrutes
|
||||
* @param attr The vertex attrute to get
|
||||
* @param <T> The attrute array type
|
||||
* @return An array, or null if not computed
|
||||
*/
|
||||
public <T> T getAttributes(VertexAttribute<T> attr);
|
||||
|
||||
/**
|
||||
* @return True if the specified attrute is provided by this model, either by returning an array from getAttributes or by setting the state in prepareVertex
|
||||
*/
|
||||
public boolean hasAttribute(VertexAttribute<?> attr);
|
||||
|
||||
/**
|
||||
* Callback to set CCRenderState for a vertex before the pipeline runs
|
||||
*/
|
||||
public void prepareVertex();
|
||||
}
|
||||
|
||||
public static VertexAttribute<Vector3[]> normalAttrib = new VertexAttribute<Vector3[]>() {
|
||||
private Vector3[] normalRef;
|
||||
|
||||
@Override
|
||||
public Vector3[] newArray(int length) {
|
||||
return new Vector3[length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
normalRef = model.getAttributes(this);
|
||||
if(model.hasAttribute(this))
|
||||
return normalRef != null;
|
||||
|
||||
if(model.hasAttribute(sideAttrib)) {
|
||||
pipeline.addDependency(sideAttrib);
|
||||
return true;
|
||||
}
|
||||
throw new IllegalStateException("Normals requested but neither normal or side attrutes are provided by the model");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
if(normalRef != null)
|
||||
setNormal(normalRef[vertexIndex]);
|
||||
else
|
||||
setNormal(Rotation.axes[side]);
|
||||
}
|
||||
};
|
||||
public static VertexAttribute<int[]> colourAttrib = new VertexAttribute<int[]>() {
|
||||
private int[] colourRef;
|
||||
|
||||
@Override
|
||||
public int[] newArray(int length) {
|
||||
return new int[length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
colourRef = model.getAttributes(this);
|
||||
return colourRef != null || !model.hasAttribute(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
if(colourRef != null)
|
||||
setColour(ColourRGBA.multiply(baseColour, colourRef[vertexIndex]));
|
||||
else
|
||||
setColour(baseColour);
|
||||
}
|
||||
};
|
||||
public static VertexAttribute<int[]> sideAttrib = new VertexAttribute<int[]>() {
|
||||
private int[] sideRef;
|
||||
|
||||
@Override
|
||||
public int[] newArray(int length) {
|
||||
return new int[length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
sideRef = model.getAttributes(this);
|
||||
if(model.hasAttribute(this))
|
||||
return sideRef != null;
|
||||
|
||||
pipeline.addDependency(normalAttrib);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
if(sideRef != null)
|
||||
side = sideRef[vertexIndex];
|
||||
else
|
||||
side = CCModel.findSide(normal);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Uses the position of the lightmatrix to compute LC if not provided
|
||||
*/
|
||||
public static VertexAttribute<LC[]> lightCoordAttrib = new VertexAttribute<LC[]>() {
|
||||
private LC[] lcRef;
|
||||
private Vector3 vec = new Vector3();//for computation
|
||||
private Vector3 pos = new Vector3();
|
||||
|
||||
@Override
|
||||
public LC[] newArray(int length) {
|
||||
return new LC[length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
lcRef = model.getAttributes(this);
|
||||
if(model.hasAttribute(this))
|
||||
return lcRef != null;
|
||||
|
||||
pos.set(lightMatrix.pos.x, lightMatrix.pos.y, lightMatrix.pos.z);
|
||||
pipeline.addDependency(sideAttrib);
|
||||
pipeline.addRequirement(Transformation.operationIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
if(lcRef != null)
|
||||
lc.set(lcRef[vertexIndex]);
|
||||
else
|
||||
lc.compute(vec.set(vert.vec).sub(pos), side);
|
||||
}
|
||||
};
|
||||
|
||||
//pipeline state
|
||||
public static IVertexSource model;
|
||||
public static int firstVertexIndex;
|
||||
public static int lastVertexIndex;
|
||||
public static int vertexIndex;
|
||||
public static CCRenderPipeline pipeline = new CCRenderPipeline();
|
||||
|
||||
//context
|
||||
public static int baseColour;
|
||||
public static int alphaOverride;
|
||||
public static boolean useNormals;
|
||||
public static LightMatrix lightMatrix = new LightMatrix();
|
||||
|
||||
//vertex outputs
|
||||
public static Vertex5 vert = new Vertex5();
|
||||
public static boolean hasNormal;
|
||||
public static Vector3 normal = new Vector3();
|
||||
public static boolean hasColour;
|
||||
public static int colour;
|
||||
public static boolean hasBrightness;
|
||||
public static int brightness;
|
||||
|
||||
//attrute storage
|
||||
public static int side;
|
||||
public static LC lc = new LC();
|
||||
|
||||
public static void reset() {
|
||||
model = null;
|
||||
pipeline.reset();
|
||||
hasNormal = hasColour = hasBrightness = false;
|
||||
baseColour = alphaOverride = -1;
|
||||
}
|
||||
|
||||
public static void setPipeline(IVertexOperation... ops) {
|
||||
pipeline.setPipeline(ops);
|
||||
}
|
||||
|
||||
public static void setPipeline(IVertexSource model, int start, int end, IVertexOperation... ops) {
|
||||
pipeline.reset();
|
||||
setModel(model, start, end);
|
||||
pipeline.setPipeline(ops);
|
||||
}
|
||||
|
||||
public static void bindModel(IVertexSource model) {
|
||||
if(CCRenderState.model != model) {
|
||||
CCRenderState.model = model;
|
||||
pipeline.rebuild();
|
||||
}
|
||||
}
|
||||
|
||||
public static void setModel(IVertexSource source) {
|
||||
setModel(source, 0, source.getVertices().length);
|
||||
}
|
||||
|
||||
public static void setModel(IVertexSource source, int start, int end) {
|
||||
bindModel(source);
|
||||
firstVertexIndex = start;
|
||||
lastVertexIndex = end;
|
||||
}
|
||||
|
||||
public static void render(IVertexOperation... ops) {
|
||||
setPipeline(ops);
|
||||
render();
|
||||
}
|
||||
|
||||
public static void render() {
|
||||
Vertex5[] verts = model.getVertices();
|
||||
for(vertexIndex = firstVertexIndex; vertexIndex < lastVertexIndex; vertexIndex++) {
|
||||
model.prepareVertex();
|
||||
vert.set(verts[vertexIndex]);
|
||||
runPipeline();
|
||||
writeVert();
|
||||
}
|
||||
}
|
||||
|
||||
public static void runPipeline() {
|
||||
pipeline.operate();
|
||||
}
|
||||
|
||||
public static void writeVert() {
|
||||
if(hasNormal)
|
||||
Tessellator.instance.setNormal((float)normal.x, (float)normal.y, (float)normal.z);
|
||||
if(hasColour)
|
||||
Tessellator.instance.setColorRGBA(colour>>>24, colour>>16 & 0xFF, colour>>8 & 0xFF, alphaOverride >= 0 ? alphaOverride : colour & 0xFF);
|
||||
if(hasBrightness)
|
||||
Tessellator.instance.setBrightness(brightness);
|
||||
Tessellator.instance.addVertexWithUV(vert.vec.x, vert.vec.y, vert.vec.z, vert.uv.u, vert.uv.v);
|
||||
}
|
||||
|
||||
public static void setNormal(double x, double y, double z) {
|
||||
hasNormal = true;
|
||||
normal.set(x, y, z);
|
||||
}
|
||||
|
||||
public static void setNormal(Vector3 n) {
|
||||
hasNormal = true;
|
||||
normal.set(n);
|
||||
}
|
||||
|
||||
public static void setColour(int c) {
|
||||
hasColour = true;
|
||||
colour = c;
|
||||
}
|
||||
|
||||
public static void setBrightness(int b) {
|
||||
hasBrightness = true;
|
||||
brightness = b;
|
||||
}
|
||||
|
||||
public static void setBrightness(IBlockAccess world, int x, int y, int z) {
|
||||
setBrightness(world.getBlock(x, y, z).getMixedBrightnessForBlock(world, x, y, z));
|
||||
}
|
||||
|
||||
public static void pullLightmap() {
|
||||
setBrightness((int)OpenGlHelper.lastBrightnessY << 16 | (int)OpenGlHelper.lastBrightnessX);
|
||||
}
|
||||
|
||||
public static void changeTexture(String texture) {
|
||||
changeTexture(new ResourceLocation(texture));
|
||||
}
|
||||
|
||||
public static void changeTexture(ResourceLocation texture) {
|
||||
Minecraft.getMinecraft().renderEngine.bindTexture(texture);
|
||||
}
|
||||
|
||||
public static void startDrawing() {
|
||||
startDrawing(7);
|
||||
}
|
||||
|
||||
private static void startDrawing(int mode) {
|
||||
Tessellator.instance.startDrawing(mode);
|
||||
if(hasColour)
|
||||
Tessellator.instance.setColorRGBA(colour>>>24, colour>>16 & 0xFF, colour>>8 & 0xFF, alphaOverride >= 0 ? alphaOverride : colour & 0xFF);
|
||||
if(hasBrightness)
|
||||
Tessellator.instance.setBrightness(brightness);
|
||||
}
|
||||
|
||||
public static void draw() {
|
||||
Tessellator.instance.draw();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.colour.ColourRGBA;
|
||||
|
||||
public class ColourMultiplier implements CCRenderState.IVertexOperation
|
||||
{
|
||||
private static ColourMultiplier instance = new ColourMultiplier(-1);
|
||||
|
||||
public static ColourMultiplier instance(int colour) {
|
||||
instance.colour = colour;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static final int operationIndex = CCRenderState.registerOperation();
|
||||
public int colour;
|
||||
|
||||
public ColourMultiplier(int colour) {
|
||||
this.colour = colour;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
if(colour == -1)
|
||||
return false;
|
||||
|
||||
CCRenderState.pipeline.addDependency(CCRenderState.colourAttrib);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
CCRenderState.setColour(ColourRGBA.multiply(CCRenderState.colour, colour));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int operationID() {
|
||||
return operationIndex;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
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.IIcon;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class EntityDigIconFX extends EntityFX
|
||||
{
|
||||
public EntityDigIconFX(World world, double x, double y, double z, double dx, double dy, double dz, IIcon 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;
|
||||
}
|
||||
|
||||
public void setScale(float scale) {
|
||||
particleScale = scale;
|
||||
}
|
||||
|
||||
public float getScale() {
|
||||
return particleScale;
|
||||
}
|
||||
|
||||
public void setMaxAge(int age){
|
||||
particleMaxAge = age;
|
||||
}
|
||||
|
||||
public int getMaxAge(){
|
||||
return particleMaxAge;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, IIcon 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, IIcon[] 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,68 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
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,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,19 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
public class PlaceholderTexture extends TextureAtlasSprite
|
||||
{
|
||||
protected PlaceholderTexture(String par1)
|
||||
{
|
||||
super(par1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load(IResourceManager manager, ResourceLocation location)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,748 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.render.uv.UV;
|
||||
import codechicken.lib.render.uv.UVScale;
|
||||
import codechicken.lib.vec.*;
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class QBImporter
|
||||
{
|
||||
private static class ImagePackNode
|
||||
{
|
||||
Rectangle4i rect;
|
||||
ImagePackNode child1;
|
||||
ImagePackNode child2;
|
||||
QBImage packed;
|
||||
|
||||
public ImagePackNode(int x, int y, int w, int h) {
|
||||
rect = new Rectangle4i(x, y, w, h);
|
||||
}
|
||||
|
||||
public boolean pack(QBImage img) {
|
||||
if(child1 != null)
|
||||
return child1.pack(img) || child2.pack(img);
|
||||
|
||||
if(packed != null)
|
||||
return false;
|
||||
|
||||
int fit = getFit(img.width(), img.height());
|
||||
if(fit == 0)
|
||||
return false;
|
||||
|
||||
if((fit & 2) != 0) {//exact fit
|
||||
packed = img;
|
||||
img.packSlot = rect;
|
||||
img.packT = new ImageTransform((fit&1)<<2);
|
||||
return true;
|
||||
}
|
||||
|
||||
int w = (fit & 1) == 0 ? img.width() : img.height();
|
||||
int h = (fit & 1) == 0 ? img.height() : img.width();
|
||||
|
||||
if(rect.w - w > rect.h - h) {//create split with biggest leftover space
|
||||
child1 = new ImagePackNode(rect.x, rect.y, w, rect.h);
|
||||
child2 = new ImagePackNode(rect.x+w, rect.y, rect.w-w, rect.h);
|
||||
} else {
|
||||
child1 = new ImagePackNode(rect.x, rect.y, rect.w, h);
|
||||
child2 = new ImagePackNode(rect.x, rect.y+h, rect.w, rect.h-h);
|
||||
}
|
||||
return child1.pack(img);
|
||||
}
|
||||
|
||||
private int getFit(int w, int h) {
|
||||
if(w == rect.w && h == rect.h)
|
||||
return 2;
|
||||
if(w == rect.h && h == rect.w)
|
||||
return 3;
|
||||
if(rect.w >= w && rect.h >= h)
|
||||
return 4;
|
||||
if(rect.w >= h && rect.h >= w)
|
||||
return 5;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static void nextSize(Rectangle4i rect, boolean square) {
|
||||
if(square) {
|
||||
rect.w <<= 1;
|
||||
rect.h <<= 1;
|
||||
}
|
||||
else {
|
||||
if(rect.w == rect.h)
|
||||
rect.w *= 2;
|
||||
else
|
||||
rect.h *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
public static ImagePackNode pack(List<QBImage> images, boolean square) {
|
||||
Collections.sort(images);
|
||||
|
||||
int area = 0;
|
||||
for(QBImage img : images)
|
||||
area+=img.area();
|
||||
|
||||
ImagePackNode node = new ImagePackNode(0, 0, 2, 2);
|
||||
while(node.rect.area() < area)
|
||||
nextSize(node.rect, square);
|
||||
|
||||
while(true) {
|
||||
boolean packed = true;
|
||||
for(QBImage img : images)
|
||||
if(!node.pack(img)) {
|
||||
packed = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if(packed)
|
||||
return node;
|
||||
|
||||
node.child1 = node.child2 = null;
|
||||
nextSize(node.rect, square);
|
||||
}
|
||||
}
|
||||
|
||||
public BufferedImage toImage() {
|
||||
BufferedImage img = new BufferedImage(rect.w, rect.h, BufferedImage.TYPE_INT_ARGB);
|
||||
write(img);
|
||||
return img;
|
||||
}
|
||||
|
||||
private void write(BufferedImage img) {
|
||||
if(child1 != null) {
|
||||
child1.write(img);
|
||||
child2.write(img);
|
||||
}
|
||||
else if(packed != null) {
|
||||
ImageTransform t = packed.packT;
|
||||
for(int u = 0; u < rect.w; u++)
|
||||
for(int v = 0; v < rect.h; v++) {
|
||||
int rgba = t.access(packed, u, v);
|
||||
img.setRGB(u+rect.x, v+rect.y, rgba>>>8 | rgba<<24);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class ImageTransform
|
||||
{
|
||||
int transform;
|
||||
|
||||
public ImageTransform(int i) {
|
||||
transform = i;
|
||||
}
|
||||
|
||||
public ImageTransform() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public boolean transpose() {
|
||||
return (transform&4) != 0;
|
||||
}
|
||||
|
||||
public boolean flipU() {
|
||||
return (transform&1) != 0;
|
||||
}
|
||||
|
||||
public boolean flipV() {
|
||||
return (transform&2) != 0;
|
||||
}
|
||||
|
||||
public int access(QBImage img, int u, int v) {
|
||||
if(transpose()) {
|
||||
int tmp = u; u = v; v = tmp;
|
||||
}
|
||||
if(flipU())
|
||||
u = img.width()-1-u;
|
||||
if(flipV())
|
||||
v = img.height()-1-v;
|
||||
|
||||
return img.data[u][v];
|
||||
}
|
||||
|
||||
public UV transform(UV uv) {
|
||||
if(transpose()) {
|
||||
double tmp = uv.u; uv.u = uv.v; uv.v = tmp;
|
||||
}
|
||||
if(flipU())
|
||||
uv.u = 1-uv.u;
|
||||
if(flipV())
|
||||
uv.v = 1-uv.v;
|
||||
|
||||
return uv;
|
||||
}
|
||||
}
|
||||
|
||||
public static class QBImage implements Comparable<QBImage>
|
||||
{
|
||||
int[][] data;
|
||||
ImageTransform packT;
|
||||
Rectangle4i packSlot;
|
||||
|
||||
public int width() {
|
||||
return data.length;
|
||||
}
|
||||
|
||||
public int height() {
|
||||
return data[0].length;
|
||||
}
|
||||
|
||||
public int area() {
|
||||
return width()*height();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(QBImage o) {
|
||||
int a = area(); int b = o.area();
|
||||
return a > b ? -1 : a == b ? 0 : 1;
|
||||
}
|
||||
|
||||
public ImageTransform transformTo(QBImage img) {
|
||||
if(width() == img.width() && height() == img.height())
|
||||
for(int i = 0; i < 4; i++) {
|
||||
ImageTransform t = new ImageTransform(i);
|
||||
if(equals(img, t))
|
||||
return t;
|
||||
}
|
||||
if(width() == img.height() && height() == img.width())
|
||||
for(int i = 4; i < 8; i++) {
|
||||
ImageTransform t = new ImageTransform(i);
|
||||
if(equals(img, t))
|
||||
return t;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean equals(QBImage img, ImageTransform t) {
|
||||
for(int u = 0; u < img.width(); u++)
|
||||
for(int v = 0; v < img.height(); v++)
|
||||
if(t.access(this, u, v) != img.data[u][v])
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void transform(UV uv) {
|
||||
packT.transform(uv);
|
||||
uv.u*=packSlot.w;
|
||||
uv.v*=packSlot.h;
|
||||
uv.u+=packSlot.x;
|
||||
uv.v+=packSlot.y;
|
||||
}
|
||||
}
|
||||
|
||||
private static final int[][] vertOrder = new int[][] {//clockwise because MC is left handed
|
||||
{3, 0},
|
||||
{1, 0},
|
||||
{1, 2},
|
||||
{3, 2}};
|
||||
|
||||
public static class QBQuad
|
||||
{
|
||||
public Vertex5[] verts = new Vertex5[4];
|
||||
public QBImage image = new QBImage();
|
||||
public ImageTransform t = new ImageTransform();
|
||||
public int side;
|
||||
|
||||
public QBQuad(int side) {
|
||||
this.side = side;
|
||||
}
|
||||
|
||||
public void applyImageT() {
|
||||
for(Vertex5 vert : verts) {
|
||||
t.transform(vert.uv);
|
||||
image.transform(vert.uv);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static QBQuad restore(Rectangle4i flat, int side, double d, QBImage img) {
|
||||
QBQuad quad = new QBQuad(side);
|
||||
quad.image = img;
|
||||
|
||||
Transformation t = new Scale(-1, 1, -1).with(Rotation.sideOrientation(side, 0)).with(new Translation(new Vector3().setSide(side, d)));
|
||||
quad.verts[0] = new Vertex5(flat.x, 0, flat.y, 0, 0);
|
||||
quad.verts[1] = new Vertex5(flat.x+flat.w, 0, flat.y, 1, 0);
|
||||
quad.verts[2] = new Vertex5(flat.x+flat.w, 0, flat.y+flat.h, 1, 1);
|
||||
quad.verts[3] = new Vertex5(flat.x, 0, flat.y+flat.h, 0, 1);
|
||||
for(Vertex5 vert : quad.verts)
|
||||
vert.apply(t);
|
||||
|
||||
return quad;
|
||||
}
|
||||
|
||||
public Rectangle4i flatten() {
|
||||
Transformation t = Rotation.sideOrientation(side, 0).inverse().with(new Scale(-1, 0, -1));
|
||||
Vector3 vmin = verts[0].vec.copy().apply(t);
|
||||
Vector3 vmax = verts[2].vec.copy().apply(t);
|
||||
return new Rectangle4i((int)vmin.x, (int)vmin.z, (int)(vmax.x-vmin.x), (int)(vmax.z-vmin.z));
|
||||
}
|
||||
}
|
||||
|
||||
public static class QBCuboid
|
||||
{
|
||||
public QBMatrix mat;
|
||||
public CuboidCoord c;
|
||||
public int sides;
|
||||
|
||||
public QBCuboid(QBMatrix mat, CuboidCoord c) {
|
||||
this.mat = mat;
|
||||
this.c = c;
|
||||
sides = 0;
|
||||
}
|
||||
|
||||
public static boolean intersects(QBCuboid a, QBCuboid b) {
|
||||
CuboidCoord c = a.c;
|
||||
CuboidCoord d = b.c;
|
||||
return c.min.x <= d.max.x &&
|
||||
d.min.x <= c.max.x &&
|
||||
c.min.y <= d.max.y &&
|
||||
d.min.y <= c.max.y &&
|
||||
c.min.z <= d.max.z &&
|
||||
d.min.z <= c.max.z;
|
||||
}
|
||||
|
||||
public static void clip(QBCuboid a, QBCuboid b) {
|
||||
if(intersects(a, b)) {
|
||||
a.clip(b);
|
||||
b.clip(a);
|
||||
}
|
||||
}
|
||||
|
||||
public void clip(QBCuboid o) {
|
||||
CuboidCoord d = o.c;
|
||||
for(int a = 0; a < 6; a+=2)
|
||||
{
|
||||
int a1 = (a+2)%6;
|
||||
int a2 = (a+4)%6;
|
||||
if(c.getSide(a1+1) <= d.getSide(a1+1) &&
|
||||
c.getSide(a1) >= d.getSide(a1) &&
|
||||
c.getSide(a2+1) <= d.getSide(a2+1) &&
|
||||
c.getSide(a2) >= d.getSide(a2)) {
|
||||
|
||||
if(c.getSide(a) <= d.getSide(a+1) && c.getSide(a) >= d.getSide(a)) {
|
||||
c.setSide(a, d.getSide(a + 1) + 1);
|
||||
sides|=1<<a;
|
||||
}
|
||||
if(c.getSide(a+1) >= d.getSide(a) && c.getSide(a+1) <= d.getSide(a+1)) {
|
||||
c.setSide(a + 1, d.getSide(a) - 1);
|
||||
sides|=2<<a;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void extractQuads(List<QBQuad> quads) {
|
||||
Cuboid6 box = c.bounds();
|
||||
for(int s = 0; s < 6; s++)
|
||||
if((sides & 1<<s) == 0)
|
||||
quads.add(extractQuad(s, box));
|
||||
}
|
||||
|
||||
private QBQuad extractQuad(int side, Cuboid6 box) {
|
||||
double[] da = new double[3];
|
||||
da[side>>1] = box.getSide(side);
|
||||
|
||||
QBQuad quad = new QBQuad(side);
|
||||
for(int i = 0; i < 4; i++) {
|
||||
int rU = vertOrder[i][0];
|
||||
int rV = vertOrder[i][1];
|
||||
int sideU = Rotation.rotateSide(side, rU);
|
||||
int sideV = Rotation.rotateSide(side, rV);
|
||||
da[sideU>>1] = box.getSide(sideU);
|
||||
da[sideV>>1] = box.getSide(sideV);
|
||||
quad.verts[i] = new Vertex5(Vector3.fromAxes(da), (3-rU)/2, rV/2);
|
||||
}
|
||||
|
||||
int sideU = Rotation.rotateSide(side, 1);
|
||||
int sideV = Rotation.rotateSide(side, 2);
|
||||
quad.image.data = new int[c.size(sideU)][c.size(sideV)];
|
||||
QBImage image = quad.image;
|
||||
|
||||
int[] ia = new int[3];
|
||||
ia[side>>1] = c.getSide(side); ia[sideU>>1] = c.getSide(sideU^1); ia[sideV>>1] = c.getSide(sideV^1);
|
||||
BlockCoord b = BlockCoord.fromAxes(ia);
|
||||
BlockCoord bU = BlockCoord.sideOffsets[sideU];
|
||||
BlockCoord bV = BlockCoord.sideOffsets[sideV];
|
||||
for(int u = 0; u < image.width(); u++)
|
||||
for(int v = 0; v < image.height(); v++)
|
||||
image.data[u][v] = mat.matrix[b.x+bU.x*u+bV.x*v][b.y+bU.y*u+bV.y*v][b.z+bU.z*u+bV.z*v];
|
||||
|
||||
return quad;
|
||||
}
|
||||
}
|
||||
|
||||
public static class QBMatrix
|
||||
{
|
||||
public String name;
|
||||
public BlockCoord pos;
|
||||
public BlockCoord size;
|
||||
public int[][][] matrix;
|
||||
|
||||
public void readMatrix(DataInputStream din, boolean compressed) throws IOException {
|
||||
if(compressed) {
|
||||
int z = 0;
|
||||
while (z < size.z)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
int data = din.readInt();
|
||||
|
||||
if (data == NEXTSLICEFLAG)
|
||||
break;
|
||||
|
||||
if (data == CODEFLAG) {
|
||||
int count = readTni(din);
|
||||
data = din.readInt();
|
||||
|
||||
for(int j = 0; j < count; j++, index++)
|
||||
matrix[index % size.x][index / size.x][z] = data;
|
||||
}
|
||||
else {
|
||||
matrix[index % size.x][index / size.x][z] = data;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
z++;
|
||||
}
|
||||
} else {
|
||||
for(int z = 0; z < size.z; z++)
|
||||
for(int y = 0; y < size.y; y++)
|
||||
for(int x = 0; x < size.x; x++)
|
||||
matrix[x][y][z] = din.readInt();
|
||||
}
|
||||
}
|
||||
|
||||
public void convertBGRAtoRGBA() {
|
||||
for(int z = 0; z < size.z; z++)
|
||||
for(int y = 0; y < size.y; y++)
|
||||
for(int x = 0; x < size.x; x++) {
|
||||
int i = matrix[x][y][z];
|
||||
matrix[x][y][z] = Integer.reverseBytes(i>>>8)|i&0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean voxelFull(boolean[][][] solid, CuboidCoord c) {
|
||||
for(BlockCoord b : c)
|
||||
if(matrix[b.x][b.y][b.z] == 0)
|
||||
return false;
|
||||
for(BlockCoord b : c)
|
||||
solid[b.x][b.y][b.z] = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private QBCuboid expand(boolean[][][] solid, BlockCoord b) {
|
||||
CuboidCoord c = new CuboidCoord(b);
|
||||
solid[b.x][b.y][b.z] = false;
|
||||
|
||||
for(int s = 0; s < 6; s++) {
|
||||
CuboidCoord slice = c.copy();
|
||||
slice.expand(s^1, -(slice.size(s)-1));
|
||||
slice.expand(s, 1);
|
||||
|
||||
while(slice.getSide(s) >= 0 && slice.getSide(s) < size.getSide(s)) {
|
||||
if(!voxelFull(solid, slice))
|
||||
break;
|
||||
slice.expand(s ^ 1, -1);
|
||||
slice.expand(s, 1);
|
||||
c.expand(s, 1);
|
||||
}
|
||||
}
|
||||
return new QBCuboid(this, c);
|
||||
}
|
||||
|
||||
public List<QBCuboid> rectangulate() {
|
||||
List<QBCuboid> list = new ArrayList<QBCuboid>();
|
||||
boolean[][][] solid = new boolean[size.x][size.y][size.z];
|
||||
for(int z = 0; z < size.z; z++)
|
||||
for(int y = 0; y < size.y; y++)
|
||||
for(int x = 0; x < size.x; x++)
|
||||
solid[x][y][z] = matrix[x][y][z] != 0;
|
||||
|
||||
for(int x = 0; x < size.x; x++)
|
||||
for(int z = 0; z < size.z; z++)
|
||||
for(int y = 0; y < size.y; y++)
|
||||
if(solid[x][y][z])
|
||||
list.add(expand(solid, new BlockCoord(x, y, z)));
|
||||
|
||||
for(int i = 0; i < list.size(); i++)
|
||||
for(int j = i+1; j < list.size(); j++)
|
||||
QBCuboid.clip(list.get(i), list.get(j));
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public List<QBQuad> extractQuads(boolean texturePlanes) {
|
||||
List<QBQuad> quads = new LinkedList<QBQuad>();
|
||||
for(QBCuboid c : rectangulate())
|
||||
c.extractQuads(quads);
|
||||
|
||||
if(texturePlanes)
|
||||
optimisePlanes(quads);
|
||||
|
||||
return quads;
|
||||
}
|
||||
|
||||
private void optimisePlanes(List<QBQuad> quads) {
|
||||
Multimap<Integer, QBQuad> map = HashMultimap.create();
|
||||
for(QBQuad quad : quads)
|
||||
map.put(quad.side | ((int)quad.verts[0].vec.getSide(quad.side))<<3, quad);
|
||||
|
||||
quads.clear();
|
||||
for(Integer key : map.keySet()) {
|
||||
Collection<QBQuad> plane = map.get(key);
|
||||
if(plane.size() == 1) {
|
||||
quads.add(plane.iterator().next());
|
||||
continue;
|
||||
}
|
||||
|
||||
int side = key&7;
|
||||
Rectangle4i rect = null;
|
||||
for(QBQuad q : plane)
|
||||
if(rect == null)
|
||||
rect = q.flatten();
|
||||
else
|
||||
rect.include(q.flatten());
|
||||
|
||||
QBImage img = new QBImage();
|
||||
img.data = new int[rect.w][rect.h];
|
||||
for(QBQuad q : plane) {
|
||||
QBImage from = q.image;
|
||||
Rectangle4i r = q.flatten();
|
||||
int du = r.x-rect.x;
|
||||
int dv = r.y-rect.y;
|
||||
for(int u = 0; u < from.width(); u++)
|
||||
for(int v = 0; v < from.height(); v++)
|
||||
img.data[du+u][dv+v] = from.data[u][v];
|
||||
}
|
||||
|
||||
quads.add(QBQuad.restore(rect, side, key>>3, img));
|
||||
}
|
||||
}
|
||||
|
||||
public CCModel buildModel(List<QBQuad> quads, BufferedImage img, boolean scaleMC) {
|
||||
CCModel m = CCModel.quadModel(quads.size()*4);
|
||||
int i = 0;
|
||||
for(QBQuad quad : quads) {
|
||||
quad.applyImageT();
|
||||
m.verts[i++] = quad.verts[0];
|
||||
m.verts[i++] = quad.verts[1];
|
||||
m.verts[i++] = quad.verts[2];
|
||||
m.verts[i++] = quad.verts[3];
|
||||
}
|
||||
m.apply(new UVScale(1D/img.getWidth(), 1D/img.getHeight()));
|
||||
m.apply(new Translation(pos.x, pos.y, pos.z));
|
||||
if(scaleMC)
|
||||
m.apply(new Scale(1/16D));
|
||||
m.computeNormals();
|
||||
return m;
|
||||
}
|
||||
|
||||
private static void addImages(List<QBQuad> quads, List<QBImage> images) {
|
||||
for(QBQuad q : quads) {
|
||||
QBImage img = q.image;
|
||||
boolean matched = false;
|
||||
for(QBImage img2 : images) {
|
||||
ImageTransform t = img.transformTo(img2);
|
||||
if(t != null) {
|
||||
q.t = t;
|
||||
q.image = img2;
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!matched)
|
||||
images.add(img);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static final int TEXTUREPLANES = 1;
|
||||
public static final int SQUARETEXTURE = 2;
|
||||
public static final int MERGETEXTURES = 4;
|
||||
public static final int SCALEMC = 8;
|
||||
public static class QBModel
|
||||
{
|
||||
public QBMatrix[] matrices;
|
||||
public boolean rightHanded;
|
||||
|
||||
public RasterisedModel toRasterisedModel(int flags) {
|
||||
List<QBImage> qbImages = new ArrayList<QBImage>();
|
||||
List<List<QBQuad>> modelQuads = new ArrayList<List<QBQuad>>();
|
||||
List<BufferedImage> images = new ArrayList<BufferedImage>();
|
||||
|
||||
boolean texturePlanes = (flags & TEXTUREPLANES) != 0;
|
||||
boolean squareTextures = (flags & SQUARETEXTURE) != 0;
|
||||
boolean mergeTextures = (flags & MERGETEXTURES) != 0;
|
||||
boolean scaleMC = (flags & SCALEMC) != 0;
|
||||
|
||||
for(QBMatrix mat : matrices) {
|
||||
List<QBQuad> quads = mat.extractQuads(texturePlanes);
|
||||
modelQuads.add(quads);
|
||||
QBMatrix.addImages(quads, qbImages);
|
||||
if(!mergeTextures) {
|
||||
images.add(ImagePackNode.pack(qbImages, squareTextures).toImage());
|
||||
qbImages.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if(mergeTextures)
|
||||
images.add(ImagePackNode.pack(qbImages, squareTextures).toImage());
|
||||
|
||||
RasterisedModel m = new RasterisedModel(images);
|
||||
for(int i = 0; i < matrices.length; i++) {
|
||||
QBMatrix mat = matrices[i];
|
||||
BufferedImage img = images.get(mergeTextures ? 0 : i);
|
||||
m.add(mat.name, mat.buildModel(modelQuads.get(i), img, scaleMC));
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RasterisedModel
|
||||
{
|
||||
private class Holder {
|
||||
CCModel m;
|
||||
int img;
|
||||
|
||||
public Holder(CCModel m, int img) {
|
||||
this.m = m;
|
||||
this.img = img;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Holder> map = new HashMap<String, Holder>();
|
||||
private List<BufferedImage> images;
|
||||
private String[] icons;
|
||||
|
||||
public RasterisedModel(List<BufferedImage> images) {
|
||||
this.images = images;
|
||||
icons = new String[images.size()];
|
||||
}
|
||||
|
||||
public void add(String name, CCModel m) {
|
||||
map.put(name, new Holder(m, Math.min(map.size(), images.size()-1)));
|
||||
}
|
||||
|
||||
public CCModel getModel(String key) {
|
||||
return map.get(key).m;
|
||||
}
|
||||
|
||||
public IIcon getIcon(String key, IIconRegister r, String iconName) {
|
||||
int img = map.get(key).img;
|
||||
if(icons[img] != null && !iconName.equals(icons[img]))
|
||||
throw new IllegalArgumentException("Attempted to get a previously registered icon by a different name: "+icons[img]+", "+iconName);
|
||||
if(icons[img] != null)
|
||||
return r.registerIcon(iconName);
|
||||
|
||||
icons[img] = iconName;
|
||||
return TextureUtils.getTextureSpecial(r, iconName).addTexture(new TextureDataHolder(images.get(img)));
|
||||
}
|
||||
|
||||
private void exportImg(BufferedImage img, File imgFile) throws IOException {
|
||||
if(!imgFile.exists())
|
||||
imgFile.createNewFile();
|
||||
ImageIO.write(img, "PNG", imgFile);
|
||||
}
|
||||
|
||||
public void export(File objFile, File imgDir) {
|
||||
try {
|
||||
if(!objFile.exists())
|
||||
objFile.createNewFile();
|
||||
if(!imgDir.exists())
|
||||
imgDir.mkdirs();
|
||||
|
||||
Map<String, CCModel> modelMap = new HashMap<String, CCModel>();
|
||||
for(Map.Entry<String, Holder> e : map.entrySet())
|
||||
modelMap.put(e.getKey(), e.getValue().m);
|
||||
|
||||
PrintWriter p = new PrintWriter(objFile);
|
||||
CCModel.exportObj(modelMap, p);
|
||||
p.close();
|
||||
|
||||
if(images.size() < map.size())
|
||||
exportImg(images.get(0), new File(imgDir, objFile.getName().replaceAll("(.+)\\..+", "$1.png")));
|
||||
else
|
||||
for(Map.Entry<String, Holder> e : map.entrySet())
|
||||
exportImg(images.get(e.getValue().img), new File(imgDir, e.getKey()+".png"));
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String readAsciiString(DataInputStream din) throws IOException {
|
||||
byte[] bytes = new byte[din.readByte()&0xFF];
|
||||
din.readFully(bytes);
|
||||
return new String(bytes, "US-ASCII");
|
||||
}
|
||||
|
||||
private static int readTni(DataInputStream din) throws IOException {
|
||||
return Integer.reverseBytes(din.readInt());
|
||||
}
|
||||
|
||||
private static final int CODEFLAG = Integer.reverseBytes(2);
|
||||
private static final int NEXTSLICEFLAG = Integer.reverseBytes(6);
|
||||
public static QBModel loadQB(InputStream input) throws IOException {
|
||||
DataInputStream din = new DataInputStream(input);
|
||||
|
||||
QBModel m = new QBModel();
|
||||
int version = din.readInt();
|
||||
int colorFormat = din.readInt();
|
||||
m.rightHanded = din.readInt() != 0;
|
||||
boolean compressed = din.readInt() != 0;
|
||||
boolean visEncoded = din.readInt() != 0;
|
||||
|
||||
if(visEncoded)
|
||||
throw new IllegalArgumentException("Encoded Visiblity States not supported");
|
||||
|
||||
m.matrices = new QBMatrix[readTni(din)];
|
||||
for(int i = 0; i < m.matrices.length; i++) {
|
||||
QBMatrix mat = new QBMatrix();
|
||||
m.matrices[i] = mat;
|
||||
mat.name = readAsciiString(din);
|
||||
mat.size = new BlockCoord(readTni(din), readTni(din), readTni(din));
|
||||
mat.pos = new BlockCoord(readTni(din), readTni(din), readTni(din));
|
||||
mat.matrix = new int[mat.size.x][mat.size.y][mat.size.z];
|
||||
mat.readMatrix(din, compressed);
|
||||
if(colorFormat == 1)
|
||||
mat.convertBGRAtoRGBA();
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
public static QBModel loadQB(ResourceLocation res) {
|
||||
try {
|
||||
return loadQB(Minecraft.getMinecraft().getResourceManager().getResource(res).getInputStream());
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException("failed to load model: "+res, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static QBModel loadQB(File file) {
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
try {
|
||||
return loadQB(fin);
|
||||
} finally {
|
||||
fin.close();
|
||||
}
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException("failed to load model: "+file.getPath(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.vec.Cuboid6;
|
||||
import codechicken.lib.vec.Rectangle4i;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
import net.minecraft.block.Block;
|
||||
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.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraftforge.client.IItemRenderer;
|
||||
import net.minecraftforge.client.MinecraftForgeClient;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import static net.minecraftforge.client.IItemRenderer.ItemRenderType.ENTITY;
|
||||
import static net.minecraftforge.client.IItemRenderer.ItemRendererHelper.BLOCK_3D;
|
||||
|
||||
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 renderFluidQuad(Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4, IIcon icon, double res)
|
||||
{
|
||||
renderFluidQuad(point2, vectors[0].set(point4).subtract(point1), vectors[1].set(point1).subtract(point2), icon, res);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a tessellated quadrilateral bottom to top, left to right
|
||||
* @param base The bottom left corner of the quad
|
||||
* @param wide The bottom of the quad
|
||||
* @param high The left side of the quad
|
||||
* @param res Units per icon
|
||||
*/
|
||||
public static void renderFluidQuad(Vector3 base, Vector3 wide, Vector3 high, IIcon icon, double res)
|
||||
{
|
||||
Tessellator t = Tessellator.instance;
|
||||
|
||||
double u1 = icon.getMinU();
|
||||
double du = icon.getMaxU()-icon.getMinU();
|
||||
double v2 = icon.getMaxV();
|
||||
double dv = icon.getMaxV()-icon.getMinV();
|
||||
|
||||
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(base.x+dx1.x+dy2.x, base.y+dx1.y+dy2.y, base.z+dx1.z+dy2.z, u1, v2-ry/res*dv);
|
||||
t.addVertexWithUV(base.x+dx1.x+dy1.x, base.y+dx1.y+dy1.y, base.z+dx1.z+dy1.z, u1, v2);
|
||||
t.addVertexWithUV(base.x+dx2.x+dy1.x, base.y+dx2.y+dy1.y, base.z+dx2.z+dy1.z, u1+rx/res*du, v2);
|
||||
t.addVertexWithUV(base.x+dx2.x+dy2.x, base.y+dx2.y+dy2.y, base.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 drawCuboidOutline(Cuboid6 c)
|
||||
{
|
||||
Tessellator var2 = Tessellator.instance;
|
||||
var2.startDrawing(3);
|
||||
var2.addVertex(c.min.x, c.min.y, c.min.z);
|
||||
var2.addVertex(c.max.x, c.min.y, c.min.z);
|
||||
var2.addVertex(c.max.x, c.min.y, c.max.z);
|
||||
var2.addVertex(c.min.x, c.min.y, c.max.z);
|
||||
var2.addVertex(c.min.x, c.min.y, c.min.z);
|
||||
var2.draw();
|
||||
var2.startDrawing(3);
|
||||
var2.addVertex(c.min.x, c.max.y, c.min.z);
|
||||
var2.addVertex(c.max.x, c.max.y, c.min.z);
|
||||
var2.addVertex(c.max.x, c.max.y, c.max.z);
|
||||
var2.addVertex(c.min.x, c.max.y, c.max.z);
|
||||
var2.addVertex(c.min.x, c.max.y, c.min.z);
|
||||
var2.draw();
|
||||
var2.startDrawing(1);
|
||||
var2.addVertex(c.min.x, c.min.y, c.min.z);
|
||||
var2.addVertex(c.min.x, c.max.y, c.min.z);
|
||||
var2.addVertex(c.max.x, c.min.y, c.min.z);
|
||||
var2.addVertex(c.max.x, c.max.y, c.min.z);
|
||||
var2.addVertex(c.max.x, c.min.y, c.max.z);
|
||||
var2.addVertex(c.max.x, c.max.y, c.max.z);
|
||||
var2.addVertex(c.min.x, c.min.y, c.max.z);
|
||||
var2.addVertex(c.min.x, c.max.y, c.max.z);
|
||||
var2.draw();
|
||||
}
|
||||
|
||||
public static void renderFluidCuboid(Cuboid6 bound, IIcon tex, double res)
|
||||
{
|
||||
renderFluidQuad(//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);
|
||||
renderFluidQuad(//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);
|
||||
renderFluidQuad(//-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);
|
||||
renderFluidQuad(//+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);
|
||||
renderFluidQuad(//-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);
|
||||
renderFluidQuad(//+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 shouldRenderFluid(FluidStack stack)
|
||||
{
|
||||
return stack.amount > 0 && stack.getFluid() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param stack The fluid stack to render
|
||||
* @return The icon of the fluid
|
||||
*/
|
||||
public static IIcon 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 TextureUtils.safeIcon(fluid.getIcon(stack));
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-enables lighting and disables blending.
|
||||
*/
|
||||
public static void postFluidRender()
|
||||
{
|
||||
GL11.glEnable(GL11.GL_LIGHTING);
|
||||
GL11.glDisable(GL11.GL_BLEND);
|
||||
}
|
||||
|
||||
public static double fluidDensityToAlpha(double density)
|
||||
{
|
||||
return Math.pow(density, 0.4);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 renderFluidCuboid(FluidStack stack, Cuboid6 bound, double density, double res)
|
||||
{
|
||||
if(!shouldRenderFluid(stack))
|
||||
return;
|
||||
|
||||
int alpha = 255;
|
||||
if(stack.getFluid().isGaseous())
|
||||
alpha = (int) (fluidDensityToAlpha(density)*255);
|
||||
else
|
||||
bound.max.y = bound.min.y+(bound.max.y-bound.min.y)*density;
|
||||
|
||||
IIcon tex = prepareFluidRender(stack, alpha);
|
||||
CCRenderState.startDrawing();
|
||||
renderFluidCuboid(bound, tex, res);
|
||||
CCRenderState.draw();
|
||||
postFluidRender();
|
||||
}
|
||||
|
||||
public static void renderFluidGauge(FluidStack stack, Rectangle4i rect, double density, double res)
|
||||
{
|
||||
if(!shouldRenderFluid(stack))
|
||||
return;
|
||||
|
||||
int alpha = 255;
|
||||
if(stack.getFluid().isGaseous())
|
||||
alpha = (int) (fluidDensityToAlpha(density)*255);
|
||||
else
|
||||
{
|
||||
int height = (int) (rect.h*density);
|
||||
rect.y +=rect.h-height;
|
||||
rect.h = height;
|
||||
}
|
||||
|
||||
IIcon tex = prepareFluidRender(stack, alpha);
|
||||
CCRenderState.startDrawing();
|
||||
renderFluidQuad(
|
||||
new Vector3(rect.x, rect.y+rect.h, 0),
|
||||
new Vector3(rect.w,0, 0),
|
||||
new Vector3(0, -rect.h, 0), tex, res);
|
||||
CCRenderState.draw();
|
||||
postFluidRender();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Renders items and blocks in the world at 0,0,0 with transformations that size them appropriately
|
||||
*/
|
||||
public static void renderItemUniform(ItemStack item) {
|
||||
renderItemUniform(item, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders items and blocks in the world at 0,0,0 with transformations that size them appropriately
|
||||
* @param spin The spin angle of the item around the y axis in degrees
|
||||
*/
|
||||
public static void renderItemUniform(ItemStack item, double spin)
|
||||
{
|
||||
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.getBlockFromItem(item.getItem()).getRenderType()))
|
||||
{
|
||||
int renderType = Block.getBlockFromItem(item.getItem()).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, 1, 1, 1);
|
||||
|
||||
entityItem.setEntityItemStack(item);
|
||||
uniformRenderItem.doRender(entityItem, 0, larger ? 0.09 : 0.06, 0, 0, (float)(spin*9/Math.PI));
|
||||
|
||||
if(larger)
|
||||
GL11.glScaled(d1, d1, d1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import org.lwjgl.opengl.ARBShaderObjects;
|
||||
import org.lwjgl.opengl.ARBVertexShader;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.vector.Matrix4f;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import static org.lwjgl.opengl.ARBShaderObjects.*;
|
||||
|
||||
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,142 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.render.TextureUtils.IIconSelfRegister;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class SpriteSheetManager
|
||||
{
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static class SpriteSheet implements IIconSelfRegister
|
||||
{
|
||||
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(IIconRegister register)
|
||||
{
|
||||
TextureMap textureMap = (TextureMap)register;
|
||||
|
||||
if(TextureUtils.refreshTexture(textureMap, resource.getResourcePath()))
|
||||
{
|
||||
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 IIcon getSprite(int index)
|
||||
{
|
||||
IIcon 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 bindTextureFX(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,59 @@
|
||||
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.bindTextureFX(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,148 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.render.SpriteSheetManager.SpriteSheet;
|
||||
import codechicken.lib.render.TextureUtils.IIconSelfRegister;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.client.renderer.texture.TextureUtil;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.client.settings.GameSettings;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class TextureSpecial extends TextureAtlasSprite implements IIconSelfRegister {
|
||||
//sprite sheet fields
|
||||
private int spriteIndex;
|
||||
private SpriteSheet spriteSheet;
|
||||
|
||||
//textureFX fields
|
||||
private TextureFX textureFX;
|
||||
|
||||
private int blankSize = -1;
|
||||
private 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 initSprite(int sheetWidth, int sheetHeight, int originX, int originY, boolean rotated) {
|
||||
super.initSprite(sheetWidth, sheetHeight, originX, originY, rotated);
|
||||
if (textureFX != null)
|
||||
textureFX.onTextureDimensionsUpdate(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateAnimation() {
|
||||
if (textureFX != null) {
|
||||
textureFX.update();
|
||||
if (textureFX.changed())
|
||||
TextureUtil.uploadTextureMipmap(new int[][]{textureFX.imageData}, width, height, originX, originY, false, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomLoader(IResourceManager manager, ResourceLocation location) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void addFrame(int[] data) {
|
||||
GameSettings settings = Minecraft.getMinecraft().gameSettings;
|
||||
BufferedImage[] images = new BufferedImage[settings.mipmapLevels+1];
|
||||
images[0] = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||
images[0].setRGB(0, 0, width, height, data, 0, width);
|
||||
|
||||
super.loadSprite(images, null, settings.anisotropicFiltering > 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load(IResourceManager manager, ResourceLocation location) {
|
||||
if (baseTextures != null) {
|
||||
for (TextureDataHolder tex : baseTextures) {
|
||||
width = tex.width;
|
||||
height = tex.height;
|
||||
addFrame(tex.data);
|
||||
}
|
||||
}
|
||||
|
||||
if (spriteSheet != null) {
|
||||
TextureDataHolder tex = spriteSheet.createSprite(spriteIndex);
|
||||
width = tex.width;
|
||||
height = tex.height;
|
||||
addFrame(tex.data);
|
||||
}
|
||||
|
||||
if (blankSize > 0) {
|
||||
width = height = blankSize;
|
||||
addFrame(new int[blankSize * blankSize]);
|
||||
}
|
||||
|
||||
if (framesTextureData.isEmpty())
|
||||
throw new RuntimeException("No base frame for texture: " + getIconName());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAnimationMetadata() {
|
||||
return textureFX != null || super.hasAnimationMetadata();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFrameCount() {
|
||||
if (textureFX != null)
|
||||
return 1;
|
||||
|
||||
return super.getFrameCount();
|
||||
}
|
||||
|
||||
public TextureSpecial blank(int size) {
|
||||
blankSize = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextureSpecial selfRegister() {
|
||||
selfRegister = true;
|
||||
TextureUtils.addIconRegistrar(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerIcons(IIconRegister register) {
|
||||
if (selfRegister)
|
||||
((TextureMap) register).setTextureEntry(getIconName(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int atlasIndex() {
|
||||
return atlasIndex;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.colour.Colour;
|
||||
import codechicken.lib.colour.ColourARGB;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.client.renderer.texture.TextureManager;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.event.TextureStitchEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL12;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class TextureUtils
|
||||
{
|
||||
public static interface IIconSelfRegister
|
||||
{
|
||||
public void registerIcons(IIconRegister register);
|
||||
|
||||
public int atlasIndex();
|
||||
}
|
||||
|
||||
static {
|
||||
MinecraftForge.EVENT_BUS.register(new TextureUtils());
|
||||
}
|
||||
|
||||
private static ArrayList<IIconSelfRegister> iconRegistrars = new ArrayList<TextureUtils.IIconSelfRegister>();
|
||||
|
||||
public static void addIconRegistrar(IIconSelfRegister registrar) {
|
||||
iconRegistrars.add(registrar);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void textureLoad(TextureStitchEvent.Pre event) {
|
||||
for (IIconSelfRegister reg : iconRegistrars)
|
||||
if (reg.atlasIndex() == event.map.getTextureType())
|
||||
reg.registerIcons(event.map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return an array of ARGB pixel data
|
||||
*/
|
||||
public static int[] loadTextureData(ResourceLocation resource) {
|
||||
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().getResourceManager().getResource(textureFile).getInputStream();
|
||||
}
|
||||
|
||||
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().bindTexture(atlasIndex == 0 ? TextureMap.locationBlocksTexture : TextureMap.locationItemsTexture);
|
||||
}
|
||||
|
||||
public static IIcon getBlankIcon(int size, IIconRegister iconRegister) {
|
||||
TextureMap textureMap = (TextureMap) iconRegister;
|
||||
String s = "blank_" + size;
|
||||
if (textureMap.getTextureExtry(s) == null) {
|
||||
TextureSpecial icon = new TextureSpecial(s).blank(size);
|
||||
textureMap.setTextureEntry(s, icon);
|
||||
}
|
||||
return iconRegister.registerIcon(s);
|
||||
}
|
||||
|
||||
public static TextureSpecial getTextureSpecial(IIconRegister iconRegister, String name) {
|
||||
TextureMap textureMap = (TextureMap) iconRegister;
|
||||
IIcon entry = textureMap.getTextureExtry(name);
|
||||
if (entry != null)
|
||||
throw new IllegalStateException("Texture: " + name + " is already registered");
|
||||
|
||||
TextureSpecial icon = new TextureSpecial(name);
|
||||
textureMap.setTextureEntry(name, icon);
|
||||
return icon;
|
||||
}
|
||||
|
||||
public static void prepareTexture(int target, int texture, int min_mag_filter, int wrap) {
|
||||
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;
|
||||
}
|
||||
|
||||
public static IIcon safeIcon(IIcon icon) {
|
||||
if (icon == null)
|
||||
icon = ((TextureMap) Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).getAtlasSprite("missingno");
|
||||
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package codechicken.lib.render;
|
||||
|
||||
import codechicken.lib.render.uv.UV;
|
||||
import codechicken.lib.render.uv.UVTransformation;
|
||||
import codechicken.lib.util.Copyable;
|
||||
import codechicken.lib.vec.Transformation;
|
||||
import codechicken.lib.vec.Vector3;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class Vertex5 implements Copyable<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 set(double x, double y, double z, double u, double v, int tex)
|
||||
{
|
||||
vec.set(x, y, z);
|
||||
uv.set(u, v, tex);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vertex5 set(Vertex5 vert) {
|
||||
vec.set(vert.vec);
|
||||
uv.set(vert.uv);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vertex5(Vertex5 vertex5)
|
||||
{
|
||||
this(vertex5.vec.copy(), vertex5.uv.copy());
|
||||
}
|
||||
|
||||
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)+")";
|
||||
}
|
||||
|
||||
public Vertex5 apply(Transformation t)
|
||||
{
|
||||
vec.apply(t);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vertex5 apply(UVTransformation t)
|
||||
{
|
||||
uv.apply(t);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.vec.IrreversibleTransformationException;
|
||||
import net.minecraft.util.IIcon;
|
||||
|
||||
public class IconTransformation extends UVTransformation {
|
||||
public IIcon icon;
|
||||
|
||||
public IconTransformation(IIcon icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
uv.u = icon.getInterpolatedU(uv.u * 16);
|
||||
uv.v = icon.getInterpolatedV(uv.v * 16);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
throw new IrreversibleTransformationException(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.vec.IrreversibleTransformationException;
|
||||
import net.minecraft.util.IIcon;
|
||||
|
||||
public class MultiIconTransformation extends UVTransformation {
|
||||
public IIcon[] icons;
|
||||
|
||||
public MultiIconTransformation(IIcon... icons) {
|
||||
this.icons = icons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
IIcon icon = icons[uv.tex % icons.length];
|
||||
uv.u = icon.getInterpolatedU(uv.u * 16);
|
||||
uv.v = icon.getInterpolatedV(uv.v * 16);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
throw new IrreversibleTransformationException(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.util.Copyable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class UV implements Copyable<UV> {
|
||||
public double u;
|
||||
public double v;
|
||||
public int tex;
|
||||
|
||||
public UV() {
|
||||
}
|
||||
|
||||
public UV(double u, double v) {
|
||||
this(u, v, 0);
|
||||
}
|
||||
|
||||
public UV(double u, double v, int tex) {
|
||||
this.u = u;
|
||||
this.v = v;
|
||||
this.tex = tex;
|
||||
}
|
||||
|
||||
public UV(UV uv) {
|
||||
this(uv.u, uv.v, uv.tex);
|
||||
}
|
||||
|
||||
public UV set(double u, double v, int tex) {
|
||||
this.u = u;
|
||||
this.v = v;
|
||||
this.tex = tex;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UV set(double u, double v) {
|
||||
return set(u, v, tex);
|
||||
}
|
||||
|
||||
public UV set(UV uv) {
|
||||
return set(uv.u, uv.v, uv.tex);
|
||||
}
|
||||
|
||||
public UV copy() {
|
||||
return new UV(this);
|
||||
}
|
||||
|
||||
public UV add(UV uv) {
|
||||
u += uv.u;
|
||||
v += uv.v;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UV multiply(double d) {
|
||||
u *= d;
|
||||
v *= d;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "UV(" + new BigDecimal(u, cont) + ", " + new BigDecimal(v, cont) + ")";
|
||||
}
|
||||
|
||||
public UV apply(UVTransformation t) {
|
||||
t.apply(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof UV))
|
||||
return false;
|
||||
UV uv = (UV) o;
|
||||
return u == uv.u && v == uv.v;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.math.MathHelper;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class UVRotation extends UVTransformation
|
||||
{
|
||||
public double angle;
|
||||
|
||||
/**
|
||||
* @param angle The angle to rotate counterclockwise in radians
|
||||
*/
|
||||
public UVRotation(double angle) {
|
||||
this.angle = angle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
double c = MathHelper.cos(angle);
|
||||
double s = MathHelper.sin(angle);
|
||||
double u2 = c*uv.u + s*uv.v;
|
||||
uv.v = - s*uv.u + c*uv.v;
|
||||
uv.u = u2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
return new UVRotation(-angle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation merge(UVTransformation next) {
|
||||
if(next instanceof UVRotation)
|
||||
return new UVRotation(angle+((UVRotation)next).angle);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRedundant() {
|
||||
return MathHelper.between(-1E-5, angle, 1E-5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "UVRotation(" + new BigDecimal(angle, cont) + ")";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class UVScale extends UVTransformation {
|
||||
double su;
|
||||
double sv;
|
||||
|
||||
public UVScale(double scaleu, double scalev) {
|
||||
su = scaleu;
|
||||
sv = scalev;
|
||||
}
|
||||
|
||||
public UVScale(double d) {
|
||||
this(d, d);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
uv.u *= su;
|
||||
uv.v *= sv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
return new UVScale(1 / su, 1 / sv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "UVScale(" + new BigDecimal(su, cont) + ", " + new BigDecimal(sv, cont) + ")";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.render.CCRenderState;
|
||||
import codechicken.lib.vec.ITransformation;
|
||||
|
||||
/**
|
||||
* Abstract supertype for any UV transformation
|
||||
*/
|
||||
public abstract class UVTransformation extends ITransformation<UV, UVTransformation> implements CCRenderState.IVertexOperation
|
||||
{
|
||||
public static final int operationIndex = CCRenderState.registerOperation();
|
||||
|
||||
public UVTransformation at(UV point) {
|
||||
return new UVTransformationList(new UVTranslation(-point.u, -point.v), this, new UVTranslation(point.u, point.v));
|
||||
}
|
||||
|
||||
public UVTransformationList with(UVTransformation t) {
|
||||
return new UVTransformationList(this, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean load() {
|
||||
return !isRedundant();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate() {
|
||||
apply(CCRenderState.vert.uv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int operationID() {
|
||||
return operationIndex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class UVTransformationList extends UVTransformation
|
||||
{
|
||||
private ArrayList<UVTransformation> transformations = new ArrayList<UVTransformation>();
|
||||
|
||||
public UVTransformationList(UVTransformation... transforms)
|
||||
{
|
||||
for(UVTransformation t : transforms)
|
||||
if(t instanceof UVTransformationList)
|
||||
transformations.addAll(((UVTransformationList)t).transformations);
|
||||
else
|
||||
transformations.add(t);
|
||||
|
||||
compact();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv)
|
||||
{
|
||||
for(int i = 0; i < transformations.size(); i++)
|
||||
transformations.get(i).apply(uv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformationList with(UVTransformation t)
|
||||
{
|
||||
if(t.isRedundant())
|
||||
return this;
|
||||
|
||||
if(t instanceof UVTransformationList)
|
||||
transformations.addAll(((UVTransformationList)t).transformations);
|
||||
else
|
||||
transformations.add(t);
|
||||
|
||||
compact();
|
||||
return this;
|
||||
}
|
||||
|
||||
public UVTransformationList prepend(UVTransformation t)
|
||||
{
|
||||
if(t.isRedundant())
|
||||
return this;
|
||||
|
||||
if(t instanceof UVTransformationList)
|
||||
transformations.addAll(0, ((UVTransformationList)t).transformations);
|
||||
else
|
||||
transformations.add(0, t);
|
||||
|
||||
compact();
|
||||
return this;
|
||||
}
|
||||
|
||||
private void compact() {
|
||||
ArrayList<UVTransformation> newList = new ArrayList<UVTransformation>(transformations.size());
|
||||
Iterator<UVTransformation> iterator = transformations.iterator();
|
||||
UVTransformation prev = null;
|
||||
while(iterator.hasNext()) {
|
||||
UVTransformation t = iterator.next();
|
||||
if(t.isRedundant())
|
||||
continue;
|
||||
|
||||
if(prev != null) {
|
||||
UVTransformation m = prev.merge(t);
|
||||
if(m == null)
|
||||
newList.add(prev);
|
||||
else if(m.isRedundant())
|
||||
t = null;
|
||||
else
|
||||
t = m;
|
||||
}
|
||||
prev = t;
|
||||
}
|
||||
if(prev != null)
|
||||
newList.add(prev);
|
||||
|
||||
if(newList.size() < transformations.size())
|
||||
transformations = newList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRedundant() {
|
||||
return transformations.size() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse()
|
||||
{
|
||||
UVTransformationList rev = new UVTransformationList();
|
||||
for(int i = transformations.size()-1; i >= 0; i--)
|
||||
rev.with(transformations.get(i).inverse());
|
||||
return rev;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String s = "";
|
||||
for(UVTransformation t : transformations)
|
||||
s+="\n"+t.toString();
|
||||
return s.trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package codechicken.lib.render.uv;
|
||||
|
||||
import codechicken.lib.math.MathHelper;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class UVTranslation extends UVTransformation {
|
||||
public double du;
|
||||
public double dv;
|
||||
|
||||
public UVTranslation(double u, double v) {
|
||||
du = u;
|
||||
dv = v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(UV uv) {
|
||||
uv.u += du;
|
||||
uv.v += dv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation at(UV point) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation inverse() {
|
||||
return new UVTranslation(-du, -dv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UVTransformation merge(UVTransformation next) {
|
||||
if (next instanceof UVTranslation) {
|
||||
UVTranslation t = (UVTranslation)next;
|
||||
return new UVTranslation(du+t.du, dv+t.dv);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRedundant() {
|
||||
return MathHelper.between(-1E-5, du, 1E-5) && MathHelper.between(-1E-5, dv, 1E-5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
|
||||
return "UVTranslation(" + new BigDecimal(du, cont) + ", " + new BigDecimal(dv, cont) + ")";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user