Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

types and polymorphism

This topic is 3736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ive got a base class called "IShader" and then several derived classes "SimpleTextureShader", "SimpleColorShader" etc... these are all sent to my renderer in a std::vector<IShader*>... but the renderer then has to sort all the shaders after their type and put into separate vectors.... how would i do that? only solution ive got so far is to assign each shader a int ID and have a virtual GetTypeID() function i "IShader"

Share this post


Link to post
Share on other sites
Advertisement
You don't.

You provide enough info to each shader that it can do what it needs when you call a common base method, or you pass the renderer to the virtual base function that then calls the suitable renderer method for the specific type, or a few other variants on that inversion principle.

Share this post


Link to post
Share on other sites
Well, you could use the typeid operator to find out about the type of your objects at run time, but I question whether there mightn't be a better solution to your problem than having to put different types in different vectors.

Share this post


Link to post
Share on other sites
If the classes have a vtbl, you could do typeid(shader).name() to get a name string and use that for sorting. [Correction:] Use typeid(s1).before(typeid(s2)) to compare.

Your solution to sort based on type of shader doesn't feel kosher though. Shouldn't you be sorting on properties visible via the base class? Don't you ever need to sort among shaders of the same type?

Share this post


Link to post
Share on other sites
typeid() is one way of doing it without adding new info. But if you are not afraid to add an extra byte to the class, why not just make an enum (or #define) of values that tell you exactly what sort of class it is:


enum ShaderType
{
TextureShader,
ColorShader,
// etc....
}

class IShader
{
protected:
ShaderType t;
public:
// ...
}




class SimpleTextureShader : public IShader
{
public:
SimpleTextureShader() {t = TextureShader;}
// ...
}




class SimpleColorShader : public IShader
{
public:
SimpleColorShader() {t = ColorShader;}
// ...
};

// And so on.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
You don't.

You provide enough info to each shader that it can do what it needs when you call a common base method, or you pass the renderer to the virtual base function that then calls the suitable renderer method for the specific type, or a few other variants on that inversion principle.


i take it that there is a problem with my design then...

ill explain a bit better...

the thing is that i have several "Geometries" which consists of a vertex/index buffer and a binded shader... i send these "Geometries" one by one to the renderer...

AddToRenderQueue(IGeometryPtr geoemtry)

then i have to find out which geometries have the same shader type in order to use instancing...

and thats where the problem is...

Share this post


Link to post
Share on other sites
Quote:
Original post by RealMarkP
typeid() is one way of doing it without adding new info. But if you are not afraid to add an extra byte to the class, why not just make an enum (or #define) of values that tell you exactly what sort of class it is:

*** Source Snippet Removed ***

*** Source Snippet Removed ***

*** Source Snippet Removed ***


well thats because it wouldnt be very flexible to add new shaders...

Share this post


Link to post
Share on other sites
Quote:
Original post by Dragon_Strike
Quote:
Original post by Telastyn
You don't.

You provide enough info to each shader that it can do what it needs when you call a common base method, or you pass the renderer to the virtual base function that then calls the suitable renderer method for the specific type, or a few other variants on that inversion principle.


i take it that there is a problem with my design then...

ill explain a bit better...

the thing is that i have several "Geometries" which consists of a vertex/index buffer and a binded shader... i send these "Geometries" one by one to the renderer...

AddToRenderQueue(IGeometryPtr geoemtry)

then i have to find out which geometries have the same shader type in order to use instancing...

and thats where the problem is...


1) Do you really want each geometry to be bound to exactly one shader?

2) Instead of having geometries bound to separate shaders of the same type, why not bind them all to the same shader? Then let each shader in your renderer apply itself to every geometry it's bound to. Whether or not there is actually more than one shader of any given type, does not matter at this point. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!