yn97
December 28, 2018, 1:54am
1
I used this code to export mesh as an obj file:
PQTorus mesh = new PQTorus(3, 8, 2f, 1f, 32, 32);
Geometry geom = new Geometry("PQTorus", mesh);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
rootNode.attachChild(geom);
int size = mesh.getTriangleCount();
StringBuilder str = new StringBuilder();
VertexBuffer pvb = mesh.getBuffer(VertexBuffer.Type.Position);
FloatBuffer positions = (FloatBuffer) pvb.getData();
Vector3f[] buf = BufferUtils.getVector3Array(positions);
str.append("o PQTorus \n");
for (Vector3f v : buf) {
str.append("v\t").append(v.x).append("\t").append(v.y).append("\t").append(v.z).append("\n");
}
for (int i = 0; i < size; i++) {
int[] indices = new int[3];
mesh.getTriangle(i, indices);
str.append("f\t").append(indices[0] + 1).append("\t").append(indices[1] + 1).append("\t").append(indices[2] + 1).append("\n");
}
System.out.println(str.toString());
try {
String path = "/path/to/PQTorus.obj/file";
Files.write(Paths.get(path), str.toString().getBytes());
} catch (IOException ex) {
System.out.println(ex);
}
and the result after importing to blender is :
I think there is a problem in shape exporting code and the pipes should not be connected together by a face.
and also there are some hole in center of pipes.
how can I fix it and fill second problem?
mitm
December 28, 2018, 4:07am
2
You can try this. It looks like it will give you an idea what you should be doing. Haven’t ran the code but it looks ok at a glance.
Much thanks for your help. Here is my solution:
@Override
public void exportSelectedData(File outputFile, Vector3f[] vertices, Integer[] indices) throws IOException {
FileWriter writer = new FileWriter(outputFile);
for (int i = 0; i < indices.length; i += 3) {
Vector3f v1 = new Vector3f(vertices[i]);
Vector3f v2 = new Vector3f(vertices[i + 1]);
Vector3f v3 = new Vector3f(vertices[i + 2]);
Vector3f normal = computeNormal(v1, v2, v3);
writer.write(String.format("v %f %f %f…
Maybe look at this also,
}
/**
* sets an index that this triangle represents if it is contained in a OBBTree.
* @param index the index in an OBBtree
*/
public void setIndex(int index) {
this.index = index;
}
public static Vector3f computeTriangleNormal(Vector3f v1, Vector3f v2, Vector3f v3, Vector3f store) {
if (store == null) {
store = new Vector3f(v2);
} else {
store.set(v2);
}
store.subtractLocal(v1).crossLocal(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z);
return store.normalizeLocal();
}
Best thing to do is start with a simple object with known vertices so you can figure out what your doing.
Basically create something simple in blender and export it as obj so you can open up the file in a text editor and compare it to what your are doing. Make adjustments to your script and soon your can write any size obj you want.
Edit: Crap, guess it would help to include the format stuff…
https://www.cs.cmu.edu/~mbz/personal/graphics/obj.html
Ali_RS
December 28, 2018, 8:07am
3
Also I have noticed jmePhonon has a way to export scene as obj in case it helps
} ); renderer=Phonon.init(settings, this); Phonon.setManager(settings,this,mng); Phonon.loadScene(settings, this, rootNode, (sx) -> { return (!(sx instanceof Geometry))||sx.getParent().getUserData("game.soundoccluder")!=null; }); // renderer.saveMeshAsObj("/tmp/scene1.obj"); } catch (Exception e1) { e1.printStackTrace(); } } this.setPauseOnLostFocus(false); this.setDisplayStatView(false); this.setDisplayFps(true);
Have not test it myself.
1 Like