I'm trying to write binary stl's from Processing, I've done ASCII stl's before but they get way to big. I'm using the wikipedia article on binary stl to write them, but I can't get it to work.
Here's my code:
try {
for (int i=0; i!=80; ++i) {
binfile.write(b[i]); //useless header, as long as it doesn't begin with "solid"
}
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(4); //needs to be a 32 bit number, so yeah...
for (int i=0; i<s.getVertexCount(); i+=3) {
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(0);
binfile.write(0);//normal vector, doesn't matter
for (int j=0; j!=3; ++j) {
float x = s.getVertex(i+j).x;
float y = s.getVertex(i+j).y;
float z = s.getVertex(i+j).z;
int convertX = Float.floatToIntBits(x);
int convertY = Float.floatToIntBits(y);
int convertZ = Float.floatToIntBits(z);
binfile.write(convertX >> 24);
binfile.write(convertX >> 16);
binfile.write(convertX >> 8);
binfile.write(convertX);
binfile.write(convertY >> 24);
binfile.write(convertY >> 16);
binfile.write(convertY >> 8);
binfile.write(convertY);
binfile.write(convertZ >> 24);
binfile.write(convertZ >> 16);
binfile.write(convertZ >> 8);
binfile.write(convertZ);
}
binfile.write(0);
binfile.write(0); //attribute byte count (16 bit), never used
}
binfile.flush();
binfile.close();
}
catch(IOException e) {
println("Oh no! ");
}
I have all the verteces in a PShape s, so I use those to write to the stl. Since the x,y,z coordinates have to be in a 32 bit floating point number, I convert them using Float.floatToIntBits. I then bitshift them because .write only writes the 8 least significant bits (see here). The four that I write after the header is the number of triangles. The program makes an stl file, but the file doesn't work, any idea where my mistake is?