First of all I want to tell you that i don’t know nothing about shaders
but I will learn.
Wish I had more time .I have to work 10 hour per day!!!
And I don’t even have internet on my computer.
After lot of brainstorming and hairpulling i get what was I looking for.
And now that it works I wont it to share with you.
This is my first attempt of importing shader from RenderMonkey to Irrlicht.
This is a pic from RenderMonkey:

And this is what I get in Irrlicht:

And this is a cpp and hlsl:
********** main.cpp **********
#include <irrlicht.h>
#include <iostream>
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
IrrlichtDevice* device = 0;
class MyEventReceiver: public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key == KEY_ESCAPE && !event.KeyInput.PressedDown)
{
device->closeDevice();
}
return false;
}
};
class MyShaderCallBack: public video::IShaderConstantSetCallBack
{
public:
virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
{
video::IVideoDriver* driver = services->getVideoDriver();
//PIXEL SHADER CONSTANTS
video::SColorf specular(1.0f, 0.95f, 0.67f, 1.0f);
video::SColorf ambient(0.92f, 0.93f, 0.93f, 1.0f);
video::SColorf diffuse(0.84f, 0.86f, 0.89f, 1.0f);
float Ka = 0.3f;
float Kd = 1.0f;
float Ks = 1.0f;
float specular_power = 64.0f;
float bumpiness = 1.0f;
//VIEW PROJECTION
core::matrix4 view_proj_matrix;
view_proj_matrix = driver->getTransform(video::ETS_PROJECTION);
view_proj_matrix *= driver->getTransform(video::ETS_VIEW);
view_proj_matrix *= driver->getTransform(video::ETS_WORLD);
view_proj_matrix.getTransposed();
services->setVertexShaderConstant("view_proj_matrix", &view_proj_matrix.M[0], 16);
//INVERSE VIEW PROJECTION
core::matrix4 inv_view_matrix = driver->getTransform(video::ETS_WORLD);
inv_view_matrix.makeInverse();
services->setVertexShaderConstant("inv_view_matrix", &inv_view_matrix.M[0], 16);
//EYE/CAMERA POSITION
core::vector3df eye_position = device->getSceneManager()->getActiveCamera()->getAbsolutePosition();
services->setVertexShaderConstant("eye_position", reinterpret_cast<f32*>(&eye_position), 4);
//LIGHT POSITION
//video::SColorf light_position(-100, 100, -100, 1);
core::vector3df light_position = device->getSceneManager()->getActiveCamera()->getAbsolutePosition();//change this
services->setVertexShaderConstant("light_position", reinterpret_cast<f32*>(&light_position), 4);
//COLORS
services->setPixelShaderConstant("ambient", reinterpret_cast<f32*>(&ambient), 4);
services->setPixelShaderConstant("diffuse", reinterpret_cast<f32*>(&diffuse), 4);
services->setPixelShaderConstant("specular", reinterpret_cast<f32*>(&specular), 4);
//VARIABLES
services->setPixelShaderConstant("specular_power", &specular_power, 1);
services->setPixelShaderConstant("Ka", &Ka, 1);
services->setPixelShaderConstant("Kd", &Kd, 1);
services->setPixelShaderConstant("Ks", &Ks, 1);
services->setPixelShaderConstant("bumpiness", &bumpiness, 1);
}
};
int main()
{
MyEventReceiver receiver;
device = createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(600, 600), 32, false, true, false, &receiver);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* guienv = device->getGUIEnvironment();
c8* vsFileName = 0;
c8* psFileName = 0;
vsFileName = "specularBump.hlsl";
psFileName = vsFileName;
video::IGPUProgrammingServices* gpu = driver->getGPUProgrammingServices();
s32 newMaterialType = 0;
if(gpu)
{
MyShaderCallBack* mc = new MyShaderCallBack();
newMaterialType = gpu->addHighLevelShaderMaterialFromFiles(
vsFileName, "vertexMain", video::EVST_VS_2_0,
psFileName, "pixelMain", video::EPST_PS_2_0,
mc, video::EMT_SOLID);
mc->drop();
}
//CAMERA
scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
cam->setPosition(core::vector3df(-50,100,-100));
cam->setTarget(core::vector3df(0,0,0));
device->getCursorControl()->setVisible(false);
//MESH
scene::IAnimatedMesh* animatedMesh = smgr->getMesh("mesh.x");
scene::IAnimatedMeshSceneNode* meshNode = smgr->addAnimatedMeshSceneNode(
animatedMesh,0,-1,core::vector3df(0,0,0));
meshNode->setMaterialTexture(0, driver->getTexture("mesh.tga"));//diffuse map
meshNode->setMaterialTexture(1, driver->getTexture("mesh_n.tga"));//normal map
meshNode->setMaterialType((video::E_MATERIAL_TYPE)newMaterialType);
//MAIN LOOP
int lastFPS = -1;
while(device->run())
if (device->isWindowActive())
{
driver->beginScene(true, true, video::SColor(255, 128, 128, 128));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Irrlicht v1.1 : specularBump [";
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
device->drop();
return 0;
}
********** specularBump.hlsl **********
float4x4 view_proj_matrix : ViewProjection;
float4 light_position;
float4 eye_position;
float4x4 inv_view_matrix : ViewInverse;
struct VS_INPUT_STRUCT
{
float4 position: POSITION;
float3 normal: NORMAL;
float2 texcoord0: TEXCOORD0;
float3 binormal: BINORMAL0;
float3 tangent: TANGENT0;
};
struct VS_OUTPUT_STRUCT
{
float4 position: POSITION;
float2 bump_map: TEXCOORD0;
float3 light_vector: TEXCOORD1;
float3 half_angle: TEXCOORD2;
};
VS_OUTPUT_STRUCT vertexMain( VS_INPUT_STRUCT vsInStruct )
{
VS_OUTPUT_STRUCT vsOutStruct;
vsOutStruct.position = mul( vsInStruct.position, view_proj_matrix);//swaped parameters!!!
vsOutStruct.bump_map = vsInStruct.texcoord0;
float3 temp_light_position = mul( inv_view_matrix, light_position );
float3 temp_light_vector = temp_light_position - vsInStruct.position;
vsOutStruct.light_vector.x = dot( temp_light_vector, vsInStruct.tangent );
vsOutStruct.light_vector.y = dot( temp_light_vector, vsInStruct.binormal );
vsOutStruct.light_vector.z = dot( temp_light_vector, vsInStruct.normal );
float3 temp_eye_position = mul( inv_view_matrix, eye_position );
float3 temp_view_vector = temp_eye_position - vsInStruct.position;
float3 temp_view_vector2;
temp_view_vector2.x = dot( temp_view_vector, vsInStruct.tangent );
temp_view_vector2.y = dot( temp_view_vector, vsInStruct.binormal );
temp_view_vector2.z = dot( temp_view_vector, vsInStruct.normal );
vsOutStruct.half_angle = vsOutStruct.light_vector + temp_view_vector2;
return vsOutStruct;
}
float4 specular;
float Ka;
float Kd;
float Ks;
float specular_power;
float bumpiness;
float4 ambient;
float4 diffuse;
sampler base_map;
sampler bump_map;
struct PS_INPUT_STRUCT
{
float2 bump_map: TEXCOORD0;
float3 light_vector: TEXCOORD1;
float3 half_angle: TEXCOORD2;
};
struct PS_OUTPUT_STRUCT
{
float4 color0: COLOR0;
};
PS_OUTPUT_STRUCT pixelMain( PS_INPUT_STRUCT psInStruct )
{
PS_OUTPUT_STRUCT psOutStruct;
float3 base = tex2D( base_map, psInStruct.bump_map );
float3 bump = tex2D( bump_map, psInStruct.bump_map );
float3 normalized_light_vector = normalize( psInStruct.light_vector );
float3 normalized_half_angle = normalize( psInStruct.half_angle );
float3 smooth = { 0.5f, 0.5f, 1.0f };
bump = lerp( smooth, bump, bumpiness );
bump = normalize( ( bump * 2.0f ) - 1.0f );
float4 n_dot_l = dot( bump, normalized_light_vector );
float4 n_dot_h = dot( bump, normalized_half_angle );
psOutStruct.color0.rgb =
( base * ambient * Ka ) +
( base * diffuse * Kd * max( 0, n_dot_l ) ) +
( specular * Ks * pow( max( 0, n_dot_h ), specular_power ) );
psOutStruct.color0.a = 1.0f;
return psOutStruct;
}
NOTE:
I wish to thank “_Synthesizer” for the tip(I was reading post from
November 11, 2005)about swaping parameters.
“vsOutStruct.position = mul(?,?);” I was getting strange results
(I wished to blow my head off with shotgun)until I swap them.
And i wish to thank Nikolaus Gebhardt
for this wonderful engine.THANK YOU VERY MUCH BOTH!!!
IMPORTANT:
When i use mesh.3ds it crashed my Irrlicht.
When i use mesh.x as binary file it crashed my Irrlicht.
Can someone explain this please, maybe it’s a bug
and it may cause problems with other shaders to?
I am using 3dsMAX7 with “Quest3D_Max6_X_Exporter” plugin
but when I export it as text file it works.
I only tested it with cube tell me what you get
(post some pictures).
I hope I ll get some more to work in the future.
And to make my own of course.
ENJOY!!!