OPENGL

Signaler
Messages postés
1
Date d'inscription
mercredi 16 juin 2004
Statut
Membre
Dernière intervention
25 juin 2007
-
Messages postés
3
Date d'inscription
samedi 16 janvier 2010
Statut
Membre
Dernière intervention
17 octobre 2012
-
Bonjour,

Je suis parti d'une source de Patrice Terrier écrit en C#, que j'ai adapté en VB. Dans le code j'ai supprimé tous les appels à GDImage.dll, en ne laissant que les commandes OPENGL.
Hélas je n'arrice pas à afficher le chart dans la form. Je ne sais pas pourquoi. parmis vous, y-aurait-il quelqu'un d'assez callé en OpenGL pour trouver le problème?

Merci d'avance.

Voici le code en VB

Option Explicit
Private Type THISCHART
   ChartNumber       As Integer
   ChartSeries       As Integer
   Radius            As GLfloat
   xoff              As GLfloat
   yoff              As GLfloat
   zoff              As GLfloat
   xrot              As GLfloat
   yrot              As GLfloat
   xCpy              As GLfloat
   yCpy              As GLfloat
   xDown             As Integer
   yDown             As Integer
   ListIndex         As Integer
End Type


Private hGLcontrol         As Long
Private Chart              As THISCHART
Private quadObj            As Long
Private ChartSeries        As Integer
Private ChartNumber        As Integer
Private ChartItem()        As Long
Private Zoom               As Single


Private Sub Form_Load()
Dim k                As Long
Dim i                As Long
Dim j                As Long
Dim Value            As Single
Dim LightDiffuse(3)  As Single
Dim LightAmbient(3)  As Single
Dim LightPosition(3) As Single
Dim yRange           As Single


ChartSeries = 7
ChartNumber = 4


ReDim ChartItem(ChartSeries * ChartNumber) As Long


'//Create the OpenGL control
If SetupPixelFormat(Form2.hDC) = False Then
   Exit Sub
End If
  
If CreateViewGLContext(Form2.hDC) = False Then
   Exit Sub
End If
  
