2013-03-15 03:14:02 +01:00
|
|
|
package net.minecraftforge.client.model.obj;
|
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
import net.minecraft.client.renderer.Tessellator;
|
|
|
|
import net.minecraft.util.Vec3;
|
2013-03-15 03:14:02 +01:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public class Face {
|
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
public Vertex[] vertices;
|
|
|
|
public Vertex[] vertexNormals;
|
2013-03-28 05:02:15 +01:00
|
|
|
public Vertex faceNormal;
|
2013-03-19 03:37:03 +01:00
|
|
|
public TextureCoordinate[] textureCoordinates;
|
|
|
|
public int glDrawingMode;
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-28 05:02:15 +01:00
|
|
|
public Vertex calculateFaceNormal() {
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
Vec3 v1 = Vec3.createVectorHelper(vertices[1].x - vertices[0].x, vertices[1].y - vertices[0].y, vertices[1].z - vertices[0].z);
|
|
|
|
Vec3 v2 = Vec3.createVectorHelper(vertices[2].x - vertices[0].x, vertices[2].y - vertices[0].y, vertices[2].z - vertices[0].z);
|
2013-03-19 20:21:10 +01:00
|
|
|
Vec3 normalVector = null;
|
|
|
|
|
|
|
|
normalVector = v1.crossProduct(v2).normalize();
|
|
|
|
|
|
|
|
return new Vertex((float)normalVector.xCoord, (float)normalVector.yCoord, (float)normalVector.zCoord);
|
2013-03-15 03:14:02 +01:00
|
|
|
}
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
public void render(Tessellator tessellator, float scale) {
|
2013-03-15 03:14:02 +01:00
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
this.render(tessellator, 0F, scale);
|
2013-03-15 03:14:02 +01:00
|
|
|
}
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
public void render(Tessellator tessellator, float textureOffset, float scale) {
|
|
|
|
|
|
|
|
tessellator.startDrawing(glDrawingMode);
|
2013-03-28 05:02:15 +01:00
|
|
|
|
|
|
|
if (faceNormal == null) {
|
|
|
|
faceNormal = this.calculateFaceNormal();
|
|
|
|
}
|
|
|
|
|
|
|
|
tessellator.setNormal(faceNormal.x, faceNormal.y, faceNormal.z);
|
|
|
|
|
|
|
|
float averageU = 0F;
|
|
|
|
float averageV = 0F;
|
|
|
|
|
|
|
|
if (textureCoordinates.length != 0) {
|
|
|
|
for (int i = 0; i < textureCoordinates.length; ++i) {
|
|
|
|
averageU += textureCoordinates[i].u;
|
|
|
|
averageV += textureCoordinates[i].v;
|
|
|
|
}
|
|
|
|
|
|
|
|
averageU = averageU / textureCoordinates.length;
|
|
|
|
averageV = averageV / textureCoordinates.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
float offsetU, offsetV;
|
|
|
|
|
|
|
|
for (int i = 0; i < vertices.length; ++i) {
|
|
|
|
|
|
|
|
if (textureCoordinates.length != 0) {
|
|
|
|
offsetU = textureOffset;
|
|
|
|
offsetV = textureOffset;
|
|
|
|
|
|
|
|
if (textureCoordinates[i].u > averageU) {
|
|
|
|
offsetU = -offsetU;
|
|
|
|
}
|
|
|
|
if (textureCoordinates[i].v > averageV) {
|
|
|
|
offsetV = -offsetV;
|
|
|
|
}
|
|
|
|
|
|
|
|
tessellator.addVertexWithUV(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale, textureCoordinates[i].u + offsetU, textureCoordinates[i].v + offsetV);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tessellator.addVertex(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tessellator.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void render(Tessellator tessellator, int glDrawingMode, float scale) {
|
|
|
|
|
|
|
|
this.render(tessellator, glDrawingMode, 0F, scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void render(Tessellator tessellator, int glDrawingMode, float textureOffset, float scale) {
|
|
|
|
|
|
|
|
tessellator.startDrawing(glDrawingMode);
|
|
|
|
|
|
|
|
if (faceNormal == null) {
|
|
|
|
faceNormal = this.calculateFaceNormal();
|
|
|
|
}
|
|
|
|
|
2013-03-27 12:37:34 +01:00
|
|
|
tessellator.setNormal(faceNormal.x, faceNormal.y, faceNormal.z);
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
float averageU = 0F;
|
|
|
|
float averageV = 0F;
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-21 14:55:28 +01:00
|
|
|
if (textureCoordinates.length != 0) {
|
|
|
|
for (int i = 0; i < textureCoordinates.length; ++i) {
|
|
|
|
averageU += textureCoordinates[i].u;
|
|
|
|
averageV += textureCoordinates[i].v;
|
|
|
|
}
|
|
|
|
|
|
|
|
averageU = averageU / textureCoordinates.length;
|
|
|
|
averageV = averageV / textureCoordinates.length;
|
2013-03-19 03:37:03 +01:00
|
|
|
}
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
float offsetU, offsetV;
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
for (int i = 0; i < vertices.length; ++i) {
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-21 14:55:28 +01:00
|
|
|
if (textureCoordinates.length != 0) {
|
|
|
|
offsetU = textureOffset;
|
|
|
|
offsetV = textureOffset;
|
|
|
|
|
|
|
|
if (textureCoordinates[i].u > averageU) {
|
|
|
|
offsetU = -offsetU;
|
|
|
|
}
|
|
|
|
if (textureCoordinates[i].v > averageV) {
|
|
|
|
offsetV = -offsetV;
|
|
|
|
}
|
|
|
|
|
|
|
|
tessellator.addVertexWithUV(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale, textureCoordinates[i].u + offsetU, textureCoordinates[i].v + offsetV);
|
2013-03-19 03:37:03 +01:00
|
|
|
}
|
2013-03-21 14:55:28 +01:00
|
|
|
else {
|
|
|
|
tessellator.addVertex(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale);
|
2013-03-19 03:37:03 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-19 17:15:09 +01:00
|
|
|
|
2013-03-19 03:37:03 +01:00
|
|
|
tessellator.draw();
|
2013-03-15 03:14:02 +01:00
|
|
|
}
|
|
|
|
}
|