Author Topic: Japi Experiments  (Read 16964 times)

0 Members and 2 Guests are viewing this topic.

Mike Lobanovsky

  • Guest
Re: Japi Experiments
« Reply #45 on: December 05, 2016, 04:57:42 AM »
Hi guys,

I realize that my FBSL submissions might be regarded as spam but what I upload is really very basic stuff that can be easily re-implemented in Oxygen. I could've done it myself but I'm not as proficient in O2 as I am in FBSL and I just wouldn't like to lose time stumbling around where a seasoned fresh Oxygen breather can get things done in minutes.

What FBSL does with these shaders is this:

Code: Script BASIC
  1. '----------------------------------------
  2. ' -=:: GLSL Julia Demo for FBSL v3.5 ::=-
  3. '----------------------------------------
  4. ' #AppType Console ' for debugging only
  5. #Option Strict
  6. #DllImports OpenGL32
  7.  
  8. #Include <Include\Windows.inc>
  9. #Include ".\COGLApp1.inc"
  10.  
  11. #Define GL_QUADS 0x0007
  12. #Define GL_VERTEX_SHADER 0x8B31
  13. #Define GL_FRAGMENT_SHADER 0x8B30
  14. #Define GL_MODELVIEW 0x1700
  15. #Define GL_PROJECTION 0x1701
  16. #Define GL_COMPILE_STATUS 0x8B81
  17. #Define GL_LINK_STATUS 0x8B82
  18. #Define GL_INFO_LOG_LENGTH 0x8B84
  19.  
  20. ' GLSL Stubs for wglGetProcAddress
  21. Declare glAttachShader()
  22. Declare glCompileShader()
  23. Declare glCreateProgram()
  24. Declare glCreateShader()
  25. Declare glDeleteProgram()
  26. Declare glDeleteShader()
  27. Declare glGetProgramInfoLog()
  28. Declare glGetProgramiv()
  29. Declare glGetShaderInfoLog()
  30. Declare glGetShaderiv()
  31. Declare glGetUniformLocation()
  32. Declare glLinkProgram()
  33. Declare glShaderSource()
  34. Declare glUseProgram()
  35. Declare glUniform1f()
  36. Declare glUniform2f()
  37.  
  38. Dim width, height, Viewer
  39. Dim juliaVS = 0, juliaFS = 0, juliaProg = 0
  40. Dim unitime = 0, unires = 0
  41.  
  42. Sub Main()
  43.   Viewer = New COGLApp1("-=:: GLSL Julia Particles Demo ::=-", _
  44.   AddressOf Renderer, AddressOf Messenger, AddressOf Updater)
  45.  
  46.   glslInit()
  47.   Init()
  48.  
  49.   With Viewer
  50.     .DataRate(2)
  51.     Center(.hWnd()): Show(.hWnd)
  52.     .Start(): Delete Viewer
  53.   End With
  54. End Sub
  55.  
  56. Sub Updater()
  57.   Viewer.Caption("-=:: GLSL Julia Particles Demo ::=-   FPS = " & Viewer.FPS())
  58. End Sub
  59.  
  60. Function Messenger(ByVal %hwnd, ByVal %msg, ByVal %wparam, ByVal %lparam) As Long
  61.   If msg = WM_KEYDOWN Then
  62.     If wparam = VK_ESCAPE Then
  63.       Viewer.Quit()
  64.       Deinit()
  65.     End If
  66.     Return 0 ' suppress default
  67.  ElseIf msg = WM_SIZE Then
  68.     width = LoWord(lparam)
  69.     height = HiWord(lparam)
  70.     Reshape()
  71.     Return 0 ' ditto
  72.  ElseIf msg = WM_MOUSEWHEEL Or msg = WM_MOUSEMOVE Then
  73.     Return 0 ' ditto
  74.  ElseIf msg = WM_CLOSE Then
  75.     Deinit()
  76.   End If
  77.   Return -1 ' allow COGLApp's default processing
  78. End Function
  79.  
  80. Sub Renderer()
  81.   glPushMatrix(): glLoadIdentity()
  82.   glMatrixMode(GL_PROJECTION)
  83.   glPushMatrix(): glLoadIdentity()
  84.   glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
  85.  
  86.   glUseProgram(juliaProg)
  87.  
  88.   glUniform1f(unitime, !Clock())
  89.   glUniform2f(unires, !width, !height)
  90.  
  91.   glBegin(GL_QUADS)
  92.   glVertex2i(0, 0)(1, 0)(1, 1)(0, 1) ' FBSL shorthand
  93.  glEnd()
  94.  
  95.   glUseProgram(0)
  96.  
  97.   glPopMatrix()
  98.   glMatrixMode(GL_MODELVIEW)
  99.   glPopMatrix()
  100. End Sub
  101.  
  102. Sub Init()
  103.   juliaVS = CompileFromFile(GL_VERTEX_SHADER, ".\JP.vs")
  104.   juliaFS = CompileFromFile(GL_FRAGMENT_SHADER, ".\JP.fs")
  105.   juliaProg = LinkProgram(2, juliaVS, juliaFS)
  106.  
  107.   glUseProgram(juliaProg)
  108.   unitime = glGetUniformLocation(juliaProg, "iGlobalTime")
  109.   unires = glGetUniformLocation(juliaProg, "iResolution")
  110.   glUseProgram(0)
  111. End Sub
  112.  
  113. Sub Deinit()
  114.   glDeleteProgram(juliaProg)
  115. End Sub
  116.  
  117. Sub Reshape()
  118.   Dim fov = 45., aspect = width / height
  119.  
  120.   glViewport(0, 0, width, height)
  121.   glMatrixMode(GL_PROJECTION)
  122.   glLoadIdentity()
  123.   fov = Tan(fov * PI / 360) * .1: aspect = aspect * fov
  124.   glFrustum(-aspect, aspect, -fov, fov, .1, 1000.)
  125.   glMatrixMode(GL_MODELVIEW)
  126. End Sub
  127.  
  128. Sub LoadSource(filePath) ' returns shader source in FileGet
  129.  FileGet(FileOpen(filePath, BINARY), FileLen(filePath))
  130.   FileClose(FileOpen) ' FBSL shorthand
  131. End Sub
  132.  
  133. Function CompileFromFile(enumtype As Long, filePath) As Long
  134.   Dim pointer, shader
  135.   Dim length As Long, result As Long
  136.  
  137.   LoadSource(filePath)
  138.   If Not Asc(FileGet) Then Return 0
  139.  
  140.   shader = glCreateShader(enumtype)
  141.   length = StrLen(FileGet)
  142.   pointer = @FileGet
  143.   glShaderSource(shader, 1, @pointer, @length)
  144.   glCompileShader(shader)
  145.   FileGet = "" ' optionally reclaim memory
  146.  
  147.   glGetShaderiv(shader, GL_COMPILE_STATUS, @result)
  148.   If Not result Then
  149.     Dim prglog As String
  150.    
  151.     glGetShaderiv(shader, GL_INFO_LOG_LENGTH, @length)
  152.     Alloc(prglog, length)
  153.     glGetShaderInfoLog(shader, length, @result, @prglog)
  154.    
  155.     Print "CompileFromFile(): Unable to compile ", filePath, ": ", prglog
  156.     Clean prglog
  157.    
  158.     glDeleteShader(shader)
  159.     Return 0
  160.   End If
  161.  
  162.   Return shader
  163. End Function
  164.  
  165. Function LinkProgram(numShaders, ParamArray shaders) As Long
  166.   Dim program = glCreateProgram(), shader
  167.  
  168.   For Dim i = 0 To numShaders - 1
  169.     shader = shaders[i]
  170.     glAttachShader(program, shader)
  171.   Next
  172.  
  173.   glLinkProgram(program)
  174.  
  175.   Dim success = 0
  176.   glGetProgramiv(program, GL_LINK_STATUS, @success)
  177.  
  178.   If Not success Then
  179.     Dim $temp * 256
  180.     glGetProgramInfoLog(program, 256, NULL, @temp)
  181.     Print "Failed to link program: ", temp
  182.     glDeleteProgram(program)
  183.     program = 0
  184.   End If
  185.  
  186.   Return program
  187. End Function
  188.  
  189. Sub GlslInit()
  190.   AddressOf glAttachShader = wglGetProcAddress("glAttachShader")
  191.   AddressOf glCompileShader = wglGetProcAddress("glCompileShader")
  192.   AddressOf glCreateProgram = wglGetProcAddress("glCreateProgram")
  193.   AddressOf glCreateShader = wglGetProcAddress("glCreateShader")
  194.   AddressOf glDeleteProgram = wglGetProcAddress("glDeleteProgram")
  195.   AddressOf glDeleteShader = wglGetProcAddress("glDeleteShader")
  196.   AddressOf glGetProgramInfoLog = wglGetProcAddress("glGetProgramInfoLog")
  197.   AddressOf glGetProgramiv = wglGetProcAddress("glGetProgramiv")
  198.   AddressOf glGetShaderInfoLog = wglGetProcAddress("glGetShaderInfoLog")
  199.   AddressOf glGetShaderiv = wglGetProcAddress("glGetShaderiv")
  200.   AddressOf glGetUniformLocation = wglGetProcAddress("glGetUniformLocation")
  201.   AddressOf glLinkProgram = wglGetProcAddress("glLinkProgram")
  202.   AddressOf glShaderSource = wglGetProcAddress("glShaderSource")
  203.   AddressOf glUseProgram = wglGetProcAddress("glUseProgram")
  204.   AddressOf glUniform1f = wglGetProcAddress("glUniform1f")
  205.   AddressOf glUniform2f = wglGetProcAddress("glUniform2f")
  206. End Sub

