Using these shaders with Irrlicht

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
Jibreel Yahya
Posts: 10
Joined: Fri Oct 23, 2015 7:59 pm

Using these shaders with Irrlicht

Post by Jibreel Yahya »

Hi, guys.

Looking for shaders on the internet I found one for dynamic reflections with dual tone sphere mapping on the link:
http://www.irrlicht3d.org/wiki/index.ph ... ByOmaremad

Since I'm new to Irrlicht, I tried to implement these shaders using Irrlicht calls, but I was not successful.
The program compiles fine, but I get an error message in the shader:
GLSL shader failed to compile
ERROR: 0:1: '.' : syntax error syntax error

Some help?

Thanks.

ref.frag

Code: Select all

uniform vec4 fvLowTone;
uniform vec4 fvSpecular;
uniform vec4 fvHighTone;
 
uniform float fSpecularPower;
 
uniform sampler2D baseMap;
uniform sampler2D cube;
 
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying vec3 Normal;
varying vec2 reflcoord;
 
void main( void )
{
   vec3  fvLightDirection = normalize( LightDirection );
   vec3  fvNormal         = normalize( Normal );
   float fNDotL           = dot( fvNormal, fvLightDirection );
 
   vec3  fvReflection     = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );
   vec3  fvViewDirection  = normalize( ViewDirection );
   float fRDotV           = max( 0.0, dot( fvReflection, fvViewDirection ) );
 
   vec4  fvBaseColor      = texture2D( baseMap, Texcoord );
 
   vec4  fvTotalAmbient   = fvLowTone * fvBaseColor;
   vec4  fvTotalDiffuse   = fvHighTone * (fNDotL) * fvBaseColor;
   vec4  fvTotalSpecular  = fvSpecular * ( pow( fRDotV, fSpecularPower ) );
   float fresnel =(1/dot( Normal, fvViewDirection) )/5;
   gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular )+(fresnel*texture2D(cube,reflcoord))+(fresnel /2);
 
}
ref.vert

Code: Select all

uniform vec3 fvLightPosition;
uniform vec3 fvEyePosition;
 
uniform mat4 matWorldInverseTranspose;
 
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying vec3 Normal;
varying vec2 reflcoord;
 
void main( void )
{
   gl_Position = ftransform();
   Texcoord    = gl_MultiTexCoord0.xy;
 
   vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
 
   ViewDirection  = fvEyePosition - fvObjectPosition.xyz;
   vec3 ViewDirectionn=normalize(ViewDirection);
   LightDirection = fvLightPosition - fvObjectPosition.xyz;
   Normal         = gl_NormalMatrix * gl_Normal;
   vec3 normal2=vec4(Normal,1)*matWorldInverseTranspose;
 
   vec3 fin=ViewDirectionn-(2*(dot(normalize(normal2),ViewDirectionn))*normal2);
   float p=sqrt(pow(fin.x,2)+pow(fin.y,2)+pow((fin.z+1),2));
 
 
   reflcoord=vec2(((fin.x/(2*p))+1/2),((fin.y/(2*p))+1/2));
}
main.cpp

Code: Select all

#include <irrlicht.h>
#pragma comment(lib, "irrlicht.lib")
 
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
class shaderCallback : public video::IShaderConstantSetCallBack
{
    public:
 
        video::SColorf lowTone;
        video::SColorf highTone;
        video::SColorf specular;
        f32 specularPower;
 
        core::vector3df lightPosition;
        core::vector3df eyePosition;
 
    private:
        
        void OnSetConstants( video::IMaterialRendererServices* services, s32 userData ) override
        {
            core::matrix4 invWorld = services->getVideoDriver()->getTransform(video::ETS_WORLD);
            invWorld.makeInverse();
            
            services->setVertexShaderConstant("matWorldInverseTranspose", invWorld.pointer(), 16);
            services->setVertexShaderConstant("fvLightPosition", &lightPosition.X, 3);
            services->setVertexShaderConstant("fvEyePosition", &eyePosition.X, 3);
 
            services->setPixelShaderConstant("fvLowTone", &lowTone.r, 4);
            services->setPixelShaderConstant("fvHighTone", &highTone.r, 4);
            services->setPixelShaderConstant("fvSpecular", &specular.r, 4);
            services->setPixelShaderConstant("fvSpecularPower", &specularPower, 1);
 
            s32 baseMap = 1;
            s32 cube = 2;
            services->setPixelShaderConstant("baseMap", &baseMap, 1);
            services->setPixelShaderConstant("baseMap", &cube, 1);
        }
};
 
