converting fragment program to cg

hi, i tried to convert the following fragment code:

 
TEMP tex0;
TEMP tex1;
TEX tex0, fragment.texcoord[0], texture[1], 2D;
SUB tex0.x, tex0.x, 0.01;
TEX tex1, fragment.texcoord[0], texture[0], 2D;
MUL outColor, tex0, tex1;
KIL tex0.x;
 

to the following cG, but it doesn’t work:

 
pixel main( fragment IN,uniform sampler2D testTexture,uniform sampler2D testTexture2 )
{
	float cippa;
	pixel OUT;
	cippa = tex2D( testTexture, IN.texcoord0 - 0.01); 
	OUT.color = tex2D( testTexture2, IN.texcoord0 * cippa ) ;
        return OUT;
}
 

Any suggestion ? Thanks

Because the Cg program does something completly different.
BTW, if you’re using Cg you can look at what the output assembly is with the stand alone compiler.

What the original code does is:
TEX tex0, fragment.texcoord[0], texture[1], 2D; // Lookup texunit 1 at TexCoord0.xy
SUB tex0.x, tex0.x, 0.01; // Subtract 0.01 from the resulting red value.
TEX tex1, fragment.texcoord[0], texture[0], 2D;// Lookup texunit 0 at TexCoord0.xy
MUL outColor, tex0, tex1; // Resulting color is either tex0 * tex1 colors
KIL tex0.x; // OR if tex0.red is < 0 don’t draw at all.

What you did wrong is to subtract 0.01 from the texcoord0.xyzw, you multiplied the texture coordinate0 by the first color and forgot the kill completely.

i guess the line:

 
cippa = tex2D( testTexture, IN.texcoord0 - 0.01);  

should become

  cippa = tex2D( testTexture, IN.texcoord0.x - 0.01); 

But i don’t know how to solve the other two issues.

bump. Can someone help please ? :smiley:

Ok, here’s a straight translation of the functions body. I’m not fluent in Cg, but this comes closest to what the assembly did.

Assuming testTexture is texunit 0 and testTexture2 is texunit 1 in the assembly.

float4 tex0; // TEMP tex0;
float4 tex1; // TEMP tex1;
tex0 = tex2D(testTexture2, IN.texcoord0.xy); // TEX tex0, fragment.texcoord[0], texture[1], 2D;
tex0.x -= 0.01f; // SUB tex0.x, tex0.x, 0.01;
if (tex0.x < 0) discard; // KIL tex0.x;
tex1 = tex2D(testTexture, IN.texcoord0.xy); // TEX tex1, fragment.texcoord[0], texture[0], 2D;
OUT.color = tex0 * tex1; // MUL outColor, tex0, tex1;
return OUT;

You should really read the Cg manual and look at the language’s assembly output for further development.