Jump to content
  • Advertisement
Sign in to follow this  
aschepis17

OpenGL common functions/files in GLSL

This topic is 4731 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 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

Share this post


Link to post
Share on other sites
Advertisement
Just a quick guess... but the return type void wouldn't have anything to do with it perhaps..? ;]

Good luck! :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!