common functions/files in GLSL

Started by
1 comment, last by James Trotter 18 years, 7 months ago
I am currently working on a materials subsystem for an engine im doing with some friends. I would like to make it possible to use a bunch of useful functions i've written in more than one shader. In an attempt to do this i created a common.vert and common.frag file that i am linking into my other shader programs. this doesn't seem to work however. If i paste the code into my main method as it would have been, it works fine. I was getting errors so i put prototypes for the methods in. Here is an example of waht i'm doing. -------------- lighting.vert (Common) --------------

void doRTLighting()
{	
	vec3 normal, lightDir;
	vec4 diffuse;
	float NdotL;
	
	// first transform the normal into eye space and normalize the result
	normal = normalize(gl_NormalMatrix * gl_Normal);
	
	// now normalize the light's direction. Note that according to the
	// OpenGL specification, the light is stored in eye space. Also since 
	// we're talking about a directional light, the position field is actually 
	// direction
	lightDir = normalize(vec3(gl_LightSource[0].position));
	
	// compute the cos of the angle between the normal and lights direction. 
	// The light is directional so the direction is constant for every vertex.
	// Since these two are normalized the cosine is the dot product. We also 
	// need to clamp the result to the [0,1] range.
	NdotL = max(dot(normal, lightDir), 0.0);
	
	// Compute the diffuse term
	diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
	
	gl_FrontColor = NdotL * diffuse;
} 
-------------- lighting.frag (Common) --------------

vec4 doRTLighting();

vec4 doRTLighting()
{
	return gl_Color;
}
-------------- Color.vert (wants to use lighting.vert) --------------

void doRTLighting();

void main()
{
	doRTLighting();
	gl_Position = ftransform();
} 
-------------- Color.frag (wants to use lighting.frag) --------------

uniform vec4 color;
vec4 doRTLighting();
void main()
{
	gl_FragColor = color * doRTLighting();
}
When i create the shader program I make sure i add all 4 shaders and then link the program. It compiles with no errors. any ideas? Thanks, Adam
Advertisement
Just a quick guess... but the return type void wouldn't have anything to do with it perhaps..? ;]

Good luck! :)
You didn't actually tell us exactly what error you get. Have you checked the shader compilation logs?

Also, are you sure that you that the glsl linker differentiates between the void doRTLighting() defined in lighting.vert and the vec4 doRTLighting() defined in lighting.frag?

Quote:Original post by gulgi
Just a quick guess... but the return type void wouldn't have anything to do with it perhaps..? ;]

No.

This topic is closed to new replies.

Advertisement