Jump to content
  • Advertisement
Sign in to follow this  
giugio

CG Phong shader

This topic is 3084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hy. I have create this CG phong shader , but it dont work when i bind it to my model matrix and camera position: i use world camera position and ProjectViewMatrix.
//VS

float4x4 VPMatrix;
float4x4 WMatrix;
float4   CameraModelPosition;
float4   Light0WorldDirection;

struct VS_OUTPUT 
{
   float4 Pos:    POSITION;
   float3 Norm:   TEXCOORD0;
   float2 Tex:    TEXCOORD1;
   float3 View:   TEXCOORD2;
   float3 Light:  TEXCOORD3;
};

VS_OUTPUT vs_main(    float4 inPos: POSITION,
                      float3 inNorm:NORMAL,
                      float2 inTex: TEXCOORD0   )
{
   VS_OUTPUT Out = (VS_OUTPUT) 0;

   Light0WorldDirection = float4(1, 1 , 0 ,0);
   
   Out.Pos = mul(mul(VPMatrix, inPos),WMatrix);
   
   Out.Light = Light0WorldDirection.xyz;
   
   Out.Tex = inTex;
   
   float3 vPosWorld = normalize( mul( WMatrix, inPos ) ).xyz;
   
   Out.View = normalize( CameraModelPosition - float4(vPosWorld.xyz,0) ).xyz;
   
   Out.Norm = normalize( mul( WMatrix, float4(inNorm.xyz,0))).xyz;   

   return Out;
}

//PS

float fShinyness;
float4 colDiffuse;
float4 colSpecular;
float4 colAmbient;
float4 lightAmbient;
float4 lightDiffuse;
float4 lightSpecular;
float4 globalAmbient;

sampler Texture0;


float4 ps_main(    float3 inNorm:  TEXCOORD0,
                   float2 inTex:   TEXCOORD1,
                   float3 inView:  TEXCOORD2,
                   float3 inLight: TEXCOORD3   ) : COLOR
{ 
	
	
   fShinyness = 8;
   colDiffuse = float4(1, 0, 0, 1);
   colSpecular= float4(1, 1, 1, 1);
   colAmbient= float4(0, 0, 0, 1);
   lightAmbient= float4(0.3, 0, 0, 1);
   lightDiffuse= float4(1, 0, 0, 1);
   lightSpecular= float4(1, 0, 0, 1);
   globalAmbient= float4(0.3, 0, 0, 1);
   //normalize inputs
   float3 vNormal = normalize(inNorm);
   float3 vLight = -normalize(inLight);
   float3 vView = normalize(inView);
   
   //calc diffuse/specular
   float fNormalDotLight = saturate ( dot(vNormal, vLight) );  
   float3 vLightReflect = 2.0*fNormalDotLight*vNormal - vLight;
   
   float fViewDotReflect = saturate( dot(vView, vLightReflect) );
   float4 fSpecIntensity = pow(fViewDotReflect, fShinyness);
   
   float4 diffuse = fNormalDotLight * colDiffuse * lightDiffuse;
   float4 specular = fSpecIntensity * colSpecular * lightSpecular;
   float4 ambient = normalize( colAmbient * lightAmbient ) + globalAmbient;
   return (ambient+ diffuse) /* tex2D(Texture0, inTex)*/ + specular;
}

technique Phong_Material
{
	pass p0 {
		
		CullFaceEnable = false;	
		VertexProgram = compile vp40 vs_main();
		FragmentProgram = compile fp40 ps_main();
	}
}


i have set all the vars for simple debug. whats wrong? [Edited by - giugio on April 11, 2010 1:26:09 PM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!