[Solved!]: writeMesh to obj file with new texture.

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

[Solved!]: writeMesh to obj file with new texture.

Post by thanhle »

Hi Guys,
Write mesh seems to write the original version of the mesh.

For example if I apply a new texture to the existing mesh then export the mesh using OBJ file writer.

The file export is the original file with the original texture. Is the file export using the cached mesh rather than the current mesh?

Is there anyway to update so the mesh pointing to the new texture?


//Updated [solved!]
CuteAlien has kindly added this feature in trunk.

Below is one example of using it correctly.

Since obj writer write the current vertice poistion of the node, which means that the vertices values (tangent, normal etc) to changed. This somehow causes the texture to incorrectly positioned. So the best way to preserve the scale and position is to set the node to 0's pos and rotation, save, then move the node back to the original position.

Note: If you want to save the terrain or other scene node type to obj then you might have to cast is to the correct type before save. You might want to update the function for your intention.

Also you might save it in a different buffer, so you might want to change the function to your needs.

Regards
thanh

Code: Select all

 
 private: void ExportObjFile(ISceneNode *m_node, const irr::io::path &outPath)
         {
             irr::io::IWriteFile *file = smgr->getFileSystem()->createAndWriteFile(outPath, false);
 
             irr::scene::IMeshWriter *writer = smgr->createMeshWriter(irr::scene::EMWT_OBJ);
 
             vector3df nodePos = m_node->getPosition();
             vector3df nodeRot = m_node->getRotation();
 
             m_node->setPosition(vector3df(0));    //Reposition the scenenode at 0.
             m_node->setRotation(vector3df(0));   //Resetrotation the scenenode at 0.
             m_node->updateAbsolutePosition();
 
             if (m_node->getType() != irr::scene::ESCENE_NODE_TYPE::ESNT_ANIMATED_MESH)
             {
 
                 IMeshSceneNode *node = (IMeshSceneNode *)m_node;
                 writer->writeMesh(file, node->getMesh());
             }
             else
             {
 
                 IAnimatedMeshSceneNode *node = (IAnimatedMeshSceneNode *)m_node;
 
                 f32 x, y, x2, y2;
                 node->getMesh()->getMeshBuffer(0)->getMaterial().getTextureMatrix(0).getTextureScale(x, y);
                 node->getMesh()->getMeshBuffer(0)->getMaterial().getTextureMatrix(0).getTextureTranslate(x2, y2);
                 writer->writeMesh(file, node->getMesh());
             }
 
             writer->drop();
             file->drop();
 
             m_node->setRotation(nodeRot);    //Reposition the scenenode to original pos.
             m_node->setPosition(nodePos);    //Reset rotation of the scenenode to original rotation.
             m_node->updateAbsolutePosition();
         }
 
 
 

Thanks
thanh
Last edited by thanhle on Sun Jan 18, 2015 9:01 am, edited 2 times in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: writeMesh to obj file with new texture.

Post by CuteAlien »

By default materials are used per node - meaning scenenodes make a copy of materials when they are created. So maybe you didn't change the mesh-material but the node material. You can access the mesh-materials with node->getMesh()->getMeshbuffer(index)->getMaterial().
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: writeMesh to obj file with new texture.

Post by thanhle »

Thanks CuteAlien,
That works, but the scene node render doesn't update or not showing the added texture. The texture only show up when export.
I end up have to do the below to have the above behavior:

node->getMaterial(0).setTexture(0, NVudu->smgr->getVideoDriver()->getTexture(filename));
node->getMesh()->getMeshBuffer(0)->getMaterial().setTexture(0, NVudu->smgr->getVideoDriver()->getTexture(filename));

One more question.
I have scaled and translate the texture in the process. Is there an option to save the object with the new texture scale and translation?

I can see that inside obj meshwriter
after file->write("map_Kd ", 7); we can add

file->write("-o ", 3); //translate
file->write("translate components u v w", textlength);
file->write("-s ", 3); //scale
file->write("scale components u v w", textlength);

Is it possible to incorporate this in the next rev?

Thanks
thanh
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: writeMesh to obj file with new texture.

Post by CuteAlien »

You mean you use a texture-matrix? On a quick look to the obj reader it seems it reads that but ignores the parameters unless I'm missing something. Would be nice to add that stuff, but only chance is really if someone gives me a complete patch + a testcase. I won't find time to code it myself anytime soon.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: writeMesh to obj file with new texture.

