Sign in to follow this  
giugio

cg shader

Recommended Posts

hy i have this cg shader :

// geometric input (sent by application)
struct appin
{
    float4 Position     : POSITION;
	float3 Normal       : NORMAL;
	float3 TCoords0     : TEXCOORD0;
};

// vertex program output
struct V2FI
{
    float4 HPosition    : POSITION;
    float3 TCoords0     : TEXCOORD0;
    float3 Normal       : TEXCOORD1;
    float3 ViewVector   : TEXCOORD2;
    float3 LightVector  : TEXCOORD3;
};

V2FI mainVS(appin IN,
         uniform float4x4 Proj   : register(c0), // projection matrix
         uniform float4x4 View   : register(c4), // modelview  matrix
         uniform float4x4 ViewI  : register(c8),
         uniform float4x4 ViewIT : register(c12),
	     uniform float4   LightPos               // light pos in object space
		 )
{
    V2FI OUT;

	// compute view vector
	float4 pos       = mul(View, IN.Position);
	OUT.ViewVector   = -pos.xyz;
	// compute light vector
	float4 lightpos  = mul(View, LightPos);
	OUT.LightVector  = (lightpos-pos).xyz;
	// project point onto screen
    OUT.HPosition    = mul(Proj, pos);
	// transform normal - should use inverse transpose !
    OUT.Normal       = mul(ViewIT, float4(IN.Normal,0.0)).xyz;
	// copy texture coordinates
    OUT.TCoords0     = IN.TCoords0;

    return OUT;
}

// fragment program output
struct PixelOut 
{
    half4 COL;
    half DEPR;
};

PixelOut mainPS(V2FI IN,
			  uniform sampler2D Texture   : texunit0
			  )
{
	// texture lookup
	half4 texcolor=h4tex2D(Texture,IN.TCoords0.xy);

	// normalize (interp. per-fragment => norm != 1.0)
	half3 n=normalize(IN.Normal.xyz);
	half3 v=normalize(IN.ViewVector.xyz);
	half3 l=normalize(IN.LightVector.xyz);

	// compute specular coeff
	half3 r=reflect(-v,n);
    half  spec=pow(clamp(dot(l,r),0.0,1.0),30.0);

	// compute diffuse coeff
	// -> clamp to keep an ambient illumination
	half diff=clamp(dot(n,l),0.3,1.0);

	// computes final color - uses alpha component for specular attenuation
	half4 color=texcolor*diff+(spec*texcolor.w);

    PixelOut OUT;
    OUT.COL = color;
    return OUT;
}

technique technique0 {
	pass p0 {
		CullFaceEnable = false;	
		VertexProgram = compile vp40  mainVS();
		FragmentProgram = compile fp40 mainPS();
	}
}

i get errors on the tecnique0 in composer. the error says that there are too few paremeters in the function call, how i can correct this cg errors? thanks.

Share this post


Link to post
Share on other sites
I think you need to pass the parameters in:

mainVS
- Proj
- View
- ViewI
- ViewIT
- LightPos

mainPS
- Texture

declaring these parames as globals instead of function params


technique technique0
{
pass p0
{
CullFaceEnable = false;
VertexProgram = compile vp40 mainVS(Proj, View, ViewI, ViewIT, LightPos);
FragmentProgram = compile fp40 mainPS(Texture);
}
}

Share this post


Link to post
Share on other sites
thanks , now compile and run,
this is the shader:

uniform float4x4 Proj ;
uniform float4x4 View ;
uniform float4x4 ViewIT ;
uniform float4 LightPos ;
uniform sampler2D Texture;
// geometric input (sent by application)
struct appin
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float3 TCoords0 : TEXCOORD0;
};

// vertex program output
struct V2FI
{
float4 HPosition : POSITION;
float3 TCoords0 : TEXCOORD0;
float3 Normal : TEXCOORD1;
float3 ViewVector : TEXCOORD2;
float3 LightVector : TEXCOORD3;
};

V2FI mainVS(appin IN)
{
V2FI OUT;

// compute view vector
float4 pos = mul(View, IN.Position);
OUT.ViewVector = -pos.xyz;
// compute light vector
float4 lightpos = mul(View, LightPos);
OUT.LightVector = (lightpos-pos).xyz;
// project point onto screen
OUT.HPosition = mul(Proj, pos);
// transform normal - should use inverse transpose !
OUT.Normal = mul(ViewIT, float4(IN.Normal,0.0)).xyz;
// copy texture coordinates
OUT.TCoords0 = IN.TCoords0;

return OUT;
}

// fragment program output
struct PixelOut
{
half4 COL;
half DEPR;
};

PixelOut mainPS(V2FI IN)
{
// texture lookup
half4 texcolor=h4tex2D(Texture,IN.TCoords0.xy);

// normalize (interp. per-fragment => norm != 1.0)
half3 n=normalize(IN.Normal.xyz);
half3 v=normalize(IN.ViewVector.xyz);
half3 l=normalize(IN.LightVector.xyz);

// compute specular coeff
half3 r=reflect(-v,n);
half spec=pow(clamp(dot(l,r),0.0,1.0),30.0);

// compute diffuse coeff
// -> clamp to keep an ambient illumination
half diff=clamp(dot(n,l),0.3,1.0);

// computes final color - uses alpha component for specular attenuation
half4 color=texcolor*diff+(spec*texcolor.w);

PixelOut OUT;
OUT.COL = color;
return OUT;
}

technique technique0 {
pass p0 {
CullFaceEnable = false;
VertexProgram = compile vp40 mainVS();
FragmentProgram = compile fp40 mainPS();
}
}





the problem now are my matrix, i use they as
1,2,3,4 (first float)
5,6,7,8(second float)
9,10,11,12(third float)
13,14,15,16(fourth float)

but it seems not work.
I dont see my mesh.
thanks

how i must send matrix as floats* to the shader for a correct work?

Share this post


Link to post
Share on other sites
float mMatrix[16]
CGparameter mParameter;

cgSetMatrixParameterfc(mParameter, &mMatrix[0]);

But make sure you calculate and send in the correct matrices to the shader.

Share this post


Link to post
Share on other sites
also i have a doubth:
I use wild magic 4 engine that transform the geometry that is in the scenegraph for default, then if i transform the geometry i transform all two times?
may be?
i must disable the recursive matrix transform of the scenegraph?
thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this