Set material of custom node / plane?

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
whythishard
Posts: 9
Joined: Tue Feb 25, 2020 1:53 pm

Set material of custom node / plane?

Post by whythishard »

Code: Select all

 
class CSampleSceneNode : public scene::ISceneNode
{
 
 
    core::aabbox3d<f32> Box;
    video::S3DVertex Vertices[4];
    video::SMaterial Material;
 
public:
 
    CSampleSceneNode(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id)
        : scene::ISceneNode(parent, mgr, id)
    {
        Material.Wireframe = false;
        Material.Lighting = false;
 
        Vertices[0] = video::S3DVertex(0,0,0, 1,1,0,
                video::SColor(255,255,255,255), 0, 1);
        Vertices[1] = video::S3DVertex(0,0,10, 1,0,0,
                video::SColor(255,255,255,255), 1, 1);
        Vertices[2] = video::S3DVertex(10,0,0, 0,1,1,
                video::SColor(255,255,255,255), 1, 0);
        Vertices[3] = video::S3DVertex(10,0,10, 0,0,1,
                video::SColor(255,255,255,255), 0, 0);
 
        Box.reset(Vertices[0].Pos);
        for (s32 i=1; i<4; ++i)
            Box.addInternalPoint(Vertices[i].Pos);
    }
 
    virtual void OnRegisterSceneNode()
    {
        if (IsVisible)
            SceneManager->registerNodeForRendering(this);
 
        ISceneNode::OnRegisterSceneNode();
    }
 
    virtual void render()
    {
        u16 indices[] = {   0,2,3, 2,1,3, 1,0,3, 2,0,1  };
        video::IVideoDriver* driver = SceneManager->getVideoDriver();
 
        driver->setMaterial(Material);
        driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
        driver->drawVertexPrimitiveList(&Vertices[0], 4, &indices[0], 4, video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
    }
 
    virtual const core::aabbox3d<f32>& getBoundingBox() const
    {
        return Box;
    }
 
    virtual u32 getMaterialCount() const
    {
        return 1;
    }
 
    virtual video::SMaterial& getMaterial(u32 i)
    {
        return Material;
    }
};
 
and in main add it by doing

Code: Select all

 CSampleSceneNode *myNode = new CSampleSceneNode(smgr->getRootSceneNode(), smgr, 1);
Now I want to apply a simple 250x250 px png texture to this plane, on one side preferebly. How do I go about doing this?
CuteAlien
Admin
Posts: 9652
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Set material of custom node / plane?

Post by CuteAlien »

Code: Select all

 
irr::video::ITexture * myTex = videoDriver->getTexture("yourtexture.png");
myNode->getMaterial(0).setTexture(0, myTex);
 
At least if the uv's in S3DVertex are all OK (I didn't test that). Default in materials is backface-culling, which means it's already one-sided.
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
whythishard
Posts: 9
Joined: Tue Feb 25, 2020 1:53 pm

Re: Set material of custom node / plane?

Post by whythishard »

I managed to apply the texture! I have played around with irrlicht before, about a year ago and I managed to create a plane without creating a custom node like I did here. When I apply the texture I get a distict line on the hypotenuse of both triangles. Is there a simple way to create a simple plane mesh without creating two triangles and putting them together like this? Thanks!
whythishard
Posts: 9
Joined: Tue Feb 25, 2020 1:53 pm

Re: Set material of custom node / plane?

Post by whythishard »

I found this way of creating a plane but I am unable to apply a texture to it...

Code: Select all

 
irr::video::ITexture * myTex = driver->getTexture("texture.png");
const irr::scene::IGeometryCreator *geomentryCreator = smgr->getGeometryCreator();
 
irr::scene::IMesh* plane = geomentryCreator->createPlaneMesh(irr::core::dimension2d<irr::f32>(5, 5), irr::core::dimension2d<irr::u32>(1, 1));// (base, height), (repeat x, repeat y)
irr::scene::ISceneNode* ground = smgr->addMeshSceneNode(plane);
plane->setMaterialFlag(irr::video::EMF_LIGHTING, false);    //This is important
ground->getMaterial(0).setTexture(0, myTex); // does not apply texture, plane is completely white. But the texture works when applied to my custom node
smgr->addMeshSceneNode(plane);
 
Edit: changing the position of "ground" ISceneNode makes the texture visible. It seems as if the texture is a seperate entity, since that is moved but the actual plane is still stuck at the same position and is still white.
CuteAlien
Admin
Posts: 9652
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Set material of custom node / plane?

Post by CuteAlien »

Uhm, no - there can't be a texture without geometry (unless you do some 2d drawing of images). Maybe you got another node still around?
Sorry, can't tell what's going on from the posted lines - problem doesn't look like it's in those.
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
whythishard
Posts: 9
Joined: Tue Feb 25, 2020 1:53 pm

Re: Set material of custom node / plane?

Post by whythishard »

Yes, realized that I had another plane created much further down, sorry! The texture that is applied onto my custom node is faulty in that it is a weird line in the middle. Further, the texture is warped onto the plane created by gemoetryCreator in my example. It seems as if that plane is created using two triangles and one of them is correctly mapped but the other has a sort of rotation to the texture when I move around the FPS camera... Any fixes for either cases?

Edit: for some reason EDT_SOFTWARE is messing with the texture, changing to literally ANY other driver type fixes the texture issue, at least on the plane created by geometryCreator. However using the OpenGL driver causes weird hacky lag when the camera is moved around, even at a steady 60 fps.
CuteAlien
Admin
Posts: 9652
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Set material of custom node / plane?

Post by CuteAlien »

First software driver is more for doing setup-UI's and stuff. Don't use that one for 3D. It can be used to display some preview models sometimes, but shouldn't be used for much more.

About hacky lag - hard to tell. I fixed recently some lag stuff in FPS-camera in svn trunk version of Irrlicht. Thought it caused me some other troubles (on X11) which I haven't figured out yet, so not sure set if I keep it like it is now. So if you use Irrlicht 1.8 with fps-camera... there had been some problems with losing mouse-events.
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