GLSL shader with Png Jpg Support [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: 7
Joined: 12 Apr 2017 22:19
Has thanked: 2 times
Been thanked: 9 times
Status: Offline

GLSL shader with Png Jpg Support [Code Included]

Postby omnikam » 17 Apr 2017 13:05

After Discovering the beautiful world of GLSL shaders`s i wanted to be able to render an image over the top it, A nice logo for example, And thanks to Samual over at Purebasic forums i can :updance:
Credit for the original code goes to Pjay for his GLSL shader compiler and Samual for his excellent texture rendering code and his enhancement to Pjay`s code
Remember to disable make unicode exe in compiler options

Glsl.jpg

Code: Select all

EnableExplicit

UsePNGImageDecoder()
UseJPEGImageDecoder()

Enumeration ;/ Window
  #Window_Main
EndEnumeration
Enumeration ;/ Gadget
  #Gad_OpenGL
  #Gad_Editor
  #Gad_ShaderSelector_Combo
EndEnumeration

Structure System
  Width.i
  Height.i
  Shader_Width.i
  Shader_Height.i
  Event.i
  Exit.i
  MouseX.i
  MouseY.i
 
  App_CurrentTime.i
  App_StartTime.i
  Editor_LastText.s
 
  Shader_Vertex_Text.s
  Shader_Fragment_Text.s
  Shader_Vertex.i
  Shader_Fragment.i
  Shader_Program.i
 
  Shader_Uniform_Time.i
  Shader_Uniform_Resolution.i
  Shader_Uniform_Mouse.i
  Shader_Uniform_SurfacePosition.i
 
  FPS_Timer.i
  Frames.i
  FPS.i
EndStructure



Global System.System

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

#GL_BGR = $80E0
#GL_BGRA = $80E1

Prototype glCreateShader(type.l)
Prototype glCreateProgram()
Prototype glCompileShader(shader.l)
Prototype glDeleteShader(ShaderObj.i)
Prototype glLinkProgram(shader.l)
Prototype glUseProgram(shader.l)
Prototype glAttachShader(Program.l, shader.l)
Prototype glShaderSource(shader.l, numOfStrings.l, *strings, *lenOfStrings) :
Prototype 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)

System\Width  = 1024
System\Height = 480
System\Shader_Width = 640
System\Shader_Height = 480

Declare CompileShader(VertexShader.s, FragmentShader.s)
Declare FreeShaderSourceBuf(*p)
Declare GetShaderSourceBuf(*shader)
Declare Render()
Declare Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
Declare SetupGLTexture(ImageHandle.i)
Declare SetupOpenGL()

Define.i CTR
Define.i Event

Global.i Image1, Image2
Global.i Texture1, Texture2

