Creare con OpenGL un ambiente virtuale 3D

Da Gambas-it.org - Wikipedia.
Versione del 19 lug 2025 alle 06:16 di Vuott (discussione | contributi)
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)
Vai alla navigazione Vai alla ricerca

Le risorse di OpenGL consentono anche di creare ambienti virtuali 3D, come ad esempio delle stanze e altri scenari all'interno dei quali l'osservatore è apparentemente inserito ed è in grado di muoversi e di agire.

Di seguito vediamo alcuni esempi di codice.
E' necessario attivare una GLArea, un Timer (laddove previsto nel codice), il Componente grafico per la compatibilità con OpenGL (ad esempio: gb.gui.qt.opengl), nonché i Componenti:

  • gb.opengl
  • gb.opengl.glu

Codice essenziale per creare una stanza 3D

Public Sub Form_Open()

 With Me
   .x = 0
   .y = 0
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With 

End


Public Sub GLArea1_Resize()   ' Imposta la proiezione prospettica

 Dim w As Integer = GLArea1.W
 Dim h As Integer = GLArea1.H
 If h = 0 Then h = 1

 GL.Viewport(0, 0, w, h)

 GL.MatrixMode(GL.PROJECTION)
 GL.LoadIdentity()
 GLU.Perspective(60.0, w / h, 0.1, 100.0)

 GL.MatrixMode(GL.MODELVIEW)

End


Public Sub GLArea1_Draw()   ' Disegna la scena fissa (visuale rispetto alla stanza)

 GL.Clear(GL.COLOR_BUFFER_BIT Or GL.DEPTH_BUFFER_BIT)
 GL.Enable(GL.DEPTH_TEST)

' Colore di sfondo:
 GL.ClearColor(0.2, 0.2, 0.25, 1.0)

' Ripristina la trasformazione base:
 GL.LoadIdentity()

' Posiziona la scena rispetto all'osservatore nel punto definito da X, Y, Z.
' Variando X, l'osservatore si muove lateralmente rispetto alla stanza virtuale;
' Variando Y, l'osservatore si muove verticalmente rispetto alla stanza virtuale;
' Variando Z, l'osservatore si avvicina o si allontana rispetto alla stanza virtuale.
' L'osservatore posto nel punto (0.0, 0.0, 0.0) è posto sul margine iniziale (apertura) della stanza, centralmente e sul pavimento.
' Qui trasliamo tutto indietro e leggermente in basso:
 GL.Translatef(0.0, -1.0, -4.0)

' Disegna la stanza:
 DrawRoom()

 GL.Flush

End


Private Procedure DrawRoom()   ' Costruisce le pareti, il pavimento e il soffitto della stanza virtuale

' Parete posteriore:
 GL.Color3f(0.5, 0.5, 0.9)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, -2.0, -6.0)
 GL.Vertex3f(3.0, -2.0, -6.0)
 GL.Vertex3f(3.0, 2.0, -6.0)
 GL.Vertex3f(-3.0, 2.0, -6.0)
 GL.End

' Parete destra:
 GL.Color3f(0.6, 0.4, 0.4)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(3.0, -2.0, -6.0)
 GL.Vertex3f(3.0, -2.0, 0.0)
 GL.Vertex3f(3.0, 2.0, 0.0)
 GL.Vertex3f(3.0, 2.0, -6.0)
 GL.End

' Parete sinistra:
 GL.Color3f(0.4, 0.6, 0.4)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, -2.0, 0.0)
 GL.Vertex3f(-3.0, -2.0, -6.0)
 GL.Vertex3f(-3.0, 2.0, -6.0)
 GL.Vertex3f(-3.0, 2.0, 0.0)
 GL.End

' Pavimento:
 GL.Color3f(0.7, 0.7, 0.7)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, -2.0, 0.0)
 GL.Vertex3f(3.0, -2.0, 0.0)
 GL.Vertex3f(3.0, -2.0, -6.0)
 GL.Vertex3f(-3.0, -2.0, -6.0)
 GL.End

' (Facoltativo) Soffitto:
 GL.Color3f(0.3, 0.3, 0.3)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, 2.0, 0.0)
 GL.Vertex3f(3.0, 2.0, 0.0)
 GL.Vertex3f(3.0, 2.0, -6.0)
 GL.Vertex3f(-3.0, 2.0, -6.0)
 GL.End

End


Far roteare la stanza virtuale

