2

I built an OBJ to JavaFX triangle mesh parser and imported the monkey head sample model from blender but it doesn't render correctly. It almost seems to have a wallhack like effect. This link has the obj I'm trying to import as well as a video showing the problem. Link. This is the code I'm using.

if(tmp.startsWith("v ")) {
    split = tmp.split(" ");
    verticies.add(Float.parseFloat(split[1]));
    verticies.add(Float.parseFloat(split[2]));
    verticies.add(Float.parseFloat(split[3]));
} else if(tmp.startsWith("f ")) {
    split = tmp.split("f |/\\d*/\\d* *");
    faces.add(Integer.parseInt(split[1]) - 1);
    faces.add(Integer.parseInt(split[2]) - 1);
    faces.add(Integer.parseInt(split[3]) - 1);
    if(split.length > 4) {
        faces.add(Integer.parseInt(split[3]) - 1);
        faces.add(Integer.parseInt(split[4]) - 1);
        faces.add(Integer.parseInt(split[1]) - 1);
    }
}
José Pereda
  • 44,311
  • 7
  • 104
  • 132
Scoopta
  • 987
  • 1
  • 9
  • 22

2 Answers2

1

Turns out the solution was really really simple and had nothing to do with my OBJ code. The Scene constructor takes a boolean parameter to determine whether or not depth buffering is enabled. If you use a constructor that doesn't take a depth buffering parameter it defaults to false. Setting it to true completely fixes the issue.

Scoopta
  • 987
  • 1
  • 9
  • 22
0

There are already a few 3D model JavaFX importers out there.

You can check the 3DViewer project from the OpenJFX project. It is open source.

If you clone the project, build it and run it, you will be able to import your obj file:

3DViewer

As you can see, there are no issues with the obj file.

You may have a look at the code for the OBJ importer, to find out about how an obj file is parsed and converted to a TriangleMesh.

Other importers can be found at interactivemesh.org, but it is not open source.

Regarding the issue you mentioned, you can play with the cull face:

meshView.setCullFace(CullFace.BACK);

or

meshView.setCullFace(CullFace.FRONT);

will change the order of winding the triangles, affecting how you see the model. Default is BACK.

José Pereda
  • 44,311
  • 7
  • 104
  • 132