Jump to content

  • Log In with Google      Sign In   
  • Create Account


Using different shaders on objects, and multiple shaders on one object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Kurasu1415   Members   -  Reputation: 170

Like
0Likes
Like

Posted 11 August 2012 - 12:20 AM

Essentially I am trying to handle my shaders in the most dynamic way possible. I want to be able to associate certain objects with shaders, like a component-based system. So an object can have multiple shaders attached to it. The simplest way I can think of doing it is by having a map of shaders to objects.

I am targeting OpenGL 3.3 so that I can get decently modern code that works on the majority of systems owned by gamers.


My biggest issues revolve around two questions :

1) What is the best way to apply different shaders to different objects?

Thoughts : From what I have read, it seems like the best way is to designate what objects use what shaders, then begin using a shader, draw the corresponding objects, move to next shader.

2) What is the best way to apply multiple shaders to an object?
Thoughts : Should I just bind a shader program, draw what I need, get a texture back, then bind the next shader and pass it my texture? For example, if I had a shader that rendered one specific object in black and white, could I apply my normal shaders, then pass a resulting texture on to the new shaders to do the final processing? Is that too much overhead?

I apologize if my issues are hard to understand. Please ask any questions that can help make the issues more clear. Thanks in advance!
-Kreed

Sponsor:

#2 scyfris   Members   -  Reputation: 168

Like
0Likes
Like

Posted 11 August 2012 - 03:59 PM

1) What is the best way to apply different shaders to different objects?


Switching shaders per object drawn could incur extra overhead. Say you have 4 objects, 2 shaders. Object 0 and Object 2 use shader 1, Object 1 and Object 3 use shader 2. If you draw object 0 - 4 in order, then you would have to bind a different shader every draw call which is enefficient (any kind of OpenGL state changes should be minimized). I would batch your object renderings so that you bind shader 0, then draw all objects that use shader 0, bind shader 1, draw all objects that use shader 1.

This can be accomplished in multiple ways. Probably the simplest (assuming you are in C++) is to use std::map with a key of shader, and value of std::vector of your objects. Then iterate over the map, and for each shader 1) bind shader, 2) loop through vector value and draw objects.

[source lang="cpp"]//Say this is your shader wrapper classclass shaderClass { Bind(); //...};//Some wrapper display class for one thing to displayclass DisplayObject { void Draw(); //Draw the object //...};//...typedef std::map < shaderClass *, std::vector < DisplayObject * > > shaderMap_T;shaderMap_T shaderMap;//...void Display () { //Say our shaders have been linked, added to shaderMap with objects, etc. //Loop through shader map for (shaderMap_T::iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) { shaderClass * sc; std::vector < DisplayObject * > * dispList; sc = it->first; dispList = &(it->second); sc->Bind(); for (int i = 0; i < dispList->size(); ++i) { (*dispList)[ i ]->Draw(); } }}[/source]

This is a simple way to do it, there are many more complex ways but usually something to this extent is sufficient. I would be interested to get other's opinions on how they do this.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS