after playing arround with irrlicht for a while i started to implement my shaders to irrlicht. so i wanted to convert from my old dx9 project a hlsl shader to irrlicht. after reading the shader tutorial and some post on the forum i started to implement my hlsl shader and failed.
screen shows nothing.
i have tested the code without a shader class just to look that everthing shows up on the screen and its working.
i show you parts of my code what i have done so far.
( code seems abit broken in the format . sorry for that )
first the shader class i have implement from the tutorial.
dont wonder about the const instructions. its just for testing.
Code: Select all
class MyShaderCallBack : public video::IShaderConstantSetCallBack
{
public:
virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
{
// some testvalues for the shader
const float lightPosition[] = { 100.0f,100.0f,100.0f,0.0f };
const float lightColor[] = { 1.0f,1.0f,1.0f,1.0f };
const float lightAmbient[] = { 1.0f,1.0f,1.0f,1.0f };
const float materialSpecular[] = { 1.0f,1.0f,1.0f,1.0f };
float shininess = 128.0f;
video::IVideoDriver* driver = services->getVideoDriver();
// set clip matrix
core::matrix4 model;
core::matrix4 modelviewproj;
core::matrix4 modelview;
core::matrix4 view;
core::matrix4 proj;
model = driver->getTransform(video::ETS_WORLD);
view = driver->getTransform(video::ETS_VIEW);
proj = driver->getTransform(video::ETS_PROJECTION);
modelviewproj = ( model * view ) * proj ;
modelview = model * view;
// set vertexshader values
services->setVertexShaderConstant("modelviewproj", &modelviewproj.M[0] ,16);
services->setVertexShaderConstant("modelview", &modelview.M[0] ,16);
services->setVertexShaderConstant("view", &view.M[0] ,16);
services->setVertexShaderConstant("lightPosition", &lightPosition[0] , 4);
// set pixelshader values
services->setPixelShaderConstant("lightColor", &lightColor[0] , 4);
services->setPixelShaderConstant("lightAmbient", &lightAmbient[0] , 4);
services->setPixelShaderConstant("materialSpecular", &materialSpecular[0] , 4);
services->setPixelShaderConstant("shininess", &shininess , 1);
}
};
Code: Select all
psFileName = "media/perpixelspec.hlsl";
vsFileName = psFileName; // both shaders are in the same file
video::IGPUProgrammingServices* gpu = driver->getGPUProgrammingServices();
s32 newMaterialType1 = 0;
if ( gpu )
{
MyShaderCallBack* mc = new MyShaderCallBack();
newMaterialType1 = gpu->addHighLevelShaderMaterialFromFiles(
vsFileName, "view_space", video::EVST_VS_1_1,
psFileName, "normal_map", video::EPST_PS_2_0,
mc, video::EMT_NORMAL_MAP_SOLID);
mc->drop();
}
// node of the bsp file
scene::ISceneNode* node = 0;
irr::core::stringc textureName;
irr::core::stringc textureName2;
// convert the mesh to tangent binormal mesh
if ( mesh )
{
scene::IMesh* tangentMesh = smgr->getMeshManipulator()->createMeshWithTangents(
mesh->getMesh(0));
node = smgr->addMeshSceneNode ( tangentMesh );
for ( int iMatCount = 0; iMatCount<node->getMaterialCount(); iMatCount++ )
{
textureName = node->getMaterial(iMatCount).Textures[0]->getName();
textureName2 = textureName.subString(0,textureName.size()-4)+"_normal.tga";
node->getMaterial(iMatCount).Textures[1] = driver->getTexture(textureName2.c_str());
node->getMaterial(iMatCount).MaterialType = (video::E_MATERIAL_TYPE)newMaterialType1;
}
tangentMesh->drop();
}
Code: Select all
//------------------------------------
float4x4 modelviewproj;
float4x4 modelview;
float4x4 view;
float4 lightPosition;
float4 lightColor;
float4 lightAmbient;
float4 materialSpecular;
float shininess;
sampler diffuseTexture : register(s0);
sampler normalTexture : register(s1);
struct a2v
{
float4 pos : POSITION;
float3 normal : NORMAL;
float4 color : COLOR0;
float2 txcoord : TEXCOORD0;
float3 tangent : TANGENT0;
float3 binormal : BINORMAL0;
};
struct v2f
{
float4 hpos : POSITION;
float2 txcoord : TEXCOORD0;
float3 vpos : TEXCOORD1;
float3 tangent : TEXCOORD2;
float3 binormal : TEXCOORD3;
float3 normal : TEXCOORD4;
float3 viewVector : TEXCOORD5;
float3 lightVector : TEXCOORD6;
};
v2f view_space(a2v IN)
{
v2f OUT;
// vertex position in object space
float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);
// compute modelview rotation only part
float3x3 modelviewrot;
modelviewrot[0]=modelview[0].xyz;
modelviewrot[1]=modelview[1].xyz;
modelviewrot[2]=modelview[2].xyz;
// vertex position in clip space
OUT.hpos=mul(pos,modelviewproj);
// vertex position in view space (with model transformations)
OUT.vpos=mul(pos,modelview).xyz;
// light position in view space
float4 lp=mul(lightPosition,view);
// view and light directions
OUT.viewVector = normalize(OUT.vpos);
OUT.lightVector = normalize(lp.xyz-OUT.vpos);
// tangent space vectors in view space (with model transformations)
OUT.tangent=mul(IN.tangent,modelviewrot);
OUT.binormal=mul(IN.binormal,modelviewrot);
OUT.normal=mul(IN.normal,modelviewrot);
// copy texture coordinates
OUT.txcoord=IN.txcoord.xy;
return OUT;
}
float4 normal_map( v2f IN, uniform sampler2D diffuseTexture, uniform sampler2D normalTexture) : COLOR
{
float4 normal=tex2D(normalTexture,IN.txcoord);
normal.xy=normal.xy*2.0-1.0; // trafsform to [-1,1] range
normal.z=sqrt(1.0-dot(normal.xy,normal.xy)); // compute z component
// transform normal to world space
normal.xyz=normalize(normal.x*IN.tangent-normal.y*IN.binormal+normal.z*IN.normal);
// color map
float4 color=tex2D(diffuseTexture,IN.txcoord);
// compute diffuse and specular
float att=saturate(dot(IN.lightVector,IN.normal.xyz));
float diff=saturate(dot(IN.lightVector,normal.xyz));
float spec=saturate(dot(normalize(IN.lightVector-IN.viewVector),normal.xyz));
// compute final color
float4 finalcolor;
finalcolor.xyz=lightAmbient*color.xyz+
att*(color.xyz*lightColor.xyz*diff+materialSpecular.xyz*pow(spec,shininess));
finalcolor.w=1.0;
return finalcolor;
}
the input stream of the vertices.
Code: Select all
struct a2v
{
float4 pos : POSITION;
float3 normal : NORMAL;
float4 color : COLOR0;
float2 txcoord : TEXCOORD0;
float3 tangent : TANGENT0;
float3 binormal : BINORMAL0;
};
i get the S3DVertexTangents format.
but iam not sure if the order of the vertex information is the same as i
declared in my shader input stream.
any help would be nice if i have missed something or have done wrong.
the shader itself works on my old dx9 project.