Sign in to follow this  

shaders with more than one function?

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to combine several functions into a single shader and call the proper one depending on a uniform variable. The functions do some texture merging. The complete procedure is
  • create shader program
  • compile vertex and fragment shader
  • attach vertex and fragment shader
  • compile vertex and fragment main func
  • attach vertex and fragment main func
  • link shader program
I don't get any compiler or linker errors, and no errors when setting the uniform variables, but the shader simply doesn't do anything. Fragment shader:
uniform sampler2D btmTex, topTex, maskTex;
uniform float grAlpha;
uniform int tmTypeFS;

void TexMergeFS0 (void) 
{
gl_FragColor = texture2D (btmTex, vec2 (gl_TexCoord [0]));
}


void TexMergeFS1 (void) 
{
vec4 btmColor = texture2D (btmTex, vec2 (gl_TexCoord [0]));
vec4 topColor = texture2D (topTex, vec2 (gl_TexCoord [1]));
if (topColor.a == 0.0)
	gl_FragColor = vec4 (vec3 (btmColor), btmColor.a * grAlpha) * gl_Color;
else
	gl_FragColor = vec4 (vec3 (mix (btmColor, topColor, topColor.a)), (btmColor.a + topColor.a) * grAlpha) * gl_Color;
}


void TexMergeFS2 (void) 
{
vec4 topColor = texture2D (topTex, vec2 (gl_TexCoord [1]));
if (abs (topColor.a * 255.0 - 1.0) < 0.5)
	discard;
else {
	vec4 btmColor = texture2D (btmTex, vec2 (gl_TexCoord [0]));
	if (topColor.a == 0.0)
		gl_FragColor = vec4 (vec3 (btmColor), btmColor.a * grAlpha) * gl_Color;
	else
		gl_FragColor = vec4 (vec3 (mix (btmColor, topColor, topColor.a)), (btmColor.a + topColor.a) * grAlpha) * gl_Color;
	}
}


void TexMergeFS3 (void)
{
float bMask = texture2D (maskTex, vec2 (gl_TexCoord [1])).a;
if (bMask < 0.5)
	discard;
else {
	vec4 btmColor = texture2D (btmTex, vec2 (gl_TexCoord [0]));
	vec4 topColor = texture2D (topTex, vec2 (gl_TexCoord [1]));
	if (topColor.a == 0.0)
		gl_FragColor = vec4 (vec3 (btmColor), btmColor.a * grAlpha) * gl_Color;
	else 
		gl_FragColor = vec4 (vec3 (mix (btmColor, topColor, topColor.a)), (btmColor.a + topColor.a) * grAlpha) * gl_Color;
	}
}


void TexMergeFS (void)
{
if (tmTypeFS == 0)
	TexMergeFS0 ();
else if (tmTypeFS == 1)
	TexMergeFS1 ();	
else if (tmTypeFS == 2)
	TexMergeFS2 ();	
else if (tmTypeFS == 3)
	TexMergeFS3 ();	
}




Vertex shader:
uniform int tmTypeVS;

void TexMergeVS0 (void)
{	
gl_TexCoord [0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
}


void TexMergeVS12 (void)
{	
gl_TexCoord [0] = gl_MultiTexCoord0;
gl_TexCoord [1] = gl_MultiTexCoord1;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
}


void TexMergeVS3 (void)
{	
gl_TexCoord [0] = gl_MultiTexCoord0;
gl_TexCoord [1] = gl_MultiTexCoord1;
gl_TexCoord [2] = gl_MultiTexCoord2;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
}


void TexMergeVS (void)
{
if (tmTypeVS == 0)
	TexMergeVS0 ();
else if ((tmTypeVS == 1) || (tmTypeVS == 2))
	TexMergeVS12 ();	
else if (tmTypeVS == 3)
	TexMergeVS3 ();	
}




Main shaders:
//vertex shader
void TexMergeVS (void); //declare

void main (void) 
{
TexMergeVS ();
}


//fragment shader
void TexMergeFS (void); //declare

void main (void) 
{
TexMergeFS ();
}




Share this post


Link to post
Share on other sites
That's exactly what I don't want, because my goal is to be able to combine various shaders depending on program settings. So the example is only the first step. Actually from what I have understood from the entire process of shader program generation you can attach several shaders to a program, so I believed what I put up here should work and do not understand why it doesn't.

Share this post


Link to post
Share on other sites
yes, you don't want that, and that's normal,
but did you just try the big fil, just to see if your shader is running right ? (have to know if it's the "linked program" that does work of if the code in itself is already not working...

Share this post


Link to post
Share on other sites
Before I had built these combined shaders, I had created several shader programs for each case of texture merging and used the appropriate program depending on the type of required merging. That worked. So principally the merging code is functional and is doing the proper things. For the combined shaders, I simply put all the single shaders together.

Share this post


Link to post
Share on other sites
in fact, i didn't play that way with my shaders experience in glsl. I think I should have, but I did't know about multiple sources in link.

So i took another direction (wich is globally the same at the end). I made, since the shaders are just plain strings before to be sent to hardware, a kind of #include macro... that way, i generate the shader containing the "main" function at runtime and just includes the files i want (ie a specular effect, a normal map read... etc.)

so i don't know what's wrong with your set of shaders.

Share this post


Link to post
Share on other sites

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this