Simplify some stuff related to the new OBJ loading

This commit is contained in:
pahimar 2013-03-18 22:37:03 -04:00
parent 3a3afc4651
commit bf864e3f88
5 changed files with 79 additions and 128 deletions

View file

@ -2,7 +2,6 @@ package com.pahimar.ee3.client.model;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.Tessellator;
import net.minecraftforge.client.model.obj.Face;
import net.minecraftforge.client.model.obj.Group;
import net.minecraftforge.client.model.obj.WavefrontObject;
import net.minecraftforge.common.ForgeDirection;
@ -50,33 +49,7 @@ public class ModelAludel extends ModelBase {
if (modelAludelOBJ.getGroups().size() != 0) {
for (Group group : modelAludelOBJ.getGroups()) {
if (group.getFaces().size() != 0) {
for (Face face : group.getFaces()) {
tessellator.startDrawing(face.getDrawingMode());
float sumU = 0F;
float sumV = 0F;
for (int i = 0; i < face.getTextureCoordinates().length; ++i) {
sumU += face.getTextureCoordinates()[i].u;
sumV += face.getTextureCoordinates()[i].v;
}
for (int i = 0; i < face.getVertices().length; ++i) {
float offsetU = Reference.MODEL_TEXTURE_OFFSET;
float offsetV = Reference.MODEL_TEXTURE_OFFSET;
if (face.getTextureCoordinates()[i].u > sumU / face.getTextureCoordinates().length) {
offsetU = -offsetU;
}
if (face.getTextureCoordinates()[i].v > sumV / face.getTextureCoordinates().length) {
offsetV = -offsetV;
}
tessellator.addVertexWithUV(face.getVertices()[i].x * scale, face.getVertices()[i].y * scale, face.getVertices()[i].z * scale, face.getTextureCoordinates()[i].u + offsetU, face.getTextureCoordinates()[i].v + offsetV);
}
tessellator.draw();
}
}
group.render(tessellator, Reference.MODEL_TEXTURE_OFFSET, scale);
}
}
}

View file

