Skip to main content
GameDev.net gamedev.net
🔒 Locked

DX10 : phong model implementation issue

Started by Adaline Mar 27, 2011 at 10:54 PM 7 replies 1.1k views
Original Post
Adaline
Adaline
Hello

I'm trying to develop a simple shader that implements Phong light model (in HLSL , for DX10.0)

The interpolated normal given in the PS seems to be good, but the specular term 'seems' to be calculated per vertex only instead of per pixel.
I'm not sure the vPosViewer member in PS_INPUT1 is interpolated.

Here's my main shader code :



struct VS_INPUT1
{
float3 vPosObject : POSITION;
float3 vNormalObject : NORMAL;
float2 vTexUV : TEXCOORD0;
};

struct PS_INPUT1
{
float4 vPosProj : SV_POSITION;
float3 vPosViewer : POSWORLD;
float3 vNormalWorld : NORMAL;
float2 vTexUV : TEXCOORD0;
};


PS_INPUT1 VS1( VS_INPUT1 input )
{
PS_INPUT1 output;

float4 vPosWorld = mul( float4(input.vPosObject,1), g_mWorld );
output.vPosViewer= vPosWorld;

output.vPosProj = mul( float4(input.vPosObject,1), g_mWorldViewProjection );

float3 vNormalWorld = normalize( mul( input.vNormalObject, (float3x3)g_mWorld ) );

output.vNormalWorld = vNormalWorld;

output.vTexUV = input.vTexUV;

return output;
}

float4 PS1( PS_INPUT1 input) : SV_Target
{
float4 output = float4(g_vAmbientColor+g_vLightColor* saturate(-dot(g_vLightDirection, input.vNormalWorld)),1);

if (g_nShininess)
{
float3 vCamera = normalize(input.vPosViewer-g_vCameraPosition);
float3 vReflection = reflect( g_vLightDirection, input.vNormalWorld );
float fPhoneValue = saturate( -dot(vReflection, vCamera) );

output.rgb += g_vLightColor* pow( fPhoneValue,100 );
}

float4 color=g_MeshTexture.Sample( samLinear, input.vTexUV );

output *= color;

return output;
}



Thank you very much

NB : if you know a site with DX10 HLSL examples, I'm interested !
GraphicsDude
GraphicsDude
I've never heard of a semantic POSWORLD and I can't seem to find any documentation on it. I tested the semantic in my own app (DX11 SM4.0) and while it does compile without errors the fact that it is not a recognized semantic means it is probably not getting sent to the interpolators. Try passing the vPosViewer through a texture coordinate (TEXCOORD1). It'll get interpolated then.
Adaline
Adaline
Thank you !

So I've changed the semantic POSWORLD in TEXCOORD, but I still have a "per vertex look like' shininess.
I still search ....
Adaline
Adaline
Of course

Thank you !
GraphicsDude
GraphicsDude
One thing I noticed, you're not normalizing your normal in the pixel shader. Even if the normal is normalized at each vertex, the interpolator will do a straight linear interpolate which means midway between two largely different normals will not be normalized.
MJP
MJP

I've never heard of a semantic POSWORLD and I can't seem to find any documentation on it. I tested the semantic in my own app (DX11 SM4.0) and while it does compile without errors the fact that it is not a recognized semantic means it is probably not getting sent to the interpolators. Try passing the vPosViewer through a texture coordinate (TEXCOORD1). It'll get interpolated then.


Semantics that aren't system values (those that start with "SV_") can have arbitrary names in SM4.0. You do not have to used the old reserved names from SM3.0 and prior.

The default behavior for any pixel shader input is that it's linearly interpolated with perspective correction. To change that behavior, you have to manually declare the interpolation mode when declaring the input variable. So I don't think that's your problem here. You could always verify very easily by debugging the pixel shader in PIX (which you should probably try doing anyway for a problem like this).

If you're looking for some D3D10 shader examples, there's some good stuff in this free book.
Adaline
Adaline
Indeed the problem was I wasn't re-normalizing normal in the pixel shader.
Thanks for your help !
GraphicsDude
GraphicsDude
Glad I could help.

@MJP: You should end all your posts with the following: (plus that cheezy little jingle that goes along with it)

the_more_you_know.jpg

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.