Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have many objects in my game world that all derive from one base class. Each o

ID: 643369 • Letter: I

Question

I have many objects in my game world that all derive from one base class.

Each object will have different materials and will therefore be required to be drawn using various rendering techniques.

I currently use the following order for rendering my objects.

Deferred
Forward
Transparent (order independent)
Each object has a rendering flag that denotes which one of the above methods should be used.

The list of base objects in the scene are then iterated through and added to separate lists of deferred, forward or transparent objects based on their rendering flag value.

The individual lists are then iterated through and drawn using the order above. Each list is cleared at the end of the frame.

This methods works fairly well but it requires different draw methods for each material type.

For example each object will require the following methods in order to be compatible with the possible flag settings.

object.DrawDeferred()
object.DrawForward()
object.DrawTransparent()
It is also hard to see where methods outside of materials, such as rendering shadow maps, would fit using this "flag & method" design.

object.DrawShadow()
I was hoping that someone may have some suggestions for improving this rendering process, possibly making it more generic and less verbose?

Explanation / Answer

You can use Strategy.

You have a base class IDrawer::draw() with subclasses DefferedDrawer, ForwardDrawer, TransperantDrawer, etc

You then have a base class for your objects: IDrawable which uses an IDrawer drawer delegate for actual draw: IDrawable::draw() { drawer->draw(); }

IDrawable also has a method for changing the drawer: IDrawable::setDrawer(IDrawer someDrawer) {this->drawer = someDrawer;}

or the drawer can be automatically updated by some sort of state machine (here it depends on your usage), depending on the context changes.

You then have a single container for your objects (which can be sorted by some criteria) and call obj.draw()