Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Loading an obj format file and constructing a 3D object from it

Author
Message
Todd Riggins
20
Years of Service
User Offline
Joined: 29th Oct 2004
Location: Texas, USA
Posted: 8th Dec 2010 09:41 Edited at: 11th Jan 2011 08:16
UPDATE: Jan 11th, 2011
- Updated code below
- Function "LoadObj" now has an extra parameter "SmoothNormals". When set to 1, the object will have it's normals smoothed and be able to be saved out to a .dbo file correctly.
- Fix: changed "local_tci" to "local_ni" when updating VertexData normals.

UPDATE: Jan 3rd, 2011
- Updated code below:
- Some obj files have extra spaces in them for whatever reason.
- Now the parser skips the extra spaces.

Hi,

The OBJ file format is a simple data-format that represents 3D geometry. It is commonly used by various 3D modeling apps. One such app that I like and use is Sculptris. Found at:
http://www.zbrushcentral.com/showthread.php?t=090617

This program reads in the obj text file and stores each line in a dynamic array of strings. During that process, it retrieves the obj 3D data. This site is where I got obj info to help me extract the data I wanted:
http://en.wikipedia.org/wiki/Obj

The only data it currently extracts is the vertex positions, texture coords, normals and face info. It only extracts 3 point triangle faces. The obj can hold 4 point quad face polygons and more( If you need help for me to add at least quad face polygons support, just ask and supply me with an obj file that uses quads ). Sculptris doesn't supply normals when exporting for me( mabie I just don't know how to export normals correctly ). So, I had to use the Set Object Normals and Set Object Smoothing commands to get smoothed normals. Note that saving out to a dbo file after creating normals in this way will not save the normals to the dbo file. I would assume creating a custom function to create normals for the object with the vertex data commands would solve that problem to save out to a dbo correctly.

After extracting the needed data, it creates a glob of polygons needed for building the obj object with. Basically, it starts by creating a triangle object, make a mesh of that object, add then add the needed amount of triangle polygons by adding the mesh triangle to the original object as a limbs. After that, make a new mesh from the object with all the limbs. Then make the final object from the mesh. This is so the object is made out of only 1 mesh of triangles. No limbs. This is what the code looks like:


After creating an object with the needed amount of polygons, it will then manipulate the object's data by reading in the stored obj data and applying it using the DBPro's VertexData Commands.

LoadObj Functions:
UPDATED: Jan 11th, 2011



Note: To render an obj object in a DirectX based program, I had to reverse order the obj's triangle vertices and also reverse the V texture coordinate to display texturing right. Again, note that if you try to load an obj file with normal data, it may not work right.

If you have any problems with this, please let me know. Thanks.

Full Example:
* I've attached the BadGuy1 obj if you need it to test with.
* Press W/S to go Forward/Backwards
* Right Mouse Click to rotate camera view
* Press Esc to escape

UPDATED: Jan 11th, 2011
NOTE: Sculptris create objects large in size. In FUNCTION InitDisplaySetup(), I have positioned the camera -1000 to fully see the badguy1.obj in screen. If you have a small object in size, you might want to position the camera closer.


ExoDev.Com - A Game Development Community Website! Featuring: XBOX360 CONTROLLER LIBRARY

Attachments

Login to view attachments
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 2nd Jan 2011 20:26
Just tried it, great work
I may need to write a C++ version of this soon, I will put it up here if I do.
Great contribution again Todd, thanks.

Todd Riggins
20
Years of Service
User Offline
Joined: 29th Oct 2004
Location: Texas, USA
Posted: 2nd Jan 2011 22:30
Thanks matty!

It's cool that I can then save the object as a .dbo file. But, I just wanted to point out to everyone that loading a Sculptris .obj file that does not have any normal data will show the object in a flat shaded-like mode. I used the Set Object Smoothing command to smooth out the sharp edges and then tried to save the .dbo object that way, but it didn't save it with smoothed normals.

The flat shaded-like look is done because I used 3 point triangle mesh made from the Make Object Triangle command multiple times to build the object with. The triangle has it's normals pointing straight outwards. Looks like custom code is needed to smooth the normals using the vertex data commands for this situation if one wants to save the object out as a .dbo file with smoothed normals.

Again hopefully, if an .obj file contains normal data, the code snippet data above should produce corrects results. I currently don't have an .obj file with normal data to test it with yet.

ExoDev.Com - A Game Development Community Website! Featuring: XBOX360 CONTROLLER LIBRARY
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 3rd Jan 2011 11:56 Edited at: 3rd Jan 2011 17:39
I have done a quick conversion to GDK, I only have vertex positions and normals setup right now and it seems to works great so far.

I did not write the parsing code myself as the PhysX SDK comes with source code for parsing obj files, so I have just converted this bit:


..and the normal part, I will do the rest soon.

I notice that when saving to dbo you still need to set the vertex data yourself after loading it, its not a big problem as creating the limbs is the slowest part and saving it means you only need to do this once.

Also, I have at least one spare triangle still in its original position, not sure if its something I have done wrong or its in both our codes, I will look at it later.
(attached model)


EDIT: Just tried that model with this dbPro version to see if there is a triangle left over, it seems it does not work with this model at all. Although I can tell you that the model is fine and it works on my ported version, so the part where you create the model is fine. The problem is probably somewhere in the parsing of the file.

EDIT2: Looked at the file and its probably this:


Seems .obj files can have slightly different formats for the index values etc

Attachments

