This is more of a design question than a graphics question, but here’s a bit of background: I’m writing a program using OpenGL (in Java, with JOGL) that can load a mesh from a .obj file and render it. No problems so far, but I need to be able to apply a series of filters or transformations to the mesh, e.g.
- Tessellating the faces
- Adding random noise to the vertices
- Applying a smoothing algorithm to the noisy mesh
- Colouring the mesh (which may or may not be based on both the smoothed mesh and the original clean mesh)
Several of these filters would be applied in order. I also want to provide some kind of consistent interface for other people to write their own (possibly general) filters. How would I go about this?
The main problem is that the filters may require different sets of parameters, e.g. one smoothing algorithm may require two parameters to be chosen by the user (at runtime, using the GUI), whereas another might require none. Similarly some colouring algorithms may require only the smoothed mesh and an RGB colour, another might require both the smoothed mesh and the clean mesh (which it generates the colours from). So the call might look like:
mesh = smoothingFilter1.filter(mesh, booleanParam);
but it might be completely different:
mesh = smoothingFilter2.filter(mesh, intParam1, intParam2, floatParam);
Obviously my code needs to be capable of invoking a general filtering method. Would it be a good idea to define an abstract class or interface for a Filter with an unimplemented method for registering itself (and its required parameters) with some kind of controlling class? Seems pretty complicated but I can’t think of any other way to get the program to work with a filter method that has an arbitrary signature. I’m not even sure this idea would work…
Is there a design pattern for handling this type of situation?
Actually you don’t need the parameters at all.
Mesh filter(Mesh mesh);would suffice.Move the other parameters to the constructor of the different concrete implementations of the filters.
Looks something like this:
For instantiating you can use something along the lines of: