specularBump code giveaway

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
belfegor
Posts: 383
Joined: Mon Sep 18, 2006 7:22 pm
Location: Serbia

specularBump code giveaway

Post by belfegor »

Hello everybody.
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:

Image

And this is what I get in Irrlicht:

Image

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!!!
Small FPS demo made using Irrlicht&NewtonDEMO
InfoHERE
Its at very early stage but i think im crazy enough to finish it all alone.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Looks pretty nice. I've moved this one because shaders aren't really beginner's stuff :wink:
Do those meshes you mentioned load in Irrlicht without this effects applied? Either it's a problem with the loader or with the data structures available afterwards.
belfegor
Posts: 383
Joined: Mon Sep 18, 2006 7:22 pm
Location: Serbia

Post by belfegor »

It only crashes with this shader aplied.And mvs2005 debugger points to
this line:
scene::IAnimatedMesh* animatedMesh = smgr->getMesh("mesh.x");
in the submenu for "animatedMesh" reads "cannot evaluate expresson".
Small FPS demo made using Irrlicht&NewtonDEMO
InfoHERE
Its at very early stage but i think im crazy enough to finish it all alone.
RapchikProgrammer
Posts: 279
Joined: Fri Dec 24, 2004 6:37 pm

Post by RapchikProgrammer »

I think irrlicht doesnt support binary .x files not sure tho! And bout the 3ds, i am completely baffled at why it wont be able to load a .3ds file thoug! And BTW thanx a lot for the specular bump coding! It is really helpful for me!
belfegor
Posts: 383
Joined: Mon Sep 18, 2006 7:22 pm
Location: Serbia

Post by belfegor »

In version 1.1 it supports binary .x.As i said it only crashes with this shader aplied.
Small FPS demo made using Irrlicht&NewtonDEMO
InfoHERE
Its at very early stage but i think im crazy enough to finish it all alone.
Post Reply