Loading mesh from .obj

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.
tobix10
Posts: 15
Joined: Thu Feb 26, 2015 5:34 pm

Loading mesh from .obj

Post by tobix10 »

Hello,
I have strange problem. After creating simple cube in DeleD editor, texturing it inside cube and exporting to obj (with normals - faces) irrlicht fails on loading it. There is assertion break in getMesh(), line msh = MeshLoaderList->createMesh(file); when calling [] operator. Without faces it works.
Normals in .obj:
vn 0 0 -1
vn 1 0 0
vn 0 0 1
vn -1 0 0
vn 0 1 0
vn 0 -1 0

looks good ;).

I created cube with these steps:
- create cube 1k x 4k x 4k
- hollow it
- select faces (interior walls) and put textures
- export (default options - without triangulation and normals)

Also I am not sure about setting normals in DeleD. I hope that someone on this forum could tell me how to properly texture walls in this simple example or maybe everything is done ok? After loading I've got object but with nothing inside it (no walls, textures). When I move camera far away I see it textured, but as I said this cube will act as a room.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Loading mesh from .obj

Post by CuteAlien »

The normals are not the problem I guess. When do you get this crash - is this your own code or is this for example in the meshviewer example? Please post the exact error - don't describe it, but simply copy&paste all the info you get. You stripped out the line-number and gave no callstack which removed the most useful information. If this is your own code please post the code.
And in case it's a problem with the model we will need the model to reproduce the bug.

Also in all bug reports it always help to tell which Irrlich version you are using and on which OS you are working.
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
tobix10
Posts: 15
Joined: Thu Feb 26, 2015 5:34 pm

Re: Loading mesh from .obj

Post by tobix10 »

Windows 7 64bit
irrlicht 1.8.1 (compiled without dx support)
I get error both in mesh viewer and custom project.

demo_sound triggered breakpoint in:

Code: Select all

//! Direct access operator
T& operator [](u32 index)
{
    _IRR_DEBUG_BREAK_IF(index>=used) // access violation
 
    return data[index];
}
values: index = used = 6;

Call stack
> Irrlicht.dll!irr::core::array<irr::core::vector3d<float>,irr::core::irrAllocator<irr::core::vector3d<float> > >::operator[](unsigned int index) Line 317 C++
Irrlicht.dll!irr::scene::COBJMeshFileLoader::createMesh(irr::io::IReadFile * file) Line 231 C++
Irrlicht.dll!irr::scene::CSceneManager::getMesh(const irr::core::string<char,irr::core::irrAllocator<char> > & filename) Line 396 C++
demo_sound.exe!createWorld() Line 190 C++
demo_sound.exe!main(int argc, char * * argv) Line 327 C++
[External Code]
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
I've looked into createMesh function break is on line for loading faces:

Code: Select all

if ( -1 != Idx[2] )
    v.Normal = normalsBuffer[Idx[2]];
https://www.dropbox.com/s/0mntrvodbgdhw ... e.zip?dl=0
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Loading mesh from .obj

Post by thanhle »

Hi your mesh loaded fine.
My version is very close to trunk.
Image

I think there must be something wrong with your code.
You media path might be wrong?

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

Re: Loading mesh from .obj

Post by CuteAlien »

Looks like a bug in Irrlicht 1.8 which is fixed in Irrlicht trunk. I just did a quick check on the side - need more time to investigate what this was. Might be something that should be backported into 1.8.
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: Loading mesh from .obj

Post by CuteAlien »

Hm, Irrlicht trunk works around some broken obj files with additional checks. I might backport those - but it's really the obj file that is wrong here. The faces sections (those with 'f' in front of the line) always has triplets with index to vertex-buffer, uv-buffer, vertex-normals-buffer. You have only 6 vn's, but it tries to access up to 24 vertex-normals.
Can't help much with the tool-export, but the obj file it writes is broken for some reason.
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
tobix10
Posts: 15
Joined: Thu Feb 26, 2015 5:34 pm

Re: Loading mesh from .obj

Post by tobix10 »

Thanks for investigation :). I tried to export normals in other ways (crease angle, smooth) and that worked.
tobix10
Posts: 15
Joined: Thu Feb 26, 2015 5:34 pm

