Public Class Form1
'declaration d'un timer
Dim tmr As New Timers.Timer(10)
'angle
Dim angle As Single
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'abonnement événement elapsed
AddHandler tmr.Elapsed, AddressOf tmr_Elapsed
'démarrage du timer
tmr.Start()
End Sub
Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs)
'changement de l'angle de rotation
angle += 1
'déclenchement de l'événement paint
Me.InvokePaint(Me, New PaintEventArgs(Me.CreateGraphics, Me.Bounds))
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'effacement
e.Graphics.Clear(Color.White)
'translation du cube
e.Graphics.TranslateTransform(100, 100)
'changement de l'angle de rotation
e.Graphics.RotateTransform(angle)
'dessin du cube
e.Graphics.DrawRectangle(New Pen(Brushes.Blue), New Rectangle(0, 0, 50, 50))
e.Graphics.DrawRectangle(New Pen(Brushes.Magenta), New Rectangle(25, -25, 50, 50))
e.Graphics.DrawLine(New Pen(Brushes.Red), New Point(0, 0), New Point(25, -25))
e.Graphics.DrawLine(New Pen(Brushes.Yellow), New Point(50, 0), New Point(75, -25))
e.Graphics.DrawLine(New Pen(Brushes.Green), New Point(0, 50), New Point(25, 25))
e.Graphics.DrawLine(New Pen(Brushes.Cyan), New Point(50, 50), New Point(75, 25))
End Sub
End Class
Dim MaMatrice As Drawing2D.Matrix
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question' Ajouter référence C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\PresentationCore.dll
Imports System.Windows.Media.Media3D
Public Class M3D
Private _Translate As System.Windows.Media.Media3D.Vector3D
Private _Rotate As System.Windows.Media.Media3D.Vector3D
Private _Scale As System.Windows.Media.Media3D.Vector3D
Private _Mat As System.Windows.Media.Media3D.Matrix3D
' _Mat comprend déjà les 3 opérations de bases, pour obtenir une matrice final il faut la multiplier a une matrice de projection.
Public Camera As System.Windows.Media.Media3D.Matrix3D
Private Final As System.Windows.Media.Media3D.Matrix3D = Nothing
Public Sub New()
_Scale.X = 1 : _Scale.Y = 1 : _Scale.Z = 1
' essai d'une camera perso
Camera.M11 = 2.414213
Camera.M22 = 2.414213
Camera.M33 = 1
Camera.M34 = -1
Camera.OffsetZ = -0.2
' Sinon voici une fonction trouvé sur le net
'Camera = Matrix3D.Identity
Dim cam2 As New PerspectiveCamera
cam2.FieldOfView = 0.005
cam2.FarPlaneDistance = 10000
cam2.NearPlaneDistance = 0.01
'myViewport.Camera = cam
Camera = Projectioncam(cam2, 1)
Maj()
End Sub
Public Function OrthographicCamera(ByRef camera As OrthographicCamera, ByRef aspectRatio As Double) As Matrix3D
' This math is identical to what you find documented for
' D3DXMatrixOrthoRH with the exception that in WPF only
' the camera's width is specified. Height is calculated
' from width and the aspect ratio.
Dim w As Double = camera.Width
Dim h As Double = w / aspectRatio
Dim zn As Double = camera.NearPlaneDistance
Dim zf As Double = camera.FarPlaneDistance
Dim m33 As Double = 1 / (zn - zf)
Dim m43 As Double = zn * m33
Return New Matrix3D(
2 / w, 0, 0, 0,
0, 2 / h, 0, 0,
0, 0, m33, 0,
0, 0, m43, 1)
End Function
Public Function Projectioncam(ByRef camera As PerspectiveCamera, ByRef aspectRatio As Double) As Matrix3D
' This math is identical to what you find documented for
' D3DXMatrixPerspectiveFovRH with the exception that in
' WPF the camera's horizontal rather the vertical
' field-of-view is specified.
Dim hFoV As Double = camera.FieldOfView
Dim zn As Double = camera.NearPlaneDistance
Dim zf As Double = camera.FarPlaneDistance
Dim xScale As Double = 1 / Math.Tan(hFoV / 2)
Dim yScale As Double = aspectRatio * xScale
Dim m33 As Double = (zf / (zn - zf))
'If zf>Double.= (zf == double.PositiveInfinity) ? -1 :
Dim m43 As Double = zn * m33
Return New Matrix3D(
xScale, 0, 0, 0,
0, yScale, 0, 0,
0, 0, m33, -1,
0, 0, m43, 0)
End Function
#Region "############## Property #################"
Public ReadOnly Property Matrix As System.Windows.Media.Media3D.Matrix3D
Get
Return _Mat
End Get
End Property
''' <summary>
''' Translation
''' </summary>
Public Property Translate() As System.Windows.Media.Media3D.Vector3D
Get
Return _Translate
End Get
Set(ByVal value As System.Windows.Media.Media3D.Vector3D)
_Translate = value
Maj()
End Set
End Property
''' <summary>
''' Rotation
''' </summary>
Public Property Rotate() As System.Windows.Media.Media3D.Vector3D
Get
Return _Rotate
End Get
Set(ByVal value As System.Windows.Media.Media3D.Vector3D)
_Rotate = value
Maj()
End Set
End Property
''' <summary>
''' Scale
''' </summary>
Public Property Scale() As System.Windows.Media.Media3D.Vector3D
Get
Return _Scale
End Get
Set(ByVal value As System.Windows.Media.Media3D.Vector3D)
_Scale = value
Maj()
End Set
End Property
''' <summary>
''' Renvoi la Matrice en Single(0..3,0..3)
''' </summary>
Public ReadOnly Property MatrixSingle() As Single(,)
Get
Dim M As Single(,)
ReDim M(3, 3)
M(0, 0) = _Mat.M11
M(0, 1) = _Mat.M12
M(0, 2) = _Mat.M13
M(0, 3) = _Mat.M14
M(1, 0) = _Mat.M21
M(1, 1) = _Mat.M22
M(1, 2) = _Mat.M23
M(1, 3) = _Mat.M24
M(2, 0) = _Mat.M31
M(2, 1) = _Mat.M32
M(2, 2) = _Mat.M33
M(2, 3) = _Mat.M34
M(3, 0) = _Mat.OffsetX
M(3, 1) = _Mat.OffsetY
M(3, 2) = _Mat.OffsetZ
M(3, 3) = _Mat.M44
Return M
End Get
End Property
#End Region
' Nouvelles coordonnées du point sans appliquer de Matrice d'Affichage en Perspective
Public Function Projection(ByRef P As System.Windows.Media.Media3D.Point3D) As System.Windows.Media.Media3D.Point3D
Return _Mat.Transform(P)
End Function
' Nouvelles coordonnées du point après avoir appliqué une Matrice d'Affichage en Perspective
Public Function Affichage(ByRef P As List(Of System.Windows.Media.Media3D.Point3D)) As List(Of System.Drawing.PointF)
Affichage = New List(Of System.Drawing.PointF)
For Each Pt In P
Dim retour As System.Windows.Media.Media3D.Point3D
retour = Final.Transform(Pt)
' applique ce code au dessous si ca ne fonctionne pas au dessus en jouant avec retour.z pour obtenir une perspective si tu veux faire simple
'retour.X = retour.X + 200
'retour.Y = retour.Y + 200
Affichage.Add(New System.Drawing.PointF(retour.X, retour.Y))
Next
End Function
Private Sub Maj()
Dim Matr As System.Windows.Media.Media3D.Matrix3D
Matr = CalculateRotationMatrix(Rotate.X, Rotate.Y, Rotate.Z)
' Pour retrouver les différents angles appliqués mais ca ne marche pas bien.
'Dim rotationX As Double = Vector3D.AngleBetween(New Vector3D(1, 0, 0), Matr.Transform(New Vector3D(1, 0, 0)))
'Dim rotationY As Double = Vector3D.AngleBetween(New Vector3D(0, 1, 0), Matr.Transform(New Vector3D(0, 1, 0)))
'Dim rotationZ As Double = Vector3D.AngleBetween(New Vector3D(0, 0, 1), Matr.Transform(New Vector3D(0, 0, 1)))
Matr.Translate(Translate)
Matr.Scale(Scale)
_Mat = Matr
Final = System.Windows.Media.Media3D.Matrix3D.Multiply(_Mat, Camera)
End Sub
Private Function CalculateRotationMatrix(ByRef x As Double, ByRef y As Double, ByRef z As Double) As Matrix3D
Dim Matrix As New Matrix3D
Matrix.Rotate(New Quaternion(New Vector3D(1, 0, 0), x))
Matrix.Rotate(New Quaternion(New Vector3D(0, 1, 0) * Matrix, y))
Matrix.Rotate(New Quaternion(New Vector3D(0, 0, 1) * Matrix, z))
Return Matrix
End Function
End Class