Shader: color + texture

Hi all,

I’m new to shaders, and I’m trying to create a shader which does nothing special. Basically, I want the resulting rendering with my own shader to be the exact same as the rendering when I’m not using my own shader.

The problem is, I can do this if I don’t use textures, but simply vertex colors, or if I only use textures and not simply colors, but not if I use both.

Following is my current shader (CG) code:

//Cg
void vshader(uniform float4x4 mat_modelproj,
    in float4 vtx_position:POSITION,
    in float2 vtx_texcoord0:TEXCOORD0,
    out float4 l_position:POSITION,
    out float2 l_texcoord0:TEXCOORD0)
{
  l_position = mul(mat_modelproj, vtx_position);
  l_texcoord0 = vtx_texcoord0;
}
void fshader(uniform sampler2D tex_0,
    in float2 l_texcoord0:TEXCOORD0,
    out float4 o_color:COLOR0)
{
  float4 texColor = tex2D(tex_0, l_texcoord0);
  o_color = texColor;
}

So basically, my question is; how should I write the fragment shader such that it uses texture if available, and just the interpolated vertex color if no texture is available?

Thanks a lot!

Kind regards,
Barno

There’s no straightforward way to know this, sorry. Perhaps you can apply a white 1x1 texture to the models that shouldn’t have a texture and multiply the result from tex2D with the interpolated vertex colour.

But the proper way to do this would be to keep two shaders around, one for vertex colours and one for textures, and apply a different one based on whether or not it uses a texture.

Thanks for your answer.

Actually, I am trying to apply a deformation to my scene, so I just want to use the vertex shader to change the vertex positions, for example to make it look like the scene is being reflected on a cylinder mirror. I don’t want to do anything special in the fragment shader, but it seems I have to implement one anyway.

The scene consists of textured objects, particle effects (no texture), and objects containing both textures and colors.

Any idea how to solve this?

Kind regards,
Barno