Rotation d'un triangle (sans opengl/directx)

Contenu du snippet

Il faut
>>Une form
>> Un timer

Source / Exemple :


Private Type COORD
    x As Long
    y As Long
End Type
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Const ALTERNATE = 1 ' ALTERNATE and WINDING are
Const WINDING = 2 ' constants for FillMode.
Const BLACKBRUSH = 4 ' Constant for brush type.

'Déclarations
Dim X3d!(1 To 5)
Dim Y3d!(1 To 5)
Dim Z3d!(1 To 5)
Dim x2!(1 To 5)
Dim y2!(1 To 5)
Dim z2!(1 To 5)
Dim x3!(1 To 5)
Dim y3!(1 To 5)
Dim z3!(1 To 5)
Dim RotX!(1 To 5)
Dim RotY!(1 To 5)
Dim RotZ!(1 To 5)
Dim x2d%(1 To 5)
Dim y2d%(1 To 5)

Const t1 = 0.03

Dim PtX(1 To 5)
Dim PtY(1 To 5)

Private Sub Form_Load()

Form1.DrawWidth = 10

Timer1.Interval = 1

'Initialisations
X3d!(1) = -10: Y3d!(1) = -10: Z3d!(1) = 0
X3d!(2) = 10: Y3d!(2) = -10: Z3d!(2) = 0
X3d!(3) = 10: Y3d!(3) = 10: Z3d!(3) = 0
X3d!(4) = -10: Y3d!(4) = 10: Z3d!(4) = 0
X3d!(5) = 0: Y3d!(5) = 0: Z3d!(5) = 10

End Sub

Private Sub Form_Unload(Cancel As Integer)
End
End Sub

Private Sub Timer1_Timer()
draw
DoEvents
End Sub

Function draw()
'Zoom de 200
Zoom = 200
'Depth de 40
depth = 40

'Effacer l'écran
Form1.Cls

'Boucle de rotation et d'affichage du triangle
For i = 1 To 5

'Rotation Z
RotZ!(i) = RotZ!(i) + t1

'Rotation X
RotX!(i) = RotX!(i) + t1

'Rotation Y
RotY!(i) = RotY!(i) + t1

'Effectuer les rotations

x2!(i) = (X3d!(i) * Cos(RotZ!(i))) - (Y3d!(i) * Sin(RotZ!(i)))
y2!(i) = (X3d!(i) * Sin(RotZ!(i))) + (Y3d!(i) * Cos(RotZ!(i)))
z2!(i) = (x2!(i) * Sin(RotY!(i))) + (Z3d!(i) * Cos(RotY!(i)))

x3!(i) = (x2!(i) * Cos(RotY!(i))) - (Z3d!(i) * Sin(RotY!(i)))
y3!(i) = (y2!(i) * Cos(RotX!(i))) - (z2!(i) * Sin(RotX!(i)))
z3!(i) = (y2!(i) * Sin(RotX!(i))) + (z2!(i) * Cos(RotX!(i)))

x2d%(i) = Zoom * (x3!(i) / (z3!(i) + depth)) + 320
y2d%(i) = Zoom * (y3!(i) / (z3!(i) + depth)) + 240

PtX(i) = (x2d%(i) * 20)
PtY(i) = (y2d%(i) * 20)

Next i

plan PtX(1), PtY(1), PtX(2), PtY(2), PtX(3), PtY(3), PtX(4), PtY(4)
plan PtX(1), PtY(1), PtX(2), PtY(2), PtX(5), PtY(5), PtX(5), PtY(5)
plan PtX(2), PtY(2), PtX(3), PtY(3), PtX(5), PtY(5), PtX(5), PtY(5)
plan PtX(3), PtY(3), PtX(4), PtY(4), PtX(5), PtY(5), PtX(5), PtY(5)
plan PtX(4), PtY(4), PtX(1), PtY(1), PtX(5), PtY(5), PtX(5), PtY(5)

End Function

Function plan(px1, py1, px2, py2, px3, py3, px4, py4)

    Dim poly(1 To 4) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long
    NumCoords = 4
    Me.ScaleMode = vbPixel
    
    poly(1).x = px1 / 15
    poly(1).y = py1 / 15
    poly(2).x = px2 / 15
    poly(2).y = py2 / 15
    poly(3).x = px3 / 15
    poly(3).y = py3 / 15
    poly(4).x = px4 / 15
    poly(4).y = py4 / 15

    
    Form1.ForeColor = vbBlue
    
    Polygon Me.hdc, poly(1), NumCoords

    hBrush = GetStockObject(BLACKBRUSH)
    
    hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)

    If hRgn Then FillRgn Me.hdc, hRgn, hBrush
    DeleteObject hRgn
End Function

Conclusion :


Pour changer la vitesse, il faut changer
Const t1 = 0.03

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.