View Full Version : CG lerp problem

11-29-2003, 09:46 AM
I'm trying to interpolate between 3 textures on a landscape using an alpha map and CG. Here's the code

float4 main(in float4 wpos : WPOS,
float2 tex1coords : TEXCOORD0,
float2 tex2coords: TEXCOORD1,
float2 tex3coords: TEXCOORD2,
float2 tex4coords: TEXCOORD3,

uniform sampler2D tex1 : TEXUNIT0,
uniform sampler2D tex2 : TEXUNIT1,
uniform sampler2D tex3 : TEXUNIT2,
uniform sampler2D tex4 : TEXUNIT3

float3 a = tex2D(tex1, tex1coords).rgb;
float3 b = tex2D(tex2, tex2coords).rgb;
float3 c = tex2D(tex3, tex3coords).rgb;
float3 d = tex2D(tex4, tex4coords).rgb;
float3 e = tex2D(tex5, tex5coords).rgb;

float3 color1 = lerp(c, a, b.x);
float3 color2 = lerp(d, color1, b.y);
return float4(color2, 1.0);

The output consists of the 1st and 2nd textures, but not the third. The alpha map has 3 blotches of red, green, and blue and the rest is white. The the first texture should show through at white, the 2nd at red, the 3rd at green and NOTHING should show through at blue.

But instead, the 3rd shows through at blue. Am I accessing the components of the texture correctly using .x, .y ?? I've tried using .r, .g, .b but I get the same result.

11-30-2003, 07:17 AM
First of all, you're using 5 samplers but only declaring 4. Did this program even compile correctly? Do you get any CG or GL errors when compiling/loading/using this program?

Second, what colors do you get if you use only texture 0, only texture 1, etc? No lerp; just change the program to move one of the values to the output. This will make sure that you're loading the textures in correct byte order etc.

Third, when you lerp() you get a very specific overlap order between textures, where some effects are sometimes hard to achieve. An alternative approach would be to use MAD to scale each texture contribution and add them all together. Alas, Cg doesn't have a "mad()" runtime library function, but hopefully the compiler will recognize it and generate the appropriate underlying code (assuming the hardware supports it).