Texture Flickering With Shader

Hello all. I’m a graduate CS student with a few years of experience doing graphics/OpenGL in c++. I’ve recently inherited a project using Panda3D/python/cg, which I am new to. In the project, one of the special effects is having the room morph into one of three states, from any other state, including a geometry and texture transition. Previously the texture interpolation was done in software, and was pretty slow, so I wrote a shader to lerp between a source texture and a target texture. Whenever a transition is triggered, I bind the secondary texture to the target texture stage and switch to this shader, and when the transition is finished, I bind the original shader and bind the base texture stage to the target texture, incrementing a parameter variable t as the transition occurs. I set the shader input to 0.0 when the transition is first triggered, and use an accumulator variable for subsequent frames. Simple enough. It works, and It runs much faster now, but there’s a problem I can’t seem to figure out. Whenever one of the transitions is triggered for the very first time for a particular object, the texture flickers, seeming to display the target texture for one frame before the interpolation occurs smoothly. If the transition is replayed for that object, then everything is fine. I’ve narrowed it down to a problem with the parameter variable t. If I hard code it to 0, 1.0, .5, or some constant variable in the cg code itself, it shows the appropriate blend for the duration of the transition of the transition without flicker. If I hard code the same values in the python code, I get flickering before it holds its value. To me this means that the shader input for “t” is not being initialized correctly. But I don’t know what to do then, because after I found the bug I set the shader input of all the objects to 0 as I load them in, and I even tried setting the shader input of render for the lerp parameter to 0. Is this a bug? Or maybe I’m doing something wrong? I would appreciate any help at this point.

Here’s the relevant fragment shader code


 
void fshader( float4 l_color0 : COLOR0,
             out float4 o_color : COLOR,
		float4 l_color1 : COLOR1,
		 float2 l_texcoord0:TEXCOORD0,
		uniform sampler2D tex_0,
		uniform sampler2D tex_1, 
		uniform float4x4 itp_modelview,
		uniform float1 k_transition,
		uniform sampler2D tex_2
		)
{
  float4 texcolor = tex2D(tex_0,l_texcoord0);
  float4 texcolor2 = tex2D(tex_1,l_texcoord0);  
  
  float4 texcolormix = lerp(texcolor, texcolor2,k_transition);
  
  o_color=texcolormix;
}

And here’s how I’m setting the shader input variable, for each object o

        o.setShaderInput("transition", self.accum)

Thanks,
Chris

I haven’t seen this particular problem but that seems like a bug. Shaders and textures don’t get loaded until the object needs to get rendered for the first time so this may have something to do with that. It might also be your graphics driver interacting poorly with some Panda code.

But an easy work around seems that you reverse your lerp so that the target becomes the source. That way the improper initialization is the on the correct side of the lerp anyways.