In quest'altro esempio l'osservatore sarà posto al centro della stanza che ruoterà intorno a esso.

La stanza virtuale avrà le seguenti caratteristiche:

  • Larghezza: da x = -3.0 a x = +3.0 → centro orizzontale a x = 0.0
  • Altezza: da y = 0.0(pavimento)a y = 3.0
  • Profondità: da z = 0.0 (apertura) a z = -6.0 (parete di fondo)
Private angleY As Float       ' Angolo di rotazione attorno all’asse Y'
Private posX As Float         ' Posizione orizzontale (X)
Private posZ As Float = 3.0   ' Posizione in profondità (Z)


Public Sub Form_Open()

 With Me
   .x = 0
   .y = 0
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With 

' Timer per l'animazione (rotazione dello sguardo):
 Timer1.Delay = 16
 Timer1.Start

End


Public Sub Timer1_Timer()

 angleY += 0.5
 If angleY > 360 Then angleY -= 360
 GLArea1.Refresh

End


Public Sub GLArea1_Resize()

 Dim w As Integer = GLArea1.W
 Dim h As Integer = GLArea1.H
 If h = 0 Then h = 1

 GL.Viewport(0, 0, w, h)

 GL.MatrixMode(GL.PROJECTION)
 GL.LoadIdentity()
 GLU.Perspective(60.0, w / h, 0.1, 100.0)

 GL.MatrixMode(GL.MODELVIEW)

End


Public Sub GLArea1_Draw()

 GL.Clear(GL.COLOR_BUFFER_BIT Or GL.DEPTH_BUFFER_BIT)
 GL.Enable(GL.DEPTH_TEST)
 GL.ClearColor(0.1, 0.1, 0.15, 1.0)

 GL.LoadIdentity()

' Ruota lo sguardo prima di muovere l'osservatore:
 GL.Rotatef(angleY, 0.0, 1.0, 0.0)

' Posiziona l'osservatore al centro della stanza.:
' (aumentando il 1° argomento (X), l'osservatore si sposta a sinistra nella stanza)
' (aumentando il 2° argomento (Y), l'osservatore si sposta in alto nella stanza)
' (aumentando il 3° argomento (Z), l'osservatore si sposta in avanti nella stanza).
 GL.Translatef(0.0, -1.5, 3.0)  ' y = -altezza occhi, z = entra o esce dalla stanza.

' Disegna la stanza intorno all’osservatore:
 DrawRoom()

 GL.Flush

End


Private Procedure DrawRoom()   ' Costruisce le pareti, il pavimento e il soffitto della stanza virtuale

' Parete posteriore:
 GL.Color3f(0.5, 0.5, 0.9)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, 0.0, -6.0)
 GL.Vertex3f(3.0, 0.0, -6.0)
 GL.Vertex3f(3.0, 3.0, -6.0)
 GL.Vertex3f(-3.0, 3.0, -6.0)
 GL.End

' Parete destra:
 GL.Color3f(0.6, 0.4, 0.4)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(3.0, 0.0, -6.0)
 GL.Vertex3f(3.0, 0.0, 0.0)
 GL.Vertex3f(3.0, 3.0, 0.0)
 GL.Vertex3f(3.0, 3.0, -6.0)
 GL.End

' Parete sinistra:
 GL.Color3f(0.4, 0.6, 0.4)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, 0.0, 0.0)
 GL.Vertex3f(-3.0, 0.0, -6.0)
 GL.Vertex3f(-3.0, 3.0, -6.0)
 GL.Vertex3f(-3.0, 3.0, 0.0)
 GL.End

' Pavimento:
 GL.Color3f(0.7, 0.7, 0.7)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, 0.0, 0.0)
 GL.Vertex3f(3.0, 0.0, 0.0)
 GL.Vertex3f(3.0, 0.0, -6.0)
 GL.Vertex3f(-3.0, 0.0, -6.0)
 GL.End

' Soffitto (opzionale):
 GL.Color3f(0.8, 0.8, 0.0)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, 3.0, 0.0)
 GL.Vertex3f(3.0, 3.0, 0.0)
 GL.Vertex3f(3.0, 3.0, -6.0)
 GL.Vertex3f(-3.0, 3.0, -6.0)
 GL.End

End


Consentire lo spostamento arbitrario dell'osservatore all'interno della stanza virtuale