Post by thanhle »

Hi CuteAlien,

I don't know how to create a patch, so I added minor change to the COBJMeshFileLoader and Writer files.

The bellow changes should allow to save texture scaling and translation as well as loading them in.

Edited:
Hi guys CuteAlien have update Irrlicht to allow this. See my first post on how to correctly use the writer.


Regards
thanh
Last edited by thanhle on Sun Jan 18, 2015 6:40 am, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [Solved!]: writeMesh to obj file with new texture.

Post by CuteAlien »

Thanks, on a quick look it seems that it's an OK way to do that. Thought I can't tell without reading obj-format documenation if it really should only be used for type 0 (but better than before anyway).
I will have to check the format documentation if it's really fine - I hope I find some time next weekend for it.

edit: Btw, if you work with Irrlicht svn you can use svn to create patches. The command "svn diff" will do that.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [Solved!]: writeMesh to obj file with new texture.

Post by CuteAlien »

*sigh* I never learn that doing things quickly is impossibly in programming. Anyway - I've applied the patch with a few changes to svn trunk r5029.

Some hints for further patches (which mainly would make my life easier ^_^):
- Generally better to patch against Irrlicht trunk. We can only add changes to released versions which will not cause any troubles when someone replaces the dll (in this case someone could get scaled textures in his game after a dll update as that parameter was ignored before). So only patches against release versions are usually bugfixes (and even there we have to be careful).
- Don't add beauty changes in a patch like adding whitespace. It makes it harder to apply the patch as we have to figure out what the real changes are.
- Don't add unrelated changes without mentioning it. For example in your case you added a change to createAndWriteFile. This part had been rewritten in newer Irrlicht, not sure right now which solution is better. But your solution wouldn't work on Linux (you have to use '/' not '\\' for portable code) so I keep the new Irrlicht solution for now.
- And the no.1 thing about patches: Please, please write a test. Code that is not tested is broken at least 90% of the time no matter how trivial the change! In this case for example testing showed me several bugs (negative numbers didn't work and the texture scale and translation functions are different from getScale, getTranslation used for node transformations so you had written different numbers than you did read).

Note that we still don't get the exact same .mtl's on writing than on reading. Although I think they should show up similar inside Irrlicht for simple cases. For example we don't write different types yet it seems - so some multitexture stuff might not work. Also we change diffuse color values (to work around some problem I think).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: [Solved!]: writeMesh to obj file with new texture.

Post by thanhle »

Hi mate,
Sorry for the trouble.
I only updated the changes that makes it work in my editor in windows. Sorry I didnt test it in linux.

If you read from beginning, I have to update to two things. Firstly, I update the scene node texture to update the node visualizasion. Secondly, I have to update the texture of the current mesh instance in order for us to save the changes.

I didn't test -ve values. Since in practice -ve values should never be used, unless manually entry. It would be good just detect -ve values and prompt the user of the error.

The getScale was the scale from texture transform matrix so I guess it is uses for texture. It does get the texture scale values. Yup make sure you change the scale and rel translation of the texture, see wiki for tutorial on setting texture u v scale.

When saving the node, I want the mat file to be written to the same directory as the obj file rather than inside the bin folder like the previous Irrlitch writer.

I'll send a few test code lines when I get home.
Regards
thanh
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [Solved!]: writeMesh to obj file with new texture.

Post by CuteAlien »

No worries, I was still very glad about your code! And I wrote my own test obviously by now - that's how I found the minor troubles (https://code.google.com/p/irr-playgroun ... dwrite.cpp and some manual tests by playing with the corresponding mtl file). But getScale was really not correct - if you check the implementation of that function you'll see. If you got the correct value then you got that accidental. Negative values might make sense sometimes, so no problem allowing them. It all works already in svn trunk - just check-out the newest version.

And mat file is also written to the same folder in svn Irrlicht versions, that's been fixed some time ago already.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: [Solved!]: writeMesh to obj file with new texture.

Post by thanhle »

Thanks CutieAlien,
Yup your right. Somehow that get scale refer to something else although it get the values from the texture matrix.
I'm glad now we have textureGetScale and textureGetTranslation, which is the necessary function to achieve those.

I'm sure will use SVN software next time for patch proposal.

Thanks mate.
thanh
Post Reply