Page 1 of 1

Flip mesh in Irricht

Posted: Mon Feb 27, 2017 4:46 pm
by Donald Duck
I need to flip a mesh in Irrlicht. I tried to do so by scaling the X coordinate with -1:

Code: Select all

sceneManager->addMeshSceneNode(sceneManager->getMesh("C:\\example.3ds"), 0, -1, irr::core::vector3df(0, 0, 0), irr::core::vector3df(0, 0, 0), irr::core::vector3df(-1, 1, 1));
This flipped the mesh, but now the faces are visible from the wrong side. What is the proper way of flipping a mesh?

Re: Flip mesh in Irricht

Posted: Mon Feb 27, 2017 7:33 pm
by CuteAlien
Faces visible from the wrong side? You mean inside of a model is now outside? That's slightly confusing - that shouldn't happen (I just did a quick test and also can't reproduce that).

Re: Flip mesh in Irricht

Posted: Mon Feb 27, 2017 8:36 pm
by Donald Duck
OK, here are a few screenshots that show exactly what's happening.

Here is a cube that I made in Blender from two opposite angles:

Image Image

In case it's not really clear from the screenshots, opposite faces are red and orange, white and yellow, blue and green.

Here is what it looks like when I insert it just like that in Irrlicht, with the following code, from a few different angles:

Code: Select all

sceneManager->addMeshSceneNode(sceneManager->getMesh("C:\\example.3ds"));
Image Image

So far, so good. But now I want to flip it to make it look like the following images (note that that blue and green are opposite faces):

Image Image

These aren't actual screenshots, I made this in Paint to show how I want it to look.

Now here is a screenshot of what I get using the code in my original question (that is by scaling the X coordinate with -1):

Image

Here is the code that did that (it's the same code as in the original question):

Code: Select all

sceneManager->addMeshSceneNode(sceneManager->getMesh("C:\\example.3ds"), 0, -1, irr::core::vector3df(0, 0, 0), irr::core::vector3df(0, 0, 0), irr::core::vector3df(-1, 1, 1));
Here, all the faces are where they should be, that is orange on top, blue at the front, green at the back, etc, but the blue and orange faces aren't visible. The faces are visible from the wrong side, that is I should be able to see the blue and orange faces since those are the ones I'm facing but instead I see all the faces behind. This is probably due to face culling (https://learnopengl.com/#!Advanced-OpenGL/Face-culling).

So everything is flipped as it should be, but the faces are pointing in the wrong direction. Of course I could flip them manually in Blender, but I want to use the same mesh several times and sometimes not flipped.

I hope this was clear.

Re: Flip mesh in Irricht

Posted: Mon Feb 27, 2017 8:45 pm
by CuteAlien
Yeah.. it's inside out. Just that makes no sense to me yet. Can you put that exported mesh obj online somewhere? Or send it to me per mail or so.

edit: Also if possible please post the complete code you are using to reproduce this. Because my main suspicion would be that you do something else with the mesh at some other place in the code.

Re: Flip mesh in Irricht

Posted: Mon Feb 27, 2017 9:10 pm
by Donald Duck
OK, here is a ZIP file with the 3DS file inside: http://www.files.com/shared/58b49159bdd0e/untitled.zip

Here is my complete code:

Code: Select all

#include <irrlicht.h>
 
int main(int argc, char **argv){
    irr::IrrlichtDevice *device = irr::createDevice (
                irr::video::EDT_OPENGL,
                irr::core::dimension2d<irr::u32>(800,600),
                32,
                false,
                true,
                false,
                0);
 
    irr::video::IVideoDriver *driver = device->getVideoDriver();
    irr::scene::ISceneManager *sceneManager = device->getSceneManager();
 
    sceneManager->addCameraSceneNode(0, irr::core::vector3df(-3, 3, 0), irr::core::vector3df(0, 0, 0));
 
    irr::scene::IMeshSceneNode *mesh = sceneManager->addMeshSceneNode(sceneManager->getMesh("C:\\Users\\dduck\\Desktop\\untitled.3ds"), 0, -1, irr::core::vector3df(0, 0, 0), irr::core::vector3df(0, 0, 0), irr::core::vector3df(-1, 1, 1));
    mesh->setMaterialFlag(irr::video::EMF_LIGHTING, false);
 
    while(device->run()){
        driver->beginScene(true, true, irr::video::SColor(255, 132, 165, 244));
        sceneManager->drawAll();
        driver->endScene();
    }
 
    return 0;
}

Re: Flip mesh in Irricht

Posted: Mon Feb 27, 2017 9:34 pm
by CuteAlien
Hm, ok, it makes sense. Makes less sense why my quick-test earlier failed. Probably I had some mesh there which had backface culling not enabled or so.

Anyway - yeah, seems scaling 1 or 3 axis with negative values will invert winding order. Sorry, for some reason I seem to have never run into this and didn't know. Guess it makes sense as polygon winding when seen from some direction is now reverted (as mesh is flipped).

So workarounds...
a) flip front/backface culling in material when you have 1 or 3 axis scaled negative. Probably easiest solution.
b) use more than one mesh - so you have flipped/non-flipped variants.
c) If speed is not a problem then disable backface culling (again in material)

