This site requires JavaScript, please enable it in your browser!
Greenfoot back

Comments for OBJ file reader

Return to OBJ file reader

DutaDuta

2013/4/3

Your code for parsing the file seems overcomplicated (and the use of substrings/indexOfs like you're doing atm makes the code hard to read). You could try something like this (I've put in a lot of comments): https://gist.github.com/Duta/5300560
@Duta, thanks for the suggestion! I understand what you are saying. I started working on this a while ago, but only got as far as the file reader. When I got back to it, I COMPLETELY forgot that the file reader actually divided my code up into the proper chunks for me! This made everything vastly more complicated and I used the substrings and such just so I could follow along. I will edit my code to create the vertices and such using these smaller lines. I will not be copying your code directly, but thank you very much for the idea.
Actually, I have looked at my code, and it would require a whole rewrite :( You could post your method. Since I only need to use this code once in my programs, speed and conciseness are not as high a priority, so I am inclined to just stick with this. Besides, it would wind up looking like yours in the end ;)
DutaDuta

2013/4/3

I don't think it'll need a whole rewrite... I opened it up and replaced CheckForVectors, CheckForWalls and readStuff with just parseObjFile. In the constructor, I replaced readStuff() with parseObjFile("Plane.obj"). Then, in draw() I made the replacement I suggested. The only thing you have to change is, in parseObjFile, change the "vertices" and "faces" to say "vectors" and "walls". And it works! :) Also, there appears to be a bug. In rotate, you do "i<vectors.size()-1" - I think this should be "i<vectors.size()" (otherwise it doesn't rotate all of the vectors).
JetLennitJetLennit

2013/4/27

How do I select the obj file?
Duta I actually wrote an obj file parser for my castle storm II engine, maybe you could check it out to get some ideas?
MatheMagicianMatheMagician

2013/4/27

In public Main(), there is this line: FileInputStream fstream = new FileInputStream("Awesome.obj"); Change "Awesome.obj" to whatever your file is called.
JetLennitJetLennit

2013/4/27

Where do i put the file?
DutaDuta

2013/4/27

Builderboy2005, I wasn't in need of one myself, the snippet I gave was just to improve MatheMagician's. I took a look at your Castle Storm II obj file parser just now, and I think I prefer mine (which, before it gets lost in the comments further up is https://gist.github.com/Duta/5300560). It doesn't have material support, but only because I was showing a better route to achieve what MatheMagician was doing, and that doesn't have mat support. To be fair, it would be pretty easy to add material support to my method - if I get round to doing that, I might upload it as a standalone scenario (i.e. as more than just a comment on this one)
Oh whoops, I meant to direct that comment to MatheMagician D: My mistake!
UpupzealotUpupzealot

2013/4/27

Awesome!
DutaDuta

2013/4/28

I didn't get round to adding materials support, but I packaged up my rewrite into a new scenario with a basic model viewer + a head model I made in Blender. It's link is http://www.greenfoot.org/scenarios/7931 in case the code interests either of you (by either of you, I mean MatheMagician & Builderboy)
JetLennitJetLennit

2013/4/28

You put the wrong link
DutaDuta

2013/4/28

Thanks! Yeah, the correct link is http://www.greenfoot.org/scenarios/8159
MatheMagicianMatheMagician

2013/4/28

Oops, that line is actually in readStuff(). Anyway, just put the file in the project file (the one that contains all the classes and their code). Thanks Duta, for pointing out the bug. I will try to fix that.
A new version of this scenario was uploaded on Sun Apr 28 11:29:47 UTC 2013 I fixed the small bug Duta mentioned.
A new version of this scenario was uploaded on Sun Apr 28 11:41:33 UTC 2013 I think I made it read on site.
DutaDuta

2013/4/28

Yeah, it's working :)
Game/maniacGame/maniac

2013/4/28

will you add texturing next?