Re: Loading mesh from .obj

Post by tobix10 »

From last time I haven't got time to do anything. Now I am trying to use blender to create static scene and read it in irrlicht. I've got it working, but without light.
Scene contains only a cube with textured inner sides. In irrlicht I had to switch from back to front face culling. After turning light on everything is black.
In blender it looks ok. Normals points inwards.

How I read model

Code: Select all

irr::scene::IAnimatedMesh* mesh = m_SceneManager->getMesh("./gfx/models/room.obj");
    irr::scene::ISceneNode* node = 0;
    if (mesh)
    {
        node = m_SceneManager->addMeshSceneNode (mesh->getMesh (0), 0, -1,
                                                   irr::core::vector3df (0, 10.0F * m_Scale, 0),
                                                   irr::core::vector3df (0, 0, 0),
                                                   irr::core::vector3df (20.0F * m_Scale, 20.0F * m_Scale, 20.0F * m_Scale));
        node->setMaterialFlag (irr::video::EMF_LIGHTING, true);
        node->setMaterialFlag (irr::video::EMF_BACK_FACE_CULLING, false);
        node->setMaterialFlag (irr::video::EMF_FRONT_FACE_CULLING, true);
    }
and add light

Code: Select all

    const float radius = m_Scale * 50.0F;
    m_SceneManager->setAmbientLight (irr::video::SColorf (0.3F, 0.3F, 0.3F, 1.0F));
    m_SceneManager->addLightSceneNode (0, irr::core::vector3df (0, 1.5F * m_EyeHeight, 0), irr::video::SColorf (0.9F, 0.9F, 0.9F), radius, 1);
Light is working, beacuse I see it on sphere_node inside cube.

Have I missed something?

Blender files and obj file can be downloaded from https://www.dropbox.com/s/oaar53nbgctayv0/room.tgz?dl=0.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Loading mesh from .obj

Post by mongoose7 »

The normals are still pointing out.
tobix10
Posts: 15
Joined: Thu Feb 26, 2015 5:34 pm

Re: Loading mesh from .obj

Post by tobix10 »

Where do you check them (blender, irrlicht, other software)?

I see in blender that they point inwards (mesh properties -> display face normals as lines). When exporting to obj they have to be in native irrlicht coordinate system? I exported them with Forward: -Z, Up: Y. Maybe it should be Forward: Y, Up: Z. I'll check that when I'll have access to project.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Loading mesh from .obj

Post by mongoose7 »

It was just a guess. Forward -Z Up Y is correct. You can display normals in Irrlicht with something like
node->setDebugDataVisible(scene::EDS_FULL);
(I think there is an enum for normals alone.)
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Loading mesh from .obj

Post by hendu »

Irr is left-handed, so forward would be +Z?
tobix10
Posts: 15
Joined: Thu Feb 26, 2015 5:34 pm

Re: Loading mesh from .obj

Post by tobix10 »

mongoose7 wrote:It was just a guess. Forward -Z Up Y is correct. You can display normals in Irrlicht with something like
node->setDebugDataVisible(scene::EDS_FULL);
(I think there is an enum for normals alone.)
EDS_NORMALS
hendu wrote:Irr is left-handed, so forward would be +Z?
I tried this and some other combinations of up and forward, but always normals in irrlicht points outward ;/.

If manually set

Code: Select all

node->getMaterial (0).AmbientColor.set (255, 255, 255, 255);
node->getMaterial (0).DiffuseColor.set (255, 255, 255, 255);
i see textures, but dimmed. Still normals problem.

// edit
My last effort fulfilled the dream xD. I exported cube with normals pointing outward and loaded it in irrlicht. Normals was pointing inside so I copied them to previous *.obj. Now normals are ok, textures are on walls inside cube and light works.
I think this exporter may be broken.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Loading mesh from .obj

Post by mongoose7 »

@hendu: Forward is towards the camera?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Loading mesh from .obj

Post by hendu »

void CCameraSceneNode::recalculateProjectionMatrix()
{
ViewArea.getTransform ( video::ETS_PROJECTION ).buildProjectionMatrixPerspectiveFovLH
}
Post Reply