Created obj + mtl loader
-
- Posts: 22
- Joined: Fri Dec 30, 2005 2:00 am
- Location: Singapore
- Contact:
Created obj + mtl loader
I've created a wavefront obj mesh loader that loads mtl file information as well. If anyone's interested, you can get more details as well as the download here:
http://loonychewy.dyndns.org/programmin ... chtobj.php
As my website may not be up all the time, anyone so inclined may also help me to host the files.
Edit:
Here's a permanent link where you can get the file
http://gdlib.net/index.php?name=Downloa ... ails&id=72
Edit 2:
Updated the permanent link above with the patches submitted by CuteAlien, hybrid and mandrav in this thread
Edit 3:
v1.11: Updated against Irrlicht 1.0
http://loonychewy.dyndns.org/programmin ... chtobj.php
As my website may not be up all the time, anyone so inclined may also help me to host the files.
Edit:
Here's a permanent link where you can get the file
http://gdlib.net/index.php?name=Downloa ... ails&id=72
Edit 2:
Updated the permanent link above with the patches submitted by CuteAlien, hybrid and mandrav in this thread
Edit 3:
v1.11: Updated against Irrlicht 1.0
Last edited by loonychewy on Tue Jul 11, 2006 11:37 pm, edited 3 times in total.
wow, excellent!
you can perm-a-host it here if you like: http://gdlib.net/
I'll add it myself if you want, but that would mean that you can't apply changes. I hope this code makes it into irrlicht 0.15!
you can perm-a-host it here if you like: http://gdlib.net/
I'll add it myself if you want, but that would mean that you can't apply changes. I hope this code makes it into irrlicht 0.15!
Another feature which I think might make a lot of people happy would be the ability to load "OBJ+OBJ" style lightmapping, where a model has two OBJs and two MTLs -- one contains texture coordinates/info, the other contains lightmap coordinates/info. I almost did it myself at one point, but I don't really have that much inclination, since I just use MIM.
-
- Posts: 313
- Joined: Tue Nov 01, 2005 5:01 am
-
- Posts: 22
- Joined: Fri Dec 30, 2005 2:00 am
- Location: Singapore
- Contact:
Thanks all for your encouragement
bitplane:
I've just submitted it to gdlib.net. Though I don't forsee any problems with anybody else applying any changes to it as they see fit.
Murphy:
I've not seen this usage of obj files before. But if there's any demand for it, it could be something interesting for me to work on (need samples though)
bitplane:
I've just submitted it to gdlib.net. Though I don't forsee any problems with anybody else applying any changes to it as they see fit.
Murphy:
I've not seen this usage of obj files before. But if there's any demand for it, it could be something interesting for me to work on (need samples though)
what murphy mentions is rather useful
A lightmap is like all ilumination and shadows baked into a tga(for example). That way, the engine does not calculate complex cpu heavy light calculations: it just overlays in somewhat multiply mode a B/w with greys bitmap that does the effect perfectly. The rendering of that tga is done in whatever the package. You bake then GI , rich lighting in your renderer, and export that mesh with it's UV mapping and a lightmap bitmap asigned to it. Softwares like Max or Gile[s] can do this internaly and output a double chanel mesh file (you need two uv channels: one for texture(s), one for lightmap(s)) .But besides few formats have this support (obj is great, but doesnt) , few tools do support this 2 uv channel thing. Blededr doesn't. But if for example you bake the lighting in blender into a tga -is possible- and export the OBJ with the tga asigned to its simple channel 1, as if it were a texture. And then export as another obj the same scene, just with its real texture asigned to again uv channel 1, well, you will have 2 objs, one with its UVs and texture, and another with it's lightmap (actually a tga or bmp, etc) and its UVs (can be the same, but often is useful do another)
What Murphy did was a tool called OBJ2MIM that took those 2 files and converted into an xml file , *.mim , where you could even edit the type of multiply mode to use for the lightmap...Was freaking useful.
I guess what he's suggesting now is you do add this support directly for the artist. he made for the testing a viewer, which was actually irrlicht 0.7, which I used to go back and force testing new changes to the lightmap and texture, etc, at speed of light. Was so fast that not much of a difference than if it were a full packge. Well, if all's done in a package, good, but actually was more direct: at the end, there's a need to export to real engine, so , I prefered to do the preview with the engine than rendering in the 3d tool viewport, often.
MY3d and other solutions are great. I worked so at companies, with similar plugins.
But the OBJ solution is uber flexible: close to all 3d packages can output an obj+uvmapping+texture. And is all required (and that tool can bake the lighting into uv, but there are a few able already)
Also, is a solution for the poor
A lightmap is like all ilumination and shadows baked into a tga(for example). That way, the engine does not calculate complex cpu heavy light calculations: it just overlays in somewhat multiply mode a B/w with greys bitmap that does the effect perfectly. The rendering of that tga is done in whatever the package. You bake then GI , rich lighting in your renderer, and export that mesh with it's UV mapping and a lightmap bitmap asigned to it. Softwares like Max or Gile[s] can do this internaly and output a double chanel mesh file (you need two uv channels: one for texture(s), one for lightmap(s)) .But besides few formats have this support (obj is great, but doesnt) , few tools do support this 2 uv channel thing. Blededr doesn't. But if for example you bake the lighting in blender into a tga -is possible- and export the OBJ with the tga asigned to its simple channel 1, as if it were a texture. And then export as another obj the same scene, just with its real texture asigned to again uv channel 1, well, you will have 2 objs, one with its UVs and texture, and another with it's lightmap (actually a tga or bmp, etc) and its UVs (can be the same, but often is useful do another)
What Murphy did was a tool called OBJ2MIM that took those 2 files and converted into an xml file , *.mim , where you could even edit the type of multiply mode to use for the lightmap...Was freaking useful.
I guess what he's suggesting now is you do add this support directly for the artist. he made for the testing a viewer, which was actually irrlicht 0.7, which I used to go back and force testing new changes to the lightmap and texture, etc, at speed of light. Was so fast that not much of a difference than if it were a full packge. Well, if all's done in a package, good, but actually was more direct: at the end, there's a need to export to real engine, so , I prefered to do the preview with the engine than rendering in the 3d tool viewport, often.
MY3d and other solutions are great. I worked so at companies, with similar plugins.
But the OBJ solution is uber flexible: close to all 3d packages can output an obj+uvmapping+texture. And is all required (and that tool can bake the lighting into uv, but there are a few able already)
Also, is a solution for the poor
Finally making games again!
http://www.konekogames.com
http://www.konekogames.com
Very nice!
I've made some minor additions:
My meshes have several materials assigned per group and so i added a hack to display them all. Each time a group gets assigned a new material i just create a new group.
Not really a patch (i still have to find out how to make them, never used patch before), but just add the + lines where the usemtl flag is set:
// if there's a group being built up now,
// get name of material
pBufPtr = goAndCopyNextWord(wordBuffer, pBufPtr, WORD_BUFFER_LENGTH, pBufEnd);
+ // create a new group
+ core::stringc groupNew = pCurrGroup->name;
+ groupNew.append(wordBuffer);
+ pCurrGroup = findOrAddGroup(wordBuffer);
// retrieve the material
pCurrMtl = findMtl( wordBuffer );
Also after loading the mesh i had to flip surfaces and mirror the mesh:
scene::IAnimatedMesh* m = mSceneManager->getMesh(filename);
for ( int i=0; i < m->getFrameCount (); ++i ) {
mSceneManager->getMeshManipulator()->flipSurfaces( m->getMesh(i) );
mSceneManager->getMeshManipulator()->scaleMesh(m->getMesh(i),core::vector3df (-1, 1, 1));
}
I hope to fix the other problems which i still have soon (i just started using the engine, so it may take a while):
- My textures are still mirrored
- Maya seems to use an extra alpha channel for textures which is ignored so far. I try to write the alpha values directly in the textures and search for fix so this value will be used (probably something with the material flags)
I've made some minor additions:
My meshes have several materials assigned per group and so i added a hack to display them all. Each time a group gets assigned a new material i just create a new group.
Not really a patch (i still have to find out how to make them, never used patch before), but just add the + lines where the usemtl flag is set:
// if there's a group being built up now,
// get name of material
pBufPtr = goAndCopyNextWord(wordBuffer, pBufPtr, WORD_BUFFER_LENGTH, pBufEnd);
+ // create a new group
+ core::stringc groupNew = pCurrGroup->name;
+ groupNew.append(wordBuffer);
+ pCurrGroup = findOrAddGroup(wordBuffer);
// retrieve the material
pCurrMtl = findMtl( wordBuffer );
Also after loading the mesh i had to flip surfaces and mirror the mesh:
scene::IAnimatedMesh* m = mSceneManager->getMesh(filename);
for ( int i=0; i < m->getFrameCount (); ++i ) {
mSceneManager->getMeshManipulator()->flipSurfaces( m->getMesh(i) );
mSceneManager->getMeshManipulator()->scaleMesh(m->getMesh(i),core::vector3df (-1, 1, 1));
}
I hope to fix the other problems which i still have soon (i just started using the engine, so it may take a while):
- My textures are still mirrored
- Maya seems to use an extra alpha channel for textures which is ignored so far. I try to write the alpha values directly in the textures and search for fix so this value will be used (probably something with the material flags)
I have added some important parts to the MTL loader, such as several texture map support, better coloring and other things. The new version can be found as a patch file at http://parsys.informatik.uni-oldenburg. ... d/irrlicht
I did not yet include the patch by CuteAlien as this seems to introduce too many mesh buffers. I will check for a better solution which will even support materials without groups (which seems to be allowed - I have seen some files using lots of usemtl statements spread through the face list) I guess that we only have to change from group->MeshBuffer to material->MeshBuffer, but I'll see.
I did not yet include the patch by CuteAlien as this seems to introduce too many mesh buffers. I will check for a better solution which will even support materials without groups (which seems to be allowed - I have seen some files using lots of usemtl statements spread through the face list) I guess that we only have to change from group->MeshBuffer to material->MeshBuffer, but I'll see.
Nice work hybrid. My patch really is just a hack that "works", but so far i'm not deep enough in that stuff to provide a good solution and i'd be very glad if this could be done with less buffers :-)
But i want to give feedback about another thing: In readColor you invert all the values (255-color*255) and at least the ambient colors (Ka) seem to work only correct for me if i don't do that (haven't checked diffuse and specular). I don't know which way is correct - i just get my .obj from a graphican and maybe he does strange stuff to them and i don't know much about the .obj format myself.
But i want to give feedback about another thing: In readColor you invert all the values (255-color*255) and at least the ambient colors (Ka) seem to work only correct for me if i don't do that (haven't checked diffuse and specular). I don't know which way is correct - i just get my .obj from a graphican and maybe he does strange stuff to them and i don't know much about the .obj format myself.
Hehe, there was just someone complaining about .obj files inverting the colors without any patch
I did not find a good source for this, but my models all had wrong colors without inverting. Though, most are still too dark...
I did not yet fix the material/group thing, but I just read that faces may even belong to several groups. So I will go for the material=meshbuffer solution I think. Groups seem to be relevant for animation (or editing) only.
I did not find a good source for this, but my models all had wrong colors without inverting. Though, most are still too dark...
I did not yet fix the material/group thing, but I just read that faces may even belong to several groups. So I will go for the material=meshbuffer solution I think. Groups seem to be relevant for animation (or editing) only.
I found a page with some description of the .mtl format. Thought no word about colors inversed or not inversed: http://www.csit.fsu.edu/~burkardt/data/mtl/mtl.html
For Alpha's it seems the default 1.0 in Maya means no transparence at all, so this seems to be inversed.
I guess it would be a good idea to create new materials with the default values given there (not sure about alpha's... default would be 0 - but 255 looks fine in my testcase):
pCurrMaterial->material.AmbientColor = video::SColor(255, (s32)(0.2*255.f), (s32)(0.2*255.f), (s32)(0.2*255.f));
pCurrMaterial->material.DiffuseColor = video::SColor(255, (s32)(0.8*255.f), (s32)(0.8*255.f), (s32)(0.8*255.f));
pCurrMaterial->material.Shininess = 0.f;
Also i always use the following default values in my code when creating new materials:
// To get the full range of transparent values (i think default in Irrlicht is 0.5 which means alpha-values below 127 will just be completly transparent)
pCurrMaterial->material.MaterialTypeParam = 0.02f;
// Without any emissive color set, the ambience color does not influence my modells.
// Maybe a problem in my own code, but when setting emissive it works.
pCurrMaterial->material.EmissiveColor = video::SColor(127,127,127,127);
For Alpha's it seems the default 1.0 in Maya means no transparence at all, so this seems to be inversed.
I guess it would be a good idea to create new materials with the default values given there (not sure about alpha's... default would be 0 - but 255 looks fine in my testcase):
pCurrMaterial->material.AmbientColor = video::SColor(255, (s32)(0.2*255.f), (s32)(0.2*255.f), (s32)(0.2*255.f));
pCurrMaterial->material.DiffuseColor = video::SColor(255, (s32)(0.8*255.f), (s32)(0.8*255.f), (s32)(0.8*255.f));
pCurrMaterial->material.Shininess = 0.f;
Also i always use the following default values in my code when creating new materials:
// To get the full range of transparent values (i think default in Irrlicht is 0.5 which means alpha-values below 127 will just be completly transparent)
pCurrMaterial->material.MaterialTypeParam = 0.02f;
// Without any emissive color set, the ambience color does not influence my modells.
// Maybe a problem in my own code, but when setting emissive it works.
pCurrMaterial->material.EmissiveColor = video::SColor(127,127,127,127);