Shaders

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
diho
Posts: 46
Joined: Fri May 20, 2011 9:01 pm
Location: Netherlands

Shaders

Post by diho »

Hello all,

I use the following code, which I found here on the forum:

Code: Select all

 
class MyShaderCallBack : public video::IShaderConstantSetCallBack
{
public:
 
    virtual void OnSetConstants(video::IMaterialRendererServices* services,
                                s32 userData)
    {
        video::IVideoDriver* driver = services->getVideoDriver();
 
        irr::s32 t[] = {0,1,2,3};
 
        services->setVertexShaderConstant("Texture0", (irr::f32*)&t[0], 1);
        services->setVertexShaderConstant("Texture1", (irr::f32*)&t[1], 1);
        services->setVertexShaderConstant("Texture2", (irr::f32*)&t[2], 1);
        services->setVertexShaderConstant("Texture3", (irr::f32*)&t[3], 1);
 
        irr::core::matrix4 mat = driver->getTransform(irr::video::ETS_WORLD);
 
        services->setVertexShaderConstant("WORLD", (irr::f32*)mat.pointer(), 16);
    }
};
 
irr::core::stringc vertexshader =   "varying vec4 pos;"
        "uniform mat4 WORLD;"
        "void main()"
        "{"
        "   gl_TexCoord[0] = gl_MultiTexCoord0;"
        "   pos = WORLD * gl_Vertex;"
        "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
        "}"
        "";
 
irr::core::stringc pixelshader =   "varying vec4 pos;"
        "uniform sampler2D Texture0;"
        "uniform sampler2D Texture1;"
        "uniform sampler2D Texture2;"
        "uniform sampler2D Texture3;"
        "void main()"
        "{"
        "   vec4 col0 = texture2D(Texture0, vec2(gl_TexCoord[0]));"
        "   vec4 col1 = texture2D(Texture1, vec2(gl_TexCoord[0]));"
        "   vec4 col2 = texture2D(Texture2, vec2(gl_TexCoord[0]));"
        "   vec4 col3 = texture2D(Texture3, vec2(gl_TexCoord[0]));"
        "   if (pos.y >= 0.0 && pos.y < 0.01)"
        "       gl_FragColor = col0;"
        "   else if (pos.y >= 0.01 && pos.y < 90.0)"
        "       gl_FragColor = col1;"
        "   else if (pos.y >= 90.0 && pos.y < 160.0)"
        "       gl_FragColor = col2;"
        "   else if (pos.y >= 160.0)"
        "       gl_FragColor = col3;"
        "}"
        "";
 
and for the terrain node:

Code: Select all

 
irr::s32 Shader = driver->getGPUProgrammingServices()->addHighLevelShaderMaterial(
                    vertexshader.c_str(), "main", irr::video::EVST_VS_5_0,
                    pixelshader.c_str(), "main", irr::video::EPST_PS_5_0,
                    new MyShaderCallBack,
                    irr::video::EMT_SOLID,
                    0);
 
            terrainArray[i][j]->setMaterialType((irr::video::E_MATERIAL_TYPE)Shader);
 
            terrainArray[i][j]->setMaterialTexture(0, driver->getTexture("../../media/terrain/extra_textures/Water.jpg"));
            terrainArray[i][j]->setMaterialTexture(1, driver->getTexture("../../media/terrain/extra_textures/Grass.jpg"));
            terrainArray[i][j]->setMaterialTexture(2, driver->getTexture("../../media/terrain/extra_textures/Mud.jpg"));
            terrainArray[i][j]->setMaterialTexture(3, driver->getTexture("../../media/terrain/extra_textures/Rock.jpg"));
 
Strange enough all the terrain uses is the water texture, even at the highest hill.

Does anyone know an explanation why this is happening?

-Diho
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Shaders

Post by mongoose7 »

Change

Code: Select all

services->setVertexShaderConstant("Texture0", (irr::f32*)&t[0], 1);
into

Code: Select all

services->setVertexShaderConstant("Texture0", &t[0], 1);
in all cases. Samplers must be integers.
diho
Posts: 46
Joined: Fri May 20, 2011 9:01 pm
Location: Netherlands

Re: Shaders

Post by diho »

Thanks mongoose7, that fixed it
Post Reply