After creating the platform independent interfaces for mesh and effect in last week’s assignment, this week required us to create a single platform independent Graphics interface. This meant instead of using separate graphics.d3d and graphics.gl for Direct3D and OpenGL respectively, we had to create a single graphics both configurations.
After analyzing the code, I found that how buffers were handled in OpenGL and Direct3D was quite different. Graphics rendering use the buffers to draw images on screen. A new image is drawn every frame and before drawing, the image from previous frame is cleared. These temporary images are stored in buffers. So to make the graphics platform independent, I created a Buffers class and used that to specify the platform dependent clearing, swapping and cleaning up of buffers code. Also, Direct3d had and additional step of using views for graphics rendering. This code of initializing this view is also done in Buffers class by me (I feel this might be confusing to initialize views using Buffers class, but to avoid creating another platform independent interface, I used the Buffers). The body of function for views is empty in case of OpenGL as it does not require the the views.
Playable EXE Download
Since as a part of this assignment we had to use index buffer instead of vertex buffer, and the graphics has to be platform independent, winding order of indices was another important thing to keep in mind. I used OpenGL winding order as the default and updated winding order of the index buffer passed while initializing in Direct3D.
Below code is used by me to clear the back buffer:
It takes 4 floats for the clear color value (RGB and alpha)
I used the below implementation to initialize the effects:
It takes the path of vertex shader and fragment shader as the argument.
I used the below implementation to initialize the geometric data:
It takes the number of triangles to draw (triangle count), number of vertices to draw those triangles (actual vertex input, 4 in our case as two vertices will be common for drawing 2 tringles), vertex coordinates (vertex data) and index buffer as input parameters.
We can see that direct 3D has significantly higher memory size as compared to OpenGL. I believe this is because Direct3D uses 3 additional pointers as compared to OpenGL. Memory alignment may be another issue that is causing some memory wastage is Direct3D.
The size difference is due to the three pointer we use in Direct 3D are of 8 byte each while the 3 used in OpenGL are 4 bytes each.
Here is a list of some interesting information I found out during this assignment: