Page 3 of 3

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Mon Mar 02, 2015 10:12 pm
by CuteAlien
EMT_TRANSPARENT_VERTEX_ALPHA which mongoose7 mentioned works for me (using meshmanipulator instead of DiffuseColor which is not about vertices but about texture-colors). It also works in the meshviewer example. Actually the one I first mentioned also works for me.

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Tue Mar 03, 2015 11:13 am
by pandoragami
CuteAlien wrote:EMT_TRANSPARENT_VERTEX_ALPHA which mongoose7 mentioned works for me (using meshmanipulator instead of DiffuseColor which is not about vertices but about texture-colors). It also works in the meshviewer example. Actually the one I first mentioned also works for me.
Not sure what you mean by Diffuse Color, I remember getting the mesh for the sphere and then setting the alpha and material type using the meshmanipulator but I don't ever recall setting the diffuse color, can you please show me where I tried that part..

thanks,

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Tue Mar 03, 2015 12:22 pm
by CuteAlien
You set DiffuseColor in pretty much every code-post you made in the last page of this forum :-) Just hit ctrl+f in your browser and type "diffuse". While you didn't use the meshmanipulator anymore as soon as you got that hint about diffuse (which is useful in other situations but not in this one).

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Tue Mar 03, 2015 11:29 pm
by pandoragami
CuteAlien wrote:You set DiffuseColor in pretty much every code-post you made in the last page of this forum :-) Just hit ctrl+f in your browser and type "diffuse". While you didn't use the meshmanipulator anymore as soon as you got that hint about diffuse (which is useful in other situations but not in this one).
This is the final version, without diffusecolor anywhere but it only works if the texture (line 94) is commented out. I put a sphere that has an alpha of zero inside a slightly larger sphere with an alpha of 155.

Code: Select all

 
#include <irrlicht.h>
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
public:
 
    struct SMouseState
    {
        core::position2di Position;
        bool LeftButtonDown;
        SMouseState() : LeftButtonDown(false) { }
    } MouseState;
 
    virtual bool OnEvent(const SEvent& event)
    {
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        if(event.EventType == EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
            case EMIE_LMOUSE_PRESSED_DOWN:
                MouseState.LeftButtonDown = true;
                break;
 
            case EMIE_LMOUSE_LEFT_UP:
                MouseState.LeftButtonDown = false;
                break;
 
            case EMIE_MOUSE_MOVED:
                MouseState.Position.X = event.MouseInput.X;
                MouseState.Position.Y = event.MouseInput.Y;
                break;
 
            default:
 
                break;
            }
        }
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    const SMouseState & GetMouseState(void) const
    {
        return MouseState;
    }
 
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
MyEventReceiver receiver;
IrrlichtDevice* device = 0;
const int SCRX = 640;
const int SCRY = 480;
const float radius = 5.0f;
 
