簡體   English   中英

如何轉換3D座標

[英]How can I convert 3D coordinates

我想將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;
}

大家好,我已經實施了一個解決方案。 我遍歷所有頂點並搜索模型的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());
        }
    }
}

任何反饋都歡迎你們薩拉姆

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM