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);
}
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));
}
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;
}