Shader blend mode

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
ison
Posts: 42
Joined: Sun Mar 24, 2013 9:09 pm

Shader blend mode

Post by ison »

Hello,

it seems that blend modes (or base material types) does not work for custom shaders.
I pass EMT_TRANSPARENT_ADD_COLOR to addHighLevelShaderMaterial but color is not added. Instead it's just replaced.
In fragment shader all I do is: gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);

red is not added to green:
Image

if i do gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);
and use EMT_TRANSPARENT_ALPHA_CHANNEL
then mesh is not displayed (full transparent) - this is ok, but if i replace 0.0 with any other greater value like
gl_FragColor = vec4(1.0, 0.0, 0.0, 0.1);
then mesh is rendered with 100% red channel (the same effect as on screenshot above)

Am I doing something wrong or it's not supposed to work this way?

This is full source code:

Code: Select all

#include <irrlicht.h>
 
using namespace irr;
using namespace video;
using namespace scene;
 
IrrlichtDevice *device;
IVideoDriver *driver;
ISceneManager *smgr;
ICameraSceneNode *cam;
 
const char *VERTEX =
{
    "void main() {"
    "    gl_Position = ftransform();"
    "}"
};
 
const char *FRAGMENT =
{
    "void main() {"
    "    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
    "}"
};
 
int main(int argc, char *argv[])
{
    device = createDevice( video::EDT_OPENGL, core::dimension2d<u32>(800, 600),32,false, false, false);
    driver = device->getVideoDriver();
    smgr = device->getSceneManager();
 
    cam = smgr->addCameraSceneNode();
    cam->setPosition(core::vector3df(100,100,100));
    cam->setTarget(core::vector3df(0,0,0));
 
    //add a simple mesh
    ISceneNode *node=smgr->addMeshSceneNode(smgr->getMesh("media/beast.b3d"));
    node->setMaterialTexture(0, driver->getTexture("media/beast1.png"));
    node->setMaterialFlag(EMF_LIGHTING, false);
 
    //build shader
    irr::u32 shader = driver->getGPUProgrammingServices()->addHighLevelShaderMaterial(VERTEX, "main", EVST_VS_1_1,FRAGMENT, "main", EPST_PS_1_1, NULL, EMT_TRANSPARENT_ADD_COLOR);
    node->setMaterialType((irr::video::E_MATERIAL_TYPE)shader);
 
    while(device->run()) {
         driver->beginScene(true, true, SColor(255, 0, 255, 0));
         smgr->drawAll();
         driver->endScene();
    }
 
    return EXIT_SUCCESS;
}
 
Thanks
hybrid
Admin
Posts: 14144
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Shader blend mode

Post by hybrid »

No, any color handling is only made by the shader. Only the base setup, e.g. setting certain environment values, is made through the base material.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Shader blend mode

Post by hendu »

This is one of the things that broke after 1.7. If you run this on 1.7, it is correctly yellow.

In 1.8 I believe you also have to manually set the EBO blend mode to ADD.
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Shader blend mode

Post by Nadro »

Yes, E_BLEND_OPERATION is important in v1.8 and newer.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
ison
Posts: 42
Joined: Sun Mar 24, 2013 9:09 pm

Re: Shader blend mode

Post by ison »

but how to set E_BLEND_OPERATION?
CuteAlien
Admin
Posts: 10058
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Shader blend mode

Post by CuteAlien »

Loop over all materials in a mesh and set it directly in SMaterial.BlendOperation.
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
ison
Posts: 42
Joined: Sun Mar 24, 2013 9:09 pm

Re: Shader blend mode

Post by ison »

it works ;)
thanks a lot
Post Reply