Diamond for test (OpenGL)

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

Diamond for test (OpenGL)

Post by threedslider »

Hi

I share you my code as fun :)

Code: Select all

;;;
;;;  Created by threedslider -- 11/13/2023
;;;

InitSprite()
InitKeyboard()


OpenWindow(0, 0, 0, 800, 600, "OpenGL : Diamond for test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)


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
glClearColor_(0.1, 0.1, 0.1, 0) ; background color
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  Repeat
    event = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        quit = 1
     EndSelect
   Until event = 0
   
   
    For x = 0 To 360
    
    move.f  +  1/100000
    
      helix_x.f =  50 *Exp((move-x)/200) * Cos(move+x)
      
      helix_y.f =  50 *Exp((move-x)/200) * Sin(move+x)
     
          
    glBegin_(#GL_LINES)
    glColor3f_(1.0, 0.3, 1.0)
    glVertex3f_( helix_x*0.3, 0.3, 0.3) 
    glVertex3f_( -0.3, -helix_y*0.3, 0.3) 
    glEnd_();
         
  Next
   
   
 FlipBuffers()
 
 ExamineKeyboard()
 
Until KeyboardPushed(#PB_Key_Escape) Or quit = 1
Happy coding !
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Diamond for test (OpenGL)

Post by Mijikai »

Nice effect :D
threedslider
User
User
Posts: 90
Joined: Sat Feb 12, 2022 7:15 pm

Re: Diamond for test (OpenGL)

Post by threedslider »

Thank you Mijikai !
juergenkulow
Enthusiast
Enthusiast
Posts: 556
Joined: Wed Sep 25, 2019 10:18 am

Re: Diamond for test (OpenGL)

Post by juergenkulow »

Code: Select all

;;;
;;;  Created by threedslider -- 11/13/2023
;;;

InitSprite()
InitKeyboard()
ExamineDesktops()
#Screen=0
OpenWindow(0, 0, 0, DesktopWidth(#Screen), DesktopHeight(#Screen), "OpenGL : Demo", #PB_Window_Maximize|#PB_Window_BorderLess)
OpenWindowedScreen(WindowID(0), 0,0, DesktopWidth(#Screen), DesktopHeight(#Screen))

glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_() 
aspectratio.f=DesktopWidth(#Screen)/DesktopHeight(#Screen)
gluPerspective_(45.0,aspectratio , 0.5, 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, DesktopWidth(#Screen), DesktopHeight(#Screen))
start=ElapsedMilliseconds()
blue.f=0.0
move.f=0.0
Repeat
  blue+1/256
  If blue>1.0 
    blue=0.0
    ; Debug Str(ElapsedMilliseconds()/1000.0)+" "+StrF(move)
  EndIf 
  glClearColor_(0.1, 0.1, blue, 0) ; background color
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  Repeat
    event = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        quit = 1
    EndSelect
  Until event = 0
  
  DisableDebugger
  For x = 0 To 360
    
    move  +  1/100000
    
    helix_x.f =  50 *Exp((move-x)/200) * Cos(move+x) 
    
    helix_y.f =  50 *Exp((move-x)/200) * Sin(move+x)
    
    
    glBegin_(#GL_LINES)
    glColor3f_(1.0, 1.0, 1.0)
    CompilerIf #PB_OS_Windows=#PB_Compiler_OS   
      glVertex3f_( helix_x*0.3, 0.3, 0.3) 
      glVertex3f_( -0.3, -helix_y*0.3, 0.3) 
    CompilerElseIf #PB_OS_Linux=#PB_Compiler_OS
      f.f=120
      glVertex3f_(f*helix_x, f, 25) 
      glVertex3f_( -f, -helix_y*f, 25)
    CompilerEndIf  
    glEnd_();
    
  Next
  If move>255.0 : move=0.0 : EndIf 
  
  FlipBuffers()
  
  ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape) Or quit = 1
EnableDebugger
; Debug ElapsedMilliseconds()-start
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 291
Joined: Thu Jul 09, 2015 9:07 am

Re: Diamond for test (OpenGL)

Post by pf shadoko »

Hello,
is there a way to make anti-alias tracings?
pjay
Enthusiast
Enthusiast
Posts: 175
Joined: Thu Mar 30, 2006 11:14 am

Re: Diamond for test (OpenGL)

Post by pjay »

Nice 8)

As my desktop scale is > 100%, it showed that it wasn't scaling properly to the window, so you're missing some dpi awareness (window @ 800*600 is larger than the windowed screen at 800*600, on my system at least).
pf shadoko wrote: Tue Nov 14, 2023 2:13 pm Hello,
is there a way to make anti-alias tracings?

Usually adding the following would be enough for smooth opengl lines:

Code: Select all

glEnable_(#GL_LINE_SMOOTH) : glHint_(#GL_LINE_SMOOTH_HINT,#GL_NICEST)
glEnable_(#GL_BLEND) : glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)

Some modified demo code that builds on the original:

Code: Select all

;;;
;;;  Created by threedslider -- 11/13/2023
;;;  - modification by PJay to simplify, make dpi aware & snazzy.

OpenWindow(0, 0, 0, 800, 600, "OpenGL : Diamond for test", #PB_Window_Maximize|#PB_Window_BorderLess)
OpenGLGadget(0,0,0,WindowWidth(0),WindowHeight(0),#PB_OpenGL_FlipSynchronization|#PB_OpenGL_Keyboard) : SetActiveGadget(0)
glOrtho_(-1,1,1,-1,0,1)

glEnable_(#GL_LINE_SMOOTH) : glHint_(#GL_LINE_SMOOTH_HINT,#GL_NICEST)
glEnable_(#GL_BLEND) : glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
glLineWidth_(6.75)

Repeat
  glClearColor_(0.0, 0.05, 0.05, 0.075) : glClear_(#GL_COLOR_BUFFER_BIT)
  
  Repeat
    event = WindowEvent()
    If event = #PB_Event_Gadget : If GetGadgetAttribute(0,#PB_OpenGL_Key) = #VK_ESCAPE : quit = 1 : EndIf : EndIf
  Until event = 0
  
  glBegin_(#GL_LINES)
  For x = 0 To 360
    move.f  +  1.0/100000.0 : t.f = (move + (x / 360.0))
    glColor4f_(0.5 + 0.5 * Cos(6.28318 * (1.0 * t + 0.263)), 0.5 + 0.5 * Cos(6.28318 * (1.0 * t + 0.416)), 0.5 + 0.5 * Cos(6.28318 * (1.0 * t + 0.557)),0.75)
    glVertex2f_(((1.0 + Sin(t)) * Exp((move-x)/200) * Cos(move+x)), 0.0)
    glVertex2f_(0.0, -((1.0 + Cos(t)) * Exp((move-x)/200) * Sin(move+x))) 
  Next
  glEnd_();
  SetGadgetAttribute(0,#PB_OpenGL_FlipBuffers,#True)
Until quit = 1
threedslider
User
User
Posts: 90
Joined: Sat Feb 12, 2022 7:15 pm

Re: Diamond for test (OpenGL)

Post by threedslider »

Thanks all and good job guys ! 8)
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 291
Joined: Thu Jul 09, 2015 9:07 am

Re: Diamond for test (OpenGL)

Post by pf shadoko »

thanks Pjay
it makes you want to make a graphic lib
the PB sprite lib is very rudimentary, we could make an improved one
do you think it's possible to remake the vectordrawing lib in opengl?
firace
Addict
Addict
Posts: 902
Joined: Wed Nov 09, 2011 8:58 am

Re: Diamond for test (OpenGL)

Post by firace »

pf shadoko wrote: Wed Nov 15, 2023 12:15 pm thanks Pjay
it makes you want to make a graphic lib
the PB sprite lib is very rudimentary, we could make an improved one
do you think it's possible to remake the vectordrawing lib in opengl?
Have you seen this? Perhaps you will find it interesting

viewtopic.php?t=81764
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 291
Joined: Thu Jul 09, 2015 9:07 am

Re: Diamond for test (OpenGL)

Post by pf shadoko »

thanks Firace,
I had a look, it looks interesting, but it's not very easy to use...

Pjay gave me a solution to draw anti-aliased lines, but it doesn't work on polygons (with glEnable_(#GL_POLYGON_SMOOTH) )
does anyone have a solution?
(I'd like to redo the VectorDrawing lib in opengl)
Post Reply