Shaders on specific objects

Started by
1 comment, last by RobbieS 16 years, 5 months ago
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!
Advertisement
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!
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}

This topic is closed to new replies.

Advertisement