This is my first post after joining the forums and purchasing PureBasic. So exciting

Let me quickly ask my question to not waste your time. My code below displays a OpenGL GLSL shader inside a box. If you use the arrow keys on the keyboard the box moves but the shader remains fixed at the center of the window.
I'm wanting to make this shader pattern move with the box and arrow keys.
This is a modified version of applePi's code over here -> http://www.purebasic.fr/english/viewtop ... 3&start=30. Look at the bottom of that page for his work. He is nicely able to move both his triangle shape and shader together with the arrow keys.
I am aware that my fragment shader code below has a static reference inside it of 800 x 600 pixels. Most likely this is what's causing the shader pattern to remain fixed at one point. But my knowledge of shaders is poor and I can't work out how to modify the shader code to make this move with the arrow keys.
By the way, I've taken this shader code from here -> http://glslsandbox.com/e#41187.0
The fragment shader doesn't look too complex to me so I'm hoping this isn't a hard question for someone with shader knowledge.
Thanks community!
Code: Select all
#GL_ARRAY_BUFFER = $8892
#GL_STATIC_DRAW = $88E4
#GL_VERTEX_SHADER = $8B31
#GL_FRAGMENT_SHADER = $8B30
Declare.s LoadFile(FileName.s)
Structure TVertex
x.f
y.f
z.f
EndStructure
Structure Colors
r.f
g.f
b.f
EndStructure
Dim matrix.f(3,3)
matrix(0,0)=1: matrix(0,1)=0:matrix(0,2)=0:matrix(0,3)=0 ;// first column
matrix(1,0)=0: matrix(1,1)=1:matrix(1,2)=0:matrix(1,3)=0 ;// second column
matrix(2,0)=0: matrix(2,1)=0:matrix(2,2)=1:matrix(2,3)=0 ; // third column
matrix(3,0)=0: matrix(3,1)=0:matrix(3,2)=0:matrix(3,3)=2 ;// fourth column
;the above is the simulation of this:
;GLfloat matrix[] = {
; 1.0f, 0.0f, 0.0f, 0.0f, // first column
; 0.0f, 1.0f, 0.0f, 0.0f, // second column
; 0.0f, 0.0f, 1.0f, 0.0f, // third column
; -0.5f, 0.0f, 0.0f, 2.0f // fourth column
; };
OpenWindow(0, 10, 10, 800, 600, "OpenGL demo ... color and translation with shader... use arrow keys and A/Z to translate in 3D space")
SetWindowColor(0, RGB(200,220,200))
OpenGLGadget(0, 20, 10, WindowWidth(0)-40 , WindowHeight(0)-20, #PB_OpenGL_Keyboard)
IncludeFile "newFunctions.pbi"
Global points_vbo.i, colors_vbo.i, vao.i
Dim Vertex.TVertex(3)
Vertex(0)\x = -0.5
Vertex(0)\y = 0.5
Vertex(0)\z = 0
Vertex(1)\x = 0.5
Vertex(1)\y = 0.5
Vertex(1)\z = 0
Vertex(2)\x = 0.5
Vertex(2)\y = -0.5
Vertex(2)\z = 0
Vertex(3)\x = -0.5
Vertex(3)\y = -0.5
Vertex(3)\z = 0
Define vertex_shader.s
Define fragment_shader.s
Define *vbuff
vertex_shader = "#version 420"+#CRLF$
vertex_shader + "layout(location = 0) in vec3 vertex_position;"+#CRLF$
vertex_shader + "uniform mat4 matrix; // our matrix"+#CRLF$
vertex_shader + "void main() {"+#CRLF$
vertex_shader + "gl_Position = matrix * vec4(vertex_position, 1.0);"+#CRLF$
vertex_shader + "}"
fragment_shader = "precision highp float;"+#CRLF$
fragment_shader + "uniform mat4 matrix;"+#CRLF$
fragment_shader + ""+#CRLF$
fragment_shader + "float ball(vec2 p) {"+#CRLF$
fragment_shader + " vec2 r = vec2(p.x , p.y);"+#CRLF$
fragment_shader + " return 0.05 / length(r);"+#CRLF$
fragment_shader + "}"+#CRLF$
fragment_shader + "void main(void) {"+#CRLF$
fragment_shader + " vec2 resolution = vec2(800 , 600); "+#CRLF$
fragment_shader + " vec2 q = vec2(gl_FragCoord.x,gl_FragCoord.y) / vec2(resolution.x,resolution.y);"+#CRLF$
fragment_shader + " vec2 p = -0.5 + q; "+#CRLF$
fragment_shader + " p.x *= resolution.x / resolution.y;"+#CRLF$
fragment_shader + " float col = 0.0;"+#CRLF$
fragment_shader + " col += ball(p);"+#CRLF$
fragment_shader + " col = max(mod(col, 0.4), min(col, 2.0));"+#CRLF$
fragment_shader + " gl_FragColor = vec4(col * 0.8, col * 0.3, col * 0.3, 1.0);"+#CRLF$
fragment_shader + "}"+#CRLF$
;*vbuff = @vertex_shader
;*fbuff = @fragment_shader
*vbuff = Ascii(vertex_shader)
*fbuff = Ascii(fragment_shader)
glEnable_(#GL_DEPTH_TEST); // enable depth-testing
;glDepthFunc_(#GL_LESS); // depth-testing interprets a smaller value as "closer"
Global points_vbo.i, colors_vbo.i, vao.i
;=================================================================================
glGenBuffers( 1, @points_vbo ) ; Vertex Buffer Object
glBindBuffer(#GL_ARRAY_BUFFER, points_vbo )
glBufferData(#GL_ARRAY_BUFFER,12 * SizeOf(float),@Vertex(0), #GL_STATIC_DRAW)
glGenVertexArrays (1, @vao);
glBindVertexArray (vao);
glBindBuffer (#GL_ARRAY_BUFFER, points_vbo);
glVertexAttribPointer (0, 3, #GL_FLOAT, #GL_FALSE, 0, #Null);
;ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
glEnableVertexAttribArray(0);
;=================================================================================
Global vs = glCreateShader(#GL_VERTEX_SHADER);
glShaderSource(vs, 1, @*vbuff, #Null) ;
glCompileShader(vs);
Global fs = glCreateShader(#GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, @*fbuff, #Null);
glCompileShader(fs) ;
Global shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme) ;
glEnable_(#GL_CULL_FACE); // cull face
glCullFace_(#GL_BACK); // cull back face
glFrontFace_(#GL_CW); // GL_CCW for counter clock-wise
rot.f = 1
SetActiveGadget(0)
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget And EventGadget() = 0
If EventType() = #PB_EventType_KeyDown
key = GetGadgetAttribute(0,#PB_OpenGL_Key )
;If key = 38 ; Up arrow key
If key = #PB_Shortcut_Up
matrix(3,1) + 0.1
ElseIf key = #PB_Shortcut_Down ; Down arrow key
matrix(3,1) - 0.1
ElseIf key = #PB_Shortcut_Left ; Left arrow key
matrix(3,0) - 0.1
ElseIf key = #PB_Shortcut_Right ; Left arrow key
matrix(3,0) + 0.1
ElseIf key = #PB_Shortcut_A ; Left arrow key
matrix(3,3) - 0.1
ElseIf key = #PB_Shortcut_Z ; Left arrow key
matrix(3,3) + 0.1
ElseIf key = #PB_Shortcut_Escape ; Esc key to exit
quit = 1
EndIf
EndIf
EndIf
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT);
glRotatef_(rot, 0, 1, 0) ;
matrix_location.l = glGetUniformLocation(shader_programme, "matrix");
glUseProgram (shader_programme);
glUniformMatrix4fv (matrix_location, 1, #GL_FALSE, @matrix(0,0));
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT);
glViewport_(0, 0, 800, 600) ;
glUseProgram(shader_programme)
glBindVertexArray(vao);
;draw points 0-3 from the currently bound VAO With current in-use shader
;glDrawArrays(#GL_TRIANGLES, 0, 3);
glDrawArrays(#GL_QUADS, 0, 4);
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Delay(16)
Until Event = #PB_Event_CloseWindow Or quit = 1
Procedure.s LoadFile(FileName.s)
Protected FF, Format, length, *mem, Text.s
FF = ReadFile(#PB_Any, FileName)
If FF
Format = ReadStringFormat(FF)
length = Lof(FF)
If length
*mem = AllocateMemory(length)
If *mem
ReadData(FF, *mem, length)
Text = PeekS(*mem, length, Format)
FreeMemory(*mem)
CloseFile(FF)
ProcedureReturn Text
EndIf
EndIf
CloseFile(FF)
EndIf
EndProcedure
Code: Select all
Prototype PFNGLGENBUFFERSPROC ( n.i, *buffers)
Global glGenBuffers.PFNGLGENBUFFERSPROC
glGenBuffers = wglGetProcAddress_( "glGenBuffers" )
Prototype PFNGLBINDBUFFERPROC ( target.l, buffer.i)
Global glBindBuffer.PFNGLBINDBUFFERPROC
glBindBuffer = wglGetProcAddress_( "glBindBuffer" )
Prototype PFNGLBUFFERDATAPROC ( target.l, size.i, *Data_, usage.l)
Global glBufferData.PFNGLBUFFERDATAPROC
glBufferData = wglGetProcAddress_( "glBufferData" )
Prototype PFNGLGENVERTEXARRAYSPROC (n.i, *arrays)
Global glGenVertexArrays.PFNGLGENVERTEXARRAYSPROC
glGenVertexArrays = wglGetProcAddress_( "glGenVertexArrays" )
Prototype PFNGLBINDVERTEXARRAYPROC(n.i)
Global glBindVertexArray.PFNGLBINDVERTEXARRAYPROC
glBindVertexArray = wglGetProcAddress_( "glBindVertexArray" )
Prototype PFNGLENABLEVERTEXATTRIBARRAYPROC ( index.i )
Global glEnableVertexAttribArray.PFNGLENABLEVERTEXATTRIBARRAYPROC
glEnableVertexAttribArray = wglGetProcAddress_( "glEnableVertexAttribArray" )
Prototype PFNGLVERTEXATTRIBPOINTERPROC ( index.i, size.i, type.l, normalized.b, stride.i, *pointer )
Global glVertexAttribPointer.PFNGLVERTEXATTRIBPOINTERPROC
glVertexAttribPointer = wglGetProcAddress_( "glVertexAttribPointer" )
Prototype.i PFNGLCREATESHADERPROC ( type.l )
Global glCreateShader.PFNGLCREATESHADERPROC
glCreateShader = wglGetProcAddress_( "glCreateShader" )
Prototype PFNGLSHADERSOURCEPROC ( shader.i, count.i, *stringBuffer, *length )
Global glShaderSource.PFNGLSHADERSOURCEPROC
glShaderSource = wglGetProcAddress_( "glShaderSource" )
Prototype PFNGLCOMPILESHADERPROC ( shader.i )
Global glCompileShader.PFNGLCOMPILESHADERPROC
glCompileShader = wglGetProcAddress_( "glCompileShader" )
Prototype PFNGLATTACHSHADERPROC ( program.i, shader.i )
Global glAttachShader.PFNGLATTACHSHADERPROC
glAttachShader = wglGetProcAddress_( "glAttachShader" )
Prototype PFNGLLINKPROGRAMPROC ( program.i )
Global glLinkProgram.PFNGLLINKPROGRAMPROC
glLinkProgram = wglGetProcAddress_( "glLinkProgram" )
Prototype.i PFNGLCREATEPROGRAMPROC ( )
Global glCreateProgram.PFNGLCREATEPROGRAMPROC
glCreateProgram = wglGetProcAddress_( "glCreateProgram" )
Prototype PFNGLUSEPROGRAMPROC ( program.i )
Global glUseProgram.PFNGLUSEPROGRAMPROC
glUseProgram = wglGetProcAddress_( "glUseProgram" )
Prototype PFNGLDRAWARRAYSPROC ( mode.l, first.i, count.i )
Global glDrawArrays.PFNGLDRAWARRAYSPROC
glDrawArrays = wglGetProcAddress_( "glDrawArrays" )
Prototype PFNGLGETUNIFORMLOCATIONPROC ( program.i, name.p-ascii )
Global glGetUniformLocation.PFNGLGETUNIFORMLOCATIONPROC
glGetUniformLocation = wglGetProcAddress_( "glGetUniformLocation" )
Prototype PFNGLUNIFORMMATRIX4FVPROC ( location.i, count.i, transpose.b, *value )
Global glUniformMatrix4fv.PFNGLUNIFORMMATRIX4FVPROC
glUniformMatrix4fv = wglGetProcAddress_( "glUniformMatrix4fv" )