At the end of the previous I spoke briefly about making the Graphics class platform independent and now it is time to walk the walk. The code that needed to be made platform specific related to dealing with the window and what is visible to the user. There were 4 operations that were occurring which had different implementations for OpenGL (OGL) and Direct3D (D3D):
Here is a screenshot of the platform independent code in Graphics.cpp that clears the back buffer.
The interface allows the clear color to be driven by the Graphics class, here is the window with the original clear color and one with it changed to red.
The second part of the assignment was to drive the data in the Sprite and Effect class from Graphics in a platform independent way. The original Sprite class had the vertex data hard coded in it, this meant that if anyone wanted to create a new sprite, they had to create another sprite class with new data! The changes I made allowed the data to be driven by the Graphics class so that if any new sprite was to be added, we just needed new data and a sprite variable.
The screenshot above shows a piece of code that initializes a sprite. To make it simple, the only input that is required is the position of the starting vertex (top left corner of the rectangle), the height and width of the sprite. Using this data I calculate all the vertex positions and generate the vertex data for D3D and OGL.
The Effect class was plagued by the same problems as the Sprite class, the vertex and fragment shader data was hardcoded in the class implementations. To make this data driven I set up the initialization function to accept the name of the shaders and the render state bits as parameters.
The Graphics class now has control over which shaders are used and can create different types of effects.
As an optional challenge I optimized the vertex data usage. Instead of having to define 6 vertices for each rectangle the code now requires only 4 vertices. This was achieved by changing the draw mode for both OGL and D3D to “Triangle Strip” instead of just “Triangles”.