The main objective of this assignment was to draw a 3D cube to the screen. We basically converted our previous 2D looking square to a cube by making a couple of changes. Along with cube we also made a camera through which we can maneuver in the game.
The 3D cube looks like the picture below:
The first important thing we do to convert 2D to 3D is to add the third dimension z-axis to the sVertex struct. We start by adding a z coordinate to our lua mesh file and update the MeshBuilder to read the z value.
We are also creating a frustum with several features like the Field Of View which describes the angle within which we can see the world. We have a near and far clipping plane. Any object outside this range is not visible.
Now the other important thing is that instead of we need to perform a MVP matrix transformation which is model view projection. Initially all out meshes exists in a Local Coordinate System.
Now a local coordinate system is a system where the object has its own local center and its orientation and coordinates will be relative to it own center or pivot.
To know where this object exists in global space or the world we have to perform a Local To World matrix transformation. By doing the objects orientation and coordinates now become relative to the world.
Now that the object exists in the world, we need to bring the object in view of the camera. We need to know whether the camera view covers the object or not . So we need to orient and transform its coordinates relative to the camera and this is called World To View
transformation. We do this because if the object exists behind the camera in the world then it will not be within the camera view and hence will not be visible.
Like a painter draws 3D objects on a 2D plane, we now need to find the projection of the objects visible to the camera, on the 2D screen . For this we do the View to Screen transformation.
We also need to do an important thing i.e. enable the depth buffer. Earlier our depth buffer was false and did not store any depth information. Essentially what was happening before was that the order in which we were drawing objects, we were just stacking them on top of each other. So the last object drawn would be the most visible. So we use the Painter’s algorithm approach before which was inconvenient.
But now we do the the depth test. The depth value ranges from +1.0 to -1.0 where 1.0 is the deepest depth. The depth buffer basically stores the depth of the pixels. So the depth of the pixels are compared to ones in the depth buffer and the lower the value that pixel gets drawn. Hence lower the depth value the closer that pixel will be to our near plane, and higher the value(1.0 being the maximum) the further away that pixel will be from the screen. So we are basically clearing the depth buffer to 1.0 because 1 is the max value for the depth and we use a less than operator because we want to draw those pixels which are closer to the camera.
So my Camera exists in the Graphics because i felt it had more graphics related stuff than gameplay. My camera has several movement functions. I also have a camSpeed which can be adjusted to move the camera faster or slower.I have given several controls to the camera . The list of controls are mentioned near the download section. I figured the rotation part for the Camera. Took me some time to figure out. Actually I was not multiplying with the ElapsedTime due to which it was rotating very fast at Computer speeds and not it real time seconds. But it works properly now. I also added the rotations to the Cube. The cube rotation seems to be a bit orbital for now.The key bindings are given at the download section.
In the picture below I was just trying to draw some basic primitive 3D shapes in order to understand the winding order properly.
Time Taken – 18 hours.
Here’s a recording of the Camera and Cube Controls.
You can download the Zip File of Assignment_10 from the following
link to get the square output.
The controls for camera are First Person Shooter controls as follows:
A – to strafe left
D – to strafe right
W – to move forward
S – To move backward
Space – To fly upward
Z – To Dive Down
E – Rotate Camera ClockWise
Q – Rotate Camera AntiClockwise
C-Rotate Cube Clockwise
X-Rotate Cube AntiClockwise
Use arrow keys to control the cube movement
After downloading the link run Game.exe.