Search code examples
java3djava-3d

How can I convert 3D coordinates


I want to convert 3D coordinates for an OBJ file to be between [0..1], so I can to draw them using Java3D. this is my try:

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;
}

Solution

  • Solution

    Well guys, I have implemented a solution. I iterate through all the vertices and search for the 6 limits of the model, then I normalize all the coordinates according to the limits. this is my implementation :

    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
    

    Then I use those limits to normalize all the vertices, so their coordinates are between [-1..1] and they will show correctly on the screen ^^ this is the normalize method :

    Normalize method

    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());
            }
        }
    }
    

    Any feedback is welcome guys Salam