@@ -1714,8 +1714,8 @@ protected BufferedReader getBufferedReader(String filename) {
17141714
17151715
17161716 protected void parseOBJ (BufferedReader reader , ArrayList <PVector > vertices , ArrayList <PVector > normals , ArrayList <PVector > textures , ArrayList <OBJFace > faces , ArrayList <OBJMaterial > materials ) {
1717- Hashtable <String , Integer > mtlHash = new Hashtable <String , Integer >();
1718-
1717+ Hashtable <String , Integer > materialsHash = new Hashtable <String , Integer >();
1718+ int mtlIdxCur = - 1 ;
17191719 try {
17201720 // Parse the line.
17211721
@@ -1760,32 +1760,130 @@ protected void parseOBJ(BufferedReader reader, ArrayList<PVector> vertices, Arra
17601760 // Object name is ignored, for now.
17611761 } else if (elements [0 ].equals ("mtllib" )) {
17621762 if (elements [1 ] != null ) {
1763- parseMTL (getBufferedReader (elements [1 ]), materials , mtlHash );
1763+ parseMTL (getBufferedReader (elements [1 ]), materials , materialsHash );
17641764 }
17651765 } else if (elements [0 ].equals ("g" )) {
17661766 // Grouping is ignored, for now. Groups are automaticallty generated during recording by the triangulator.
17671767 } else if (elements [0 ].equals ("usemtl" )) {
1768-
1768+ // Getting material index for the material set for use.
1769+ if (elements [1 ] != null ) {
1770+ String mtlname = elements [1 ];
1771+ if (materialsHash .containsKey (mtlname )) {
1772+ Integer tmpInt = materialsHash .get (mtlname );
1773+ mtlIdxCur = tmpInt .intValue ();
1774+ } else {
1775+ mtlIdxCur = -1 ;
1776+ }
1777+ }
17691778 } else if (elements [0 ].equals ("f" )) {
1770- // face setting
1779+ // Face setting
1780+ OBJFace face = new OBJFace ();
1781+ face .matIdx = mtlIdxCur ;
1782+
1783+ for (int i = 1 ; i < elements .length ; i ++) {
1784+ String seg = elements [i ];
1785+
1786+ if (seg .indexOf ("/" ) > 0 ) {
1787+ String [] forder = seg .split ("/" );
1788+
1789+ if (forder .length > 2 ) {
1790+ if (forder [0 ].length () > 0 ) {
1791+ face .vertIdx .add (Integer .valueOf (forder [0 ]));
1792+ }
1793+
1794+ if (forder [1 ].length () > 0 ) {
1795+ face .texIdx .add (Integer .valueOf (forder [1 ]));
1796+ }
1797+
1798+ if (forder [2 ].length () > 0 ) {
1799+ face .normIdx .add (Integer .valueOf (forder [2 ]));
1800+ // f.normals.add(getVertex(Integer.valueOf(forder[2])));
1801+ }
1802+ } else if (forder .length > 1 ) {
1803+ if (forder [0 ].length () > 0 ) {
1804+ face .vertIdx .add (Integer .valueOf (forder [0 ]));
1805+ }
1806+
1807+ if (forder [1 ].length () > 0 ) {
1808+ face .texIdx .add (Integer .valueOf (forder [1 ]));
1809+ }
1810+ } else if (forder .length > 0 ) {
1811+ if (forder [0 ].length () > 0 ) {
1812+ face .vertIdx .add (Integer .valueOf (forder [0 ]));
1813+ }
1814+ }
1815+ } else {
1816+ if (seg .length () > 0 ) {
1817+ face .vertIdx .add (Integer .valueOf (seg ));
1818+ }
1819+ }
1820+ }
1821+
1822+ faces .add (face );
1823+
17711824 }
17721825 }
17731826 }
1827+
1828+ if (materials .size () == 0 ) {
1829+ // No materials definition so far. Adding one default material.
1830+ OBJMaterial defMtl = new OBJMaterial ();
1831+ materials .add (defMtl );
1832+ }
17741833
17751834 } catch (Exception e ) {
17761835 e .printStackTrace ();
17771836 }
17781837 }
17791838
17801839
1781- protected void parseMTL (BufferedReader reader , ArrayList <OBJMaterial > materials , Hashtable <String , Integer > mtlHash ) {
1840+ protected void parseMTL (BufferedReader reader , ArrayList <OBJMaterial > materials , Hashtable <String , Integer > materialsHash ) {
17821841 try {
17831842 String line ;
1843+ OBJMaterial currentMtl = null ;
1844+ while ((line = reader .readLine ()) != null ) {
1845+ // Parse the line
1846+ line = line .trim ();
17841847
1785- //Material currentMtl = null ;
1848+ String elements [] = line . split ( " \\ s+" ) ;
17861849
1787- while ((line = reader .readLine ()) != null ) {
1788-
1850+ if (elements .length > 0 ) {
1851+ // Extract the material data.
1852+
1853+ if (elements [0 ].equals ("newmtl" )) {
1854+ // Starting new material.
1855+ String mtlname = elements [1 ];
1856+ currentMtl = new OBJMaterial (mtlname );
1857+ materialsHash .put (mtlname , new Integer (materials .size ()));
1858+ materials .add (currentMtl );
1859+ } else if (elements [0 ].equals ("map_Kd" ) && elements .length > 1 ) {
1860+ // Loading texture map.
1861+ String texname = elements [1 ];
1862+ currentMtl .kdMap = parent .loadImage (texname );
1863+ } else if (elements [0 ].equals ("Ka" ) && elements .length > 3 ) {
1864+ // The ambient color of the material
1865+ currentMtl .ka .x = Float .valueOf (elements [1 ]).floatValue ();
1866+ currentMtl .ka .y = Float .valueOf (elements [2 ]).floatValue ();
1867+ currentMtl .ka .z = Float .valueOf (elements [3 ]).floatValue ();
1868+ } else if (elements [0 ].equals ("Kd" ) && elements .length > 3 ) {
1869+ // The diffuse color of the material
1870+ currentMtl .kd .x = Float .valueOf (elements [1 ]).floatValue ();
1871+ currentMtl .kd .y = Float .valueOf (elements [2 ]).floatValue ();
1872+ currentMtl .kd .z = Float .valueOf (elements [3 ]).floatValue ();
1873+ } else if (elements [0 ].equals ("Ks" ) && elements .length > 3 ) {
1874+ // The specular color weighted by the specular coefficient
1875+ currentMtl .ks .x = Float .valueOf (elements [1 ]).floatValue ();
1876+ currentMtl .ks .y = Float .valueOf (elements [2 ]).floatValue ();
1877+ currentMtl .ks .z = Float .valueOf (elements [3 ]).floatValue ();
1878+ } else if ((elements [0 ].equals ("d" ) || elements [0 ].equals ("Tr" )) && elements .length > 1 ) {
1879+ // Reading the alpha transparency.
1880+ currentMtl .d = Float .valueOf (elements [1 ]).floatValue ();
1881+ } else if (elements [0 ].equals ("ns" ) && elements .length > 1 ) {
1882+ // The specular component of the Phong shading model
1883+ currentMtl .ns = Float .valueOf (elements [1 ]).floatValue ();
1884+ }
1885+
1886+ }
17891887 }
17901888 } catch (Exception e ) {
17911889 e .printStackTrace ();
@@ -1816,22 +1914,43 @@ protected void recordOBJ(ArrayList<PVector> vertices, ArrayList<PVector> normals
18161914 // Recording current face.
18171915 a3d .beginShape ();
18181916 for (int j = 0 ; j < face .vertIdx .size (); j ++){
1819- int vertIdx = face .vertIdx .get (j ).intValue () - 1 ;
1820- int normIdx = face .normIdx .get (j ).intValue () - 1 ;
1821- PVector vert = (PVector ) vertices .get (vertIdx );
1822- PVector norms = (PVector ) normals .get (normIdx );
1917+ int vertIdx , normIdx ;
1918+ PVector vert , norms ;
1919+
1920+ vert = norms = null ;
1921+
1922+ vertIdx = face .vertIdx .get (j ).intValue () - 1 ;
1923+ vert = (PVector ) vertices .get (vertIdx );
1924+
1925+ if (j < face .normIdx .size ()) {
1926+ normIdx = face .normIdx .get (j ).intValue () - 1 ;
1927+ norms = (PVector ) normals .get (normIdx );
1928+ }
18231929
18241930 if (mtl != null && mtl .kdMap != null ) {
18251931 // This face is textured.
1826- int texIdx = face .texIdx .get (j ).intValue () - 1 ;
1827- PVector tex = (PVector ) textures .get (texIdx );
1932+ int texIdx ;
1933+ PVector tex = null ;
1934+
1935+ if (j < face .texIdx .size ()) {
1936+ texIdx = face .texIdx .get (j ).intValue () - 1 ;
1937+ tex = (PVector ) textures .get (texIdx );
1938+ }
18281939
18291940 a3d .texture (mtl .kdMap );
1830- a3d .normal (norms .x , norms .y , norms .z );
1831- a3d .vertex (vert .x , vert .y , vert .z , tex .x , tex .y );
1941+ if (norms != null ) {
1942+ a3d .normal (norms .x , norms .y , norms .z );
1943+ }
1944+ if (tex != null ) {
1945+ a3d .vertex (vert .x , vert .y , vert .z , tex .x , tex .y );
1946+ } else {
1947+ a3d .vertex (vert .x , vert .y , vert .z );
1948+ }
18321949 } else {
18331950 // This face is not textured.
1834- a3d .normal (norms .x , norms .y , norms .z );
1951+ if (norms != null ) {
1952+ a3d .normal (norms .x , norms .y , norms .z );
1953+ }
18351954 a3d .vertex (vert .x , vert .y , vert .z );
18361955 }
18371956 }
@@ -1865,13 +1984,17 @@ protected class OBJMaterial {
18651984 PImage kdMap ;
18661985
18671986 OBJMaterial () {
1868- name = "default" ;
1987+ this ("default" );
1988+ }
1989+
1990+ OBJMaterial (String name ) {
1991+ this .name = name ;
18691992 ka = new PVector (0.5f , 0.5f , 0.5f );
18701993 kd = new PVector (0.5f , 0.5f , 0.5f );
18711994 ks = new PVector (0.5f , 0.5f , 0.5f );
18721995 d = 1.0f ;
18731996 ns = 0.0f ;
18741997 kdMap = null ;
1875- }
1998+ }
18761999 }
18772000}
0 commit comments