Page 1 of 1

Complex spiral in OpenGL

Posted: Sat Jan 27, 2024 9:51 am
by threedslider
Hello

Here is a complex spiral as I share you ^^

But warnings for dizzying mind ! Your eyes move ! :mrgreen:

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Coded with Purebasic v.6.04 by threedslider 01/27/2024
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

move.f = 0

Procedure spiral()
  
  Shared move
  
   multiplier.f = 500.0 * #PI
  
  
    radius.f = 20.0
    
 
  
  glBegin_(#GL_LINE_STRIP)
    
  For mult = 0 To 360
     
  spiral_x.f =   radius * Cos(((radius*move)/7000)+multiplier)/2
  spiral_y.f =   radius * Sin(((radius*move)/7000)+multiplier)/2
  
  
  glColor3f_(mult/10.0, mult/100.0, mult/100.0)
  
  glVertex3f_(spiral_x*0.3,  spiral_y*0.3 , 0.0) 
 
  radius = radius + 1.0
  
  
  Next
  glEnd_()
  
EndProcedure


InitSprite()
InitKeyboard()

OpenWindow(1, 0,0,800,600,"Spiral with complex color in OpenGL ", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(1),0,0,800,600,0,0,0)


glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_() 
gluPerspective_(45.0, 800/600, 1.0, 60.0)
glMatrixMode_(#GL_MODELVIEW)
glTranslatef_(0, 0, -50)
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)



glClearColor_(0.1, 0.1, 0.1, 0) ; background color

Repeat
  ExamineKeyboard()
  event = WindowEvent()
  ClearScreen(RGB(0,0,0))
  
   glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
 
  For x = 0 To 500
    
    move  +  1/1000
    
    spiral()  
         
  Next
    
 
  
 FlipBuffers()
  
Until event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
End



Re: Complex spiral in OpenGL

Posted: Sat Jan 27, 2024 10:42 am
by infratec
A bit modified to use the OpenGlGadget()

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Coded with Purebasic v.6.04 by threedslider 01/27/2024
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

EnableExplicit


Procedure spiral(move.f)
  
  Protected.i mult
  Protected.f multiplier, radius, spiral_x, spiral_y
  
  multiplier = 500.0 * #PI
  radius = 20.0
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  glBegin_(#GL_LINE_STRIP)
  
  For mult = 0 To 360
    
    spiral_x = radius * Cos(((radius * move) / 7000) + multiplier) / 2
    spiral_y = radius * Sin(((radius * move) / 7000) + multiplier) / 2
    
    glColor3f_(mult / 10.0, mult / 100.0, mult / 100.0)
    glVertex3f_(spiral_x * 0.3,  spiral_y * 0.3 , 0.0) 
    
    radius = radius + 1.0
    
  Next
  
  glEnd_()
  
  SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
  
EndProcedure



Define move.f, event.i


OpenWindow(1, 0, 0, 800, 600, "Spiral with complex color in OpenGL", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenGLGadget(0, 0, 0, 800, 600)
SetGadgetAttribute(0, #PB_OpenGL_SetContext, #True)

glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
gluPerspective_(45.0, 800 / 600, 1.0, 60.0)
glMatrixMode_(#GL_MODELVIEW)
glTranslatef_(0, 0, -50)
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
  If move < 1000
    event = WindowEvent()
    
    move + 5
    spiral(move)
  Else
    event = WaitWindowEvent()
  EndIf
  
Until event = #PB_Event_CloseWindow

Re: Complex spiral in OpenGL

Posted: Sat Jan 27, 2024 10:50 am
by Fred
A bit modified again to be DPI aware :):

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Coded with Purebasic v.6.04 by threedslider 01/27/2024
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

EnableExplicit


Procedure spiral(move.f)
  
  Protected.i mult
  Protected.f multiplier, radius, spiral_x, spiral_y
  
  multiplier = 500.0 * #PI
  radius = 20.0
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  glBegin_(#GL_LINE_STRIP)
  
  For mult = 0 To 360
    
    spiral_x = radius * Cos(((radius * move) / 7000) + multiplier) / 2
    spiral_y = radius * Sin(((radius * move) / 7000) + multiplier) / 2
    
    glColor3f_(mult / 10.0, mult / 100.0, mult / 100.0)
    glVertex3f_(spiral_x * 0.3,  spiral_y * 0.3 , 0.0) 
    
    radius = radius + 1.0
    
  Next
  
  glEnd_()
  
  SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
  
EndProcedure



Define move.f, event.i


OpenWindow(1, 0, 0, 800, 600, "Spiral with complex color in OpenGL", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenGLGadget(0, 0, 0, 800, 600)
SetGadgetAttribute(0, #PB_OpenGL_SetContext, #True)

Define ScreenPixelWidth = DesktopScaledX(800)
Define ScreenPixelHeight = DesktopScaledY(600)

glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
gluPerspective_(45.0, ScreenPixelWidth / ScreenPixelHeight, 1.0, 60.0)
glMatrixMode_(#GL_MODELVIEW)
glTranslatef_(0, 0, -50)
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_LIGHT0)
glEnable_(#GL_LIGHTING)
glEnable_(#GL_COLOR_MATERIAL)
glEnable_(#GL_DEPTH_TEST)
glEnable_(#GL_CULL_FACE)   
glViewport_(0, 0, ScreenPixelWidth, ScreenPixelHeight)

Repeat
  If move < 1000
    event = WindowEvent()
    
    move + 5
    spiral(move)
  Else
    event = WaitWindowEvent()
  EndIf
  
Until event = #PB_Event_CloseWindow

Re: Complex spiral in OpenGL

Posted: Sat Jan 27, 2024 2:57 pm
by threedslider
Nice ! And thank you all :)

After running this modification, it works but in some time it stopped it and it doesn"t go futher :/

Is it on purpose or not ?

Happy coding !

Re: Complex spiral in OpenGL

Posted: Sat Jan 27, 2024 3:12 pm
by threedslider
Ok an updated of my little modification from Infratec and Fred, it works now :)

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Coded with Purebasic v.6.04 by threedslider 01/27/2024
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; A little of modification from Infratec and Fred, thank you all :)

; Updated for my smooth running, it works now !

EnableExplicit


Procedure spiral(move.f)
  
  Protected.i mult
  Protected.f multiplier, radius, spiral_x, spiral_y
  
  multiplier = 500.0 * #PI
  radius = 20.0
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  glBegin_(#GL_LINE_STRIP)
  
  For mult = 0 To 360
    
    spiral_x = radius * Cos(((radius * move) / 7000) + multiplier) / 2
    spiral_y = radius * Sin(((radius * move) / 7000) + multiplier) / 2
    
    glColor3f_(mult / 10.0, mult / 100.0, mult / 100.0)
    glVertex3f_(spiral_x * 0.3,  spiral_y * 0.3 , 0.0) 
    
    radius = radius + 1.0
    
  Next
  
  glEnd_()
  
  SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
  
EndProcedure



Define move.f, event.i


OpenWindow(1, 0, 0, 800, 600, "Spiral with complex color in OpenGL", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenGLGadget(0, 0, 0, 800, 600)
SetGadgetAttribute(0, #PB_OpenGL_SetContext, #True)

Define ScreenPixelWidth = DesktopScaledX(800)
Define ScreenPixelHeight = DesktopScaledY(600)

glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
gluPerspective_(45.0, ScreenPixelWidth / ScreenPixelHeight, 1.0, 60.0)
glMatrixMode_(#GL_MODELVIEW)
glTranslatef_(0, 0, -50)
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_LIGHT0)
glEnable_(#GL_LIGHTING)
glEnable_(#GL_COLOR_MATERIAL)
glEnable_(#GL_DEPTH_TEST)
glEnable_(#GL_CULL_FACE)   
glViewport_(0, 0, ScreenPixelWidth, ScreenPixelHeight)

Repeat
  
  If move < 10000
    event = WindowEvent()
    
    move + 0.5
    spiral(move)
  Else
    event = WaitWindowEvent()
  EndIf
  
Until event = #PB_Event_CloseWindow


Re: Complex spiral in OpenGL

Posted: Mon Jan 29, 2024 10:26 am
by dige
Great 👍 there are a lot of nice effects, if you let it run any longer...

Code: Select all

 If move < 1000000
    event = WindowEvent()
    
    move + 10

Re: Complex spiral in OpenGL

Posted: Mon Jan 29, 2024 5:30 pm
by threedslider
dige wrote: Mon Jan 29, 2024 10:26 am Great 👍 there are a lot of nice effects, if you let it run any longer...

Code: Select all

 If move < 1000000
    event = WindowEvent()
    
    move + 10
Hello dige and thank you :mrgreen: