I've been attempting to play around with the ObjLoader class, I've tried to get the vertices and indices from each of the mesh parts within each of the model's nodes. After this, I'm trying to apply this to create a PolygonRegion, but I've noticed that the textures aren't mapping correctly and are all messed up during rendering.
Am I doing this correctly?
public static PolygonRegion createFromObjFile(TextureRegion textureRegion, String filePath, String nodeId) {
ObjLoader objLoader = new ObjLoader();
FileHandle fileHandle = Gdx.files.absolute(filePath);
Model model = objLoader.loadModel(fileHandle);
Mesh mesh = getMeshPart(model, nodeId);
assert mesh != null : "Unable to find nodeId from model. Mesh is null.";
float[] vertices = getVertices(mesh);
short[] indices = getIndices(mesh);
final int vertexSize = mesh.getVertexSize() / 4;
float[] newVertices = new float[vertexSize];
int vertexIndex = 0;
int newVertexIndex = 0;
for (int i = 0; i < vertices.length; i++) {
if (newVertexIndex >= newVertices.length) {
break;
}
if (vertexIndex < 2) {
newVertices[newVertexIndex] = vertices[i] * TEXTURE_WIDTH;
vertexIndex++;
newVertexIndex++;
} else {
vertexIndex = 0;
}
}
return new PolygonRegion(textureRegion, CollectionUtils.copyOf(newVertices), CollectionUtils.copyOf(indices));
}
public static float[] getVertices(Mesh mesh) {
final int vertexSize = mesh.getVertexSize() / 4;
int numVertices = mesh.getNumVertices();
float[] vertices = new float[numVertices * vertexSize];
mesh.getVertices(0, vertices.length, vertices);
return vertices;
}
public static short[] getIndices(Mesh mesh) {
short[] indices = new short[(int) (mesh.getNumIndices())];
mesh.getIndices(indices);
return indices;
}
TLDR: I'm trying to create polygon regions from parts of a loaded model, but the textures aren't mapping correctly.
Attempted to retrieve model's vertices and indices for each of it's node's mesh parts, but rendering of the textures are all wrong.