Class subclass question

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Beroc
Posts: 18
Joined: Fri Sep 28, 2007 3:25 pm

Class subclass question

Post by Beroc »

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.)
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

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
Beroc
Posts: 18
Joined: Fri Sep 28, 2007 3:25 pm

Post by Beroc »

Awesome, thanks for hte fast response, exactly what I needed.
Beroc
Posts: 18
Joined: Fri Sep 28, 2007 3:25 pm

Post by Beroc »

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)

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;
};
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?
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

Do you mean you want the base class to have a virtual function? an empty function that does not get called at all, but sub classes must implement it?
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

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;
};
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Beroc
Posts: 18
Joined: Fri Sep 28, 2007 3:25 pm

Post by Beroc »

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.
Post Reply