OpenGL Switch vsync on/off with all OS Systems

Everything related to 3D programming
mpz
Enthusiast
Enthusiast
Posts: 494
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

OpenGL Switch vsync on/off with all OS Systems

Post by mpz »

Hello to all,

i want to switch on/off the Vsync of a OpenglGadegt on the fly. i send a testcode and it works with windows and linux. I need a solution for mac. For now i use the FreeGadget/OpenGLGadget solution. The problem ist light and camera must be reactivated and i want to change it on the fly

works with all os system, but not good

Code: Select all

   
    If Var = 0
       FreeGadget(0)
       OpenGLGadget(0, 0, 0, WindowWidth(0),WindowHeight(0),#PB_OpenGL_NoFlipSynchronization)
       VSync_Rendertime = 0
    ElseIf Var = 1
       FreeGadget(0)
       OpenGLGadget(0, 0, 0, WindowWidth(0),WindowHeight(0),#PB_OpenGL_FlipSynchronization)
       VSync_Rendertime = 1
    EndIf



I hope anybody can help...
Greetings Michael


Here is the code for testing, works with windows and ubuntu, i get ideas from the following link:
https://www.purebasic.fr/english/viewto ... ss#p483674

i dont know the missing function (wglSwapIntervalEXT -> linux/ glXSwapIntervalEXT-> Windows). Google has not an answer for me :oops:

Code: Select all

; OpenGL Gadget demonstration
;
; (c) Fantaisie Software

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS

  ;ImportC "-framework OpenGL" ; only for tests, not working
  ;  glXSwapIntervalEXT(type.b) As "_glXSwapIntervalEXT"
  ;  wglGetProcAddress(name.i) As "_wglGetProcAddress"
  ;EndImport
  
   Macro setGLEXT(var, extname)
       var = dlsym_(#RTLD_DEFAULT,extname)
   EndMacro
  
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
  
  ImportC "-lGL"
  ;ImportC "/usr/lib/x86_64-linux-gnu/libGL.so"
    glXGetProcAddress(name.i)
  EndImport

CompilerEndIf
    
    
Global RollAxisX.f
Global RollAxisZ.f

Global ZoomFactor.f = 1.0 ; Distance of the camera. Negative value = zoom back

Procedure DrawCube(Gadget)
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  
  glPushMatrix_()                  ; Save the original Matrix coordinates
  glMatrixMode_(#GL_MODELVIEW)

  glTranslatef_(0, 0, ZoomFactor)  ;  move it forward a bit

  glRotatef_ (RollAxisX, 1.0, 0, 0) ; rotate around X axis
  glRotatef_ (RollAxisZ, 0, 0, 1.0) ; rotate around Z axis
 
  RollAxisX + 1
  RollAxisZ + 1 

  glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  glDisable_(#GL_LIGHTING)
  
  glBegin_  (#GL_QUADS)
  glNormal3f_ (0,0,1.0)
  glColor3f_  (0,0,1.0)
  glVertex3f_ (0.5,0.5,0.5)   
  glColor3f_  (0,1.0,1.0)         
  glVertex3f_ (-0.5,0.5,0.5)
  glColor3f_  (1.0,1.0,1.0)
  glVertex3f_ (-0.5,-0.5,0.5)
  glColor3f_  (0,0,0)
  glVertex3f_ (0.5,-0.5,0.5) 
  glNormal3f_ (0,0,-1.0)
  glColor3f_  (0,0,1.0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glColor3f_  (0,0,1.0)
  glVertex3f_ (-0.5,0.5,-0.5)
  glColor3f_  (1.0,1.0,1.0)
  glVertex3f_ (0.5,0.5,-0.5)
  glColor3f_  (1.0,1.0,1.0)
  glVertex3f_ (0.5,-0.5,-0.5)
  glEnd_()
  
  ; draw shaded faces

  glEnable_(#GL_LIGHTING)
  glEnable_(#GL_LIGHT0)
  glBegin_ (#GL_QUADS)

  glNormal3f_ (   0, 1.0,   0)
  glVertex3f_ ( 0.5, 0.5, 0.5)
  glVertex3f_ ( 0.5, 0.5,-0.5)
  glVertex3f_ (-0.5, 0.5,-0.5)
  glVertex3f_ (-0.5, 0.5, 0.5)

  glNormal3f_ (0,-1.0,0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glVertex3f_ (0.5,-0.5,-0.5)
  glVertex3f_ (0.5,-0.5,0.5)
  glVertex3f_ (-0.5,-0.5,0.5)

  glNormal3f_ (1.0,0,0)
  glVertex3f_ (0.5,0.5,0.5)
  glVertex3f_ (0.5,-0.5,0.5)
  glVertex3f_ (0.5,-0.5,-0.5)
  glVertex3f_ (0.5,0.5,-0.5)

  glNormal3f_ (-1.0,   0,   0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glVertex3f_ (-0.5,-0.5, 0.5)
  glVertex3f_ (-0.5, 0.5, 0.5)
  glVertex3f_ (-0.5, 0.5,-0.5)

  glEnd_()

  glPopMatrix_()
  glFinish_()

  SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
EndProcedure


Procedure SetupGL()
    
  glMatrixMode_(#GL_PROJECTION)
  gluPerspective_(30.0, 200/200, 1.0, 10.0) 
  glMatrixMode_(#GL_MODELVIEW)
  glTranslatef_(0, 0, -5.0)
  glEnable_(#GL_DEPTH_TEST)   ; Enabled, it slowdown a lot the rendering. It's to be sure than the
  glEnable_(#GL_CULL_FACE)    ; This will enhance the rendering speed as all the back face will be
  glShadeModel_(#GL_SMOOTH)
EndProcedure

ProcedureDLL MP_Windows_Fps(Title.s)
  
  Global Now = ElapsedMilliseconds()
  Global Ticks , FrameCounter
  If (Now-Ticks) > 999
     Ticks = Now
     SetWindowTitle(0,Title+" FPS:"+ Str( FrameCounter )) 
     FrameCounter = 0
  EndIf
  FrameCounter + 1
      
EndProcedure


;- Start windows and gl open
OpenWindow(0, 0, 0, 530, 530, "OpenGL Gadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

OpenGLGadget(0, 10, 10, 510, 510)
SetupGL()

CompilerIf #PB_Compiler_OS = #PB_OS_Windows  ; You get the information only after start of Opengl
   Global wglSwapIntervalEXT = wglGetProcAddress_("wglSwapIntervalEXT")
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
   Global wglSwapIntervalEXT = glXGetProcAddress("glXSwapIntervalEXT")
 CompilerElseIf  #PB_Compiler_OS = #PB_OS_MacOS
   Global wglSwapIntervalEXT
   setGLEXT(wglSwapIntervalEXT, "missing_the_name")
CompilerEndIf
;- close windows and gl open

;- Needed function
If wglSwapIntervalEXT ; Vsync switch
   CallFunctionFast(wglSwapIntervalEXT,0) 
EndIf 

AddWindowTimer(0, 123, 1000) ; show vsync on/off

Repeat
  Event = WindowEvent()
  
  If Event = #PB_Event_Timer And EventTimer() = 123
  switch=~switch & 1
    If wglSwapIntervalEXT
      CallFunctionFast(wglSwapIntervalEXT,switch) 
    EndIf
  EndIf  
  
  MP_Windows_Fps("OpenGL Gadget")

  DrawCube(0)
  
Until Event = #PB_Event_CloseWindow

Working on - MP3D Library - PB 5.73 version ready for download