Merged @StarBrilliant's matrix calculation with modifications for correct 3d rendering.