'----------------------------------------
' -=:: GLSL Julia Demo for FBSL v3.5 ::=-
'----------------------------------------
' #AppType Console ' for debugging only
#Option Strict
#DllImports OpenGL32
#Include <Include\Windows.inc>
#Include ".\COGLApp1.inc"
#Define GL_QUADS 0x0007
#Define GL_VERTEX_SHADER 0x8B31
#Define GL_FRAGMENT_SHADER 0x8B30
#Define GL_MODELVIEW 0x1700
#Define GL_PROJECTION 0x1701
#Define GL_COMPILE_STATUS 0x8B81
#Define GL_LINK_STATUS 0x8B82
#Define GL_INFO_LOG_LENGTH 0x8B84
' GLSL Stubs for wglGetProcAddress
Declare glAttachShader()
Declare glCompileShader()
Declare glCreateProgram()
Declare glCreateShader()
Declare glDeleteProgram()
Declare glDeleteShader()
Declare glGetProgramInfoLog()
Declare glGetProgramiv()
Declare glGetShaderInfoLog()
Declare glGetShaderiv()
Declare glGetUniformLocation()
Declare glLinkProgram()
Declare glShaderSource()
Declare glUseProgram()
Declare glUniform1f()
Declare glUniform2f()
Dim width, height, Viewer
Dim juliaVS = 0, juliaFS = 0, juliaProg = 0
Dim unitime = 0, unires = 0
Sub Main()
Viewer = New COGLApp1("-=:: GLSL Julia Particles Demo ::=-", _
AddressOf Renderer, AddressOf Messenger, AddressOf Updater)
glslInit()
Init()
With Viewer
.DataRate(2)
Center(.hWnd()): Show(.hWnd)
.Start(): Delete Viewer
End With
End Sub
Sub Updater()
Viewer.Caption("-=:: GLSL Julia Particles Demo ::=- FPS = " & Viewer.FPS())
End Sub
Function Messenger(ByVal %hwnd, ByVal %msg, ByVal %wparam, ByVal %lparam) As Long
If msg = WM_KEYDOWN Then
If wparam = VK_ESCAPE Then
Viewer.Quit()
Deinit()
End If
Return 0 ' suppress default
ElseIf msg = WM_SIZE Then
width = LoWord(lparam)
height = HiWord(lparam)
Reshape()
Return 0 ' ditto
ElseIf msg = WM_MOUSEWHEEL Or msg = WM_MOUSEMOVE Then
Return 0 ' ditto
ElseIf msg = WM_CLOSE Then
Deinit()
End If
Return -1 ' allow COGLApp's default processing
End Function
Sub Renderer()
glPushMatrix(): glLoadIdentity()
glMatrixMode(GL_PROJECTION)
glPushMatrix(): glLoadIdentity()
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
glUseProgram(juliaProg)
glUniform1f(unitime, !Clock())
glUniform2f(unires, !width, !height)
glBegin(GL_QUADS)
glVertex2i(0, 0)(1, 0)(1, 1)(0, 1) ' FBSL shorthand
glEnd()
glUseProgram(0)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
End Sub
Sub Init()
juliaVS = CompileFromFile(GL_VERTEX_SHADER, ".\JP.vs")
juliaFS = CompileFromFile(GL_FRAGMENT_SHADER, ".\JP.fs")
juliaProg = LinkProgram(2, juliaVS, juliaFS)
glUseProgram(juliaProg)
unitime = glGetUniformLocation(juliaProg, "iGlobalTime")
unires = glGetUniformLocation(juliaProg, "iResolution")
glUseProgram(0)
End Sub
Sub Deinit()
glDeleteProgram(juliaProg)
End Sub
Sub Reshape()
Dim fov = 45., aspect = width / height
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
fov = Tan(fov * PI / 360) * .1: aspect = aspect * fov
glFrustum(-aspect, aspect, -fov, fov, .1, 1000.)
glMatrixMode(GL_MODELVIEW)
End Sub
Sub LoadSource(filePath) ' returns shader source in FileGet
FileGet(FileOpen(filePath, BINARY), FileLen(filePath))
FileClose(FileOpen) ' FBSL shorthand
End Sub
Function CompileFromFile(enumtype As Long, filePath) As Long
Dim pointer, shader
Dim length As Long, result As Long
LoadSource(filePath)
If Not Asc(FileGet) Then Return 0
shader = glCreateShader(enumtype)
length = StrLen(FileGet)
pointer = @FileGet
glShaderSource(shader, 1, @pointer, @length)
glCompileShader(shader)
FileGet = "" ' optionally reclaim memory
glGetShaderiv(shader, GL_COMPILE_STATUS, @result)
If Not result Then
Dim prglog As String
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, @length)
Alloc(prglog, length)
glGetShaderInfoLog(shader, length, @result, @prglog)
Print "CompileFromFile(): Unable to compile ", filePath, ": ", prglog
Clean prglog
glDeleteShader(shader)
Return 0
End If
Return shader
End Function
Function LinkProgram(numShaders, ParamArray shaders) As Long
Dim program = glCreateProgram(), shader
For Dim i = 0 To numShaders - 1
shader = shaders[i]
glAttachShader(program, shader)
Next
glLinkProgram(program)
Dim success = 0
glGetProgramiv(program, GL_LINK_STATUS, @success)
If Not success Then
Dim $temp * 256
glGetProgramInfoLog(program, 256, NULL, @temp)
Print "Failed to link program: ", temp
glDeleteProgram(program)
program = 0
End If
Return program
End Function
Sub GlslInit()
AddressOf glAttachShader = wglGetProcAddress("glAttachShader")
AddressOf glCompileShader = wglGetProcAddress("glCompileShader")
AddressOf glCreateProgram = wglGetProcAddress("glCreateProgram")
AddressOf glCreateShader = wglGetProcAddress("glCreateShader")
AddressOf glDeleteProgram = wglGetProcAddress("glDeleteProgram")
AddressOf glDeleteShader = wglGetProcAddress("glDeleteShader")
AddressOf glGetProgramInfoLog = wglGetProcAddress("glGetProgramInfoLog")
AddressOf glGetProgramiv = wglGetProcAddress("glGetProgramiv")
AddressOf glGetShaderInfoLog = wglGetProcAddress("glGetShaderInfoLog")
AddressOf glGetShaderiv = wglGetProcAddress("glGetShaderiv")
AddressOf glGetUniformLocation = wglGetProcAddress("glGetUniformLocation")
AddressOf glLinkProgram = wglGetProcAddress("glLinkProgram")
AddressOf glShaderSource = wglGetProcAddress("glShaderSource")
AddressOf glUseProgram = wglGetProcAddress("glUseProgram")
AddressOf glUniform1f = wglGetProcAddress("glUniform1f")
AddressOf glUniform2f = wglGetProcAddress("glUniform2f")
End Sub