Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the visitor pattern be used for rendering?

I have a game engine that currently uses inheritance to provide a generic interface to do rendering:

class renderable
{
public:
    void render();
};

Each class calls the gl_* functions itself, this makes the code hard to optimize and hard to implement something like setting the quality of rendering:

class sphere : public renderable
{
public:
    void render()
    {
        glDrawElements(...);
    }
};

I was thinking about implementing a system where I would create a Renderer class that would render my objects:

class sphere
{
    void render( renderer* r )
    {
        r->renderme( *this );
    }
};

class renderer
{
    renderme( sphere& sphere )
    {
         // magically get render resources here
         // magically render a sphere here
    }
};

My main problem is where should I store the VBOs and where should I Create them when using this method? Should I even use this approach or stick to the current one, perhaps something else?

like image 776
akaltar Avatar asked Oct 29 '25 10:10

akaltar


1 Answers

(Disclaimer: I'm neither a GameEngine nor a C++ performance expert, so take this with a grain of salt)

There are some existing game engines that use the visitor approach, e.g. GamePlay3D. For performance reasons, you probably should exclude non-visible objects from the rendering routine.

like image 166
Frank Schmitt Avatar answered Oct 31 '25 01:10

Frank Schmitt