Use shader for 2D triangle primitives?

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
GrafZahl
Posts: 37
Joined: Mon Oct 20, 2014 6:24 pm

Use shader for 2D triangle primitives?

Post by GrafZahl »

Hello

I draw some triangles with "draw2DVertexPrimitiveList" and try to set a Material with a Shader.
However the shader is not applied to that triangles, they just use the default solid material type.

This is my code to load the shader:

Code: Select all

 
  video::IGPUProgrammingServices * const pGPUService = pDriver->getGPUProgrammingServices();
  s32 GlassShader = pGPUService->addHighLevelShaderMaterialFromFiles(
      "../../media/Glass.vert", "main", video::EVST_VS_1_1,
      "../../media/Glass.frag", "main", video::EPST_PS_1_1,
      nullptr, video::EMT_SOLID, 0, video::EGSL_DEFAULT);
 
  irr::video::SMaterial Glass;
  Glass.setFlag(irr::video::EMF_ZBUFFER,            false);
  Glass.setFlag(irr::video::EMF_BACK_FACE_CULLING,  false);
  Glass.setFlag(irr::video::EMF_FRONT_FACE_CULLING, false);
  Glass.MaterialType = (video::E_MATERIAL_TYPE)GlassShader;
 
And this is the part inside the main loop:

Code: Select all

 
 
    pDriver->beginScene(true, true, irr::video::SColor(255,100,101,140));
 
    pSceneManager->drawAll();
 
    pDriver->getMaterial2D() = Glass;
    pDriver->enableMaterial2D(true);
    pDriver->setMaterial(Glass);
    pDriver->draw2DVertexPrimitiveList(RectVertex, 4, Indices, 2, irr::video::EVT_STANDARD, irr::scene::EPT_TRIANGLES, irr::video::EIT_16BIT);
    pDriver->enableMaterial2D(false);
 
    pDriver->endScene();
 
When I simply substitute "draw2DVertexPrimitiveList" by "drawVertexPrimitiveList" the shader is applied... however then of course the matrices are not correct for 2D drawing.
So do I anything wrong in setting the 2D material or does 2D material simply not support custom shaders?

Best regards,
Graf Zahl
Irrlicht related projects:
Looking for a cool and easy to use GUI? Try the IrrIMGUI bindings for IMGUI: https://github.com/ZahlGraf/IrrIMGUI
Try out my Irrlicht CMake build system: https://github.com/ZahlGraf/IrrlichtCMake
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Use shader for 2D triangle primitives?

Post by hendu »

The 2d functions prevent using shaders, though it might work to set the materialtype using the 2d override material.

If you're covering the entire screen, you should use one of the screenquads from the forum.
GrafZahl
Posts: 37
Joined: Mon Oct 20, 2014 6:24 pm

Re: Use shader for 2D triangle primitives?

Post by GrafZahl »

Hi,

I already set the material type of the 2D override material:
pDriver->getMaterial2D() = Glass;
I also tried to set it directly without assign the prepared Material.
Too bad, that 2D primitives are not supporting this.

I don't want to draw a full screen rect, I just wanted to draw some 2D elements for a GUI and use a custom shader for it.

Best regards,
Graf Zahl
Irrlicht related projects:
Looking for a cool and easy to use GUI? Try the IrrIMGUI bindings for IMGUI: https://github.com/ZahlGraf/IrrIMGUI
Try out my Irrlicht CMake build system: https://github.com/ZahlGraf/IrrlichtCMake
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Use shader for 2D triangle primitives?

Post by hendu »

Did you also enable the 2d override material? It doesn't get used otherwise.
GrafZahl
Posts: 37
Joined: Mon Oct 20, 2014 6:24 pm

Re: Use shader for 2D triangle primitives?

Post by GrafZahl »

Yes with:

Code: Select all

 
pDriver->enableMaterial2D(true);
 
(see full code-example above)

Or is there another switch for that?
Irrlicht related projects:
Looking for a cool and easy to use GUI? Try the IrrIMGUI bindings for IMGUI: https://github.com/ZahlGraf/IrrIMGUI
Try out my Irrlicht CMake build system: https://github.com/ZahlGraf/IrrlichtCMake
GrafZahl
Posts: 37
Joined: Mon Oct 20, 2014 6:24 pm

Re: Use shader for 2D triangle primitives?

Post by GrafZahl »

Hi Hendu,

I have one other question. You wrote:
hendu wrote:The 2d functions prevent using shaders...
Was this a design decision on purpose, because shaders for 2D element would not work correctly with Irrlicht. Or is this somehow a side-effect of the 2D function implementation that was not intended, but nobody complained about this so far?

If it is the second answer: How complicated would it be to enable shaders for 2D elements?
I had a look at the OpenGL driver class, but I could not find the part in the code where custom shaders are enabled, I just found some lines, where it enables the build in shaders dependent on material type.

Best regards,
Graf Zahl
Irrlicht related projects:
Looking for a cool and easy to use GUI? Try the IrrIMGUI bindings for IMGUI: https://github.com/ZahlGraf/IrrIMGUI
Try out my Irrlicht CMake build system: https://github.com/ZahlGraf/IrrlichtCMake
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Use shader for 2D triangle primitives?

Post by hendu »

I don't think it was intentionally excluded, more like no such need was seen at the time. Should not be that complicated to enable, but needs then heavy testing to make sure it doesn't break any existing app.
GrafZahl
Posts: 37
Joined: Mon Oct 20, 2014 6:24 pm

Re: Use shader for 2D triangle primitives?

Post by GrafZahl »

Thanks for the answer.

Yes of course this must be tested very well.
I just asked because I think about patching it on my local Irrlicht copy.

Do you have a hint where to look for?
Is the main driver file the correct one (for example COpenGLDriver.cpp)? Or is this located in another file.

You don't need to give me exact file position or function names, just some hints, when you have them in your mind.
Irrlicht related projects:
Looking for a cool and easy to use GUI? Try the IrrIMGUI bindings for IMGUI: https://github.com/ZahlGraf/IrrIMGUI
Try out my Irrlicht CMake build system: https://github.com/ZahlGraf/IrrlichtCMake
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Use shader for 2D triangle primitives?

Post by hendu »

Yes, that's the right file. Follow the codepaths where materials are touched.
GrafZahl
Posts: 37
Joined: Mon Oct 20, 2014 6:24 pm

Re: Use shader for 2D triangle primitives?

Post by GrafZahl »

Thanks, I will have a deeper look into it the next days :)
Irrlicht related projects:
Looking for a cool and easy to use GUI? Try the IrrIMGUI bindings for IMGUI: https://github.com/ZahlGraf/IrrIMGUI
Try out my Irrlicht CMake build system: https://github.com/ZahlGraf/IrrlichtCMake
Post Reply