Changing a MeshBuffer texture after geometry creation

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
daralect
Posts: 3
Joined: Tue Aug 16, 2016 4:57 pm

Changing a MeshBuffer texture after geometry creation

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

Re: Changing a MeshBuffer texture after geometry creation

Post 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 ...).
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
daralect
Posts: 3
Joined: Tue Aug 16, 2016 4:57 pm

Re: Changing a MeshBuffer texture after geometry creation

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

Re: Changing a MeshBuffer texture after geometry creation

Post 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....
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: Changing a MeshBuffer texture after geometry creation

Post 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.
"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: Changing a MeshBuffer texture after geometry creation

Post 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.
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
daralect
Posts: 3
Joined: Tue Aug 16, 2016 4:57 pm

Re: Changing a MeshBuffer texture after geometry creation

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

Re: Changing a MeshBuffer texture after geometry creation

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