Call InitGraph
Call GL.glEnable(GL.GL_TEXTURE_2D)
'//Setup drawing mode
Call GL.glClearColor(0#, 0#, 0#, 0#)
Call GL.glClearDepth(1#)
Call GL.glDepthFunc(GL.GL_LESS)
Call GL.glEnable(GL.GL_DEPTH_TEST)
Call GL.glShadeModel(GL.GL_SMOOTH)
Call GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST)
'//Create quadric object
quadObj = GLU.gluNewQuadric
Call GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
Call GL.glEnable(GL.GL_BLEND)
Call GL.glEnable(GL.GL_LINE_SMOOTH)
Call GLU.gluQuadricNormals(quadObj, GLU.GLU_SMOOTH)
'//Setup ambient lighting
LightDiffuse(0) = 1#
LightDiffuse(1) = 1#
LightDiffuse(2) = 1#
LightDiffuse(3) = 1#


LightAmbient(0) = 0.05
LightAmbient(1) = 0.05
LightAmbient(2) = 0.05
LightAmbient(3) = 1#


LightPosition(0) = -50#
LightPosition(1) = 50#
LightPosition(2) = 100#
LightPosition(3) = 1#


Call GL.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, LightDiffuse(0))   ' Setup The Diffuse Light
Call GL.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, LightAmbient(0))   ' Setup The Ambient Light
Call GL.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, LightPosition(0)) ' Position The Light
Call GL.glEnable(GL.GL_LIGHT1)                                    ' Enable Light ONE
Call GL.glEnable(GL.GL_LIGHTING)                                  ' Enable Lighting
Call GL.glEnable(GL.GL_COLOR_MATERIAL)                            ' Enable Coloring Of Material
Call GL.glColorMaterial(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE)
'//Init list index
Chart.ListIndex = 1
Call GL.glGenLists(Chart.ListIndex)
'//Create x,y,z axis
yRange = 9#
Call DrawAxis(15#, yRange, 11#)


k = 0
For i = 1 To Chart.ChartNumber
   For j = 1 To Chart.ChartSeries
      Randomize
      Value = CSng(CInt(yRange) * Rnd()) + 1
      k = k + 1


      ChartItem(k) = Chart.ListIndex + k
      Call GL.glNewList(ChartItem(k), GL.GL_COMPILE)
         Call GL.glRotatef(-90#, 1#, 0#, 0#)
         Call GLU.gluQuadricNormals(quadObj, GLU.GLU_SMOOTH)  ' Create Smooth Normals
         Call GLU.gluQuadricTexture(quadObj, 1)               ' Create Texture Coords
         Call GLU.gluCylinder(quadObj, Chart.Radius, Chart.Radius, Value, 32, 1)


         Call GLU.gluDisk(quadObj, 0#, Chart.Radius, 32, 32)
         Call GL.glTranslatef(0#, 0#, Value)
         Call GLU.gluDisk(quadObj, 0#, Chart.Radius, 32, 32)
         Call GL.glTranslatef(0#, 0#, -Value)
         Call GL.glRotatef(90#, 1#, 0#, 0#)    '
      Call GL.glEndList
   Next
Next
End Sub


Private Sub Form_Unload(Cancel As Integer)
Dim k As Integer


Call GL.glDeleteLists(1, 1)
For k = 1 To UBound(ChartItem)
   Call GL.glDeleteLists(ChartItem(k), k + 1)
Next


If quadObj <> 0 Then
   GLU.gluDeleteQuadric quadObj
   quadObj = 0
End If


If WGL.wglGetCurrentContext() <> 0 Then
   WGL.wglMakeCurrent Form2.hDC, 0
End If


If hGLcontrol <> 0 Then
   WGL.wglDeleteContext hGLcontrol
   hGLcontrol = 0
End If
End Sub


Private Sub Timer1_Timer()
If hGLcontrol <> 0 Then
   Call RenderOpenGL
End If
End Sub


Private Sub DrawAxis(ByVal x As Single, ByVal y As Single, ByVal z As Single)
Dim a          As Long
Dim COLOR1(3)  As Single
Dim COLOR2(3)  As Single
Dim COLOR3(3)  As Single


COLOR1(0) = 0.6
COLOR1(1) = 0.6
COLOR1(2) = 0.6
COLOR1(3) = 0.3


COLOR2(0) = 1#
COLOR2(1) = 1#
COLOR2(2) = 1#
COLOR2(3) = 1#


COLOR3(0) = 0.6
COLOR3(1) = 0#
COLOR3(2) = 0#
COLOR3(3) = 1#


Call GL.glNewList(Chart.ListIndex, GL.GL_COMPILE)
   Call GL.glBegin(GL.GL_QUADS)
      Call GL.glColor4fv(COLOR1(0))
      Call GL.glVertex3f(0#, y, z)
      Call GL.glVertex3f(0#, -1#, z)
      Call GL.glVertex3f(0#, -1#, -1#)
      Call GL.glVertex3f(0#, y, -1#)
  
      Call GL.glVertex3f(-1#, y, 0#)
      Call GL.glVertex3f(-1#, -1#, 0#)
      Call GL.glVertex3f(x, -1#, 0#)
      Call GL.glVertex3f(x, y, 0#)
   Call glEnd
  
   Call GL.glColor4fv(COLOR2(0))
   For a = 1 To y - 1
      Call GL.glBegin(GL.GL_LINE_STRIP)
         Call GL.glVertex3f(0.1, CSng(a), z)
         Call GL.glVertex3f(0.1, CSng(a), 0.1)
         Call GL.glVertex3f(x, CSng(a), 0.1)
      Call GL.glEnd
   Next
  
   Call GL.glColor4fv(COLOR3(0))
   Call GL.glBegin(GL.GL_LINE_STRIP)
      Call GL.glVertex3f(0.1, 0#, z)
      Call GL.glVertex3f(0.1, 0#, 0.1)
      Call GL.glVertex3f(x, 0#, 0.1)
   Call GL.glEnd
  
   Call GL.glBegin(GL.GL_LINES)
      Call GL.glVertex3f(0.1, -1#, 0.1)
      Call GL.glVertex3f(0.1, y, 0.1)
   Call GL.glEnd
Call GL.glEndList
End Sub


Private Sub InitGraph()
'// Graph Offsets
Chart.xoff = -3.5
Chart.yoff = 3.25
Chart.zoff = -36
'// Graph Rotation
Chart.xrot = 45
Chart.yrot = -30
Chart.Radius = 1#


Chart.ChartSeries = 7
Chart.ChartNumber = 4
End Sub


Private Sub RenderOpenGL()
Dim k             As Long
Dim i             As Long
Dim j             As Long
Dim Color(3)      As Single


'// Switch back to default color
Call GL.glColor3f(1#, 1#, 1#)
Call GL.glClear(GL.GL_COLOR_BUFFER_BIT Or GL.GL_DEPTH_BUFFER_BIT)
Call GL.glLoadIdentity
'// ***************
'// Draw chart
'// ***************
Zoom = 50
glScalef Zoom, Zoom, Zoom


Call GL.glTranslatef(Chart.xoff, Chart.yoff, Chart.zoff)
Call GL.glRotatef(Chart.xrot, 1#, 0#, 0#)
Call GL.glRotatef(Chart.yrot, 0#, 1#, 0#)
'//Draw the XYZ grid
Call GL.glLineWidth(1#)
Call GL.glCallList(Chart.ListIndex)
'//Shall we use the texture mode
Call GL.glDisable(GL.GL_TEXTURE_2D)


k = 0
Call GL.glTranslatef(Chart.Radius + 0.5, 0#, Chart.Radius + 0.5)
For i = 1 To Chart.ChartNumber
   '// Set color for the chart serie
   Select Case i   Case 1: Color(0) 255: Color(1) 255: Color(2) = 255: Color(3) = 255   Case 2: Color(0) 220: Color(1) 0: Color(2) = 255: Color(3) = 0   Case 3: Color(0) 200: Color(1) 0: Color(2) = 0: Color(3) = 255   Case 4: Color(0) 192: Color(1) 255: Color(2) = 0: Color(3) = 0
   End Select
   Call GL.glColor4fv(Color(0))
  
   For j = 1 To Chart.ChartSeries
      k = k + 1
      Call GL.glCallList(ChartItem(k))
      Call GL.glTranslatef(2# * Chart.Radius, 0#, 0#)
   Next
   Call GL.glTranslatef(-2# * Chart.Radius * Chart.ChartSeries, 0#, 2# * Chart.Radius + 0.5)
Next


'// Make sure to restore the texture mode
Call GL.glEnable(GL.GL_TEXTURE_2D)
'//Draw the scene
Call SwapBuffers(Form2.hDC)
End Sub


Private Function CreateViewGLContext(ByVal hDC As Long) As Boolean
hGLcontrol = WGL.wglCreateContext(hDC)
If hGLcontrol = 0 Then
   CreateViewGLContext = False
   Exit Function
End If


If WGL.wglMakeCurrent(hDC, hGLcontrol) = 0 Then
   CreateViewGLContext = False
   Exit Function
End If


CreateViewGLContext = True
End Function


Private Function SetupPixelFormat(ByVal hDC As Long) As Boolean
Dim PixelDesc        As PIXELFORMATDESCRIPTOR
Dim PixelFormat      As Integer




PixelDesc.nSize = Len(PixelDesc)
PixelDesc.nVersion = 1
PixelDesc.cColorBits = 32
PixelDesc.cDepthBits = 16
PixelDesc.dwFlags = PFD_DOUBLEBUFFER Or _
                    PFD_DRAW_TO_WINDOW Or _
                    PFD_SUPPORT_OPENGL


PixelDesc.iLayerType = PFD_MAIN_PLANE
PixelDesc.iPixelType = PFD_TYPE_RGBA
PixelFormat = ChoosePixelFormat(hDC, PixelDesc)
If PixelFormat = 0 Then
   PixelFormat = 1 '// par défaut on force l'index à 1
   If DescribePixelFormat(hDC, PixelFormat, Len(PixelDesc), PixelDesc) = 0 Then
      SetupPixelFormat = False
      Exit Function
   End If
End If


If SetPixelFormat(hDC, PixelFormat, PixelDesc) = 0 Then
   SetupPixelFormat = False
   Exit Function
End If


SetupPixelFormat = True
End Function 

2 réponses

Messages postés
17286
Date d'inscription
mercredi 2 janvier 2002
Statut
Modérateur
Dernière intervention
23 décembre 2019
67
encore une histoire de valeur de Form1.AutoRedraw ?
Messages postés
3
Date d'inscription
samedi 16 janvier 2010
Statut
Membre
Dernière intervention
17 octobre 2012

Bonjour,

Je cherche un exemple en vbnet pour lire un ficher STL (CAO) dans un picturebox en OPENGL.
En vb6, sans souci mais je cherche une personne qui l'aurais deja fait en VB9/VB10.
Merci
Michel67650