Assignment 2 is a practice on refactoring and platform-independent coding. The goal of this assignment is to separate out platform-independent code about rendering (meshes and shaders) then have them wrapped up within a platform-independent interface.
After reading through the code in the code base, I figure out which part belongs to a mesh class and which part belongs to an effect class in the Graphics.[platfomr].cpp files by tracking the data variables defined within those files. I created a Mesh class and an Effect class with a platform-independent interface and separate implementations for Direct3D and OpenGL. The Mesh class contains three public functions, Initialize(), CleanUp() and Draw(). The Effect class also contains three public functions, Initialize(), CleanUp() and Bind(). After that I hard coded another triangle within Mesh class’s initialize function so that a rectangle will be drawn when the Mesh::Draw() function is called.
A Windows application that renders two triangle meshes in a window where a fragment shader changes the color of the triangle continuously over time.
For the extra challenge, I shifted the original 6 vertices to be at the bottom-middle to form the base of the house shape. Then I added another triangle of 3 vertices as a cap of the house.
Binding effect and drawing mesh within Graphics.[platform].cpp files:
Initialization in Graphics.d3d.cpp requires an initialization of the views before initializing meshes and shaders. Similarly, the clean up code in Graphics.d3d.cpp also requires cleaning up the views before cleaning up the other data.
Because of the difference is small (only a few lines of code), I think it should be good enough to use the preprocessor to deal with it so that we can keep the functions within a single Graphics.cpp file. But to make Graphics.cpp completely platform-independent, we would need to define some other platform-independent interface for Initialize() and CleanUp() so we can do our platform specific implementations of those functions outside of Graphics.cpp.
Naming conventions: File name prefix: s for struct, c for class, i for interface, e for Enum, etc to help increase the readability of the code base.
Platform-Independent Coding: Have a platform-independent interface for user’s sake and avoid duplicated code by having separate implementations for different platforms behind the scenes.
It was a relatively easy assignment to work on, but the stuff I learned from it was significant. The process of refactoring made me think deeply about how to structure a user-friendly code base which provide a general, easy-to-use interface as well as handling different platform requirements without polluting the user side. It is also cool to learn about the new graphic-specific debugging tools which I never touched before.