Refactoring our code
Just like with 2D, we need to refactor our code so it remains clean. We've drawn one model now, but if we were to draw many models and add movement to them, our code would get cluttered. So we will add some classes in the same manner as we did with 2D.
The base class
The base class for all our 3D objects is called GameObject3D
. This class will contain the position, rotation, and scale of the object, along with the necessary methods to initialize, update, and draw models. This class is abstract, because we don't need to be able to instantiate it.
Properties
This class will have four properties, of which three are public. The position, rotation, and scale are public, while the World
matrix is protected (the World
matrix is the compiled version of the position, rotation, and scale, as explained in the previous section). The rotation is of type Quaternion
, a structure that has four components, and very suited for holding rotations, because unlike matrices it avoids gimbal lock...