   Specular lightning (hlsl)
  I'm trying to write a hlsl shader that does per pixel lightning with specular highlights (phong) for point lights.Right now it's working, but the specular highlight doesn't seem to be affected by my camera position.In other words, when I move the camera, the specular highlight stays in the same spot on the surface of my object.I can't really figure out what I'm doing wrong.Here's my current code:float4x4 world;float4x4 view;float4x4 projection;float3 cameraPosition;float3 lightpos;float4 lightcolor;float lightradius;float lightpower;texture2D diffuse;sampler diffuseSampler = sampler_state{    Texture = (diffuse);Filter = MIN_MAG_MIP_LINEAR;    AddressU = CLAMP;    AddressV = CLAMP;};struct VS_IN{float4 pos : POSITION;float2 texCoord : TEXCOORD;float4 normal : NORMAL;};struct PS_IN{float4 pos : SV_POSITION;float3 Position : TEXCOORD0;float2 texCoord : TEXCOORD1;float4 normal : TEXCOORD2;};PS_IN VS( VS_IN input ){PS_IN output = (PS_IN)0;float4 worldPos = mul(input.pos, world);float4 viewPos = mul(worldPos, view);output.pos = mul(viewPos, projection);output.texCoord = input.texCoord;output.Position = worldPos.xyz;output.normal = normalize(mul(input.normal, world));return output;}float4 pointLight( float3 diffuseColor, float3 normal, float3 surfacePosition ){float specularIntensity = 0.9f;float specularExponent = 100.0f;float3 lightDistance = lightpos - surfacePosition;float3 lightVector = normalize(lightDistance);//attenuationfloat attenuation = saturate(1.0f - length(lightDistance)/lightradius);//diffuse colorfloat3 diffuse = diffuseColor * max( 0.0f, dot( normal, lightVector ) );diffuse *= lightcolor.rgb;//reflection vector    float3 reflectionVector = normalize(reflect(-lightVector, normal));    //camera-to-surface vector    float3 directionToCamera = normalize(cameraPosition - surfacePosition);    //compute specular light    float specularLight = specularIntensity * pow( saturate(dot(reflectionVector, directionToCamera)), specularExponent);return float4(attenuation * diffuse * lightpower + specularLight, 1);}float4 PS( PS_IN input ) : SV_Target{float3 normal = normalize(input.normal).xyz;float3 diffuseColor = (diffuse.Sample(diffuseSampler, input.texCoord)).rgb;return pointLight( diffuseColor, normal, input.Position );}technique10 test{pass P0{SetVertexShader( CompileShader( vs_4_0, VS() ) );SetGeometryShader( 0 );SetPixelShader( CompileShader( ps_4_0, PS() ) );}}Thanks,Hyu   
  I used a simple sphere with wireframe fillmode to see if that specular highlight really wasn't moving at all.Turns out it actually is, but into the wrong direction?As you can see the point light is positioned right over the center of the sphere:Now if I move the camera to the left, the highlight moves to the right (away from the camera) instead of to the left (the direction of the camera):I went over my code again, but I cannot figure out what's wrong :(   ;  Still trying to solve this problem.I double and triplechecked my normals, and they all point to the correct direction.The reflection vector also seems to be correct.The camera Vector (directionToCamera) however appears to be wrong.It does not change when I orbit the camera around the sphere, but it should, since it's in world space, right?.However due to some very odd reason it does change when I move the camera away from the sphere?I'd understand slight changes on the outer pixels, but the changes when moving the camera away are quite extreme all over the sphere, including the center, which shouldn't change at all.This doesn't make any sense to me at all.The camera position is correct, and I don't see how multiplying a vertex by it's object's world matrix would result in something else then the proper world position.Getting the surface to camera vector is also something I've done quite a few times, so I know thatfloat3 directionToCamera = normalize(cameraPosition - surfacePosition); is the correct way of doing so.I'm completely lost as where else to look for this error.Edit:May be related to this issue:http://www.gamedev.net/community/forums/topic.asp?topic_id=571016Edit2: Definitely related, the problem is caused by an incorrect View Matrix it seems.[Edited by - Hyunkel on May 12, 2010 5:05:04 PM];  You are translating your normal, not just rotating it. You must cast you matrix to float3x3 first.   ;  Do you mean this?output.normal = normalize(mul(input.normal, world));This seems to be working fine though, I checked my normal vectors, and they're all correct.The problem is the surface to camera vector, which is not related to normals.Infact I'm now convinced that this issue is caused by an incorrect view matrix, as explained in the linked topic.   ;  I can only refer you to my previous post. I wasn't suggesting something to try, I was highlighting a bug in your code that you say doesn't work.It is absolutely wrong at every level to multiply a normal by a 4x4 matrix. If fixing this has no effect then I guess your translation part might be empty but you're still wasting cycles and tbh your translation part shouldn't be empty either unless you intend to use this code in a world where every object is at [0,0,0]If doing this math right doesn't fix the visuals, then there is also another problem in there.   ;  Yes, I see what you're saying.I'm not only wasting cycles, but also space in my vertex buffer since I'm storing my normals as a 4d vector.Using a 3d vector and multiplying with a 3x3 world matrix is the better thing to do.I also managed to solve my specular issue, it was indeed related to a broken view matrix.   