1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Public Class Visu3D
Dim EtatBouton As Integer
Dim XPrec As Integer, YPrec As Integer
Dim AX As Double, AY As Double
Dim AXPrec As Double, AYPrec As Double
Dim PX As Single, PY As Single, PZ As Single
Dim PXPrec As Single, PYPrec As Single, PZPrec As Single
Dim Debut As Boolean
Public Sub New()
Debut = True
InitializeComponent()
End Sub
Private Sub GlControl1_Load(sender As System.Object, e As System.EventArgs) Handles GlControl1.Load
GL.ClearColor(Color.Black)
End Sub
Public Sub Montrer()
EtatBouton = 0
Debut = True
Me.ShowDialog()
End Sub
Private Sub GlControl1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseDown
Dim eX As Integer, eY As Integer
eX = e.X
eY = e.Y
Select Case e.Button
Case Windows.Forms.MouseButtons.Left : EtatBouton = 1
Case Windows.Forms.MouseButtons.Right : EtatBouton = 2
Case Else : EtatBouton = 0
End Select
If EtatBouton <> 0 Then
XPrec = eX
YPrec = eY
End If
Select Case EtatBouton
Case 0
Case 1
AXPrec = AX
AYPrec = AY
Case 2
PXPrec = PX
PYPrec = PY
PZPrec = PZ
End Select
End Sub
Private Sub GlControl1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseMove
Dim DX As Integer, DY As Integer
Dim eX As Integer, eY As Integer
eX = e.X
eY = e.Y
DX = eX - XPrec
DY = eY - YPrec
Select Case EtatBouton
Case 0 'RIEN
Case 1 'ROTATION
AX = AXPrec + DX * 0.3
AY = AYPrec + DY * (-0.3)
GlControl1.Invalidate()
Case 2 'DEPLACEMENT
'PX = PXPrec + DX
PY = PYPrec + DY * (-0.5F)
PZ = PZPrec + DX * (-0.5F)
GlControl1.Invalidate()
End Select
End Sub
Private Sub GlControl1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseUp
Dim eX As Integer
Dim eY As Integer
Select Case EtatBouton
Case 0
Case 1
AXPrec = AX
AYPrec = AY
Case 2
PXPrec = PX
PYPrec = PY
PZPrec = PZ
End Select
EtatBouton = 0
eX = e.X
eY = e.Y
End Sub
Private Sub Visu3D_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
Dim eX As Integer
Dim eY As Integer
eX = e.X
eY = e.Y
End Sub
Private Sub GlControl1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
If Debut Then
AX = 0
AY = 0
PX = 0
PY = 0
Call Init3D(AX, AY, PX, PY, PZ)
Debut = False
Else
'ICI C'EST DEBILE : on créé toute la scène à chaque fois
'alors qu'il faudrait juste la déplacer si AX, AY, PX, PY, PZ changent)
'et l'afficher
Call Init3D(AX, AY, PX, PY, PZ)
End If
End Sub
Private Sub Init3D(AX As Double, AY As Double, PX As Single, PY As Single, PZ As Single)
'First Clear Buffers
GL.Clear(ClearBufferMask.ColorBufferBit)
GL.Clear(ClearBufferMask.DepthBufferBit)
Dim perspective As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1.04, 4 / 3, 1, 10000) 'Setup Perspective
Dim lookat As Matrix4 = Matrix4.LookAt(100, 20, 0, 0, 0, 0, 0, 1, 0) 'Setup camera
GL.MatrixMode(MatrixMode.Projection) 'Load Perspective
GL.LoadIdentity()
GL.LoadMatrix(perspective)
GL.MatrixMode(MatrixMode.Modelview) 'Load Camera
GL.LoadIdentity()
GL.LoadMatrix(lookat)
GL.Viewport(0, 0, GlControl1.Width, GlControl1.Height) 'Size of window
GL.Enable(EnableCap.DepthTest) 'Enable correct Z Drawings
GL.DepthFunc(DepthFunction.Less) 'Enable correct Z Drawings
'Rotating
'GL.Rotate(NumericUpDown1.Value, 0, 0, 1)
'GL.Rotate(NumericUpDown2.Value, 0, 1, 0)
GL.Rotate(AX, 0, 1, 0)
GL.Rotate(AY, 0, 0, 1)
'Draw pyramid, Y is up, Z is twards you, X is left and right
'Vertex goes (X,Y,Z)
GL.Begin(BeginMode.Triangles)
'Face 1
GL.Color3(Color.Red)
GL.Vertex3(50 + PX, 0 + PY, 0 + PZ)
GL.Color3(Color.Red)
GL.Vertex3(0 + PX, 25 + PY, 0 + PZ)
GL.Color3(Color.Red)
GL.Vertex3(0 + PX, 0 + PY, 50 + PZ)
'Face 2
GL.Color3(Color.Green)
GL.Vertex3(-50 + PX, 0 + PY, 0 + PZ)
GL.Color3(Color.Green)
GL.Vertex3(0 + PX, 25 + PY, 0 + PZ)
GL.Color3(Color.Green)
GL.Vertex3(0 + PX, 0 + PY, 50 + PZ)
'Face 3
GL.Color3(Color.White)
GL.Vertex3(50 + PX, 0 + PY, 0 + PZ)
GL.Color3(Color.White)
GL.Vertex3(0 + PX, 25 + PY, 0 + PZ)
GL.Color3(Color.White)
GL.Vertex3(0 + PX, 0 + PY, -50 + PZ)
'Face 4
GL.Color3(Color.Blue)
GL.Vertex3(-50 + PX, 0 + PY, 0 + PZ)
GL.Color3(Color.Blue)
GL.Vertex3(0 + PX, 25 + PY, 0 + PZ)
GL.Color3(Color.Blue)
GL.Vertex3(0 + PX, 0 + PY, -50 + PZ)
'Finish the begin mode with "end"
GL.End()
'Finally...
GraphicsContext.CurrentContext.VSync = True 'Caps frame rate as to not over run GPU
GlControl1.SwapBuffers() 'Takes from the 'GL' and puts into control
End Sub
End Class |
Partager