Wave in OpenGL

Share your advanced PureBasic knowledge/code with the community.
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Wave in OpenGL

Post by threedslider »

A simple wave made with OpenGL :)

Here the code :

Code: Select all

; Created by threedslider 18/02/2025


InitSprite()
InitKeyboard()


OpenWindow(0, 0, 0, 800, 600, "OpenGL : test pour L'onde", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

; Paramètres de l'onde
Define.f time = 0.0
Define.f amplitude = 1.0
Define.f frequency = 0.1
Define.f speed = 0.05
Dim wave.f(200)
Define.i i = 0
Define event = 0
Define quit = 0

glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_() 
gluPerspective_(45.0, 800/600, 1.0, 60.0)
glMatrixMode_(#GL_MODELVIEW)
glTranslatef_(-1, 0, -4)
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_LIGHT0)
glEnable_(#GL_LIGHTING)
glEnable_(#GL_COLOR_MATERIAL)
glEnable_(#GL_DEPTH_TEST)
glEnable_(#GL_CULL_FACE)   
glViewport_(0, 0, 800, 600)


Repeat
glClearColor_(0.8, 0.8, 0.8, 1.0) ; fond couleur
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  Repeat
    event = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        quit = 1
     EndSelect
   Until event = 0
   
  
     ; Mise à jour de l'onde de vague
  time + speed
  For i = 0 To 199
    wave(i) = Sin(i * frequency + time) * amplitude
  Next i
  
  
  ; Dessiner l'onde
  glBegin_(#GL_LINE_STRIP)
  glColor3f_(0.0, 0.0, 1.0)  ; Bleu
  glNormal3f_(0.0,0.0,1.0)
  For i = 0 To 199
    glVertex2f_(i*0.01   , wave(i)  ) ; Position de l'onde
  Next i
  glEnd_()
 
 FlipBuffers()
 
 ExamineKeyboard()
 
Until KeyboardPushed(#PB_Key_Escape) Or quit = 1
Happy coding !
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Wave in OpenGL

Post by miso »

Nice, thanks for sharing.
Post Reply