Skip to content

Instantly share code, notes, and snippets.

@StillManic
Created July 16, 2015 20:07
Show Gist options
  • Save StillManic/93efbde312a2536b0c66 to your computer and use it in GitHub Desktop.
Save StillManic/93efbde312a2536b0c66 to your computer and use it in GitHub Desktop.
public static class Group implements IModelPart
{
public static final String DEFAULT_NAME = "OBJModel.Default.Element.Name";
public static final String ALL = "OBJModel.Group.All.Key";
public static final String ALL_EXCEPT = "OBJModel.Group.All.Except.Key";
private String name = DEFAULT_NAME;
private TRSRTransformation transform = TRSRTransformation.identity();
private LinkedHashSet<Face> faces = new LinkedHashSet<Face>();
private boolean visible = true;
public Group(String name, LinkedHashSet<Face> faces)
{
this(name, TRSRTransformation.identity(), faces);
}
public Group(String name, TRSRTransformation transform, LinkedHashSet<Face> faces)
{
this.name = name != null ? name : DEFAULT_NAME;
this.transform = transform;
this.faces = faces == null ? new LinkedHashSet<Face>() : faces;
}
public void applyTransform()
{
LinkedHashSet<Face> faceSet = new LinkedHashSet<Face>();
for (Face f : this.faces)
{
faceSet.add(f.bake(this.transform));
}
this.faces = faceSet;
}
public String getName()
{
return this.name;
}
public void setTransform(TRSRTransformation transform)
{
this.transform = transform == null ? TRSRTransformation.identity() : transform;
}
public TRSRTransformation getTransform()
{
return this.transform;
}
public LinkedHashSet<Face> getFaces()
{
return this.faces;
}
public void setFaces(LinkedHashSet<Face> faces)
{
this.faces = faces;
}
public void addFace(Face face)
{
this.faces.add(face);
}
public void addFaces(List<Face> faces)
{
this.faces.addAll(faces);
}
public void setVisible(boolean visible)
{
this.visible = visible;
}
public boolean getVisible()
{
return this.visible;
}
}
@Override
public List<BakedQuad> getGeneralQuads()
{
if (quads == null)
{
quads = new LinkedHashSet<BakedQuad>();
LinkedHashSet<Face> faces = new LinkedHashSet<Face>();
// if (this.state instanceof OBJState)
// {
// FMLLog.info("OBJBakedModel: state is OBJState");
// if (((OBJState) this.state).parent != null && ((OBJState) this.state).parent instanceof TRSRTransformation)
// {
// FMLLog.info("OBJBakedModel: state parent is a TRSRTransformation");
// TRSRTransformation parent = (TRSRTransformation) ((OBJState) this.state).parent;
// FMLLog.info("%n%s", parent.getMatrix());
// }
// }
for (Group e : this.model.getMatLib().getGroups().values())
{
if (e.getVisible())
{
e.applyTransform();
faces.addAll(e.getFaces());
}
}
for (Face f : faces)
{
buffer.clear();
String texture = this.model.getMatLib().library.get(f).getName();
if (this.state instanceof OBJState)
{
if (((OBJState) this.state).parent != null && ((OBJState) this.state).parent instanceof TRSRTransformation)
{
f = f.bake((TRSRTransformation) (((OBJState) this.state).parent));
}
}
else if (this.state instanceof TRSRTransformation)
{
texture = this.model.getMatLib().library.get(f).getName();
f = f.bake((TRSRTransformation) this.state);
}
TextureAtlasSprite sprite = this.textures.get("missingno");
if (this.model.getMatLib().materials.get(texture).isWhite()) sprite = ModelLoader.White.instance;
else sprite = this.textures.get(texture);
float minU = 0.0f;
float maxU = 1.0f;
float minV = 0.0f;
float maxV = 1.0f;
if (f.texCoords != null && !f.areUVsNormalized())
{
for (TextureCoordinate t : f.texCoords)
{
minU = t.getPosition().getX() < minU ? t.getPosition().getX() : minU;
maxU = t.getPosition().getX() > maxU ? t.getPosition().getX() : maxU;
minV = t.getPosition().getY() < minV ? t.getPosition().getY() : minV;
maxV = t.getPosition().getY() > maxV ? t.getPosition().getY() : maxV;
}
for (int i = 0; i < f.texCoords.length; i++)
{
TextureCoordinate t = f.texCoords[i];
float U = (t.getPosition().getX() - minU) / (maxU - minU);
float V = (t.getPosition().getY() - minV) / (maxV - minV);
Vector3f normPos = new Vector3f(U, V, t.getPosition().getZ());
f.texCoords[i] = new TextureCoordinate(normPos);
}
}
TextureCoordinate def1 = new TextureCoordinate(new Vector3f(minU, minV, 1));
TextureCoordinate def2 = new TextureCoordinate(new Vector3f(maxU, minV, 1));
TextureCoordinate def3 = new TextureCoordinate(new Vector3f(maxU, maxV, 1));
TextureCoordinate def4 = new TextureCoordinate(new Vector3f(minU, maxV, 1));
// putVertexData(f.verts[0], f.texCoords != null ? f.texCoords[0] : null, f.norms != null ? f.norms[0] : f.getNormal(), sprite);
// putVertexData(f.verts[1], f.texCoords != null ? f.texCoords[1] : null, f.norms != null ? f.norms[1] : f.getNormal(), sprite);
// putVertexData(f.verts[2], f.texCoords != null ? f.texCoords[2] : null, f.norms != null ? f.norms[2] : f.getNormal(), sprite);
// putVertexData(f.verts[3], f.texCoords != null ? f.texCoords[3] : null, f.norms != null ? f.norms[3] : f.getNormal(), sprite);
putVertexData(f.verts[0], f.texCoords != null ? f.texCoords[0] : def1, f.norms != null ? f.norms[0] : f.getNormal(), sprite);
putVertexData(f.verts[1], f.texCoords != null ? f.texCoords[1] : def2, f.norms != null ? f.norms[1] : f.getNormal(), sprite);
putVertexData(f.verts[2], f.texCoords != null ? f.texCoords[2] : def3, f.norms != null ? f.norms[2] : f.getNormal(), sprite);
putVertexData(f.verts[3], f.texCoords != null ? f.texCoords[3] : def4, f.norms != null ? f.norms[3] : f.getNormal(), sprite);
buffer.flip();
int[] data = new int[VERTICES_IN_QUAD * format.getNextOffset() / BYTES_IN_INT];
buffer.asIntBuffer().get(data);
quads.add(new ColoredBakedQuad(data, -1, EnumFacing.getFacingFromVector(f.getNormal().normal.x, f.getNormal().normal.y, f.getNormal().normal.z)));
}
}
List<BakedQuad> quadList = new ArrayList<BakedQuad>();
quadList.addAll(quads);
return quadList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment