update ModelTileMesh to use the new base class

This commit is contained in:
Gered 2013-07-21 17:24:56 -04:00
parent 0c192bdbd0
commit 2140924e62

View file

@ -19,7 +19,7 @@ import com.blarg.gdx.math.MathHelpers;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
public class ModelTileMesh extends TileMesh {
public class ModelTileMesh extends BaseModelTileMesh {
static final Vector3 tmpPosition = new Vector3();
static final Vector3 tmpNormal = new Vector3();
static final BoundingBox tmpModelBounds = new BoundingBox();
@ -84,7 +84,6 @@ public class ModelTileMesh extends TileMesh {
model.getBoundingBox(tmpModelBounds);
if (scaleToSize != null) {
model.getBoundingBox(tmpModelBounds);
MathHelpers.getScaleFactor(tmpModelBounds.getDimensions(), scaleToSize, tmpScaleFactor);
bounds = new BoundingBox().set(Vector3.Zero, scaleToSize);
} else {
@ -93,63 +92,7 @@ public class ModelTileMesh extends TileMesh {
}
for (int i = 0; i < model.nodes.size; ++i)
addModelNodeVertices(model.nodes.get(i), textures, tmpScaleFactor);
}
private void addModelNodeVertices(Node node, MaterialTileMapping textures, Vector3 scaleFactor) {
final Matrix4 transform = node.globalTransform; // TODO: test that this is the right transform to use?
for (int i = 0; i < node.parts.size; ++i) {
NodePart nodePart = node.parts.get(i);
MaterialTileMapping.TileTexture texture = textures.get(nodePart.material.id);
MeshPart meshPart = nodePart.meshPart;
ShortBuffer indices = meshPart.mesh.getIndicesBuffer();
FloatBuffer vertices = meshPart.mesh.getVerticesBuffer();
final int strideInFloats = meshPart.mesh.getVertexSize() / (Float.SIZE / 8);
for (int j = 0; j < meshPart.numVertices; ++j) {
int index = indices.get(meshPart.indexOffset + j);
int offset = index * strideInFloats;
tmpPosition.set(vertices.get(offset), vertices.get(offset + 1), vertices.get(offset + 2))
.add(positionOffset)
.scl(scaleFactor)
.mul(transform);
this.vertices.setPos(tmpPosition);
offset += 3;
if (meshPart.mesh.getVertexAttribute(VertexAttributes.Usage.Color) != null) {
// TODO: blend mesh color and source model color somehow?
this.vertices.setCol(vertices.get(offset), vertices.get(offset + 1), vertices.get(offset + 2), vertices.get(offset + 3));
offset += 4;
} else
this.vertices.setCol(color);
// TODO: better to throw exception (or check beforehand) if this is missing? setting zero's doesn't feel like the best solution
if (meshPart.mesh.getVertexAttribute(VertexAttributes.Usage.Normal) != null) {
tmpNormal.set(vertices.get(offset), vertices.get(offset + 1), vertices.get(offset + 2))
.rot(transform);
this.vertices.setNor(tmpNormal);
offset += 3;
} else
this.vertices.setNor(Vector3.Zero);
// TODO: better to throw exception (or check beforehand) if this is missing? setting zero's doesn't feel like the best solution
if (meshPart.mesh.getVertexAttribute(VertexAttributes.Usage.TextureCoordinates) != null) {
this.vertices.setUV(
TextureAtlas.scaleTexCoordU(vertices.get(offset), texture.materialMinU, texture.materialMaxU, texture.region),
TextureAtlas.scaleTexCoordV(vertices.get(offset + 1), texture.materialMinV, texture.materialMaxV, texture.region)
);
offset += 3;
} else
this.vertices.setUV(Vector2.Zero);
this.vertices.moveNext();
}
}
for (int i = 0; i < node.children.size; ++i)
addModelNodeVertices(node.children.get(i), textures, tmpScaleFactor);
collectModelNodeVertices(model.nodes.get(i), vertices, textures, color, tmpScaleFactor, positionOffset);
}
private void setupCollisionVertices(Model collisionModel) {
@ -162,40 +105,7 @@ public class ModelTileMesh extends TileMesh {
int numVertices = countModelVertices(collisionModel);
collisionVertices = new Array<Vector3>(true, numVertices, Vector3.class);
for (int i = 0; i < collisionModel.nodes.size; ++i)
addModelNodeCollisionVertices(collisionModel.nodes.get(i), tmpScaleFactor);
}
private void addModelNodeCollisionVertices(Node node, Vector3 scaleFactor) {
final Matrix4 transform = node.globalTransform; // TODO: test that this is the right transform to use?
for (int i = 0; i < node.parts.size; ++i) {
NodePart nodePart = node.parts.get(i);
MeshPart meshPart = nodePart.meshPart;
ShortBuffer indices = meshPart.mesh.getIndicesBuffer();
FloatBuffer vertices = meshPart.mesh.getVerticesBuffer();
final int strideInFloats = meshPart.mesh.getVertexSize() / (Float.SIZE / 8);
for (int j = 0; j < meshPart.numVertices; ++j) {
int index = indices.get(meshPart.indexOffset + j);
int offset = index * strideInFloats;
tmpPosition.set(vertices.get(offset), vertices.get(offset + 1), vertices.get(offset + 2))
.add(positionOffset)
.scl(scaleFactor)
.mul(transform);
collisionVertices.add(new Vector3(tmpPosition));
}
}
for (int i = 0; i < node.children.size; ++i)
addModelNodeCollisionVertices(node.children.get(i), tmpScaleFactor);
}
private int countModelVertices(Model model) {
int numVertices = 0;
for (int i = 0; i < model.meshParts.size; ++i)
numVertices += model.meshParts.get(i).numVertices;
return numVertices;
collectModelNodeVertexPositions(collisionModel.nodes.get(i), collisionVertices, tmpScaleFactor, collisionPositionOffset);
}
@Override