My program reads in an ASCII encoded .stl file and parses it to a TriangleMesh, in order to display it on the screen. It works fine as long as I only want a single colour for the entire geometry. But now I want to be able to assign different colours to different polygons on the mesh. Later down the line we are going to know which face will have to be given what colour but right now a prove of concept is sufficient. Unfortunately I can't get it to work. Here is what I have thus far:
public static MeshView parseSTLwithColor(String stlString){
TriangleMesh mesh = new TriangleMesh();
PhongMaterial textureMaterial = new PhongMaterial();
Image texture = new Image("http://nikijacob.com/wp-content/uploads/2011/01/Mosaic.jpg");
textureMaterial.setDiffuseMap(texture);
Just setting up a quick test texture with different colours in it.
String withoutHeader = stlString.substring(12);
String[] facetsArray = withoutHeader.split("endfacet");
for(int h=0;h<facetsArray.length;h++){
if(facetsArray[h].contains("endloop")){
facetsArray[h] = facetsArray[h].substring(0,facetsArray[h].indexOf("endloop"));
}
}
for(int i= 0 ; i<facetsArray.length;i++){
int facetOffset = i*3;
String[] verticesArray = facetsArray[i].split("vertex");
if(verticesArray.length != 4) break;
for(int j=1;j<4;j++){
String[] vectorArray = verticesArray[j].split(" ");
for(int k=1;k<4;k++) mesh.getPoints().addAll(Float.parseFloat(vectorArray[k]));
Yeah, not that great a way to do it but it was just a first try. Basically, The string of ASCII encoded mesh data is split into the mesh's faces, then each face is split into its 3 points, then each point gets split into 3 vectors, as we are in 3d. Each of those gets parsed into a Float and added to the TriangleMesh's points array.
mesh.getTexCoords().addAll(1/(100+j),1/(100+j));
}
Next I assign each point a texture coordinate. I tried giving each point slightly different coordinates, because I thought that might help. (But it didn't.)
mesh.getFaces().addAll((facetOffset), (facetOffset),(1+facetOffset), (1+facetOffset),(2+facetOffset), (2+facetOffset));
}
MeshView meshView = new MeshView(mesh);
meshView.setMaterial(textureMaterial);
return meshView;
}
And just a little bit of housekeeping. Build the faces from the points and texture coordinates, instantiate a MeshView and give it the texture, so that a textured Meshview can be returned.
Now, the problem manifests in the way that instead of assigning different colours to the faces, the entire surface of the 3d object seems to be given a mix of the entire texture. When I tested with a halve red and halve blue texture, the entire geometry was rendered purple.