如何转换3D坐标?

时间:2014-05-28 13:29:11

标签: java 3d java-3d

我想将OBJ文件的3D坐标转换为[0..1]之间,因此我可以使用Java3D绘制它们。 这是我的尝试:

private BranchGroup drawFDPS() {
    BranchGroup lineGroup = new BranchGroup();
    Appearance app = new Appearance();
    ColoringAttributes ca = new ColoringAttributes(new Color3f(.0f, 204.0f, .0f), ColoringAttributes.SHADE_FLAT);
    app.setColoringAttributes(ca);

    Point3f[] plaPts = new Point3f[4];
    int count = 0;
// this is just for testing...
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            plaPts[count] = new Point3f(i / 10.0f, j / 10.0f, 0);
            count++;
        }
    }
    PointArray pla = new PointArray(4, GeometryArray.COORDINATES);

    pla.setCoordinates(0, plaPts);
    // between here!
    PointAttributes a_point_just_bigger = new PointAttributes();
    a_point_just_bigger.setPointSize(10.0f);// 10 pixel-wide point
    a_point_just_bigger.setPointAntialiasingEnable(true);
    app.setPointAttributes(a_point_just_bigger);
    // and here! sets the point-attributes so it is easily seen.
...
    return lineGroup;
}

1 个答案:

答案 0 :(得分:0)

解决方案

好吧,伙计们,我已经实施了一个解决方案。 我遍历所有顶点并搜索模型的6个极限,然后根据极限标准化所有坐标。这是我的实施:

private Vector3f[] getLimits() {
    Vector3f currentVertex = new Vector3f();

    // Find the limits of the model
    Vector3f[] limit = new Vector3f[2];
    limit[0] = new Vector3f(Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE);
    limit[1] = new Vector3f(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
    for (int i = 0; i < positions.size(); i++) {

        currentVertex = positions.get(i);

        // Keep track of limits for normalization
        if (currentVertex.getX() < limit[0].getX())
            limit[0].setX(currentVertex.getX());
        if (currentVertex.getX() > limit[1].getX())
            limit[1].setX(currentVertex.getX());
        if (currentVertex.getY() < limit[0].getY())
            limit[0].setY(currentVertex.getY());
        if (currentVertex.getY() > limit[1].getY())
            limit[1].setY(currentVertex.getY());
        if (currentVertex.getZ() < limit[0].getZ())
            limit[0].setZ(currentVertex.getZ());
        if (currentVertex.getZ() > limit[1].getZ())
            limit[1].setZ(currentVertex.getZ());
    }
    return limit;
} // End of getLimits

然后我使用这些限制来标准化所有顶点,因此它们的坐标在[-1..1]之间,它们将在屏幕上正确显示^^ 这是规范化方法:

规范化方法

private void normalize() {
    Vector3f[] limits = getLimits();
    for(int i = 0; i < positions.size(); i++) {

        if(positions.get(i).getX() >= 0) {
            positions.get(i).setX(positions.get(i).getX() / limits[1].getX());
        } else {
            positions.get(i).setX(Math.abs(positions.get(i).getX()) / limits[0].getX());
        }

        if(positions.get(i).getY() >= 0) {
            positions.get(i).setY(positions.get(i).getY() / limits[1].getY());
        } else {
            positions.get(i).setY(Math.abs(positions.get(i).getY()) / limits[0].getY());
        }

        if(positions.get(i).getZ() >= 0) {
            positions.get(i).setZ(positions.get(i).getZ() / limits[1].getZ());
        } else {
            positions.get(i).setZ(Math.abs(positions.get(i).getZ()) / limits[0].getZ());
        }
    }
}

任何反馈都是受欢迎的家伙 萨拉姆