Shader- add in texture

I have a shader for a flashlight and modulate2x blending for a lightmap. Is it possible to have the shader use the model’s textures for the first 2 texunits(0,1) and have the 3rd texunit(2) specified in the code? Right now I can get the first texture(colormap) to work, but the second(lightmap) isn’t working. It keeps popping up in texunit 2, where the flashlight texture should be. And when I tried something else(I forget what it was), I got the flashlight working, along with the colormaps, but no lightmap. So is it possible to somehow have the shader read the first 2 textures from the egg and the third from the code? Or do I have to specify all 3 by using texture stages with different sort values.

Here’s the shader code:

//Cg
void vshader(float4 vtx_position : POSITION,
		   float3 vtx_normal   : NORMAL,
		   float2 vtx_texcoord0 : TEXCOORD0,
		   float2 vtx_texcoord1 : TEXCOORD1,
		
           uniform float4x4 mat_modelproj,
           uniform float4x4 trans_model_to_clip_of_light,
		   
		   out float4 l_position       : POSITION,
		   out float2 l_texcoord0	: TEXCOORD0,
		   out float2 l_texcoord1	: TEXCOORD1,
		   out float4 l_texcoord2    : TEXCOORD2
		   )
{
  l_position = mul(mat_modelproj, vtx_position);
  
  //pass the color and lightmap coords to the fragment shader
  l_texcoord0 = vtx_texcoord0;
  l_texcoord1 = vtx_texcoord1;

  // Compute texture coordinates for
  // querying the projective texture
  l_texcoord2 = mul(trans_model_to_clip_of_light, vtx_position);
}

void fshader(in float2 l_texcoord1    : TEXCOORD1,
		   in float2 l_texcoord0	: TEXCOORD0,
		   in float4 l_texcoord2	: TEXCOORD2,
	
           uniform sampler2D tex_0 : TEXUNIT0,
		   uniform sampler2D tex_1 : TEXUNIT1,
		   uniform sampler2D tex_2 : TEXUNIT2,
		   uniform float4 k_flashLightOn,
		   
		   out float4 o_color : COLOR
		   )
{
	// Color and lightmap texture coords
	float4 colormapColor = tex2D(tex_0, l_texcoord0);
	float4 lightmapColor = tex2D(tex_1, l_texcoord1);
	if (k_flashLightOn.r > 0.5) {
	// Fetch color from the projective texture
	float4 projTexColor = tex2Dproj(tex_2, l_texcoord2);
	//final light
	o_color = colormapColor * ((lightmapColor * 1.5) + projTexColor); 
	}
	else {  //light off
	o_color = colormapColor * lightmapColor * 1.5; 
	}
}

Huh. Could I see the code where you’re assigning the textures?
You need to make sure the TextureStage order is correct, otherwise they will appear in random order.

Well, What I’m trying to do is only assign the third texunit in the code, and have the first two get referenced from the egg. However, If I try to assign the third texunit in the code, the shader uses the second, and if I don’t assign anything, the shader uses the first texunit for the third.

Here’s a hopefully easier to understand version:

Assigned?  |  Texunit1  | Texunit2  |  Texunit3

    Yes    |   Texture1 | Texture2  | Texture2
    No     |  Texture1  | Texture2  | Texture1
	
Desired:
  Texture1  | Texture2  | Texture3

And here’s the code assigning the texture.(for the “no” above, I comment out the last line).

		textureFlashlight = loader.loadTexture("materials/textures/flashlight.png")
		textureFlashlight.setWrapU(Texture.WMClamp)
		textureFlashlight.setWrapV(Texture.WMClamp)
		stageFlashlight = TextureStage("flashlight")
		stageFlashlight.setSort(2)
		self.worldNP.setTexture(stageFlashlight, textureFlashlight)

I’ve also updated the shader code in the first post to remove some unused lines that were commented out.

Hmm. Maybe the .egg loader already specifies a sort value for the stages? Try making the sort value higher.

I’ve taken a short break from programming, and so I’ve just gotten back to this. And everything is working fine as long as the sort value of the flashlight texture stage is 11 or above. 10 and under use the wrong textures for everything. I assume that this may change for different models. Is there any way to find out which number I should use, or it it best to just set it to a very high number. Also, I have another question; is there any way to change the origin of a loaded texture. I want to have the origin at the center of the image, as of right now, the flashlight appears above and to the right of the center, where it is being projected. Is there any way for me to get the center of the image to the center of the projector?

The egg loader automatically assigns sort values to the texture stages it creates, according to the order in which they are applied to polygons. It assigns 0 to the first stage, 10 to the second stage, 20 to the third stage, and so on. If you’re adding your own stage on top of this, you just need to be aware of the sort values on your existing stages (you can query these directly using getSort()).

If you weren’t using a shader, you could use nodePath.setTexOffset() to shift a texture from its origin. Since you are using a shader, you’ll have to program that into your shader.

David