using material values inside custom shader?

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
Max Power
Posts: 98
Joined: Wed Mar 14, 2012 10:09 am

using material values inside custom shader?

Post by Max Power »

How?...

I've only used the IShaderConstantSetCallBack::OnSetConstants function so far.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: using material values inside custom shader?

Post by mongoose7 »

Read the shader example, Example 10.
Max Power
Posts: 98
Joined: Wed Mar 14, 2012 10:09 am

Re: using material values inside custom shader?

Post by Max Power »

I did, but it doesn't say how I would use the material's set ambient color, for example, inside the shader.

I would set it as a constant, if I knew how to get to it...

I guess I will need to use onSetMaterial and store the material-variables inside the derived callback class.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: using material values inside custom shader?

Post by CuteAlien »

You have to pass each variable you want to use on the card to the card. In GLSL you have the uniform variables for this. I think in HLSL they are just globals.
So first create such a variable in the shader where you want to use it.
In GLSL it would be:

Code: Select all

 
uniform vec4 myAmbient;
 
Not sure about HLSL - I think they would use a float4:

Code: Select all

 
float4 myAmbient;
 
The ambient color is likely used in the vertex-shader and added to the vertex color before it's passed on to the pixel shader.

In the CPU side you have 3 steps. First you get an id for the name of the variable (as it's used in the shader), like:
AmbientID = services->getVertexShaderConstantID("myAmbient");
You need to do that only once at the first run of OnSetConstants.

Next you need to get the value of the current material, that's unfortunately not part of the shader example.
In MyShaderCallback::OnSetMaterial there is the current material set. So you save it into variable in your MyShaderCallback class:

Code: Select all

 
// MaterialAmbient is SColorf member variable
// material is the parameter passed in OnSetMaterial
MaterialAmbient = SColorf(material.AmbientColor);
 
Last you pass that value to the graphic-card each time OnSetConstants is called, using the ID you got for the shader-variable in the first run of OnSetConstants:

Code: Select all

 
services->setVertexShaderConstant(AmbientID, reinterpret_cast<f32*>(&MaterialAmbient), 4);
 
If you need another example you can take a look at my racer sources, this is the file with my Shader callbacks: https://bitbucket.org/mzeilfelder/trunk ... at=default
The corresponding shaders are in the shaders folder: https://bitbucket.org/mzeilfelder/trunk ... at=default
All names starting with ES2 are my custom shaders (the rest are the shaders from Irrlicht's ES2 branch).
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
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: using material values inside custom shader?

Post by mongoose7 »

Can you set material.ColorMaterial = video::ECM_AMBIENT and just read glFrontColor in the shader?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: using material values inside custom shader?

Post by CuteAlien »

Don't know.
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
Max Power
Posts: 98
Joined: Wed Mar 14, 2012 10:09 am

Re: using material values inside custom shader?

Post by Max Power »

Thanks cuteAlien, like I had guessed. Works well.
Post Reply