where Viewer is an instance of FBSL OpenGL application, and Messenger(), Renderer() and Updater() are its callbacks for handling the Windows message pipe, ensure timely OpenGL rendering, and  perform auxiliary tasks like e.g. display the FPS rate, respectively.

Similar things can be easily set up in standard Oxygen Basic to use the same shaders without modification.

I've just found yet another awesome shader that's strictly in line with the current topic and is IMHO even more beautiful than the notorious Julia Rings by Relsoft. It also shows that our perception of Julia fractal as a flat 2D pattern is too simplistic; in fact Julia has a very pronounced z-axis component, and the particles roller coasting below are a very nice visualization of that fact.

I took every effort to make the shader as light as possible, and it shouldn't stress your GTX GPU more than 50% (CPU <= .5%) @60FPS full screen while older geForce GPUs may experience a drop to 30FPS when the window is maximized.

Feel free to experiment with the shape and number of particles as well as with other arguments of the calc but keep an eye on your GPU load and temperature. I wouldn't want to see you burning down your wives' Amazon/eBay surfing gear. ;)

Enjoy! :)

.

Charles Pegge

  • Guest
Re: Japi Experiments
« Reply #46 on: December 06, 2016, 01:53:56 AM »
Hi Mike,

A stunningly excellent find!

and thanks also for the FBSL source. GLSL setup is complex, and I am trying to establish a framework, robust enough to carry the burden, without being too restrictive.
« Last Edit: December 06, 2016, 02:02:25 AM by Charles Pegge »

