Drawing an OBJ from structs

Member
Posts: 281
Joined: 2009.04
Post: #16
Erm, well anything above 1,000 would waste memory. I guess I'll use 1,000.

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #17
I had a little time to screw around this evening and whipped together a little obj loader you might be able to use. You can find it here.

Don't expect too much because it isn't well-tested and it's not the best way to go about loading obj models, but it's about as simple as I could think to come up with and still cover enough features to make it generally useful on a basic level. It appears to work with a reasonable amount of obj files that I tested.

I noticed that your laser carbine doesn't have anything but vertices so you should probably output some normals and/or texture coordinates for it in your 3D editor. As-is, it'll just render all white. Also, it won't recognize file names with spaces in them, so make sure Laser Carbine is something like LaserCarbine or Laser_Carbine instead, so that it'll pick up any mtl file you generate for it.

Anyway, I hope it helps.
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #18
Have you tried my OBJ loader? It is here:

http://www.computer-graphics.se/TSBK07.html

under "Laborations", Lab 2, or directly as

http://www.computer-graphics.se/TSBK07-f...8-2.tar.gz

In the same lab material, there are also a few OBJs that we know works with this code. Used successfully by hundreds of students, so I would call it tested.

A newer variant of it is in this archive:

http://www.ragnemalm.se/lightweight/OpenGL-demos.zip

in the folder "LoadOBJDemo". It is not as well tested as the one above, but IMHO better structured, and complete with drawing code.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #19
Ingemar Wrote:A newer variant of it is in this archive:

http://www.ragnemalm.se/lightweight/OpenGL-demos.zip

in the folder "LoadOBJDemo". It is not as well tested as the one above, but IMHO better structured, and complete with drawing code.

Yes, that one is quite a bit better than mine in several areas, and handles many of the issues that my full size version does. In comparison to my simple obj loader, it does tessellation of n-sided polygons, although it doesn't *appear* to handle non-convex polygons, which is why I use gluTess instead of doing it myself. Also has a more generalized parser like my full version, whereas my simple obj loader only handles a few cases. Also, it will calculate normals and mine doesn't, which isn't a terribly big deal IMHO, since those should be output by the DCC tool anyway. Still, I should probably add normal calcs to mine too since it isn't terribly difficult.

But what is *really* cool about yours is that it has a hash table system for duplicate vertex removal and thus conversion to glDrawElement friendly format. I think I'll have to steal that if you don't mind Wink My full version obj loader uses NSDictionary for the hash table to convert to glDrawElements. It works fantastic, but it takes like seven seconds for about 50 thousand triangles -- which isn't really a problem for me since I like to do off-line processing to my own format, but still... Your implementation looks wicked fast by comparison, and isn't tied to Cocoa.

The only thing that appears to be missing from yours is material/texture loading.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #20
OK, I downloaded AnotherJake's OBJ loader, and it loads the cube.obj fine. However, when it draws LaserCarbine.obj, there is no textures.

Here's the start of LaserCarbine.obj:

(It contains no vt's?)

Code:
#Produced by Art of Illusion 2.7.2, Wed Jul 08 16:28:40 BST 2009
mtllib LaserCarbine.mtl
g PolyMesh_1
usemtl Black
v 0.07183 0.11517 0.17757
v 0.07755 0.02938 0.0043
v 0.07755 0.02938 -0.20912
v 0.07183 0.11517 -0.17945
v -0.0375 0.1887 0.26009
v 0.06529 0.1887 0.26009
v 0.06529 0.1887 -0.24044
v -0.0375 0.1887 -0.24044
v -0.02033 0.02442 0.05972
v -0.02033 0.02442 0.23015
v -0.02033 0.09442 0.23015
v 0.05912 0.02239 -0.25644
v 0.05912 0.11285 -0.25644
v 0.06529 0.11901 -0.24044
v -0.01181 0.01622 -0.24044
v -0.03134 0.02239 -0.25644
v -0.0375 0.11901 -0.24044
v -0.03134 0.11285 -0.25644
v -0.01196 0.12272 -0.27044
v 0.03974 0.12272 -0.27044
# And so on...

And LaserCarbine.mtl:

Code:
#Produced by Art of Illusion 2.7.2, Wed Jul 08 16:28:40 BST 2009
newmtl
Kd 0.41539 0.41555 0.30355
Ks 0.03406 0.03406 0.03406
Ka 0 0 0
illum 2
Ns 30

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #21
That's correct, because your mtllib doesn't specify a texture, and your obj file doesn't specify texture coordinates. Heck it doesn't even specify normals either, so you don't get to use lighting. If you download Ingemar's demo from that last link, that loader will automatically calculate your normals so you'd at least get lighting, but it won't automatically calculate texture coordinates (because that is largely impractical), so you still wouldn't get texturing (not to mention that loader doesn't handle materials anyway).