Login to view attachments
Todd Riggins
20
Years of Service
User Offline
Joined: 29th Oct 2004
Location: Texas, USA
Posted: 4th Jan 2011 02:28 Edited at: 4th Jan 2011 03:30
I'm able to see the bowl, but i do see how the object is all messed up. Before I look at my code, I'm thinking it has to do with how i'm getting the normals. The code is simply broken for that even though I thought it would handle it.

f 1/1/1

That's stating a face with a vertex index, texture coord index and normal index( in that order ).

I'll attempt to fix this in my dbpro code. Thanks for supplying the .obj with normal data

Quote: "I notice that when saving to dbo you still need to set the vertex data yourself after loading it"


Not sure what you are talking about here. With my badguy1.obj file attached to the first post, I can save it out as a dbo and then load it without having to do anything to it( well except for calling the set object smoothing command to get smooth normals for it ).

EDIT: My parser has a strict spacing rule. I see stuff like:

in the bowl obj file. That's probably the problem right there as the parser only allows one space in between. Lets see if I can do something about that.

EDIT2: Yup, did a manual editing on your bowl obj file and deleted all of the extra spacing (fingers r tired) and it produced a fine looking bowl. Now get the code to work right.

EDIT3: Forgot to say that I did spot a missing triangle from your bowl. I'll look into that too.

ExoDev.Com - A Game Development Community Website! Featuring: XBOX360 CONTROLLER LIBRARY
Todd Riggins
20
Years of Service
User Offline
Joined: 29th Oct 2004
Location: Texas, USA
Posted: 4th Jan 2011 05:00 Edited at: 4th Jan 2011 05:02
Just updated the first post with updated code to skip empty spaces. Also, now I know parsing the normals work correctly.

Matty, after correcting my code, that missing triangle I spotted after manually editing your obj file seems to have corrected itself. Obviously I missed deleting a space somewhere. But now I can load in your bowl_sp.obj and view it correctly.

Is that just an extra polygon that's not needed in which you may have created in your dark gdk version? Or do you see a hole in your bowl where that polygon needs to go to? The obj file seems to be in all in one piece in my dbpro code.

ExoDev.Com - A Game Development Community Website! Featuring: XBOX360 CONTROLLER LIBRARY
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 4th Jan 2011 11:26 Edited at: 4th Jan 2011 11:44
The extra triangle was my code, turned out it was about 1000 extra triangles lol, now fixed.

I will take a closer look at both our codes as my dbo just saves the correct amount of triangles all in the same positions, its probably something I'm doing wrong.

Also, your badguy1 model does not load properly using my parser, the tris are fine but it parses the texture data as normals and then has no texture coords, should this:
f 738/45 613/49 785/44
Not be this:
f 738//45 613//49 785//44

If thats correct its a problem with the exporter in your modelling program, or is it my parser thats the problem?


EDIT: Saving is now working properly, I think I left out the final 'make mesh from object - make object' part.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Jan 2011 19:14
Nice work mate!

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
Todd Riggins
20
Years of Service
User Offline
Joined: 29th Oct 2004
Location: Texas, USA
Posted: 4th Jan 2011 20:16
Thanks Phaelax!


matty, the link: http://en.wikipedia.org/wiki/Wavefront_.obj_file will show that:
f 738/45 613/49 785/44
means a face has vertex/tex coords data with no normals.
And:
f 738//45 613//49 785//44
means a face has vertex and normal data with no tex coords data.

Sculptris saves it's obj file correctly "according" to the wavefront obj file format wiki document I listed above. I don't have an option to change how it saves an obj file anyways.


Quote: "EDIT: Saving is now working properly, I think I left out the final 'make mesh from object - make object' part."


Does that mean you can now load the badguy1 and see it correctly now?

ExoDev.Com - A Game Development Community Website! Featuring: XBOX360 CONTROLLER LIBRARY
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 4th Jan 2011 21:25 Edited at: 4th Jan 2011 21:27
Quote: "f 738//45 613//49 785//44
means a face has vertex and normal data with no tex coords data."


Ok cool, I have the source so I'll try and fix that in the parser code. Your model wont work properly with my parser because of this issue, it assigns the normal data to the texture coords, or the other way around

I've almost finished a small exe that will simplify the whole process for people, when finished I will credit you as I have referenced parts of your code a few times, if thats fine by you?

Todd Riggins
20
Years of Service
User Offline
Joined: 29th Oct 2004
Location: Texas, USA
Posted: 4th Jan 2011 22:56
Quote: "when finished I will credit you as I have referenced parts of your code a few times, if that's fine by you?"


That would be nice but not required. I figure posting code snippets would be public domain. It's up to you and thanks/cheers if ya do.

ExoDev.Com - A Game Development Community Website! Featuring: XBOX360 CONTROLLER LIBRARY
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 10th Jan 2011 17:21
Cool.
You were saying something about smoothing above, I find if you apply smoothing then make mesh-delete object-make object etc then the smoothed normals are saved in the dbo, which is good news as its a slow process.

Todd Riggins
20
Years of Service
User Offline
Joined: 29th Oct 2004
Location: Texas, USA
Posted: 11th Jan 2011 07:48 Edited at: 11th Jan 2011 08:17
Quote: "apply smoothing then make mesh-delete object-make object etc then the smoothed normals are saved in the dbo"


Nice tip! I'll update the first post code with that here in a bit. Thanks

EDIT: First post now updated with latest code

ExoDev.Com - A Game Development Community Website! Featuring: XBOX360 CONTROLLER LIBRARY

Login to post a reply

Server time is: 2024-11-21 11:56:47
Your offset time is: 2024-11-21 11:56:47