3D Renderer (C++)
This is a 3D renderer that I wrote in C++ to load in and
display “.md2” files, a fairly old format but a good basis into understanding
graphics programming. An “.md2” file merely a list of vertices based around an
origin, and a set of 3 integers which is loaded in as a polygon. A polygon
contains 3 indexes, each point to a vertex in a list, contained within the
model. This means that if a vertex is used more than once, it won’t be
duplicated.
The program started off with “Vertex” and “Matrix” classes
which held the instructions to multiply them together; this is the basis of
transforming and viewing the vertices on the screen. Once we had these, we
could implement the full viewing pipeline, using perspective projection. And
after this we could visually display these points on screen.
To begin with I focused more on transformations and camera
positioning whilst I had the points showing in world space. To visually show
the model I made a “DrawWireFrame” method which drew lines between each of the
points in the polygon, providing a shape of the object. With this I could test
transformations and culling. Backface culling and frustum culling are also
included in the program. These used normalisation, dot products and cross
products, methods to generate these are included in the “Vertex” class.
Once I had the mathematical stuff down I was able to work on
lighting and graphics. I used faceted shading to begin with, which meant I
could perfect the lighting, before moving on to gouraud shading. The program features
implementation of Ambient, Directional and Point Lights. The lighting is
calculated using the Phong Reflection model. I used windows GDI+ graphics
library to fill the polygons to begin with, and later on implemented my own
“FillPolygon” routine.
Once the lighting was finished, I could work on a better
shading option, for this I used Gouraud shading. This stores a colour at each
Vertex and then uses interpolation to work out the colours when it fills the
triangle. The result makes for a much better model.



Comments
Post a Comment