Up to this point I’ve only drawn wireframes. To create colored 3d objects I need two things. Backface Culling and Z Sorting.
Time to start with the first: Backface Culling.
- First of all I changed to using triangles instead of just points/lines.
- Then I calculate the cross product on two of the sides to get a normal vector.
- Compare the normal with your viewing vector (a line from the camera to the middle of the triangle) to see if the triangle is facing towards you or away from you
- Don’t draw if the triangle is facing away from you
Now you no longer see any lines “behind” the cubes.
I also draw the normal vectors for each triangle.
From this point I just filled the triangles with a single color.
With the normal vectors already calculated on each triangle, I tried to implement some directional lighting as well.
It went much smoother than I would ever have thought.
Pretty interesting! I made a spinning cube with lighting almost exactly ten years ago. However I did not understand much of it, and going back to that tutorial it seems to be full of weird tricks. This time however I did it all by myself =D.
Next up is Z-Sorting so we can have more than just one cube at once