Page 1 of 1

Changing a MeshBuffer texture after geometry creation

Posted: Tue Aug 16, 2016 5:09 pm
by daralect
I'm having a problem changing textures for MeshBuffers after vertices and indexes have been added. I'm not sure if it has to do with the type of Mesh I'm constructing, i.e. a SkinnedMesh.

scene::SSkinMeshBuffer *buf = Mesh->addMeshBuffer();
buf->getMaterial().setTexture(0,_driver->getTexture("test1.png")); // this works

but, if I add vertices and indexes to the MeshBuffer and then try to change the texture it won't display the new texture. I'm trying to create an animated texture effect, so I need to change textures every few frames.

As a workaround I tried creating a Node using the MeshBuffer's geometry, and made it a child node of the original Node. Swapping textures for an entire Node works, but the ZBuffer seems to be messed up. The meshes in the Parent node get displayed in front of my Child mesh even though the Child mesh is in front of the Parent.

Thanks.

Re: Changing a MeshBuffer texture after geometry creation

Posted: Tue Aug 16, 2016 7:04 pm
by CuteAlien
The way you change the meshbuffer above does change the texture in the meshbuffer.

Your problem is likely that you want to change the material in the node instead. Nodes are by default initialized with the meshbuffer materials, but they have their own material copy. That is so you can have the same mesh with different materials. To change p.E. the first texture in first meshbuffer you do: node->getMaterial(0).setTexture(0, myTex);

If you want all nodes to share the same material you can also get Irrlicht to use the mesh-materials directly with IMeshSceneNode:: setReadOnlyMaterials (true). Although I found some problems with that recently when it comes to alpha's (on my todo-buglist, but that's used so rarely that no-one had even noticed so far that there's a problem ...).

Re: Changing a MeshBuffer texture after geometry creation

Posted: Tue Aug 16, 2016 8:34 pm
by daralect
Thanks CuteAlien. Accessing the Material from the Node works. I'm assuming that Materials get applied to nodes in the order that the MeshBuffers are added, so since the MeshBuffer I want was the 2nd added, I would use Material #1.

node->getMaterial(1).setTexture(0, myTex);

Thanks.

Re: Changing a MeshBuffer texture after geometry creation

Posted: Tue Aug 16, 2016 8:38 pm
by CuteAlien
Yeah - index for meshbuffer and material should always be identical. I'm not sure right now what would happen if you add a meshbuffer when using the mesh already in a node - uhm....

Re: Changing a MeshBuffer texture after geometry creation

Posted: Wed Aug 17, 2016 12:21 am
by Mel
in fact, meshBuffers could live perfectly without materials, as, in the end, most of the times, they're completely overriden by the node's materials. But i guess they're kept for simplicity.

Although one thing should be clear, the meshbuffer is almost the smallest rendering unit, before entering into vertices and index lists, and can only be rendered with a single material. Therefore if you want a mesh with several materials, you have to compose it of several meshbuffers, one per material. That is where scene nodes come in hand, they close this gap, so you can use everything as a single entity, and you can reuse the same meshes and meshbuffers across the scene.

Re: Changing a MeshBuffer texture after geometry creation

Posted: Wed Aug 17, 2016 6:37 am
by CuteAlien
Mel wrote:in fact, meshBuffers could live perfectly without materials, as, in the end, most of the times, they're completely overriden by the node's materials. But i guess they're kept for simplicity.
The reason for that design was probably that it allows to load meshes with materials. Materials just have to be kept somewhere in memory ...
But yeah, once you have nodes the mesh-materials are basically no longer needed.

Re: Changing a MeshBuffer texture after geometry creation

Posted: Thu Aug 18, 2016 7:48 pm
by daralect
Thanks for the insight. Exposing Materials at the MeshBuffer level is a little confusing while learning, because there's an expectation that the Materials will still be used by the MeshBuffers.

However, I'm still not up to speed on how Irrlicht deals with skinning and animations via vertex groups, so the concept of various geometries and materials may only make sense at a Node level when final rendering gets applied.

Another comment on my limited knowledge, I can't seem to find a reliable way to import hierarchical Node structures with multiple materials. I've taken to rolling my own specialized .obj importer to deal with multiple objects/materials, and I'll post the code along with my reasoning for creating it when it's finished.

Thanks.

Re: Changing a MeshBuffer texture after geometry creation

Posted: Thu Aug 18, 2016 9:36 pm
by CuteAlien
Basically when you need node-hierarchies you need scene-formats. While for geometries and materials you work with mesh-formats.
Irrlicht supports only 2 scene-formats I think - it's own .irr format and partial support for the collada format.
A short glossary of terms used in Irrlicht can be found here, maybe it helps a little bit: http://www.irrlicht3d.org/wiki/index.ph ... ommonTerms