Another PureBasic GLSL Editor/Player [Code Included]

Help other Members. Share your Source Codes
Forum rules
If you share codes or scripts that you haven't developed by yourself,
then you have to mention and give some credits in your post to the original author of the code or script.
omnikam
PureBasicCoder.
Posts: 6
Joined: 12 Apr 2017 22:19
Has thanked: 2 times
Been thanked: 8 times
Status: Offline

Another PureBasic GLSL Editor/Player [Code Included]

Postby omnikam » 15 Apr 2017 14:50

Hi All, in my quest for everything GLSL and Purebasic i found the source code for a GLSL player/Editor
You can load/save/edit and play your shaders
All Credit goes to Pjay for the original and Comtois from games-creators.org for the adaption
One important note!! you must disable create unicode executable from compiler options to get this to work
Instrutions.. Load your shader or just start writing it on the open screen
Press Compile to check the results and save when your happy

Code: Select all

 ;EnableExplicit

Enumeration ;/ Window
   #Window_Main
   #Window_OpenGl
   #Menu
   #Window_Error
EndEnumeration

Enumeration ;/ Gadget
   #Gad_OpenGL
   #Gad_PopupMenu
   #Gad_Editor
   #Gad_Editor_error
   #Gad_Button_error
EndEnumeration

Structure Shader
   Width.i
   Height.i
   Vertex_Text.s
   Fragment_Text.s
   Program.i
   
   Uniform_Time.i
   Uniform_Resolution.i
   Uniform_Mouse.i
   Uniform_SurfacePosition.i
EndStructure

Structure System
   Event.i
   MouseX.i
   MouseY.i
   
   App_CurrentTime.i
   App_StartTime.i
   Editor_LastText.s
   
   FPS_Timer.i
   Frames.i
   FPS.i
EndStructure

Structure Screen
   Width.i
   Height.i
EndStructure

Define Event
Global Screen.Screen
Global System.System
Global Shader.Shader
Shader\Width = 600
Shader\Height = 600
Shader\Vertex_Text = "attribute vec3 position;"
Shader\Vertex_Text + "attribute vec2 surfacePosAttrib;"
Shader\Vertex_Text + "varying vec2 surfacePosition;"
Shader\Vertex_Text + "   void main() {"
Shader\Vertex_Text + "      surfacePosition = surfacePosAttrib;"
Shader\Vertex_Text + "      gl_Position = vec4( position, 1.0 );"
Shader\Vertex_Text + "   }"

Screen\Width = 800
Screen\Height = 600

Declare FenetreErreur(Text.s, Ligne.i)
Declare Erreur(Titre.s, Message.s)
Declare Init_Main()
Declare Init_OpenGL()
Declare Shader_Compile_Link_Use(Vertex.s,Fragment.s,Use.i=1)
Declare LoadSource()
Declare SaveSource()
Declare Shader_Set()
Declare Render()

