Sign in to follow this  
RoyChen204

Regarding GLSL

Recommended Posts

Hi, I am new to this forum and am in a course of game programming. I was given an assignment which I wanted to attempt implementing shaders but I have some theory-based problems. Thus far, I know how to compile and link a shader, but otherwise I am still relatively weak. My question is as follows, for example, I have a polygon depicting the ground. There are various effects I wish to implement unto it, but when I have another polygon, I only want part of the effect that was implemented onto the first polygon. How is that achieved? I am under the impression that shaders affect everything in the display when in execution, hence I feel confused about proceeding. Thanks.

Share this post


Link to post
Share on other sites
Shaders don't affect things that have already been drawn, only those things which are drawn thereafter.

You'd probably want two shaders, one more complex than the other, the rendering sequence would then be something like this:

1) Bind complex shader
2) Render first polygon
3) Bind less-complex shader
4) Render second polygon

Share this post


Link to post
Share on other sites
I see, but is there no way for the functionities of the different shaders to merge? does that mean I have to

for example

polygon 1 takes 2 effects

then if I follow your method

I ll have to type another shader file with one of the effects and add another effect?

Share this post


Link to post
Share on other sites
With GLSL you can divide code up into functions. You can also divide the functions up into individual files creating a library.

So as kind of a general example to get what you could potentially do. Note that I'm leaving out whether your effect is being done in the vertex or fragment shader since this is more about how the linker can be used with GLSL so you don't have to create a monolithic shader.


// foo.glsl
void foo()
{
// Create the foo effect
}




// bar.glsl
void bar()
{
// Create the foo effect
}




// barEffect.glsl
void bar(); // Forward declaration of bar

void main()
{
bar();
}




// foobarEffect.glsl

void foo(); // Forward declaration of foo
void bar(); // Forward declaration of bar

void main()
{
foo();
bar();
}



So as a kind of quick pseudocode as how to setup the program objects.


void initializeFooBarShaders()
{
// Compile shader code
// Compile foo.glsl
// Compile bar.glsl
// Compile barEffect.glsl
// Compile foobarEffect.glsl

// Create program objects
// Create barEffectObject
// Add compiled bar.glsl
// Add compiled barEffect.glsl
// Call the linker
// Create foobarEffectObject
// Add compiled foo.glsl
// Add compiled bar.glsl
// Add compiled foobarEffect.glsl
// Call the linker
}



From there you'd do like dmatter suggested in terms of the drawing.

If you want more concrete examples of how to create a shader library check out my gamedev article <http://www.gamedev.net/reference/programming/features/glsllib/>

Share this post


Link to post
Share on other sites

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