This is a stupid newb question for ya....
Can I make a base class, inherit it to other classes, then access changes in the base class from the derived classes?
(could you site examples in the answer if you can)
This would be useful in creating a shader subset where the C++ passes the same variables to all the created shaders for lights and stuff. (i.e. the base class would have variables for all my lighting info, and the terrain shader sees the same info as other shaders so everything is lit the same, without calling major copying functions.)
Class subclass question
Yep what you want is something like this:
Code: Select all
class BaseShaderCallback : public video::IShaderConstantSetCallBack
{
public:
virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
{
// Set basic lighting stuff here.
}
};
class TerrainShaderCallback : public BaseShaderCallback
{
public:
virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
{
// This next line calls the function in the base class.
BaseShaderCallback::OnSetConstants(services, userData);
// Set terrain specific stuff here.
}
};
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
OK, next question, though it is a little more complicated...
I created a base class and a terrain class based on the following code:
(the callbacks are callbacks to shaders)
The problem I am having is that when I call updateLight(n); it tries to access the callback from the base class, since it is empty, it gives me an error. if I copy the functions which use the callback to the terrain class, they call the callback correctly. if I call a function in the base class which was not copied over, and call a copied function, it accesses the base function thus, giving me an error.
So, can I force the baseclass to use the terrain's callback? Or, am I just doing this the wrong way?
I created a base class and a terrain class based on the following code:
(the callbacks are callbacks to shaders)
Code: Select all
class CBaseShader
{
public:
static const int MAX_LIGHTS = 4;
void createLight(int n,ISceneNode *parent, vector3df pos, SColor color, float radius);
protected:
ILightSceneNode *lights[MAX_LIGHTS];
CBase_Shader_callback *callback;
};
class CTerrain : public CBaseShader
{
public:
CTerrain(c8* HeightMapFileName);
~CTerrain();
void setTextureMultiplier(float f);
void setTexture(int n, c8 * tex);
protected:
ITerrainSceneNode *terrain;
CTerrain_Shader_callback *callback;
};
So, can I force the baseclass to use the terrain's callback? Or, am I just doing this the wrong way?
-
- Posts: 914
- Joined: Fri Aug 03, 2007 12:43 pm
- Location: South Africa
- Contact:
You have two variables named callback, you only need one-
Code: Select all
class CBaseShader
{
public:
static const int MAX_LIGHTS = 4;
void createLight(int n,ISceneNode *parent, vector3df pos, SColor color, float radius);
protected:
ILightSceneNode *lights[MAX_LIGHTS];
CBase_Shader_callback *callback;
};
class CTerrain : public CBaseShader
{
public:
CTerrain(c8* HeightMapFileName);
~CTerrain();
void setTextureMultiplier(float f);
void setTexture(int n, c8 * tex);
protected:
ITerrainSceneNode *terrain;
};
According to VC++ if I try "virtual CBase_Shader_callback *callback;" it gives me an error that says that you cannot virtualize a data declaration.
What I want is in my terrain class, to create lights, and edit them, then update the data in the callback I created specifically for the terrain shader.
Now that I think about it, I may have more problems as I go.. What I really want is to create the two shaders classes seperately, an environment shader and a terrain shader... Which would bring me to another question... when I call the callback of the environment shader from the terrain shader, will it dump the environment shader's variables into my terrain shader? My guess would be so since I would be posting the services from the terrain shader call into the environment call.
virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
I think I would then just be able to simply make the base class a normal class, and not inherit it into the terrain class and forget the whole thing all together.
What I want is in my terrain class, to create lights, and edit them, then update the data in the callback I created specifically for the terrain shader.
Now that I think about it, I may have more problems as I go.. What I really want is to create the two shaders classes seperately, an environment shader and a terrain shader... Which would bring me to another question... when I call the callback of the environment shader from the terrain shader, will it dump the environment shader's variables into my terrain shader? My guess would be so since I would be posting the services from the terrain shader call into the environment call.
virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
I think I would then just be able to simply make the base class a normal class, and not inherit it into the terrain class and forget the whole thing all together.