Init_OpenGL()
HideWindow(#Window_OpenGl, 1)

;{[Opengl shader setup & routines

#GL_VERTEX_SHADER = $8B31
#GL_FRAGMENT_SHADER = $8B30

Prototype glCreateShader(type.l)
Prototype glCreateProgram()
Prototype glCompileShader(shader.l)
Prototype glLinkProgram(shader.l)
Prototype glUseProgram(shader.l)
Prototype glAttachShader(Program.l, shader.l)
Prototype glShaderSource(shader.l, numOfStrings.l, *strings, *lenOfStrings) :
Prototype.i glGetUniformLocation(Program.i, name.s)
Prototype glUniform1i(location.i, v0.i)
Prototype glUniform2i(location.i, v0.i, v1.i)
Prototype glUniform1f(location.i, v0.f)
Prototype glUniform2f(location.i, v0.f, v1.f)
Prototype glGetShaderInfoLog(shader.i, bufSize.l, *length_l, *infoLog)

Global glCreateShader.glCreateShader = wglGetProcAddress_("glCreateShader")
Global glCreateProgram.glCreateProgram = wglGetProcAddress_("glCreateProgram")
Global glCompileShader.glCompileShader = wglGetProcAddress_("glCompileShader")
Global glLinkProgram.glLinkProgram = wglGetProcAddress_("glLinkProgram")
Global glUseProgram.glUseProgram = wglGetProcAddress_("glUseProgram")
Global glAttachShader.glAttachShader = wglGetProcAddress_("glAttachShader")
Global glShaderSource.glShaderSource = wglGetProcAddress_("glShaderSource")
Global glGetUniformLocation.glGetUniformLocation = wglGetProcAddress_("glGetUniformLocation")
Global glUniform1i.glUniform1i = wglGetProcAddress_("glUniform1i")
Global glUniform2i.glUniform2i = wglGetProcAddress_("glUniform2i")
Global glUniform1f.glUniform1f = wglGetProcAddress_("glUniform1f")
Global glUniform2f.glUniform2f = wglGetProcAddress_("glUniform2f")
Global glGetShaderInfoLog.glGetShaderInfoLog = wglGetProcAddress_("glGetShaderInfoLog")
;}

Init_Main()

Repeat
   
   Repeat
      System\Event = WindowEvent()
      Select System\Event
           
         Case #PB_Event_Menu
           
            Select EventMenu()  ; To see which menu has been selected
               Case 1                  ; Load
                  LoadSource()
               Case 2
                  SaveSource()
               Case 3            ; Compiler
                  HideWindow(#Window_OpenGl, 0)
                  Shader_Set()
                 
               Case 4 ; Quit
                  Quit = 1
                 
               Case 8 ; About
                  MessageRequester("About", "Cool Menu example", 0)
                 
               Default
                  MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)
                 
            EndSelect
           
         Case #PB_Event_Gadget
            Select EventGadget()
                 
               Case #Gad_Button_error
                  CloseWindow(#Window_Error)
                 
               Case #Gad_OpenGL
                  Select EventType()
                     Case   #PB_EventType_RightButtonDown
                        DisplayPopupMenu(#Gad_PopupMenu, WindowID(#Window_Main))  ; now display the popup-menu
                       
                     Case #PB_EventType_MouseMove
                        System\MouseX = GetGadgetAttribute(#Gad_OpenGL,#PB_OpenGL_MouseX)
                        System\MouseY = GetGadgetAttribute(#Gad_OpenGL,#PB_OpenGL_MouseY)
                        glUniform2f(Shader\Uniform_Mouse,System\MouseX / Shader\Width,(Shader\Height-System\MouseY) / Shader\Height)
                  EndSelect
            EndSelect
           
         Case #PB_Event_CloseWindow
            If EventWindow() = #Window_OpenGl
               shader\Program = 0
               HideWindow(#Window_OpenGl, 1)
            Else
               Quit = 1
            EndIf
           
      EndSelect
   Until System\Event = 0
   
   System\App_CurrentTime = ElapsedMilliseconds()
   If shader\Program
      Render()
   EndIf
   
Until Quit = 1

Procedure Erreur(Titre.s, Message.s)
   MessageRequester(Titre, Message, #PB_MessageRequester_Ok)
   End
EndProcedure

Procedure FenetreErreur(Text.s, Ligne.i)
   If Ligne > 10
      Ligne = 10
   EndIf
   
   OpenWindow(#Window_Error,0,0,400,Ligne*25 + 25,"Erreur compilation",#PB_Window_ScreenCentered)
   EditorGadget(#Gad_Editor_error,0,0,400,Ligne*25, #PB_Editor_WordWrap)
   ButtonGadget(#Gad_Button_error,180,Ligne*25+2, 40, 20, "OK")
   
   SetGadgetText(#Gad_Editor_error, Text)
   
EndProcedure

Procedure LoadSource()
   Protected file$, length, bytes, *MemoryID
   file$ = OpenFileRequester("Select a file","","Shader (.txt)|*.txt|All files (*.*)|*.*",0)
   If file$
      If ReadFile(0, file$)
         Shader\Fragment_Text = ""
         While Eof(0) = 0           ; loop as long the 'end of file' isn't reached
            Shader\Fragment_Text + ReadString(0)   
            Shader\Fragment_Text + Chr(10)
         Wend
         
         CloseFile(0)
      EndIf
   EndIf
   SetGadgetText(#Gad_Editor, Shader\Fragment_Text)
EndProcedure

Procedure SaveSource()
   Protected file$, length, bytes, *MemoryID
   file$ = SaveFileRequester("Save", "", "Text File (*.txt)|*.txt|Bitmap File (*.bmp)|*.bmp|Any File|*.*", 0)

If file$
   Select SelectedFilePattern()
      Case 0 : If LCase(Right(file$, 4)) <> ".txt" : file$ + ".txt" : EndIf

   EndSelect

EndIf
   If file$
      If CreateFile(0, file$)
         WriteString(0, GetGadgetText(#Gad_Editor))
         CloseFile(0)
      EndIf
   EndIf
EndProcedure

Procedure Init_Main()
   
   If OpenWindow(#Window_Main,0,0,Screen\Width,Screen\Height,"Editeur GLSL",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) = 0
      Erreur("OpenWindow", "Error creating the main window")
   EndIf
   
   If CreateMenu(#Menu, WindowID(#Window_Main))
      MenuTitle("File")
      MenuItem( 1, "&Load...")
      MenuItem( 2, "Save")
      MenuItem( 3, "Compiler")
      MenuItem( 4, "&Quit")
   EndIf
   
   EditorGadget(#Gad_Editor, 0, 0, Screen\Width,Screen\Height)
   
   System\App_StartTime = ElapsedMilliseconds()
   
EndProcedure

Procedure Init_OpenGL()
   
   If OpenWindow(#Window_OpenGl,0,0,Shader\Width,Shader\Height,"Editeur GLSL",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) = 0
      Erreur("OpenWindow", "Erreur lors de la création de la fenêtre principale")
   EndIf
   
   If OpenGLGadget(#Gad_OpenGL,0,0,Shader\Width,Shader\Height,#PB_OpenGL_Keyboard) = 0
      Erreur("OpenGLGadget", "Erreur lors de la création du OpenGLGadget")
   EndIf
   
EndProcedure


Procedure Shader_Compile_Link_Use(Vertex.s,Fragment.s,Use.i=1)
   Protected VertShader.i, FragShader.i, *TxtPointer, Program.i
   Protected Textlength.i, Mytext.s = Space(1024)
   
   ;/ Compile Vertex shader
   VertShader.i = glCreateShader(#GL_VERTEX_SHADER)
   *TxtPointer = @Vertex
   glShaderSource(VertShader, 1, @*TxtPointer, #Null)
   glCompileShader(VertShader)
   Debug "Vert: "+VertShader
   glGetShaderInfoLog(VertShader,1023,@Textlength,@Mytext)
   Debug MyText
   ;/ Compile Fragment Shader
   FragShader.i = glCreateShader(#GL_FRAGMENT_SHADER)
   *TxtPointer = @Fragment
   glShaderSource(FragShader, 1, @*TxtPointer, #Null)
   glCompileShader(FragShader)
   Debug "Frag: "+FragShader
   
   glGetShaderInfoLog(FragShader,1023,@Textlength,@Mytext)
   
   If Mytext
      FenetreErreur(Mytext, CountString(Mytext, Chr(10)))
   EndIf
   
   ;/ Create Shader Program
   Program = glCreateProgram()
   glAttachShader(Program,VertShader)
   Debug "Attached Vert Shader"
   glAttachShader(Program,FragShader)
   Debug "Attached Frag Shader"
   glLinkProgram(Program)
   Debug "Link program"
   
   If Use = 1
      glUseProgram(Program)
   EndIf
   
   ProcedureReturn Program
EndProcedure


Procedure Shader_Set()
   If Shader\Program <> 0 ;/ delete the previous shaders
      glUseProgram(0)          ;
   EndIf
   
   ;LoadSource()
   Shader\Fragment_Text = GetGadgetText(#Gad_Editor)
   
   Shader\Program = Shader_Compile_Link_Use(Shader\Vertex_Text,Shader\Fragment_Text)
   
   If Shader\Program = 0
      MessageRequester("Unsupported Device?","No Shader Support Available",#PB_MessageRequester_Ok)
      End
   EndIf
   
   ;/ store shader uniform locations
   Debug "Shader: " + Shader\Program
   Shader\Uniform_Time = glGetUniformLocation(Shader\Program, "time")
   Shader\Uniform_Mouse = glGetUniformLocation(Shader\Program, "mouse")
   Shader\Uniform_Resolution = glGetUniformLocation(Shader\Program, "resolution")
   Shader\Uniform_SurfacePosition = glGetUniformLocation(Shader\Program, "surfacePosition")
   Debug "Time location: "+Shader\Uniform_Time
   Debug "Mouse location: "+Shader\Uniform_Mouse
   Debug "Res location: "+Shader\Uniform_Resolution
   Debug "SurfacePos location: "+Shader\Uniform_SurfacePosition
   
   ;SetGadgetText(#Gad_Editor,SystemShader_Fragment_Text)
EndProcedure



Procedure Render()
   ;/ set shader Uniform values
   glUniform2f(Shader\Uniform_Resolution,Shader\Width, Shader\Height)
   glUniform1f(Shader\Uniform_Time,(System\App_CurrentTime-System\App_StartTime) / 1000.0)
   glUniform2i(Shader\Uniform_SurfacePosition,1.0,1.0)
   
   glBegin_(#GL_QUADS)
   glVertex2f_(-1,-1)
   glVertex2f_( 1,-1)
   glVertex2f_( 1, 1)
   glVertex2f_(-1, 1)
   glEnd_()           
   
   System\Frames + 1
   If System\App_CurrentTime > System\FPS_Timer
      System\FPS = System\Frames
      System\Frames = 0
      System\FPS_Timer = System\App_CurrentTime  + 1000
      SetWindowTitle(#Window_OpenGl,"GLSL viewer - FPS: "+Str(System\FPS))
   EndIf
   
   SetGadgetAttribute(#Gad_OpenGL,#PB_OpenGL_FlipBuffers,1)
   
EndProcedure
Only registered members are able to see and download attachments from Aftermath. You must LOGIN or REGISTER to view these files.

User avatar
WhiteWolf
Site Admin
Posts: 285
Joined: 11 Apr 2016 17:49
Has thanked: 69 times
Been thanked: 50 times
Status: Offline

Re: Another PureBasic GLSL Editor/Player [Code Included]

Postby WhiteWolf » 15 Apr 2017 21:41

Tested on PB 5.30 works perfect thanx for sharing this code Omnikam :icon_strichauge:
(¯`·._.·currently working on Windows CODEF_Editor Tool Project.·._.·´¯)


alveo

Return to “Purebasic”

Who is online

Users browsing this forum: No registered users and 1 guest

Created by Matti from StylesFactory.pl and Warlords of Draenor


317,393 Page visits

Powered by phpBB® Forum Software © phpBB Limited