Sign in to follow this  
Quasimodem

OpenGL Shaders on specific objects

Recommended Posts

Hello, I am just beginning to delve into OpenGL shaders and am confused about how to use shaders locally, rather than applying them to the whole scene. For example, I have a scene with several objects in it and want to use my "turn red" shader on only one of them, not the entire scene. Is there an easy way to do this? Should I organize things to have only 1 shader program and when it comes time to render the object I want red, attach the shader, link, and then remove the shader when the object is done rendering? This seems like a lot of overhead, and I don't want each shader to have to have an "enabled/disabled" uniform flag that the object sets either! So far all the reading I've done says nothing of how to localize shaders, they either operate on each vertex and fragment in the whole scene or not at all! Thank you for any insight into this!

Share this post


Link to post
Share on other sites
OP here, looks like I found the answer to my question. See http://www.gamedev.net/community/forums/topic.asp?topic_id=471826 for shader noobs like myself having the same problem.

The key is have separate programs for each object in your world that might need shaders, then to switch between active programs with glUseProgram(). Pretty slick!

Share this post


Link to post
Share on other sites
To expound on this, when you want to render different objects with different shaders, typically, you can set up multiple programs and then use glUsePrograms to switch between programs. So the code path would be basically like:


RenderLoop
{
glUseProgram(firstProg)
DrawFirstObject
glUseProgram(secondProg)
DrawSecondObject
glUseProgram(0) //optional to return to the fixed function pipeline
}

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