Maybe an example like this will help a little bit more? It shows how you can render textured quads on top of your shader.
I also changed your compile procedure a bit. This new one supports Unicode and ASCII and it will also free up the unneeded shader resources after creating the shader program.
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.
Image1 = LoadImage(#PB_Any, #PB_Compiler_Home+"Examples\3D\Data\Textures\ogrelogo-small.jpg")
Image2 = LoadImage(#PB_Any, #PB_Compiler_Home+"Examples\3D\Data\Textures\grass1.png")
;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")
\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,\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, 0, 0, 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