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
| Option Explicit
' Temps à attendre pour le prochaine affichage
Private gWaitTime As Long
' Temps écoulé pour calcul du frame rate
Private gElapsedTime As Long
' Nombre d'affichage pour calcul du frame rate
Private gDisplayCount As Long
' RotationX
Private gRotateX As Double
' RotationY
Private gRotateY As Double
' Zoom
Private gZoom As Double
Sub Fenetre_OpenGL() 'appelée par mon appli pour afficher la visu 3D
' Chargement de freeglut
If LoadLibrary(App.Path & "\freeglut.dll") = 0 Then
MsgBox "Impossible de charger la librairie freeglut"
Exit Sub
End If
' Initialisation de la librairie
glutInit 0&, ""
' Initialisation du mode d'affichage
glutInitDisplayMode GLUT_RGBA Or GLUT_DOUBLE Or GLUT_DEPTH
' Création d'une fenêtre
glutCreateWindow "Tutoriel fenêtre GLUT"
' Définition de l'option de sortie de boucle
glutSetOption GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS
' Fonction d'affichage
glutDisplayFunc AddressOf CallBackDraw
' Fonction d'attente
glutIdleFunc AddressOf CallBackIdle
' Fonctions de rappel clavier
glutSpecialFunc AddressOf CallBackSpecial
' Fonction de rappel molette de souris
glutMouseWheelFunc AddressOf CallBackMouseWheel
' Appel de la fonction d'initialisation
InitScene
' Boucle principale
glutMainLoop
End Sub
Public Sub InitScene()
' Initialisation du temps déclencheur de l'affichage
gWaitTime = glutGet(GLUT_ELAPSED_TIME)
' Tests de profondeur
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
' Initialisation des variables de visualisation
gRotateX = 0
gRotateY = 0
gZoom = 10
End Sub
' Fonction d'affichage
Public Sub CallBackDraw()
'appel de la fonction de rendu
Call Render
' Echange les buffers
glutSwapBuffers
' Calcul et affichage du frame rate
gDisplayCount = gDisplayCount + 1
If gDisplayCount = 100 Then
glutSetWindowTitle "Frame rate : " & format(gDisplayCount / (glutGet(GLUT_ELAPSED_TIME) - gElapsedTime) * 1000, "0,0")
gDisplayCount = 0
gElapsedTime = glutGet(GLUT_ELAPSED_TIME)
End If
End Sub
Public Sub CallBackIdle()
Dim lTimer As Long
' Temps présent
lTimer = glutGet(GLUT_ELAPSED_TIME)
' Si temps présent >= au temps attendu
If lTimer >= gWaitTime Then
' Rrafraichir l'affichage
glutPostRedisplay
' n affichages par seconde
gWaitTime = lTimer + (1000 / 50)
End If
End Sub
Public Sub Render()
Dim i As Long
' Passage en matrice de projection
glMatrixMode GL_PROJECTION
' Initialisation de la matrice
glLoadIdentity
' Définition de la perspective
gluPerspective 45, 1, 0.1, 100
' Passage en matrice de modélisation-visualisation
glMatrixMode GL_MODELVIEW
' Initialisation de la matrice
glLoadIdentity
' Position de la caméra
gluLookAt 0, 0, gZoom, 0, 0, 0, 0, 1, 0
glRotated gRotateX, 1, 0, 0
glRotated gRotateY, 0, 1, 0
' Vide les buffers couleur et profondeur
glClear GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT
' Sauvegarde la matrice
glPushMatrix
' Rotation du cube
glRotated 30, 1, 1, 1
' Début de la primitive pour l'aile
glBegin GL_TRIANGLE_STRIP 'triangles en bande
'en vert
Call glColor3d(0, 1, 0)
'les points alternativement d'un côté et de l'autre
With ProfilInitial
For i = 1 To .NbPointsE
Call glVertex3d(.PointE(i).x, .PointE(i).y, 0)
Call glVertex3d(.points(i).x, .points(i).y, .EcartementZ)
Next i
'fin de la primitive
glEnd
'les faces (qui posent problème)
glBegin GL_POLYGON
Call glColor3d(1, 0, 0)
For i = 1 To .NbPointsE
Call glVertex3d(.PointE(i).x, .PointE(i).y, 0)
Next i
glEnd
glBegin GL_POLYGON
Call glColor3d(1, 0, 0)
For i = 1 To .NbPointsE
Call glVertex3d(.points(i).x, .points(i).y, .EcartementZ)
Next i
glEnd
End With
' Restaure la matrice
glPopMatrix
End Sub
'gestion des rotations avec les flèches du clavier
Public Sub CallBackSpecial(ByVal key As Long, ByVal x As Long, ByVal y As Long)
Select Case key
Case GLUT_KEY_LEFT
gRotateY = gRotateY - 10
Case GLUT_KEY_RIGHT
gRotateY = gRotateY + 10
Case GLUT_KEY_UP
gRotateX = gRotateX - 10
Case GLUT_KEY_DOWN
gRotateX = gRotateX + 10
Case GLUT_KEY_F2
gZoom = gZoom + 1
Case GLUT_KEY_F3
gZoom = gZoom - 1
End Select
End Sub
'gestion du zoom avec la molette de la souris
Public Sub CallBackMouseWheel(ByVal wheel As Long, ByVal direction As Long, ByVal x As Long, ByVal y As Long)
gZoom = gZoom + direction
End Sub |
Partager