Different textures on different faces of the cube

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
offset
Posts: 1
Joined: Wed Feb 23, 2022 3:38 pm

Different textures on different faces of the cube

Post by offset »

Hello everyone, I need a hint.
If I create a cube in the scene:

Code: Select all

scene::ISceneNode* sn=smgr->addCubeSceneNode(1.f);
sn->setPosition(core::vector3df(0,0,0));
sn->setScale(core::vector3df(10.f,15.f,20.f));
sn->setMaterialTexture(0,driver->getTexture("wood-texture.jpg"));
sn->setMaterialFlag(video::EMF_LIGHTING,false);
then in this cube the texture "wood-texture.jpg" is set on all 6 faces.

How can I set different textures to different faces of this cube?
Thanks.
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Different textures on different faces of the cube

Post by CuteAlien »

You need a different kind of cube. That one uses just 12 vertices and all have uv-coordinates going from 0-1 (and as side-effect their normals go in the average direction of the 3 connected side on each corner).

What you need is a cube with 4 vertices per side, so each side can have independent UV's. On top of that if you need the sides to use different textures (instead of for example having one big texture-atlas to look up tiles in it) then each side needs to be in an own meshbuffer. So for a single cube you end up with 6 meshbuffers where each just contains a plane.

Irrlicht svn trunk has a function for that in CGeometryCreator::createCubeMesh. If you use svn trunk you can just pass parameter ECMT_6BUF_4VTX_NP to get such a cube. Or otherwise check the code and copy that part (https://sourceforge.net/p/irrlicht/code ... reator.cpp).
Alternatively you can create such a cube in a 3d tool and import it in Irrlicht.

Unfortunately problems won't end there. Doing that the typical way with UV's again from 0-1 will often end up with tiny white lines between the sides. I think the best way to prevent that is to move the uv's by half a pixel so the corners are in the center of the first/last pixels. How much half a pixel is depends on the texture-size (you add or subtract 0.5/texture_size from each uv). Possibly also solvable with correct uv-wrapping settings (not sure, I'd have to experiment more to figure this out). edit: Yeah, using TextureWrapU and V with ETC_CLAMP_TO_EDGE is easier and works nicely.

Also be careful that this works only well if you have a low number of cubes. Once it goes into 5-digit numbers of cubes you will need to figure out more optimized solutions (maybe even long before that, depending on which hardware you want to support).
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