How?...
I've only used the IShaderConstantSetCallBack::OnSetConstants function so far.
using material values inside custom shader?
Re: using material values inside custom shader?
Read the shader example, Example 10.
Re: using material values inside custom shader?
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.
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.
Re: using material values inside custom shader?
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:
Not sure about HLSL - I think they would use a float4:
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:
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:
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).
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;
Code: Select all
float4 myAmbient;
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);
Code: Select all
services->setVertexShaderConstant(AmbientID, reinterpret_cast<f32*>(&MaterialAmbient), 4);
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: using material values inside custom shader?
Can you set material.ColorMaterial = video::ECM_AMBIENT and just read glFrontColor in the shader?
Re: using material values inside custom shader?
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: using material values inside custom shader?
Thanks cuteAlien, like I had guessed. Works well.