   Per Pixel Lighting Questions - Interpolation
  I've been writing a basic app that does per vertex and per pixel lighting on a simple heightmapped terrain (128x128 height map). My lighting is working but I'm not sure if it is correct so I need someone to quickly check it for me and I also have a question about the final color outputted by the pixel shader.Lets deal with the color first, lets say i have calculated my ambient, diffuse and specular intensities (Ia,Id,Is) correctly, what is the final outputted color taking into account both the light color and surface color, assume that the ambient and light colors are already in the Ia,Id,Is terms.What makes sense to me is the following:float4 finalColor = saturate( surfaceColor * (Ia+Id) + Is ); is that correct? If not what is the correct combination? The phong model states that the intensity at a point is I = Ia + Id + Is, so you'd assume the final color would be I * surfaceColor, but unfortunately doing so gives really dull highlights and so moving the Is term out seems correct in my opinion?now for my shaders: this is the difference between per pixel lighting and per vertex lighting at the moment and I dont know if thats right, the quality discrepency might simply be since I'm using a very low poly mesh. The mesh is 128x128 vertices as is 64x64 units large in the scene, there is a simple directional light with the directional vector set to [0,-1,0] - I do negate it when I do my lighting calculationsPer Vertex Lighting  Per Pixel LightingNow in my opinion per vertex looks better in regards to the specular highlights which makes me think I might have done something wrong in my implementation of per pixel lighting.My HLSL Code:LightIntensities calcLighting( Material M, float4 LColor, float3 N, float3 L, float3 H ){LightIntensities LI;LI.Ia = M.Ka * ambientLight;LI.Id = M.Kd * LColor * saturate( dot(N,L) );LI.Is = M.Ks * LColor * pow( saturate(dot(N,H)), M.A );return LI;} PS_INPUT_PP VS_PIXEL_LIGHTING( VS_INPUT input ){PS_INPUT_PP output;input.p = mul( input.p, World );    output.p = mul( input.p, View );   output.p = mul( output.p, Projection );output.t = input.t;output.c = input.c;output.n = normalize( mul(input.n, (float3x3)World) );output.interpPos = (float3) mul( input.p, World );return output;  }float4 PS_TEXTURED_PIXEL_LIGHTING( PS_INPUT_PP input ) : SV_Target{//calculate lightinginput.n = normalize( input.n );float3 V = normalize( eye - input.interpPos );float3 H = normalize( -light.dir + V );LightIntensities LI = calcLighting( material, light.color, input.n, -light.dir, H );   float4 finalColor = saturate(input.c * (LI.Ia + LI.Id) + LI.Is);    return finalColor;    }What I'm doing is interpolating the world position across the primitive by putting the vertex world position into a float3 texcoord. What I also tried doing is calculating the half vector H at the primitives vertices and putting that into the float3 texcoords and not calculating it in pixel shader (more efficient) and it gives me the exact same result so I'm thinking either my implementation is right and my terrain just cant show me the difference or I'm doing something fundamentally wrong?!Any help or advice would be much appreciated.thanks in advance![Edited by - Coldon on April 21, 2010 4:30:25 AM]
  If I were you, I'd put the vertex normals in a bump map, and sample that bump map for each pixel. This will remove the triangulation artifact you are suffering due to interpolating normals along edges. (And of course, ideally, you would have a higher resolution bump map than vertex tessellation)If your lighting calculation is the same for both vertex and pixel lighting, it can only be the inputs which are giving your visually different results.   ;  Hi Martin,Yes, a bump map would be a better option but for now I need to just do simple per vertex / per pixel shading and all th examples i've found just return the light color and dont take into account the surface color, so i just want some confirmation that what i've done is correct.I also want to know that my lighting shader are correct. I think they are correct since I tested with a sphere object and the per pixel gave a much nicer specular highlight than the per vertex so I've answered my own question there.I'm just curious about the final color.   ;  Maybe normals of the mesh are not smoothed.   ;  no they arent, the normals are simply averaged at every vertex, like I said a very, very simple example.   ;Quote:Original post by ColdonHi Martin,Yes, a bump map would be a better option but for now I need to just do simple per vertex / per pixel shading and all th examples i've found just return the light color and dont take into account the surface color, so i just want some confirmation that what i've done is correct.I think it's correct, people tend to use a texture map to simulate how arriving light it reflected to produce a surface colour. A gloss map is used to specify how much specular reflection occurs. In that way, all your lighting calculation has to do it produce some factor of the incoming light radiance and let texture modulation simulate the exit radiance.Quote:Original post by ColdonI also want to know that my lighting shader are correct. I think they are correct since I tested with a sphere object and the per pixel gave a much nicer specular highlight than the per vertex so I've answered my own question there.:)   