PDA

View Full Version : Not really an OpenGL question! again



McZ
01-18-2001, 10:52 PM
Hi !

I have made an object class and a scene class and I use them to have control of my demo (only a rotating cube yet http://www.opengl.org/discussion_boards/ubb/smile.gif ).

in the scene class I have an array of the object class and in the objectclass I have the render function for the object and in the scene class the renderfunction for the scene. like this :

class OGL_Object {
void Render(void); // function to set material, rotate and render the current object
};

class OGL_Scene {
OGL_Object Object[50]; //the array of objects
void RenderScene(void); // function to set the cameraposition and objectpositions and then render them using
}

at the moment the class is for simple objetcs only like a cube or something the problem now is that I want to insert a function to an object, this function would be called every time this object will be renderd. this is for the use of morphing.

is there posibly to make it like this :
void MorphObject()
{
//some code to morph the object
}

Object.AddExtraFunction(MorphObject);

and then in the Object.Render(); function I would call the MorphObject function or what I called it! is this possible ??

thanks for yout answers ! http://www.opengl.org/discussion_boards/ubb/smile.gif

DFrey
01-19-2001, 03:12 AM
If I understand you correctly, you are basically wanting a subclass of your OGL_Object class. So do that, make a subclass of OGL_Object. No need to make the AddExtraFunction(...) function.

class OGL_Object {
virtual void Render(void); // function to set material, rotate and render the current object
};

class OGL_MorphObject : public OGL_Object {
virtual void Morph(void);
virtual void Render(void){Morph(); OGL_Object::Render();};
};

class OGL_Scene {
OGL_Object *pObjects[50];
void RenderScene(void);
void InitScene(void); // first call this to instantiate the objects and morphobjects and to fill the array with pointers to each one
}

Personally I'd probably use the STL vector template class to make it easier to add (and remove) pointers to the array and to remove that arbitrary 50 object limit.

Also you are probably going to want to make the Morph function time dependent so I'd look into adding a global timekeeper or make it a function of time, i.e. void Morph(int milliseconds). Doing this would require changing Render so that it too was a function of time however. But unless you have a need for objects to pass through time at different rates, I'd stick with a global timekeeper.

If you have more than one scene, then I'd also subclass the OGL_Scene class, and make the base class, OGL_Scene virtual. Or at least enough so that InitScene could be overridden by each scene subclass.



[This message has been edited by DFrey (edited 01-19-2001).]