;-MAIN WINDOW
OpenWindow(#Window_Main,0,0,System\Width,System\Height,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenGLGadget(#Gad_OpenGL,0,0,System\Shader_Width,System\Shader_Height,#PB_OpenGL_Keyboard)
ComboBoxGadget(#Gad_ShaderSelector_Combo,System\Shader_Width+4,2,System\Width - (System\Shader_Width+8),24)
For CTR = 1 To 1
  AddGadgetItem(#Gad_ShaderSelector_Combo,-1,"Shader: "+Str(CTR))
Next

SetGadgetState(#Gad_ShaderSelector_Combo,0)
EditorGadget(#Gad_Editor,System\Shader_Width+4,30,System\Width - (System\Shader_Width+8),System\Height-30)

SetupOpenGL()

;Load images.

Image2 = CatchImage(#PB_Any, ?Logo)

;Now we create textures which can be used for rendering with OpenGL.
Texture1 = SetupGLTexture(Image1)
Texture2 = SetupGLTexture(Image2)

;Get OpenGL functions
Global glCreateShader.glCreateShader = wglGetProcAddress_("glCreateShader")
Global glCreateProgram.glCreateProgram = wglGetProcAddress_("glCreateProgram")
Global glCompileShader.glCompileShader = wglGetProcAddress_("glCompileShader")
Global glDeleteShader.glDeleteShader = wglGetProcAddress_("glDeleteShader")
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")

;Store the shaders within string variables.
System\Shader_Vertex_Text = "attribute vec3 position;"+Chr(10)+
                            "attribute vec2 surfacePosAttrib;"+Chr(10)+
                            "varying vec2 surfacePosition;"+Chr(10)+
                            "void main() {"+Chr(10)+
                            "    surfacePosition = surfacePosAttrib;"+Chr(10)+
                            "    gl_Position = vec4( position, 1.0 );"+Chr(10)+
                            "}"

System\Shader_Fragment_Text = "uniform float time;"+Chr(10)+
                              "uniform vec2 resolution;"+Chr(10)+
                              "void main( void ) {"+Chr(10)+
                              "    vec2 p = ( gl_FragCoord.xy / resolution.xy ) - 0.2;"+Chr(10)+
                              "    float sx = 0.3 * (p.x + 0.8) * sin( 3.0 * p.x - 1. * time);"+Chr(10)+
                              "    float dy = 4./ ( 123. * abs(p.y - sx));"+Chr(10)+
                              "    dy += 1./ (160. * length(p - vec2(p.x, 0.)));"+Chr(10)+
                              "    gl_FragColor = vec4( (p.x + 0.1) * dy, 0.3 * dy, dy, 2.1 );"+Chr(10)+
                              "}"

;Setup system data
With System
  \Shader_Program = CompileShader(\Shader_Vertex_Text, \Shader_Fragment_Text)
  glUseProgram(\Shader_Program)
 
  If System\Shader_Program = 0
    MessageRequester("Unsupported Device?","No Shader Support Available",#PB_MessageRequester_Ok)
    End
  EndIf
 
  \App_StartTime = ElapsedMilliseconds()
 
  ;/ store shader uniform locations
  ;Debug "Shader: "+\Shader_Program
  \Shader_Uniform_Time = glGetUniformLocation(\Shader_Program, "time")
  \Shader_Uniform_Mouse = glGetUniformLocation(\Shader_Program, "mouse")
  ;resolution is blacking out the shader not really sure why.
  ;\Shader_Uniform_Resolution = glGetUniformLocation(\Shader_Program, "resolution")
 ;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,\Shader_Fragment_Text)
EndWith

;-MAIN REPEAT
Repeat
 
  Event = WindowEvent()
 
  System\MouseX = WindowMouseX(#Window_Main)
  System\MouseY = WindowMouseY(#Window_Main)
 
  System\App_CurrentTime = ElapsedMilliseconds()
  Render()
 
Until Event = #PB_Event_CloseWindow
End

Procedure CompileShader(VertexShader.s, FragmentShader.s)
 
  Define.i shader_program
  Define.i vs, fs
  Define.i *vbuff, *fbuff
 
  *vbuff = GetShaderSourceBuf(@VertexShader)
  *fbuff = GetShaderSourceBuf(@FragmentShader)
 
  vs = glCreateShader(#GL_VERTEX_SHADER)
  glShaderSource(vs, 1, @*vbuff, #Null)
  glCompileShader(vs)
 
  fs = glCreateShader(#GL_FRAGMENT_SHADER)
  glShaderSource(fs, 1, @*fbuff, #Null)
  glCompileShader(fs)   
 
  shader_program = glCreateProgram()
  glAttachShader(shader_program, fs)
  glDeleteShader(fs)
  glAttachShader(shader_program, vs)
  glDeleteShader(vs)
  glLinkProgram(shader_program)

  FreeShaderSourceBuf(*vbuff)
  FreeShaderSourceBuf(*fbuff)
 
  ProcedureReturn shader_program
 
EndProcedure

Procedure FreeShaderSourceBuf(*p)
 
  CompilerIf #PB_Compiler_Unicode   
    If *p
      FreeMemory(*p)
    EndIf
  CompilerEndIf
 
EndProcedure

Procedure GetShaderSourceBuf(*shader)
 
  Protected *p, l, t$
 
  CompilerIf #PB_Compiler_Unicode
   
    t$ = PeekS(*shader, -1) : l = Len(t$)
    *p = AllocateMemory(l + SizeOf(Character))
    If *p
      PokeS(*p, t$, l, #PB_Ascii)
    EndIf
   
  CompilerElse
   
    *p = *shader
   
  CompilerEndIf

  ProcedureReturn *p

EndProcedure

Procedure Render()
 
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  glClearColor_(0.2, 0.2, 0.2, 1.0)
 
  glUseProgram(System\Shader_Program)
  ;/ set shader Uniform values
  glUniform2f(System\Shader_Uniform_Resolution,System\Shader_Width,System\Shader_Height)
  glUniform1f(System\Shader_Uniform_Time,(System\App_CurrentTime-System\App_StartTime)/1000)
  glUniform2i(System\Shader_Uniform_SurfacePosition,System\MouseX,System\MouseY)
 
  glBegin_(#GL_QUADS)
    glVertex3f_(-1,-1,0)
    glVertex3f_( 1,-1,0)
    glVertex3f_( 1, 1,0)
    glVertex3f_(-1, 1,0)
  glEnd_()           
  glUseProgram(0)
 
  ;## DRAWING TEXTURES/IMAGES
  ;First enable the Texture system.
  glEnable_(#GL_TEXTURE_2D)
 
  ;This procedure will create a quad and apply a texture to it.
  ;The Texture variable contains the texture created earlier using SetupGLTexture().
;  Render2DQuad(Texture1, 0, 0, ImageWidth(Image1), ImageHeight(Image1), 1)
  Render2DQuad(Texture2, 150, 90, ImageWidth(Image2), ImageHeight(Image2), 2)
 
  ;After all the textures have been displayed disable the texture system.
  ;Otherwise it will conflict with the non texture graphics.
  glDisable_(#GL_TEXTURE_2D)
 
  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_Main,"GLSL Testing - PJ 07/06/2014 - FPS: "+Str(System\FPS))
  EndIf
 
  SetGadgetAttribute(#Gad_OpenGL, #PB_OpenGL_FlipBuffers, #True)
 
EndProcedure

Procedure Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
 
  ;The texture is first bound which tells OpenGL to use this texture for any future rendering.
  glBindTexture_(#GL_TEXTURE_2D, OGLTexture)
  glBegin_(#GL_QUADS)
    glColor4f_   (1,1,1,1)
    glNormal3f_  (0,0,1.0)
    glTexCoord2f_(1.0,1.0)
    glVertex3f_  (StartX+Width,StartY,Z)
    glTexCoord2f_(0.0,1.0)
    glVertex3f_  (StartX,StartY,Z)
    glTexCoord2f_(0.0,0.0)
    glVertex3f_  (StartX,StartY+Height,Z)
    glTexCoord2f_(1.0,0.0)
    glVertex3f_  (StartX+Width,StartY+Height,Z)
  glEnd_()

EndProcedure

Procedure SetupGLTexture(ImageHandle.i)
 
  Define.i ImageW, ImageH, ImageD
  Define.i MemoryAddress
  Define.i TextureHandle

  If IsImage(ImageHandle) = 0
    ProcedureReturn #False
  EndIf
 
  ImageD = ImageDepth(ImageHandle, #PB_Image_InternalDepth)
 
  StartDrawing(ImageOutput(ImageHandle))
    MemoryAddress = DrawingBuffer()
  StopDrawing()
 
  If MemoryAddress = 0
    ProcedureReturn #False
  EndIf
 
  glGenTextures_(1, @TextureHandle)
  glBindTexture_(#GL_TEXTURE_2D, TextureHandle)
 
  ImageW = ImageWidth(ImageHandle)
  ImageH = ImageHeight(ImageHandle)
 
  If ImageD = 32
    glTexImage2D_(#GL_TEXTURE_2D, 0, 4, ImageW, ImageH, 0, #GL_BGRA, #GL_UNSIGNED_BYTE, MemoryAddress)
  Else
    glTexImage2D_(#GL_TEXTURE_2D, 0, 3, ImageW, ImageH, 0, #GL_BGR, #GL_UNSIGNED_BYTE, MemoryAddress)
  EndIf
 
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
 
  ProcedureReturn TextureHandle
 
EndProcedure

Procedure SetupOpenGL()
 
  glMatrixMode_(#GL_PROJECTION)

  glOrtho_(0.0, System\Shader_Width, System\Shader_Height, 0.0, -1000.0, 1000.0)
 
  glMatrixMode_(#GL_MODELVIEW)
 
  glEnable_(#GL_DEPTH_TEST)
 
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
   
EndProcedure
 DataSection
    Logo:
    IncludeBinary "pacelogo2fpl6y.png"
EndDataSection

Last edited by omnikam on 18 Apr 2017 12:46, edited 2 times in total.

User avatar
WhiteWolf
Site Admin
Posts: 303
Joined: 11 Apr 2016 17:49
Has thanked: 77 times
Been thanked: 56 times
Status: Online

Re: GLSL shader with Png Jpg Support [Code Included]

Postby WhiteWolf » 17 Apr 2017 15:11

Wow this would be a great discovery for further demotool development thanx for the share omnikam also following the dbf thread :icon_l33t:
(¯`·._.·currently working on Windows [AM]WEBDEFF Editor DemoTool Project.·._.·´¯)

omnikam
PureBasicCoder.
Posts: 7
Joined: 12 Apr 2017 22:19
Has thanked: 2 times
Been thanked: 9 times
Status: Offline

Re: GLSL shader with Png Jpg Support [Code Included]

Postby omnikam » 17 Apr 2017 17:07

Cheer`s :icon_elkgrin: Now all we need is a working Scroller OpenGl style :icon_klatsch:

inc
Site Admin
Posts: 92
Joined: 18 Apr 2016 19:38
Has thanked: 49 times
Been thanked: 52 times
Status: Offline

Re: GLSL shader with Png Jpg Support [Code Included]

Postby inc » 17 Apr 2017 17:26

omnikam wrote:Cheer`s :icon_elkgrin: Now all we need is a working Scroller OpenGl style :icon_klatsch:


yes yes. comes next.

first i include a option into the shader tool, to load a logo on the fly into the shader :D

currently:

Image

attached: demo
- hit esc for exit
- grab window with mouse on the header
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: 303
Joined: 11 Apr 2016 17:49
Has thanked: 77 times
Been thanked: 56 times
Status: Online

Re: GLSL shader with Png Jpg Support [Code Included]

Postby WhiteWolf » 17 Apr 2017 19:59

WoW you guys did it works smooth over here excellent job Omnikam keep up the good work mate :icon_l33t:
(¯`·._.·currently working on Windows [AM]WEBDEFF Editor DemoTool Project.·._.·´¯)

omnikam
PureBasicCoder.
Posts: 7
Joined: 12 Apr 2017 22:19
Has thanked: 2 times
Been thanked: 9 times
Status: Offline

Re: GLSL shader with Png Jpg Support [Code Included]

Postby omnikam » 18 Apr 2017 15:10

Ignorance is a blight on the world i say. Share your discovery's :icon_party: Thats what i like about these 2 sites, there is so much generosity from others to help people learn, i only wish i had more time to study

inc
Site Admin
Posts: 92
Joined: 18 Apr 2016 19:38
Has thanked: 49 times
Been thanked: 52 times
Status: Offline

Re: GLSL shader with Png Jpg Support [Code Included]

Postby inc » 18 Apr 2017 19:12

omnikam wrote:Ignorance is a blight on the world i say. Share your discovery's :icon_party: Thats what i like about these 2 sites, there is so much generosity from others to help people learn, i only wish i had more time to study


the time will come. sharing knowledge is a rule here and at DBF. if someone have a question, then ask, wait, get answer :)
we're always learning and only, when ppl share wisdom.


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


443,897 Page visits

Powered by phpBB® Forum Software © phpBB Limited