Flip mesh in Irricht

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
Donald Duck
Posts: 34
Joined: Sat Jan 21, 2017 6:51 pm
Location: Duckburg
Contact:

Flip mesh in Irricht

Post 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?
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Flip mesh in Irricht

Post 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).
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
Donald Duck
Posts: 34
Joined: Sat Jan 21, 2017 6:51 pm
Location: Duckburg
Contact:

Re: Flip mesh in Irricht

Post 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.
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Flip mesh in Irricht

Post 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.
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
Donald Duck
Posts: 34
Joined: Sat Jan 21, 2017 6:51 pm
Location: Duckburg
Contact:

Re: Flip mesh in Irricht

Post 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;
}
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Flip mesh in Irricht

Post 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!
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
Donald Duck
Posts: 34
Joined: Sat Jan 21, 2017 6:51 pm
Location: Duckburg
Contact:

Re: Flip mesh in Irricht

Post by Donald Duck »

I think option a seems the best. How can I do it?
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Flip mesh in Irricht

Post 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).
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
Donald Duck
Posts: 34
Joined: Sat Jan 21, 2017 6:51 pm
Location: Duckburg
Contact:

Re: Flip mesh in Irricht

Post 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;
}
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Flip mesh in Irricht

Post 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.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Flip mesh in Irricht

Post 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.
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
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Flip mesh in Irricht

Post 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;
}
 
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

Re: Flip mesh in Irricht

Post 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?
Post Reply