Your best course of action is to go back into Art of Illusion and figure out how to add lighting and texturing to your lasercarbine. The obj file needs to have vn (vertex normals) and vt (vertex texture) coordinates in addition to the v (vertex) coordinates. If you supply a texture, the mtl file will need to have something like this in it: map_Kd myTexture.png
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #22
Hmm... Well, trying out Art of Illusion, it looks like it always exports vertex normals, although there is a checkbox in the export to have it always write surface normals -- doesn't seem to matter if I check it or not.

Also, I didn't try it, but I assume that the texture coordinates are only written out if you actually texture the object.

Strangely, looking at your obj snippet that you posted above, it specifies the LaserCarbine.mtl, and then says to use material Black, but Black isn't defined in the mtl file. If that's true, then it appears to be a bug in Art of Illusion. However, you could actually edit the mtl file yourself if needed. If that material is supposed to be Black, then you could modify where it says

newmtl

To:

newmtl Black

and that should be picked up by the loader then.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #23
Oops... Found a little bug. I uploaded a newer version (0.1.1).

I tested your lasercarbine using output from Art of Illusion and I hand modified the mtl as I suggested above, and the gun looks right for me, so you should be able to get it working for you too.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #24
Right, the mtl Black part I made, hoping it would (somehow) connect to my original textures?!?

Art of Illusion DOES NOT export normals or tex coords. Would it not be impossible to hand code 3000+ lines of vt's, even if I did mess around with the MTL?

I tried exporting it to Blender, but none of AoI's exporting formats match. Sad

What am I supposed to do now?!!?!?

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #25
Ingemar:

Hey, I just did some testing of your LoadObjDemo with my test models, and here are the only issues I found:

1) doesn't handle materials/textures (as mentioned earlier)
2) doesn't work with obj files that have negative (relative) indices
3) again as mentioned earlier, it indeed does not work with obj files which contain non-convex polygons. gluTess handles this correctly though, so that should be easy to implement if you wanted to. The non-convex polygon thing is not covered in the spec AFAIK though, so technically you could skip it on that basis. Of course, many obj files don't follow the spec anyway, so ...

Other than those issues, it loads all the same stuff that my full loader does, but a *lot* faster.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #26
mikey Wrote:Right, the mtl Black part I made, hoping it would (somehow) connect to my original textures?!?

Art of Illusion DOES NOT export normals or tex coords. Would it not be impossible to hand code 3000+ lines of vt's, even if I did mess around with the MTL?

I tried exporting it to Blender, but none of AoI's exporting formats match. Sad

What am I supposed to do now?!!?!?

Hmm... Well, I know for a fact that it at least exports normals because it did so for me just now. I totally do not understand why it won't for you. I don't know enough about AoI though.

Yeah, I wouldn't bother doing any of that by hand. We'll figure this out, one way or another. Like I said, I can export from AoI and get the normals and material working just fine with SimpleObjLoader on my end, so we just need to figure out why AoI is giving you grief right now.

It says you're using AoI 2.7.2 and so am I, so that double confuses me as to why it won't spit out normals for you. ... need to think on it a bit I suppose...
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #27
Oh, hang on -

It does export normals after all, I've been looking at Xcode's OBJ, not the one exported by AoI. And I think I've been getting lighting now too. Still there are no vt's in the OBJ. I don't see (if I have to put these in , do I???) how I can produce these.

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #28
If you want texture coordinates you're going to need to UV unwrap the model. I've never used AoI, but this looks like the way to go: http://www.housepixels.com/aoimanual/tex...tml#uv_map

Blender has gained some nice UV tools in recent releases. Select verts in edit mode and hit U for the unwrap tools. Set a splitscreen view to UV/Image Editor for easy viewing.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #29
mikey Wrote:Still there are no vt's in the OBJ. I don't see (if I have to put these in , do I???) how I can produce these.

Depends on the 3D editor. Cheetah3D puts them in regardless, but I don't know about Art of Illusion. My assumption is that it won't include texture coordinates until you texture map your object in the editor.
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #30
AnotherJake Wrote:Ingemar:

Hey, I just did some testing of your LoadObjDemo with my test models, and here are the only issues I found:

1) doesn't handle materials/textures (as mentioned earlier)
2) doesn't work with obj files that have negative (relative) indices
3) again as mentioned earlier, it indeed does not work with obj files which contain non-convex polygons. gluTess handles this correctly though, so that should be easy to implement if you wanted to. The non-convex polygon thing is not covered in the spec AFAIK though, so technically you could skip it on that basis. Of course, many obj files don't follow the spec anyway, so ...

Other than those issues, it loads all the same stuff that my full loader does, but a *lot* faster.

Sounds good! Great points, I'll have to consider them for the next revision. IMHO, material support is the important thing.

Like you say, the important thing is straightening out the data so it can be passed as array. If I managed to get the speed up, the better.

I have a super-simple OBJ loader somewhere as well, using immediate mode so it can draw directly basically from the raw OBJ data. Such a loader can be done in much less space, but it won't be fast (but a display list helps, of course).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Weird behaviour with structs and the = operand Jones 2 2,590 Oct 12, 2006 07:11 PM
Last Post: Jones
  Free Structs Justin Brimm 4 3,376 May 30, 2006 06:20 AM
Last Post: Jones