int main()
{
    // Create device
    video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
 
    // start up the engine
    SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
    params.AntiAlias = true;
    params.DriverType = video::EDT_OPENGL;
    params.WindowSize = core::dimension2d<u32>(800, 600);
    IrrlichtDevice *device = createDeviceEx(params);
    if (!device) {
        printf("Error creating Irrlicht device\n");
        return 0;
    }
 
    // Obtain device internals
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    // Set a window caption
    device->setWindowCaption(L"NormalMapShader - Irrlicht Engine Demo");
 
    // Create GLSL shaders
    video::IGPUProgrammingServices* gpu = driver->getGPUProgrammingServices();
    s32 mtlToonShader = video::EMT_SOLID; // Fallback material type
 
        shaderCallback* callback = new shaderCallback;
 
        callback->highTone = SColorf(1, 1, 1);
        callback->lowTone = SColorf(2, 2, 2);
        callback->specular = SColorf(5, 0, 0);
        callback->lightPosition = vector3df(0, 0, 0);
        callback->eyePosition = vector3df(0, 0, 0);
        callback->specularPower = 20.f;
 
        mtlToonShader = gpu->addHighLevelShaderMaterial(
            "ref.vert", "main", video::EVST_VS_1_1,
            "ref.frag", "main", video::EPST_PS_1_1,
            callback, video::EMT_SOLID);
 
    // Add an animated mesh
    IAnimatedMesh* mesh = smgr->getMesh("cycle/light-cycle.obj");
    ISceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
    if (node)
    {
        node->setMaterialFlag(EMF_LIGHTING, false);
        node->setMaterialTexture(0, driver->getTexture("cycle/cycle_diffuse.png"));
        node->setMaterialTexture(1, driver->getTexture("cycle/cycle_normal.png"));
        node->setMaterialType((video::E_MATERIAL_TYPE)mtlToonShader); // Override material type
    }
 
    // Add a viewing camera
    smgr->addCameraSceneNodeFPS(0, 100, 0.01);
 
    // Main rendering loop
    while (device->run())
    {
        driver->beginScene(true, true, SColor(150, 150, 150, 150));
        smgr->drawAll();
        driver->endScene();
    }
 
    device->drop();
 
    // Done
    return 0;
}
 
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Using these shaders with Irrlicht

Post by CuteAlien »

Instead of addHighLevelShaderMaterial use addHighLevelShaderMaterialFromFiles. Otherwise you don't use the files but pass shader-programs as strings. Your complete vertex-shader code is for example "ref.vert" - that's why you get an error in line 0 about that dot :-)
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
Jibreel Yahya
Posts: 10
Joined: Fri Oct 23, 2015 7:59 pm

Re: Using these shaders with Irrlicht

Post by Jibreel Yahya »

Thank you, @CuteAlien.

I did as you said, but now there's another bug in the shader:
GLSL shader failed compile
ERROR: 0:24: '=' : cannot convert from '4-component vector of float' to '3-component vector of float'


I'm using the Irrlicht 1.8.4.
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Using these shaders with Irrlicht

Post by CuteAlien »

The error tells what is wrong (and gives a line). I suspect it's this one:
vec3 normal2=vec4(Normal,1)*matWorldInverseTranspose;

Should probably be something like:
vec3 normal2=vec3(vec4(Normal,1)*matWorldInverseTranspose);

But no guarantees (I didn't compile). You find info about that stuff on the web (for example: https://en.wikibooks.org/wiki/GLSL_Prog ... Operations)
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
Jibreel Yahya
Posts: 10
Joined: Fri Oct 23, 2015 7:59 pm

Re: Using these shaders with Irrlicht

Post by Jibreel Yahya »

Thank you, CuteAlien.

It works fine now. You're the Doom Guy! :wink:
kornwaretm
Competition winner
Posts: 189
Joined: Tue Oct 16, 2007 3:53 am
Location: Indonesia
Contact:

Re: Using these shaders with Irrlicht

Post by kornwaretm »

if i may add. it is importan to put #version directive on top of your shader, without it it may break on other machine
Post Reply