@ -2,7 +2,6 @@ package com.pahimar.ee3.client.model;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.Tessellator;
import net.minecraftforge.client.model.obj.Face;
import net.minecraftforge.client.model.obj.Group;
import net.minecraftforge.client.model.obj.WavefrontObject;
@ -50,33 +49,7 @@ public class ModelCalcinator extends ModelBase {
if (modelCalcinatorOBJ.getGroups().size() != 0) {
for (Group group : modelCalcinatorOBJ.getGroups()) {
if (group.getName().equalsIgnoreCase("calcinator")) {
if (group.getFaces().size() != 0) {
for (Face face : group.getFaces()) {
tessellator.startDrawing(face.getDrawingMode());
float sumU = 0F;
float sumV = 0F;
for (int i = 0; i < face.getTextureCoordinates().length; ++i) {
sumU += face.getTextureCoordinates()[i].u;
sumV += face.getTextureCoordinates()[i].v;
}
for (int i = 0; i < face.getVertices().length; ++i) {
float offsetU = Reference.MODEL_TEXTURE_OFFSET;
float offsetV = Reference.MODEL_TEXTURE_OFFSET;
if (face.getTextureCoordinates()[i].u > sumU / face.getTextureCoordinates().length) {
offsetU = -offsetU;
}
if (face.getTextureCoordinates()[i].v > sumV / face.getTextureCoordinates().length) {
offsetV = -offsetV;
}
tessellator.addVertexWithUV(face.getVertices()[i].x * scale, face.getVertices()[i].y * scale, face.getVertices()[i].z * scale, face.getTextureCoordinates()[i].u + offsetU, face.getTextureCoordinates()[i].v + offsetV);
}
tessellator.draw();
}
}
group.render(tessellator, Reference.MODEL_TEXTURE_OFFSET, scale);
}
}
}

View file

@ -1,68 +1,66 @@
package net.minecraftforge.client.model.obj;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.util.Vec3;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class Face {
public int[] vertexIndices;
public int[] vertexNormalIndices;
public int[] textureCoordinateIndices;
private Vertex[] vertices;
private Vertex[] vertexNormals;
private TextureCoordinate[] textureCoordinates;
private int glDrawingMode;
public int[] getVertexIndices() {
return vertexIndices;
public Vertex[] vertices;
public Vertex[] vertexNormals;
public TextureCoordinate[] textureCoordinates;
public boolean invertNormal = false;
public int glDrawingMode;
public Vec3 getFaceNormal() {
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);
return v1.crossProduct(v2).normalize();
}
public void render(Tessellator tessellator, float scale) {
public Vertex[] getVertices() {
return vertices;
this.render(tessellator, 0F, scale);
}
public void render(Tessellator tessellator, float textureOffset, float scale) {
public void setVertexIndices(int[] vertexIndices) {
this.vertexIndices = vertexIndices;
}
public void setVertices(Vertex[] vertices) {
this.vertices = vertices;
}
public int getDrawingMode() {
return glDrawingMode;
}
public void setDrawingMode(int glDrawingMode) {
this.glDrawingMode = glDrawingMode;
}
public Vertex[] getVertexNormals() {
return vertexNormals;
}
public void setVertexNormals(Vertex[] vertexNormals) {
this.vertexNormals = vertexNormals;
}
public TextureCoordinate[] getTextureCoordinates() {
return textureCoordinates;
}
public void setTextureCoordinates(TextureCoordinate[] textureCoordinates) {
this.textureCoordinates = textureCoordinates;
tessellator.startDrawing(glDrawingMode);
tessellator.setNormal((float) getFaceNormal().xCoord, (float) getFaceNormal().yCoord, (float) getFaceNormal().zCoord);
float averageU = 0F;
float averageV = 0F;
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) {
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);
}
tessellator.draw();
}
}

View file

@ -2,6 +2,7 @@ package net.minecraftforge.client.model.obj;
import java.util.ArrayList;
import net.minecraft.client.renderer.Tessellator;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@ -38,8 +39,8 @@ public class Group {
Vertex currentVertex = null;
for (int i = 0; i < faces.size(); i++) {
currentFace = faces.get(i);
for (int j = 0; j < currentFace.getVertices().length; j++) {
currentVertex = currentFace.getVertices()[j];
for (int j = 0; j < currentFace.vertices.length; j++) {
currentVertex = currentFace.vertices[j];
if (Math.abs(currentVertex.x) > minX) {
minX = Math.abs(currentVertex.x);
}
@ -69,4 +70,18 @@ public class Group {
return min;
}
public void render(Tessellator tessellator, float scale) {
for (Face face : faces) {
face.render(tessellator, 0F, scale);
}
}
public void render(Tessellator tessellator, float textureOffset, float scale) {
for (Face face : faces) {
face.render(tessellator, textureOffset, scale);
}
}
}

View file

@ -48,7 +48,7 @@ public class FaceParser extends LineParser {
private void parseTriangles() {
face.setDrawingMode(GL11.GL_TRIANGLES);
face.glDrawingMode = GL11.GL_TRIANGLES;
parseLine(3);
}
@ -101,7 +101,7 @@ public class FaceParser extends LineParser {
private void parseQuad() {
face.setDrawingMode(GL11.GL_QUADS);
face.glDrawingMode = GL11.GL_QUADS;
parseLine(4);
}
@ -134,13 +134,9 @@ public class FaceParser extends LineParser {
group.indices.add(group.indexCount++);
group.indices.add(group.indexCount++); // create index list for current object
face.vertexIndices = vindices;
face.vertexNormalIndices = nindices;
face.textureCoordinateIndices = tindices;
face.setVertexNormals(normals);
face.setVertexNormals(normals);
face.setVertices(vertices);
face.setTextureCoordinates(textures);
face.vertexNormals = normals;
face.vertices = vertices;
face.textureCoordinates = textures;
wavefrontObject.getCurrentGroup().addFace(face);
}
@ -164,13 +160,9 @@ public class FaceParser extends LineParser {
group.indices.add(group.indexCount++);
group.indices.add(group.indexCount++); // create index list for current object
face.vertexIndices = vindices;
face.vertexNormalIndices = nindices;
face.textureCoordinateIndices = tindices;
face.setVertexNormals(normals);
face.setVertexNormals(normals);
face.setVertices(vertices);
face.setTextureCoordinates(textures);
face.vertexNormals = normals;
face.vertices = vertices;
face.textureCoordinates = textures;
wavefrontObject.getCurrentGroup().addFace(face);
}