Well, at this rate I'll have all my problems solved very quicky
I figured it out, and to those of you using RenderMonkey, swap your viewMatrix and position around, it helps quite a bit
Hmm, it works, but not how its supposed to. I believe that I am messing the info up from Irrlicht to the shader. For example, a specular highlight won't behave correctly and the lighting is incorrect. Maybe someone could help find what's wrong here, or maybe point me to some good tutorials.
--main.cpp--
class MyShaderCallBack : public video::IShaderConstantSetCallBack
{
public:
virtual void OnSetConstants(video::IMaterialRendererServices* services)
{
video::IVideoDriver* driver = services->getVideoDriver();
//Setup for vertex-----------------------------------
//get the view matrix
core::matrix4 View = driver->getTransform(video::ETS_VIEW);
services->setVertexShaderConstant("view_matrix", &View.M[0], 16);
//get the view proj matrix
core::matrix4 viewProj;
viewProj = driver->getTransform(video::ETS_PROJECTION);
viewProj *= driver->getTransform(video::ETS_VIEW);
viewProj *= driver->getTransform(video::ETS_WORLD);
services->setVertexShaderConstant("view_proj_matrix", &viewProj.M[0], 16);
video::SColorf lightDir(0.5f,0.5f,0.5f,0.0f); //must be better way of storing a float4 for the shader
services->setVertexShaderConstant("lightDir", reinterpret_cast<f32*>(&lightDir), 4);
//Setup for Pixel--------------------------------------
//set lighting info (want to get from real light)
//Ambient
video::SColorf ambientC(0.7f,0.8f,1.0f,1.0f); //rbgw
irr::f32 ambientK = 0.3f; //ambient brightness
services->setPixelShaderConstant("ambient", reinterpret_cast<f32*>(&ambientC), 4); //send amb colour
services->setPixelShaderConstant("Ka", &ambientK, 1); //set amb power
//Diffuse
video::SColorf diffuseC(1.0f,0.9f,1.0f,0.85f); //rbgw
float diffuseK = 0.8f; //ambient brightness
services->setPixelShaderConstant("diffuse", reinterpret_cast<f32*>(&diffuseC), 4); //send diff colour
services->setPixelShaderConstant("Kd", &diffuseK, 1); //set diff power
//Spec
video::SColorf specC(1.0f,0.8f,0.7f,1.0f); //rbgw
float specK = 1.0; //spec brightness
services->setPixelShaderConstant("specular", reinterpret_cast<f32*>(&specC), 4); //send spec colour
services->setPixelShaderConstant("Ks", &specK, 1); //set spec power
//Spec Exponent
float specP2 = 20.0;
services->setPixelShaderConstant("n_specular", &specP2, 1); //set spec exp
}
};
--shader1_vs.hlsl--
float4x4 view_matrix;
float4x4 view_proj_matrix;
float4 lightDir;
struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 Norm : TEXCOORD0;
float3 View : TEXCOORD1;
float3 Light : TEXCOORD2;
float2 Tex : TEXCOORD4;
};
VS_OUTPUT vertexMain(
float4 inPos : POSITION,
float3 inNorm : NORMAL,
float2 inTex : TEXCOORD0 )
{
VS_OUTPUT Out = (VS_OUTPUT) 0;
// Output transformed position:
Out.Pos = mul( inPos, view_proj_matrix );
// Output light vector:
Out.Light = -lightDir;
// Compute position in view space:
float3 Pview = mul( inPos, view_matrix);
// Transform the input normal to view space:
Out.Norm = normalize( mul( inNorm, view_matrix ) );
// Compute the view direction in view space:
Out.View = - normalize( Pview );
// Propagate texture coordinate for the object:
Out.Tex = inTex;
return Out;
}
--shader1_ps.hlsl--
float4 ambient;
float Ks;
float4 diffuse;
float Kd;
float4 specular;
float n_specular;
float Ka;
sampler baseMap;
float4 pixelMain( float4 Diff : COLOR0,
float3 Normal : TEXCOORD0,
float3 View : TEXCOORD1,
float3 Light : TEXCOORD2,
float2 Tex : TEXCOORD4 ) : COLOR
{
// Compute the reflection vector:
float3 vReflect = normalize( 2 * dot( Normal, Light) * Normal - Light );
// Compute ambient term:
float4 AmbientColor = ambient * Ka;
// Compute diffuse term:
float4 DiffuseColor = diffuse * Kd * max( 0, dot( Normal, Light ));
// Compute specular term:
float4 SpecularColor = specular * Ks * pow( max( 0, dot(vReflect, View)), n_specular );
float4 FinalColor = (AmbientColor + DiffuseColor) * tex2D( baseMap, Tex) + SpecularColor;
return FinalColor;
}