我正在使用Java3d(VERSION 1.6),我试图从任何对象读取所有多边形。
我使用以下代码加载了一个对象:
private BranchGroup loadObj(String p) {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.3);
Matrix4d matrix = new Matrix4d();
t3d.get(matrix);
try
{
Scene s = null;
ObjectFile f = new ObjectFile ();
String basepath = new File(p).getAbsolutePath();
System.out.println(basepath);
f.setBasePath(basepath);
f.setFlags (0);
s = f.load (s1);
s.getSceneGroup().setBoundsAutoCompute(true);
tg.addChild (s.getSceneGroup ());
objRoot.addChild(tg);
bounds.add(objRoot.getBounds());
objRoot.compile();
}
现在我喜欢从BranchGroup或Scene Object中读取计算出的多边形,并将每个多边形放在一个主要是Point3d数组的类中。通过该课程,我构建了一些算法来搜索特定的点和内容。那我怎么得到这些多边形?
我需要它的原因是因为我试图在不平坦的表面上“行走”。我不能使用BoundingBoxes或球体,因为这不够精确。我也很感激不同的解决方案!
编辑:在gouessej的帮助下,我到目前为止:
try
{
Scene s = null;
ObjectFile f = new ObjectFile ();
String basepath = new File(p).getAbsolutePath();
System.out.println(basepath);
f.setBasePath(basepath);
f.setFlags (ObjectFile.TRIANGULATE);
String s1 = p;
s = f.load (s1);
BranchGroup branch = s.getSceneGroup();
branch.setBoundsAutoCompute(true);
Shape3D shape = (Shape3D)branch.getChild(0);
Geometry g = shape.getGeometry();
TriangleArray ta = (TriangleArray)shape.getGeometry();
System.out.println(ta.getVertexCount()); // Prints around 95.000, sounds about right
System.out.println(ta.getVertexFormat()); // prints 387
double[] coords = ta.getCoordRefDouble(); // line: 526; Here it throws the exception
System.out.println(Arrays.toString(coords));
tg.addChild (branch);
objRoot.addChild(tg);
bounds.add(objRoot.getBounds());
System.out.println();
objRoot.compile();
}
但在ta.getCoordRefDouble()
线上,它给我一个例外:
Exception in thread "main" java.lang.IllegalStateException: GeometryArray: cannot access individual array references in INTERLEAVED mode
at javax.media.j3d.GeometryArray.getCoordRefDouble(GeometryArray.java:5755)
at com.object.simpleTest.Test1.loadObj(Test1.java:526)
at com.object.simpleTest.Test1.<init>(Test1.java:428)
at com.object.simpleTest.Test1.main(Test1.java:686)
它是什么意思以及如何解决它?
起初,Java 3D并没有死,因为你可以看到here(请编辑你的问题)。
其次,你可以看看the Java documentation of the class ObjectFile。我建议你使用标志“TRIANGULATE”,以确保得到一个只包含凸多边形的多边形数组,以方便您的计算。
Scene对象的分支组包含一个Shape3D对象。此Shape3D对象包含一个Geometry对象,它存储您的多边形。 ObjectFile的源代码是here。看看this line。
编辑:您可以通过调用Scene.getSceneGroup()来获取场景的BranchGroup。您可以看到该组已添加到场景here中。调用Group.getAllChildren(),循环所有子节点,使用instanceof检查子节点是否是Shape3D的一个实例。对于每个Shape3D,请调用getGeometry()或getAllGeometries()。几何应该是GeometryArray,也许是TriangleArray。 getCoordRefBuffer()在Java 3D 1.6中可能无法完全以相同的方式工作,因为我们删除了J3DBuffer,使用了getCoordRefDouble(),getCoordRefFloat()或getCoordinate()或getCoordinates()的任何变体。请确保您使用Java 3D 1.6,以便我们讨论相同的代码和相同的版本。旧版本已过时且未维护。
Edit.2:如果顶点是交错的,则调用getInterleavedVertices(),因为它的名字暗示。请记住,它也可能包含法线(在第一个位置),而不仅是顶点坐标(在第二个位置):nx ny nz vx vy vz