Samll bug in OBJLoader

Hi guys,



I’m using this OBJ file, which has a comment of 0 length, as follows:



[java]

Max2Obj Version 4.0 Mar 10th, 2001

#

object Plane01 to come …

#

v -0.085781 0.378683 0.720427

v -0.089537 0.369320 0.726735

[/java]



When trying to parse this file, the OBJLoader skipped the first line right after the 0 length comment. The culprit turns out to be in the line skipping logic, which is in the nextStatement() method:



[java]

protected void nextStatement(){

scan.useDelimiter(nl);

scan.next();

scan.useDelimiter(ws);

}

[/java]



If the pointer is at the end of line already, this will skip the next line entirely. To fix this, we can use the following regular expression instead:



[java]

protected void nextStatement(){

scan.skip(".*n");

}

[/java]



Here is my diff patch:

[patch]

This patch file was generated by NetBeans IDE

It uses platform neutral UTF-8 encoding and n newlines.

— Base (BASE)

+++ Locally Modified (Based On LOCAL)

@@ -334,13 +334,8 @@

}

}


  • private static final Pattern nl = Pattern.compile("n");
  • private static final Pattern ws = Pattern.compile("\p{javaWhitespace}+");

    -

    protected void nextStatement(){
  •    scan.useDelimiter(nl);<br />
    
  •    scan.next();<br />
    
  •    scan.useDelimiter(ws);<br />
    
  •    scan.skip(&quot;.*n&quot;);<br />
    

}



protected boolean readLine() throws IOException{

[/patch]

2 Likes

Cool. +1 :D.

glaucomardano said:
Cool. +1 :D.

glaucomardano this bug effected you too?
Momoko_Fan said:
glaucomardano this bug effected you too?


@Mamoko_Fan: NO! I don't use .obj extensions :D. You are the OBJLoader creator, right?

I tested this patch and it seems to fail on files that use windows style line breaks (e.g. \r\n instead of \n).

If you give me the windows file, I’ll be able to fix it.

Ok here it is

http://www.jmonkeyengine.com/downloads/test.obj

This should do it…



[patch]

This patch file was generated by NetBeans IDE

It uses platform neutral UTF-8 encoding and n newlines.

— Base (BASE)

+++ Locally Modified (Based On LOCAL)

@@ -334,13 +334,8 @@

}

}


  • private static final Pattern nl = Pattern.compile("n");
  • private static final Pattern ws = Pattern.compile("\p{javaWhitespace}+");

    -

    protected void nextStatement(){
  •    scan.useDelimiter(nl);<br />
    
  •    scan.next();<br />
    
  •    scan.useDelimiter(ws);<br />
    
  •    scan.skip(&quot;.*r{0,1}n&quot;);<br />
    

}



protected boolean readLine() throws IOException{

[/patch]

Fixed in SVN

1 Like

Awesome! Will update svn now. Thanks.