BUMP
After i overlooked the problem i found out that the error lies in the light position.
Its odd since it seems to be all ok. But the light does not cast diffuse/specular in the right place as it should:

Here is my shader code and my shader callback, sorry for the slopy code and comments, i'm testing stuff so most of the things in the shader and inside the callback are temporary...
Shader CallBack and structs:
Code: Select all
///Shader material structures
//material struct
struct ShaderMaterial
{
video::SColorf Ambient;
video::SColorf Emissive;
video::SColorf Diffuse;
video::SColorf Specular;
float Shininess;
};
//light struct
struct ShaderLight
{
core::vector3df Position;
video::SColorf Color;
};
class MyShaderCallback : public video::IShaderConstantSetCallBack
{
public:
scene::ISceneNode *eyeNode;
scene::ISceneNode *lightNode;
ShaderMaterial mat;
ShaderLight light;
MyShaderCallback(scene::ISceneNode *node, scene::ISceneNode *spectator)
{
mat.Ambient = video::SColorf(0, 0, 0);
mat.Emissive = video::SColorf(0, 0, 0);
mat.Diffuse = video::SColorf(0.5, 0.5, 0.9);
mat.Specular = video::SColorf(1, 0, 0);
mat.Shininess = 50;
light.Color = video::SColorf(1, 1, 1);
lightNode = node;
eyeNode = spectator;
}
virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
{
video::IVideoDriver* driver = services->getVideoDriver();
//quando uma variável é passada como argumento da função principal de tipo UNIFORM é necessário o '$' antes do nome no set constant
///Projecion X View X World
core::matrix4 modelViewProj = driver->getTransform(video::ETS_PROJECTION);
modelViewProj *= driver->getTransform(video::ETS_VIEW);
modelViewProj *= driver->getTransform(video::ETS_WORLD);
services->setVertexShaderConstant("modelViewProj", reinterpret_cast<f32*>(&modelViewProj), 16);
//Colors
video::SColorf ambient(0.0f, 0.0f, 1.0f, 0.0f);//RGBA
services->setPixelShaderConstant("globalAmbient", reinterpret_cast<f32*>(&ambient), 3);
services->setPixelShaderConstant("mat", reinterpret_cast<f32*>(&mat), 17);
light.Position = lightNode->getAbsolutePosition();
services->setPixelShaderConstant("light", reinterpret_cast<f32*>(&light), 6);
//Camera position
core::vector3df cameraPos = eyeNode->getAbsolutePosition();
services->setPixelShaderConstant("eyePosition", reinterpret_cast<f32*>(&cameraPos), 3);
}
};
Shader file
Code: Select all
//{Structs
///phong material struct
struct Material
{
float4 Ambient;
float4 Emissive;
float4 Diffuse;
float4 Specular;
float Shininess;
};
///light struct
struct Light
{
float3 Position;
float3 Color;
};
//}
//{
void computeLight(Light activeLight, float3 P, float3 N, float3 eyePos , float shininess,
out float3 diffuseOutput, out float3 specularOutput)
{
//calcula a cor difusa
float3 L = normalize(activeLight.Position - P);
float diffuseLight = max(dot(N, L), 0);
diffuseOutput = activeLight.Color * diffuseLight;
//calcula a cor specular
float3 V = normalize(eyePos - P);
float3 H = normalize(L + V);
float specularLight = pow(max(dot(N, H), 0), shininess);
if(diffuseLight <= 0)
{
specularLight = 0;
}
specularOutput = activeLight.Color * specularLight;
}
//}
//Constants cant be define as global, they get some sort of linking error when it happens
float4x4 modelViewProj;
float3 globalAmbient;
float3 eyePosition;
//material and light variables
Material mat;
Light light;
//{Vertex Shader
void mainVX( in float4 position : POSITION, //posição de entrada
in float4 normal : NORMAL, //normal de entrada
in float2 texCoord : TEXCOORD0,
out float4 oPosition : POSITION, //posição de saída
out float2 oTexCoord : TEXCOORD0,
out float3 objectPos : TEXCOORD1,
out float4 oNormal : TEXCOORD2 )
{
///Multiplicação linear de um vetor 1x4 e uma matriz 4x4, resulta em uma matriz 4x4
oPosition = mul(position, modelViewProj);
objectPos = position.xyz;
oNormal = normal;
oTexCoord = texCoord;
}
//}
//{Pixel Shader
void mainPX( float2 texCoord : TEXCOORD0,
float3 position: TEXCOORD1,
float3 normal: TEXCOORD2,
uniform sampler2D decal,
out float4 oColor: COLOR)
{
float3 emissive = mat.Emissive;
float3 ambient = mat.Ambient * globalAmbient;
//--------------
float3 diffuseLight;
float3 specularLight;
float3 diffuseSum = 0;
float3 specularSum = 0;
computeLight(light, position.xyz, normal, eyePosition, mat.Shininess, diffuseLight, specularLight);
diffuseSum += diffuseLight;
specularSum += specularLight;
float3 diffuse = diffuseSum * mat.Diffuse;
float3 specular = specularSum * mat.Specular;
oColor.rgb = (emissive + ambient + diffuse + specular) * tex2D(decal, texCoord);
oColor.a = 1;
}
//}
The light position is somehow inverted.... And the same problem happeens when i set the light position as the camera position..
Help please?
Thanks!