In quest'altro esempio l'osservatore avrà la possibilità di muoversi all'interno della stanza usando alcuni tasti della tastiera:

  • "W" → spostamento in avanti;
  • "S" → spostamento in dietro;
  • "A" → rotazione in senso antiorario;
  • "D" → rotazione in senso orario;
Private angleY As Float       ' Angolo di rotazione attorno all’asse Y
Private posX As Float         ' Posizione orizzontale (X)
Private posZ As Float = 3.0   ' Posizione in profondità (Z)


Public Sub Form_Open()

 With Me
   .x = 0
   .y = 0
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With 
 
End
 

Public Sub GLArea1_Resize()

 Dim w As Integer = GLArea1.W
 Dim h As Integer = GLArea1.H
 If h = 0 Then h = 1

 GL.Viewport(0, 0, w, h)

 GL.MatrixMode(GL.PROJECTION)
 GL.LoadIdentity()
 GLU.Perspective(60.0, w / h, 0.1, 100.0)

 GL.MatrixMode(GL.MODELVIEW)

End


Public Sub GLArea1_Draw()

 GL.Clear(GL.COLOR_BUFFER_BIT Or GL.DEPTH_BUFFER_BIT)
 GL.Enable(GL.DEPTH_TEST)
 GL.ClearColor(0.1, 0.1, 0.15, 1.0)

 GL.LoadIdentity()

' Applica la rotazione:
 GL.Rotatef(angleY, 0.0, 1.0, 0.0)

' Posiziona l'osservatore al centro della stanza.:
' (aumentando il 1° argomento (X), l'osservatore si sposta a sinistra nella stanza)
' (aumentando il 2° argomento (Y), l'osservatore si sposta in alto nella stanza)
' (aumentando il 3° argomento (Z), l'osservatore si sposta in avanti nella stanza).
 GL.Translatef(-posX, -1.5, posZ)  ' y = -altezza occhi, z = entra o esce dalla stanza.

' Disegna la stanza intorno all’osservatore:
 DrawRoom()

 GL.Flush

End


Private Procedure DrawRoom()   ' Costruisce le pareti, il pavimento e il soffitto della stanza virtuale

' Parete posteriore:
 GL.Color3f(0.5, 0.5, 0.9)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, 0.0, -6.0)
 GL.Vertex3f(3.0, 0.0, -6.0)
 GL.Vertex3f(3.0, 3.0, -6.0)
 GL.Vertex3f(-3.0, 3.0, -6.0)
 GL.End

' Parete destra:
 GL.Color3f(0.6, 0.4, 0.4)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(3.0, 0.0, -6.0)
 GL.Vertex3f(3.0, 0.0, 0.0)
 GL.Vertex3f(3.0, 3.0, 0.0)
 GL.Vertex3f(3.0, 3.0, -6.0)
 GL.End

' Parete sinistra:
 GL.Color3f(0.4, 0.6, 0.4)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, 0.0, 0.0)
 GL.Vertex3f(-3.0, 0.0, -6.0)
 GL.Vertex3f(-3.0, 3.0, -6.0)
 GL.Vertex3f(-3.0, 3.0, 0.0)
 GL.End

' Pavimento:
 GL.Color3f(0.7, 0.7, 0.7)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, 0.0, 0.0)
 GL.Vertex3f(3.0, 0.0, 0.0)
 GL.Vertex3f(3.0, 0.0, -6.0)
 GL.Vertex3f(-3.0, 0.0, -6.0)
 GL.End

' Soffitto (opzionale):
 GL.Color3f(0.8, 0.8, 0.0)
 GL.Begin(GL.QUADS)
 GL.Vertex3f(-3.0, 3.0, 0.0)
 GL.Vertex3f(3.0, 3.0, 0.0)
 GL.Vertex3f(3.0, 3.0, -6.0)
 GL.Vertex3f(-3.0, 3.0, -6.0)
 GL.End

End


Public Sub Form_KeyPress()

 Dim stepSize As Float = 0.1

 Select Case Key.Code
   Case Key["W"]   ' Sposta in avanti l'osservatore
     posX += Sin(Rad(angleY)) * stepSize
     posZ += stepSize

   Case Key["S"]   ' Sposta indietro l'osservatore
     posX -= Sin(Rad(angleY)) * stepSize
     posZ -= stepSize

   Case Key["A"]   ' Ruota l'osservatore in senso antiorario
     angleY -= 1

   Case Key["D"]   ' Ruota l'osservatore in senso orario
     angleY += 1
 End Select

 GLArea1.Refresh

End