Arnold

  • Guest
Re: Japi Experiments
« Reply #47 on: December 09, 2016, 03:56:22 AM »
Hi Charles,

I was prevented from my pc the last few days. Yesterday I tried out your JuliaKeyb demo, but two subs/macros/structs(?) were reported as missing: NewTexture and DynSynthTexture. I assume you added these two items in ConsoleG.inc or in one of the other dependant include files? What will be the purpose of these two routines?

Roland

Arnold

  • Guest
Re: Japi Experiments
« Reply #48 on: December 09, 2016, 03:57:29 AM »
Hi Mike,

some time ago I installed FBSL 3.40.10 to learn more about the language. Is it possible to get the latest FBSL v3.5 which will run your provided FBSL Julia Demo code? In the FBSL forum I found the link for FBSL v3.5 RC2.zip. Is this the required package?

Roland

Mike Lobanovsky

  • Guest
Re: Japi Experiments
« Reply #49 on: December 09, 2016, 07:08:05 PM »
Hi Roland,

Yes, if the older v3.4.10 is still functional then that's all you need for upgrading. Extract the \FBSLv3 folder from your RC2 download and copy it on top of your existing older \FBSLv3 installation folder wherever it is on your PC, and allow Windows to overwrite the older files whenever it asks you for a permission. Copy the other two folders from the zip anywhere at your option. They just contain a few examples of DynAsm and DynC unavailable in the earlier versions of FBSL. Read the included PDF for the description of new features.

Don't try to copy the \FBSLv3 folder anywhere else to "preserve" the older installation. It won't work. FBSL v3.5 is still a W.I.P. That's why it isn't fully functional without an earlier installation of FBSL and that's also why it doesn't yet have its own full-blown installer.

Most of your earlier sample scripts won't run in the new FBSL v3.5 environment but the PDF and the Newbie's board on the forum have instructions how to make the older scripts functional again. It's easy.

Don't regret you won't have access to the earlier v3.4.10 any more. It was much, much weaker than the new v3.5 and it is entirely deprecated and unsupported today.

Charles Pegge

  • Guest
Re: Japi Experiments
« Reply #50 on: December 11, 2016, 01:34:01 AM »
Hi Roland,

Code: [Select]

NewTexture creates a new texture name and assigns a texture index number to it

  macro NewTexture(n)
  ===================
  static int n
  texe++
  n=texe
  end macro

DynSynthTexture generates a texture from a square block of pixels

Code: [Select]
  sub DynSynthTexture(int *n, any*v, int c)
  =========================================
  int w=sqr(c/4)
  MakeTexture @v, w, w, texn[n]
  end sub

PS:
inc/sysutil.inc has an uncommented datestamp (line 4). Sorry!   :o


Arnold

  • Guest
Re: Japi Experiments
« Reply #51 on: December 13, 2016, 02:33:01 AM »
Hi Mike,

thanks for your info about FBSL 3.5. It will take some time until I will be able to create some meaningful code.

Roland