int main()
{
    device = createDevice( irr::video::EDT_OPENGL, core::dimension2d<u32>( SCRX, SCRY), 16, false, false, false, &receiver);
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    scene::IMeshManipulator* mesh = smgr->getMeshManipulator();
 
    driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS( 0, 50.0f, 0.01f, -1, 0, 0, false, 0.0f, false, true);
 
    device->getCursorControl()->setVisible(false);
 
    scene::IMeshSceneNode* sphere_1 = smgr->addSphereSceneNode(radius, 32, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere_1->setMaterialFlag(video::EMF_LIGHTING, false);
    sphere_1->setMaterialTexture( 0, driver->getTexture("graph.jpg"));
 
    sphere_1->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
    smgr->getMeshManipulator()->setVertexColors(sphere_1->getMesh(), video::SColor( 255, 0, 255, 0));
    mesh->setVertexColorAlpha(sphere_1->getMesh(), 0);
 
    scene::IMeshSceneNode* sphere_2 = smgr->addSphereSceneNode(radius+1, 32, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere_2->setMaterialFlag(video::EMF_LIGHTING, false);
    sphere_2->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
    smgr->getMeshManipulator()->setVertexColors(sphere_2->getMesh(), video::SColor( 255, 255, 0, 0));
    mesh->setVertexColorAlpha(sphere_2->getMesh(), 155);
 
    cam->setPosition( core::vector3df( 0, 0,-15));
    cam->setTarget( core::vector3df( 0.0f, 0.0f, 0.0f));
 
    int lastFPS = -1;
 
    while(device->run())
    if (device->isWindowActive())
    {
        driver->beginScene(true, true, video::SColor( 255, 0, 0, 0));
        smgr->drawAll();
        driver->endScene();
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw str = L"Irrlicht Text Node [";
            str += driver->getName();
            str += "] FPS:";
            str += fps;
 
            device->setWindowCaption(str.c_str());
            lastFPS = fps;
        }
 
        device->sleep(50);
    }
 
    device->drop();
 
    return 0;
}
 
I also looked at mel's code (the guys a genius) here: http://irrlicht.sourceforge.net/forum/v ... =9&t=47221

He doesn't seem to use any alphas but loads textures?

this snippet is mentioned for the ctor

Code: Select all

 
       material.BackfaceCulling = false;
    material.MaterialType = video::EMT_ONETEXTURE_BLEND;
    material.setFlag(video::EMF_LIGHTING, false);
    material.setTexture(0, smgr->getVideoDriver()->getTexture("beamside.png"));
    //material.setTexture(0, smgr->getVideoDriver()->getTexture("media/wall.bmp"));
   
    //FIXME
    //material.BlendOperation = video::EBO_ADD;
    material.MaterialTypeParam = irr::video::pack_textureBlendFunc
    (
        video::EBF_SRC_ALPHA,
        video::EBF_ONE_MINUS_SRC_ALPHA,
        video::EMFN_MODULATE_1X,
        video::EAS_VERTEX_COLOR | video::EAS_TEXTURE
    );
 
But then he mentions there's no need for it?

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Wed Mar 04, 2015 12:29 am
by CuteAlien
So did you actually try it with the materialtypes we said you should use? Or did you just try this one you posted here?

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Wed Mar 04, 2015 2:06 am
by pandoragami
CuteAlien wrote:So did you actually try it with the materialtypes we said you should use? Or did you just try this one you posted here?

You're right, I had to go back to mongoose's post and try that again so I changed the material type to EMT_TRANSPARENT_VERTEX_ALPHA
from EMT_TRANSPARENT_ALPHA_CHANNEL and it looks like something is starting to work but only part of the "graph.jpg" is vanishing. Could that be because the black lines are the alpha channel in the original jpg? Notice I added this line (96),

std::cout<<tex->hasAlpha()<<std::endl;

to test for alpha, I get 1 for the output, I assume that's true.


Here's the updated code plus pictures below along with graph.jpg as the last image. In the zero alpha image it seems the larger (yellow parts) aren't vanishing, is there a way to fix it?

Code: Select all

 
#include <irrlicht.h>
#include <ITexture.h>
#include <iostream>
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
public:
 
    struct SMouseState
    {
        core::position2di Position;
        bool LeftButtonDown;
        SMouseState() : LeftButtonDown(false) { }
    } MouseState;
 
    virtual bool OnEvent(const SEvent& event)
    {
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        if(event.EventType == EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
            case EMIE_LMOUSE_PRESSED_DOWN:
                MouseState.LeftButtonDown = true;
                break;
 
            case EMIE_LMOUSE_LEFT_UP:
                MouseState.LeftButtonDown = false;
                break;
 
            case EMIE_MOUSE_MOVED:
                MouseState.Position.X = event.MouseInput.X;
                MouseState.Position.Y = event.MouseInput.Y;
                break;
 
            default:
 
                break;
            }
        }
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    const SMouseState & GetMouseState(void) const
    {
        return MouseState;
    }
 
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
MyEventReceiver receiver;
IrrlichtDevice* device = 0;
const int SCRX = 640;
const int SCRY = 480;
const float radius = 5.0f;
 
int main()
{
    device = createDevice( irr::video::EDT_OPENGL, core::dimension2d<u32>( SCRX, SCRY), 16, false, false, false, &receiver);
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    scene::IMeshManipulator* mesh = smgr->getMeshManipulator();
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS( 0, 50.0f, 0.01f, -1, 0, 0, false, 0.0f, false, true);
 
    device->getCursorControl()->setVisible(false);
 
    scene::IMeshSceneNode* sphere_1 = smgr->addSphereSceneNode(radius, 32, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere_1->setMaterialFlag(video::EMF_LIGHTING, false);
 
    video::ITexture* tex = driver->getTexture("graph.jpg");
    std::cout<<tex->hasAlpha()<<std::endl;
    sphere_1->setMaterialTexture( 0, tex);
 
    sphere_1->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
    smgr->getMeshManipulator()->setVertexColors(sphere_1->getMesh(), video::SColor( 255, 0, 255, 0));
    mesh->setVertexColorAlpha(sphere_1->getMesh(), 255);
 
 
    scene::IMeshSceneNode* sphere_2 = smgr->addSphereSceneNode(radius+1, 32, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere_2->setMaterialFlag(video::EMF_LIGHTING, false);
    sphere_2->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
    smgr->getMeshManipulator()->setVertexColors(sphere_2->getMesh(), video::SColor( 255, 255, 0, 0));
    mesh->setVertexColorAlpha(sphere_2->getMesh(), 255);
 
    cam->setPosition( core::vector3df( 0, 0,-15));
    cam->setTarget( core::vector3df( 0.0f, 0.0f, 0.0f));
 
    int lastFPS = -1;
 
    while(device->run())
    if (device->isWindowActive())
    {
        driver->beginScene(true, true, video::SColor( 255, 0, 0, 0));
        smgr->drawAll();
        driver->endScene();
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw str = L"Irrlicht Text Node [";
            str += driver->getName();
            str += "] FPS:";
            str += fps;
 
            device->setWindowCaption(str.c_str());
            lastFPS = fps;
        }
 
        device->sleep(50);
    }
 
    device->drop();
 
    return 0;
}
 
This is what it looks like with mesh->setVertexColorAlpha(sphere_1->getMesh(), 0); ( line 101). Notice that the yellow parts aren't vanishing, is that the way it's supposed to be?

Image

This is what it looks like with mesh->setVertexColorAlpha(sphere_1->getMesh(), 255); ( line 101)

Image


This is the texture used "graph.jpg"

Image

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Wed Mar 04, 2015 3:52 am
by mongoose7
1. JPGs have *no* alpha channel.
2. alpha = 255 is *opaque*.
3. You can set alpha where you set the colour - it is just the first value.
So, you should have a green sphere inside a red sphere. With alpha = 255 you shouldn't be able to see the green sphere. With alpha = 0 you shouldn't be able to see the red sphere. However, looking at the pictures, it seems that TRANSPARENT_VERTEX_ALPHA is turning on blending. Nadro committed a fix to the trunk so maybe you should use that (the trunk).

As has been said before, with 1.9 taking soooooo long, maybe the trunk should be released as 1.8.2. Maybe 1.8.2.beta for those scared by new releases.

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Wed Mar 04, 2015 5:03 am
by thanhle
Hi mate I think there is a bug with the sphere scenenode.
If you use the default 16 polycount it works fine. Play around with the polycount. I think it works fine up to 21.


IMeshSceneNode *node = smgr->addSphereSceneNode(5.0f,21);
node->setMaterialFlag(video::EMF_LIGHTING, true);
video::ITexture* tex = mVudu->device->getVideoDriver()->getTexture("media/graph.jpg");
node->setMaterialTexture(0, tex);

//Your other codes
node->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
IMeshManipulator *manip = mVudu->device->getSceneManager()->getMeshManipulator();
manip->setVertexColors(node->getMesh(), video::SColor(255, 255, 0, 0));
manip->setVertexColorAlpha(node->getMesh(), 100);


Example picture:
Image


Regards
thanh

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Wed Mar 04, 2015 10:45 am
by CuteAlien
mongoose7 wrote:As has been said before, with 1.9 taking soooooo long, maybe the trunk should be released as 1.8.2. Maybe 1.8.2.beta for those scared by new releases.
I admit I've used trunk for testing, guess I have to check what 1.8 does (but no time right now).
Btw - can't release trunk as 1.8.2 as it's not binary compatible. Minor version numbers are not allowed to break that. But just getting 1.9 out at some point before ogl-es merge is a solution I'm thinking about. But even that needs some spare weekends to get it into that state and it'll be a while until I have those again.

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Wed Mar 04, 2015 12:02 pm
by mongoose7
I understand the pressure. Also, I guess there are not that many problems that people talk about here that would be solved by the trunk. However, I guess on SF it looks as if this project is no longer maintained.

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Wed Mar 04, 2015 2:25 pm
by pandoragami
mongoose7 wrote:I understand the pressure. Also, I guess there are not that many problems that people talk about here that would be solved by the trunk. However, I guess on SF it looks as if this project is no longer maintained.
You mean Irrlicht is no longer maintained. I was thinking of going into openGL before putting too much time building anything serious with Irrlicht so I guess it would be good to know before hand.

@Thanle

Here's what I managed,

Image


It seems that the water effect does show as transparent across the spheres, albeit a bit blended but I guess looking through a tinted window would be the same in the real world. I used the water and stones jpgs from tutorial 8 special FX.

Code: Select all

#include <irrlicht.h>
#include <ITexture.h>
#include <iostream>
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
public:
 
    struct SMouseState
    {
        core::position2di Position;
        bool LeftButtonDown;
        SMouseState() : LeftButtonDown(false) { }
    } MouseState;
 
    virtual bool OnEvent(const SEvent& event)
    {
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        if(event.EventType == EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
            case EMIE_LMOUSE_PRESSED_DOWN:
                MouseState.LeftButtonDown = true;
                break;
 
            case EMIE_LMOUSE_LEFT_UP:
                MouseState.LeftButtonDown = false;
                break;
 
            case EMIE_MOUSE_MOVED:
                MouseState.Position.X = event.MouseInput.X;
                MouseState.Position.Y = event.MouseInput.Y;
                break;
 
            default:
 
                break;
            }
        }
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    const SMouseState & GetMouseState(void) const
    {
        return MouseState;
    }
 
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
MyEventReceiver receiver;
IrrlichtDevice* device = 0;
const int SCRX = 640;
const int SCRY = 480;
const float radius = 5.0f;
 
int main()
{
    device = createDevice( irr::video::EDT_OPENGL, core::dimension2d<u32>( SCRX, SCRY), 16, false, false, false, &receiver);
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    scene::IMeshManipulator* mesh = smgr->getMeshManipulator();
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS( 0, 50.0f, 0.01f, -1, 0, 0, false, 0.0f, false, true);
//lighting
    smgr->addLightSceneNode( 0, core::vector3df(-50.0f,10.0f,-50.0f), video::SColorf(1.0f,1.0f,1.0f,0.0f), 5000.0f );
//water and textures
    scene::IAnimatedMesh* plane_mesh = smgr->addHillPlaneMesh( "water_plane", core::dimension2d<f32>(20,20), core::dimension2d<u32>(40,40), 0, 0, core::dimension2d<f32>(0,0), core::dimension2d<f32>(10,10));
    scene::ISceneNode* water_node = smgr->addWaterSurfaceSceneNode ( plane_mesh, 2.0f, 300.0f, 10.0f, 0, -1, core::vector3df(0, -5.0f, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    water_node->setMaterialTexture(0, driver->getTexture("stones.jpg"));
    water_node->setMaterialTexture(1, driver->getTexture("water.jpg"));
    water_node->setMaterialType(video::EMT_REFLECTION_2_LAYER);
 
    device->getCursorControl()->setVisible(false);
//sphere 1
    scene::IMeshSceneNode* sphere_1 = smgr->addSphereSceneNode(radius, 16, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere_1->setMaterialFlag(video::EMF_LIGHTING, true);
    sphere_1->setMaterialType(video::EMT_SOLID);
 
    video::ITexture* tex = driver->getTexture("graph.jpg");
    std::cout<<tex->hasAlpha()<<std::endl;
    sphere_1->setMaterialTexture( 0, tex);
 
    sphere_1->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
    smgr->getMeshManipulator()->setVertexColors(sphere_1->getMesh(), video::SColor( 155, 0, 255, 0));
    mesh->setVertexColorAlpha(sphere_1->getMesh(), 0);
 
//sphere 2
    scene::IMeshSceneNode* sphere_2 = smgr->addSphereSceneNode(radius+1, 16, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
    sphere_2->setMaterialFlag(video::EMF_LIGHTING, true);
    sphere_2->setMaterialType(video::EMT_SOLID);
    sphere_2->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);
    smgr->getMeshManipulator()->setVertexColors(sphere_2->getMesh(), video::SColor( 155, 255, 0, 0));
    mesh->setVertexColorAlpha(sphere_2->getMesh(), 155);
 
    cam->setPosition( core::vector3df( 0, 0,-15));
    cam->setTarget( core::vector3df( 0.0f, 0.0f, 0.0f));
 
    int lastFPS = -1;
    int alpha = 0;
 
    while(device->run())
    if (device->isWindowActive())
    {
         if(alpha < 256)
         {
            mesh->setVertexColorAlpha(sphere_1->getMesh(), alpha);
            alpha++;
         }
         else
            alpha = 0;
 
        driver->beginScene(true, true, video::SColor( 0, 0, 0, 155));
        smgr->drawAll();
        driver->endScene();
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw str = L"Irrlicht Text Node [";
            str += driver->getName();
            str += "] FPS:";
            str += fps;
 
            device->setWindowCaption(str.c_str());
            lastFPS = fps;
        }
 
        device->sleep(50);
    }
 
    device->drop();
 
    return 0;
}
 

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Wed Mar 04, 2015 3:39 pm
by CuteAlien
mongoose7 wrote:However, I guess on SF it looks as if this project is no longer maintained.
It's constantly maintained and SF shows that: https://sourceforge.net/p/irrlicht/activity/?

There just haven't been any releases last year.

Re: [SOLVED] setting transparency (alpha) for spherescenenod

Posted: Wed Mar 04, 2015 4:09 pm
by thanhle
@anonymouse
Maybe set be set lightning on the objects to false, will remove that dark area. Also decrease the transparent value, it will look better as you can then see the water.

Never give up :).

I'm self teach in most of the things that I do. Now that I'm old, my learning rate is getting slower each day.

Best regards
thanh