1- using SharpMath . Geometry ;
2- using SharpMath . Trigonometry ;
1+ // Author: Dominic Beger (Trade/ProgTrade) 2016
2+
33using System ;
44using System . Drawing ;
55using System . Drawing . Drawing2D ;
66using System . Windows . Forms ;
7+ using SharpMath . Geometry ;
78
89namespace SharpMath . _3DTest
910{
1011 public partial class PerspectiveForm : Form
1112 {
13+ private readonly Vector4 [ ] _vertices =
14+ {
15+ new Vector4 ( - 0.5 , 0.5 , 1.5 , 1 ) , new Vector4 ( 0.5 , 0.5 , 1.5 , 1 ) , new Vector4 ( 0.5 , - 0.5 , 1.5 , 1 ) ,
16+ new Vector4 ( - 0.5 , - 0.5 , 1.5 , 1 ) ,
17+ new Vector4 ( - 0.5 , 0.5 , 2.5 , 1 ) , new Vector4 ( 0.5 , 0.5 , 2.5 , 1 ) , new Vector4 ( 0.5 , - 0.5 , 2.5 , 1 ) ,
18+ new Vector4 ( - 0.5 , - 0.5 , 2.5 , 1 )
19+ } ;
20+
1221 private Color _color = Color . White ;
13- private Vector4 [ ] _vertices = new Vector4 [ ] { new Vector4 ( - 0.5 , 0.5 , 1.5 , 1 ) , new Vector4 ( 0.5 , 0.5 , 1.5 , 1 ) , new Vector4 ( 0.5 , - 0.5 , 1.5 , 1 ) , new Vector4 ( - 0.5 , - 0.5 , 1.5 , 1 ) ,
14- new Vector4 ( - 0.5 , 0.5 , 2.5 , 1 ) , new Vector4 ( 0.5 , 0.5 , 2.5 , 1 ) , new Vector4 ( 0.5 , - 0.5 , 2.5 , 1 ) , new Vector4 ( - 0.5 , - 0.5 , 2.5 , 1 ) } ;
15- private Matrix4x4 _view ;
16- private Matrix4x4 _world ;
17- private Matrix4x4 _scalation = Matrix4x4 . Scalation ( 0.5f ) ;
1822 private Matrix4x4 _rotationX = Matrix4x4 . Identity ;
1923 private Matrix4x4 _rotationY = Matrix4x4 . Identity ;
2024 private Matrix4x4 _rotationZ = Matrix4x4 . Identity ;
25+ private Matrix4x4 _scalation = Matrix4x4 . Scalation ( 0.5f ) ;
26+ private Matrix4x4 _view ;
27+ private Matrix4x4 _world ;
28+ private float angle ;
29+ private float delta = 1f ;
2130 //private Matrix4x4 _projection;
2231
2332 public PerspectiveForm ( )
@@ -45,25 +54,29 @@ private void UpdateMatrices()
4554
4655 private void perspectivePanel_Paint ( object sender , PaintEventArgs e )
4756 {
48- var transformationMatrix = Matrix4x4 . Translation ( 0 , 0 , 2 ) * _view * _world * _rotationX * _rotationY * _rotationZ * _scalation * Matrix4x4 . Translation ( 0 , 0 , - 2 ) ;
49- var projectionData = new ProjectionData ( perspectivePanel . Size , 16f / 9f , ( float ) Math . PI / 3f , 0.1f , 100f ) ;
57+ var transformationMatrix = Matrix4x4 . Translation ( 0 , 0 , 2 ) * _view * _world * _rotationX * _rotationY * _rotationZ *
58+ _scalation * Matrix4x4 . Translation ( 0 , 0 , - 2 ) ;
59+ var projectionData = new ProjectionData ( perspectivePanel . Size , 16f / 9f , ( float ) Math . PI / 3f , 0.1f , 100f ) ;
5060
51- Func < Vector4 , Vector2 > projectPerspective = ( vector ) =>
61+ Func < Vector4 , Vector2 > projectPerspective = vector =>
5262 {
53- var perspectiveVector = vector * Matrix4x4 . PerspectiveFieldOfView ( projectionData ) ;
63+ var perspectiveVector = vector * Matrix4x4 . PerspectiveFieldOfView ( projectionData ) ;
5464 perspectiveVector . X /= perspectiveVector . W ;
5565 perspectiveVector . Y /= perspectiveVector . W ;
5666 perspectiveVector . Z /= perspectiveVector . W ;
5767
58- var deviceVector = perspectiveVector * Matrix4x4 . Scalation ( projectionData . CanvasSize . Width / 2.0 , projectionData . CanvasSize . Height / 2.0 , 1 ) ;
59- return new Vector2 ( deviceVector . X + projectionData . CanvasSize . Width / 2 , ( projectionData . CanvasSize . Height / 2 ) - deviceVector . Y ) ;
68+ var deviceVector = perspectiveVector *
69+ Matrix4x4 . Scalation ( projectionData . CanvasSize . Width / 2.0 ,
70+ projectionData . CanvasSize . Height / 2.0 , 1 ) ;
71+ return new Vector2 ( deviceVector . X + projectionData . CanvasSize . Width / 2 ,
72+ ( projectionData . CanvasSize . Height / 2 ) - deviceVector . Y ) ;
6073 } ;
6174
6275 PointF [ ] displayCoordinates = new PointF [ _vertices . Length ] ;
6376 for ( int i = 0 ; i < _vertices . Length ; i ++ )
6477 {
65- Vector2 displayVector = projectPerspective ( _vertices [ i ] * transformationMatrix ) ;
66- displayCoordinates [ i ] = new PointF ( ( float ) displayVector . X , ( float ) displayVector . Y ) ;
78+ Vector2 displayVector = projectPerspective ( _vertices [ i ] * transformationMatrix ) ;
79+ displayCoordinates [ i ] = new PointF ( ( float ) displayVector . X , ( float ) displayVector . Y ) ;
6780 }
6881
6982 Graphics g = e . Graphics ;
@@ -82,24 +95,22 @@ private void perspectivePanel_Paint(object sender, PaintEventArgs e)
8295
8396 private void trackBar1_Scroll ( object sender , EventArgs e )
8497 {
85- _rotationX = Matrix4x4 . RotationX ( Converter . DegreesToRadians ( trackBar1 . Value ) ) ;
98+ _rotationX = Matrix4x4 . RotationX ( MathHelper . DegreesToRadians ( trackBar1 . Value ) ) ;
8699 UpdateMatrices ( ) ;
87100 }
88101
89102 private void trackBar2_Scroll ( object sender , EventArgs e )
90103 {
91- _rotationY = Matrix4x4 . RotationY ( Converter . DegreesToRadians ( trackBar2 . Value ) ) ;
104+ _rotationY = Matrix4x4 . RotationY ( MathHelper . DegreesToRadians ( trackBar2 . Value ) ) ;
92105 UpdateMatrices ( ) ;
93106 }
94107
95108 private void trackBar3_Scroll ( object sender , EventArgs e )
96109 {
97- _rotationZ = Matrix4x4 . RotationZ ( Converter . DegreesToRadians ( trackBar3 . Value ) ) ;
110+ _rotationZ = Matrix4x4 . RotationZ ( MathHelper . DegreesToRadians ( trackBar3 . Value ) ) ;
98111 UpdateMatrices ( ) ;
99112 }
100113
101- private float delta = 1f ;
102- private float angle = 0f ;
103114 private void rotationTimer_Tick ( object sender , EventArgs e )
104115 {
105116 if ( angle <= 0 )
@@ -108,13 +119,13 @@ private void rotationTimer_Tick(object sender, EventArgs e)
108119 delta = - 1f ;
109120 angle += delta ;
110121
111- _rotationX = Matrix4x4 . RotationX ( Converter . DegreesToRadians ( angle ) ) ;
112- _rotationY = Matrix4x4 . RotationY ( Converter . DegreesToRadians ( angle ) ) ;
113- _rotationZ = Matrix4x4 . RotationZ ( Converter . DegreesToRadians ( angle ) ) ;
122+ _rotationX = Matrix4x4 . RotationX ( MathHelper . DegreesToRadians ( angle ) ) ;
123+ _rotationY = Matrix4x4 . RotationY ( MathHelper . DegreesToRadians ( angle ) ) ;
124+ _rotationZ = Matrix4x4 . RotationZ ( MathHelper . DegreesToRadians ( angle ) ) ;
114125
115- trackBar1 . Value = ( int ) angle ;
116- trackBar2 . Value = ( int ) angle ;
117- trackBar3 . Value = ( int ) angle ;
126+ trackBar1 . Value = ( int ) angle ;
127+ trackBar2 . Value = ( int ) angle ;
128+ trackBar3 . Value = ( int ) angle ;
118129
119130 // Causes headaches
120131 //Random randomGen = new Random();
@@ -137,7 +148,7 @@ private void autoRotateCheckBox_CheckedChanged(object sender, EventArgs e)
137148
138149 private void scalationTrackBar_Scroll ( object sender , EventArgs e )
139150 {
140- _scalation = Matrix4x4 . Scalation ( scalationTrackBar . Value / 720f ) ;
151+ _scalation = Matrix4x4 . Scalation ( scalationTrackBar . Value / 720f ) ;
141152 UpdateMatrices ( ) ;
142153 }
143154 }
0 commit comments