Skip to main content
GameDev.net gamedev.net
🔒 Locked

GLSL Changing between shaders.

Started by CoMaNdore Jul 18, 2009 at 9:10 PM 2 replies 5.6k views
Original Post
CoMaNdore
CoMaNdore
Hey I'm in the middle of trying out some glsl shaders. Now I got multiple shaders that each work when they are the only one being tested. The problem comes when I try to use more of them in the same frame. Say I want to render mesh-A with shader-A, and mesh-B with shader-B and so on. Right now I just load all the shaders in the start. Keep the program-ID. ( from glCreateProgram() ). And when I want to use it I just do glUseProgram( programId ); Now this don't work when I try multiple shaders, like this:

glUseProgram( shader-A );
modelA->Draw();

glUseProgram( shader-B );
modelB->Draw();

glUseProgram( shader-C );
modelC->Draw();
So the question is, what is needed to perform this? Do I need to link each shader before its used everytime?(note: tried it and it dint work for me.. ) Or should I call glUseProgram(0) after each finished model? (note: this dont work either). cheers, Sondre
Me
rextimmy
rextimmy
What you are trying todo will work.

You do not have to continuously link the program's each frame before using them, only once (which it sounds like you are doing at load time).

I would double check and make sure all shader's are definitely getting linked and also check what glGetError is reporting.

Also what exactly is happening when you are trying to render the second and third mesh with the second and third shader?
melbow
melbow
I ran into the problem a while back when first learning glsl. I don't know if this is how you're actually supposed to fix it or not but for some reason setting the first shader at the end of my code instead of the beginning made it work. With your example it would be:

modelA->Draw();

glUseProgram( shader-B );
modelB->Draw();

glUseProgram( shader-C );
modelC->Draw();

glUseProgram( shader-A );

Again, not sure why this worked for me or if it is how you are supposed to do it. Perhaps someone else has a better fix.
AndyEsser
AndyEsser
I do a similar thing. Except that I 'unbind' the program after rendering the model.

glUseProgram( 0 );

But this probably has some performance hit and shouldn't need to be done.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.