I have a problem with one of my shaders, I took it from the Microsoft shader tutorial and the first issue I had with it was that when I placed an object in the scene that was rendered with it that it flipped the objects position. For example if I have the object a position of (0,0,-8) this would normally put the object directly in front of my camera, but with this shader it placed it behind me. I figured this shader was for a left handed system where XNA is right (maybe it's the other way around) so I decided to flip the variables in the shader to fix this, so setting the World *= -1 etc.. and this worked a treat.
Now the issue I have is that when I rotate the model; it's position in the world is also rotated, for example, if I rotate the object with object.Rotate(new Vector3(0,1,0),MathHelper.PiOver2) the object then has the same position value but in the rendered world it appears to the right of my camera. I have fixed this issue but I would like a better solution. The fix is in the SetParameters of the shader class and all it does is swap the way the World matrix is calculated from scale * rotation * position to scale * position * rotation
Here is my fix:
Matrix World;
if (!myObject.AlwaysFacingCamera)
{
if (myObject.UseLeftHandedWorldCalc)
{
World = Matrix.CreateScale(myObject.Scaling) *
Matrix.CreateTranslation(myObject.Position) *
Matrix.CreateFromQuaternion(myObject.Rotation);
}
else
{
World = Matrix.CreateScale(myObject.Scaling) *
Matrix.CreateFromQuaternion(myObject.Rotation) *
Matrix.CreateTranslation(myObject.Position);
}
}
else
{
World = Matrix.CreateScale(myObject.Scaling) *
Matrix.CreateFromQuaternion(RCCameraManager.ActiveCamera.Rotation*-1) *
Matrix.CreateTranslation(myObject.Position);
}
This works a treat now but I would like a better solution to this, but my weak shader knowledge has stopped me from being able to convert the shader. If any of you guys could help that would be great

If there is a better way of doing this in code I would not be adverse to that either, but a fix to the shader or a guide on how to do this would be best.
Here is the shader after my first fix
float3 DirFromLight < string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};
float3 DirFromSky < string UIDirectional = "Direction from Sky"; > = { 0.0f, -1.0f, 0.0f };
// light intensity
float4 LightAmbientIntensity = { 0.8f, 0.8f, 0.8f, 1.0f }; // ambient
float4 LightSpecularColor = { 1.0f, 1.0f, 1.0f, 1.0f }; // specular
// material reflectivity
float4 MaterialAmbientIntensity = { 0.5f, 0.5f, 0.5f, 1.0f }; // ambient
float4 MaterialSpecularColor = { 0.2f, 0.2f, 0.2f, 1.0f }; // specular
int MaterialSpecularPower = 32; // power
// transformations
float4x3 World : WORLD;
float4x3 View : VIEW;
float4x4 ViewProjection : VIEWPROJECTION;
float3 CameraPos : CAMERAPOSITION;
texture EnvironmentMap
<
string type = "CUBE";
string name = "default_reflection.dds";
>;
samplerCUBE EnvironmentSampler = sampler_state
{
Texture = (EnvironmentMap);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
float4 CubeMapLookup(float3 CubeTexcoord)
{
return texCUBE(EnvironmentSampler, CubeTexcoord);
}
float3 CalcReflectionVector(float3 ViewToPos, float3 Normal)
{
return reflect(ViewToPos, Normal);
}
float4 CalcAmbient()
{
return LightAmbientIntensity * MaterialAmbientIntensity;
}
float4 CalcSpecular(float3 Normal, float3 DirFromLight, float3 EyeToVertex)
{
float3 R = normalize(reflect(DirFromLight, Normal));
return MaterialSpecularColor * LightSpecularColor * pow(max(0, dot(R, -EyeToVertex)), MaterialSpecularPower/4);
}
// vertex shader output structure
struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Diff : COLOR0;
float4 Spec : COLOR1;
float3 CubeTexcoord : TEXCOORD0;
};
VS_OUTPUT VS(
float3 InPos : POSITION, // Vertex position in model space
float3 InNormal : NORMAL, // Vertex normal in model space
float Occ : TEXCOORD0) // Occlusion factor
{
VS_OUTPUT Out = (VS_OUTPUT)0;
// Fix as this was from a left handed system.
World = World * -1;
InPos = InPos * -1;
// transform the position and normal
float3 Pos = mul(float4(InPos, 1), (float4x3)World); // position (view space)
float3 Normal = normalize(mul(InNormal, (float3x3)World)); // normal (view space)
float3 EyeToVertex = normalize(Pos - CameraPos); // vector from vertex towards eye
Out.Pos = mul(float4(Pos, 1), ViewProjection); // position (projected)
Out.Diff += CalcAmbient();
Out.Spec += CalcSpecular(Normal, DirFromLight, EyeToVertex);
Out.CubeTexcoord = CalcReflectionVector(EyeToVertex, Normal);
return Out;
}
float4 PS ( VS_OUTPUT In) : COLOR
{
float4 OutColor;
float4 Environment = CubeMapLookup(In.CubeTexcoord);
OutColor = lerp(In.Diff, Environment, 0.25);
OutColor += In.Spec;
return OutColor;
}
technique Go
{
pass P0
{
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
}
Thanks.