And thanks for test-case!

Re: Flip mesh in Irricht

Posted: Mon Feb 27, 2017 9:46 pm
by Donald Duck
I think option a seems the best. How can I do it?

Re: Flip mesh in Irricht

Posted: Mon Feb 27, 2017 10:09 pm
by CuteAlien
Exchange the values of FrontfaceCulling and BackfaceCulling in SMaterial should do that (I hope... never used this...). You access the material with your_node->getMaterial(0) (returns a reference).

Re: Flip mesh in Irricht

Posted: Tue Feb 28, 2017 12:40 pm
by Donald Duck
Thanks. The following code worked:

Code: Select all

irr::scene::IMeshSceneNode *mesh = sceneManager->addMeshSceneNode(sceneManager->getMesh("C:\\example.3ds"), 0, -1, irr::core::vector3df(0, 0, 0), irr::core::vector3df(0, 0, 0), irr::core::vector3df(-1, 1, 1));
for(unsigned int i = 0; i < mesh->getMaterialCount(); i++){
    mesh->getMaterial(i).BackfaceCulling = false;
    mesh->getMaterial(i).FrontfaceCulling = true;
}

Re: Flip mesh in Irricht

Posted: Fri Mar 03, 2017 10:34 am
by Mel
The mesh manipulator of the scene manager can produce meshes of fliped triangles. Scaling by -1 has that it will invert the orientation of the vertices, but not their "winding", so everything would look as if it was inside out. The mesh manipulator is also capable of fliping the triangles as well.

Re: Flip mesh in Irricht

Posted: Fri Mar 03, 2017 1:40 pm
by CuteAlien
Yeah, meshmanipulator can do that. But it's probably slower using that. Unless it's used at the start once to create a flipped mesh-copy.

Re: Flip mesh in Irricht

Posted: Sat Mar 04, 2017 11:11 am
by Mel
If it is for quick usage, then it would be safer to disable the culling of the materials (not to just invert it :)) so only matters the mesh inversion

Code: Select all

 
for(unsigned int i = 0; i < mesh->getMaterialCount(); i++){
    mesh->getMaterial(i).BackfaceCulling = false;
    mesh->getMaterial(i).FrontfaceCulling = false;
}
 

Re: Flip mesh in Irricht

Posted: Sun Mar 05, 2017 5:58 am
by chronologicaldot
I've had meshes appear inside out, but this was because I was using transparency blending and forgot to set the scene manager parameter ALLOW_ZWRITE_ON_TRANSPARENT. Does your blender mesh have transparent material data?