@@ -66,10 +66,9 @@ public final class OBJLoader implements AssetLoader {
6666 protected final ArrayList <Vector3f > verts = new ArrayList <Vector3f >();
6767 protected final ArrayList <Vector2f > texCoords = new ArrayList <Vector2f >();
6868 protected final ArrayList <Vector3f > norms = new ArrayList <Vector3f >();
69-
70- protected final ArrayList <Face > faces = new ArrayList <Face >();
71- protected final HashMap <String , ArrayList <Face >> matFaces = new HashMap <String , ArrayList <Face >>();
72-
69+
70+ private final ArrayList <Group > groups = new ArrayList <Group >();
71+
7372 protected String currentMatName ;
7473 protected String currentObjectName ;
7574
@@ -87,6 +86,16 @@ public final class OBJLoader implements AssetLoader {
8786 protected String objName ;
8887 protected Node objNode ;
8988
89+ private static class Group {
90+ private String name ;
91+ private final ArrayList <Face > faces = new ArrayList <Face >();
92+ private final HashMap <String , ArrayList <Face >> matFaces = new HashMap <String , ArrayList <Face >>();
93+
94+ public Group (final String name ) {
95+ this .name = name ;
96+ }
97+ }
98+
9099 protected static class Vertex {
91100
92101 Vector3f v ;
@@ -164,8 +173,7 @@ public void reset(){
164173 verts .clear ();
165174 texCoords .clear ();
166175 norms .clear ();
167- faces .clear ();
168- matFaces .clear ();
176+ groups .clear ();
169177
170178 vertIndexMap .clear ();
171179 indexVertMap .clear ();
@@ -289,10 +297,17 @@ protected void readFace(){
289297 f .verticies [i ] = vertList .get (i );
290298 }
291299
292- if (matList != null && matFaces .containsKey (currentMatName )){
293- matFaces .get (currentMatName ).add (f );
300+ Group group = groups .get (groups .size () - 1 );
301+
302+ if (currentMatName != null && matList != null && matList .containsKey (currentMatName )){
303+ ArrayList <Face > matFaces = group .matFaces .get (currentMatName );
304+ if (matFaces == null ) {
305+ matFaces = new ArrayList <Face >();
306+ group .matFaces .put (currentMatName , matFaces );
307+ }
308+ matFaces .add (f );
294309 }else {
295- faces .add (f ); // faces that belong to the default material
310+ group . faces .add (f ); // faces that belong to the default material
296311 }
297312 }
298313
@@ -337,13 +352,6 @@ protected void loadMtlLib(String name) throws IOException{
337352 } catch (AssetNotFoundException ex ){
338353 logger .log (Level .WARNING , "Cannot locate {0} for model {1}" , new Object []{name , key });
339354 }
340-
341- if (matList != null ){
342- // create face lists for every material
343- for (String matName : matList .keySet ()){
344- matFaces .put (matName , new ArrayList <Face >());
345- }
346- }
347355 }
348356
349357 protected boolean nextStatement (){
@@ -387,8 +395,14 @@ protected boolean readLine() throws IOException{
387395 // specify MTL lib to use for this OBJ file
388396 String mtllib = scan .nextLine ().trim ();
389397 loadMtlLib (mtllib );
390- }else if (cmd .equals ("s" ) || cmd .equals ("g" )){
398+ }else if (cmd .equals ("s" )) {
399+ logger .log (Level .WARNING , "smoothing groups are not supported, statement ignored: {0}" , cmd );
400+ return nextStatement ();
401+ }else if (cmd .equals ("mg" )) {
402+ logger .log (Level .WARNING , "merge groups are not supported, statement ignored: {0}" , cmd );
391403 return nextStatement ();
404+ }else if (cmd .equals ("g" )) {
405+ groups .add (new Group (scan .nextLine ().trim ()));
392406 }else {
393407 // skip entire command until next line
394408 logger .log (Level .WARNING , "Unknown statement in OBJ! {0}" , cmd );
@@ -565,11 +579,14 @@ public Object load(AssetInfo info) throws IOException{
565579 }
566580
567581 objNode = new Node (objName + "-objnode" );
582+
583+ Group defaultGroupStub = new Group (null );
584+ groups .add (defaultGroupStub );
568585
569586 if (!(info .getKey () instanceof ModelKey ))
570587 throw new IllegalArgumentException ("Model assets must be loaded using a ModelKey" );
571588
572- InputStream in = null ;
589+ InputStream in = null ;
573590 try {
574591 in = info .openStream ();
575592
@@ -583,25 +600,43 @@ public Object load(AssetInfo info) throws IOException{
583600 }
584601 }
585602
586- if (matFaces .size () > 0 ){
587- for (Entry <String , ArrayList <Face >> entry : matFaces .entrySet ()){
588- ArrayList <Face > materialFaces = entry .getValue ();
589- if (materialFaces .size () > 0 ){
590- Geometry geom = createGeometry (materialFaces , entry .getKey ());
603+ for (Group group : groups ) {
604+ if (group == defaultGroupStub ) {
605+ materializeGroup (group , objNode );
606+ } else {
607+ Node groupNode = new Node (group .name );
608+ materializeGroup (group , groupNode );
609+ if (groupNode .getQuantity () == 1 ) {
610+ Spatial geom = groupNode .getChild (0 );
611+ geom .setName (groupNode .getName ());
591612 objNode .attachChild (geom );
613+ } else if (groupNode .getQuantity () > 1 ) {
614+ objNode .attachChild (groupNode );
592615 }
593616 }
594- }else if (faces .size () > 0 ){
595- // generate final geometry
596- Geometry geom = createGeometry (faces , null );
597- objNode .attachChild (geom );
598617 }
599618
600619 if (objNode .getQuantity () == 1 )
601620 // only 1 geometry, so no need to send node
602- return objNode .getChild (0 );
621+ return objNode .getChild (0 );
603622 else
604623 return objNode ;
605624 }
606-
625+
626+ private void materializeGroup (Group group , Node container ) throws IOException {
627+ if (group .matFaces .size () > 0 ) {
628+ for (Entry <String , ArrayList <Face >> entry : group .matFaces .entrySet ()){
629+ ArrayList <Face > materialFaces = entry .getValue ();
630+ if (materialFaces .size () > 0 ){
631+ Geometry geom = createGeometry (materialFaces , entry .getKey ());
632+ container .attachChild (geom );
633+ }
634+ }
635+ } else if (group .faces .size () > 0 ) {
636+ // generate final geometry
637+ Geometry geom = createGeometry (group .faces , null );
638+ container .attachChild (geom );
639+ }
640+ }
641+
607642}
0 commit comments