Change Nodes Transparency

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
psychophoniac
Posts: 101
Joined: Wed Dec 03, 2008 5:33 pm
Location: ger

Change Nodes Transparency

Post by psychophoniac »

I read the question "how to change the transparency of a node" few times these days, so i wrote this:

Code: Select all


#include <irrlicht.h>
#include <iostream>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


bool setAlpha(u8 Alpha, video::ITexture* tex)
{
    if(!tex)
    {
        printf("texPointer == NULL\n");
        return false;
    };

    u32 size = tex->getSize().Width*tex->getSize().Height;  // get Texture Size

    switch(tex->getColorFormat()) //getTexture Format, (nly 2 support alpha)
    {
        case video::ECF_A1R5G5B5: //see video::ECOLOR_FORMAT for more information on the texture formats.
        {
          //  printf("16BIT\n");
            u16* Data = (u16*)tex->lock(); //get Data for 16-bit Texture
            for(u32 i = 0; i < size ; i++)
            {
                Data[i] = video::RGBA16(video::getRed(Data[i]), video::getGreen(Data[i]), video::getBlue(Data[i]), Alpha);
            }
            //printf("AlphaValueOfTexture(16bit): %i\n", (Data[0] & 0x80) << 8);
            tex->unlock();
            break;
        };
        case video::ECF_A8R8G8B8:
        {
           // printf("32BIT\n");
            u32* Data = (u32*)tex->lock();
            for( u32 i = 0 ; i < size ; i++)
            {
                ((u8*)&Data[i])[3] = Alpha;//get Data for 32-bit Texture
            }
            //printf("AlphaValueOfTexture(32bit): %i\n", ((u8*)&Data[0])[3]);
            tex->unlock();
            break;
        };
        default: printf("Seems Like There is no Alpha Channel Supported\n");
            return false;
    };
    return true;
};

int main(void)
{
    video::E_DRIVER_TYPE driverType;
	printf("Please select the driver you want for this example:\n"\
		" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
		" (d) Software Renderer\n (e) Burning's Software Renderer\n"\
		" (f) NullDevice\n (otherKey) exit\n\n");

	char i;
	std::cin >> i;

	switch(i)
	{
		case 'a': driverType = video::EDT_DIRECT3D9;break;
		case 'b': driverType = video::EDT_DIRECT3D8;break;
		case 'c': driverType = video::EDT_OPENGL;   break;
		case 'd': driverType = video::EDT_SOFTWARE; break;
		case 'e': driverType = video::EDT_BURNINGSVIDEO;break;
		case 'f': driverType = video::EDT_NULL;     break;
		default: return 1;
	}
	IrrlichtDevice *device =
		createDevice(driverType, core::dimension2d<s32>(640, 480), 32);

    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();

    driver->setTextureCreationFlag(ETCF_ALWAYS_32_BIT, true);

    #define ID_START (50)
    u32 ID = ID_START;

    for(u32 k = 0; k < 5; k++)
    for(u32 i = 0; i < 3; i++)
    {
        IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/dwarf.x"));
            if (node)
            {
               node->setID(++ID);
               node->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);  //this is important to use
               node->setMaterialTexture(0, driver->getTexture("../../media/dwarf.jpg"));
               node->setMaterialFlag(EMF_LIGHTING, false);
               node->setPosition(vector3df(100*i, 0 , k* 70));
            };
    };

    device->getCursorControl()->setVisible(false);

    ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
    camera->setFarValue(10000);

    u32 lastTime = 0, lastFPS = 0;
    bool down = false;
    u8 alpha = 1;

    while(device->run())
    {
        ISceneNode* test = smgr->getSceneNodeFromId(ID_START + 2, smgr->getRootSceneNode()); // get a node

        if(test )
        {
          if(lastTime + 50 < device->getTimer()->getTime()) // only change every 50 ms so you can see the effect better
          {
              printf("AlphaValue:%i\n", alpha);
              setAlpha(alpha, test->getMaterial(0).getTexture(0));
              lastTime = device->getTimer()->getTime();
              if(down)
                {
                    alpha --;
                }
              else
                {
                    alpha ++;
                };

              if(!down && alpha == 254)
              down = true;
               if(down && alpha == 0)
              down = false;
          };
        }
        else
        printf("failed to fetch node.\n");

        driver->beginScene(true, true, SColor(0,100,100,100));

        smgr->drawAll();
        guienv->drawAll();

        driver->endScene();

        u32 FPS = driver->getFPS();
        if(FPS != lastFPS)
        {
            stringw s = "PolyCount: ";
                s += driver->getPrimitiveCountDrawn();
                s += " ::: FPS: ";
                s += driver->getFPS();
                s += " ::: Treiber: ";
                s += driver->getName();
                s += "::: TimeDiff: ";
                s += device->getTimer()->getTime() - lastTime;
            device->setWindowCaption(s.c_str());
            lastFPS = FPS;
        };
    }
    device->drop();
    return 0;
}

NOTES:
please note that if you use a Texture on multiple meshes, all meshs will be affected...

an alternative is to manipulate the mesh, i posted some code here:
http://irrlicht.sourceforge.net/phpBB2/ ... ansparency

many thanks to Sudi at the Irrlicht-IRC for helping me out!
i love skateboarding!
Post Reply