summaryrefslogtreecommitdiff
path: root/demo/bouncing_ball/shader.lua
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-10-03 14:20:47 -0500
committersanine <sanine.not@pm.me>2022-10-03 14:20:47 -0500
commit94fbc0a7d62c5b48eeb056419bc841d4db8f91b4 (patch)
treeea4b7682d94df1a55f4140a02ab3abd487500f03 /demo/bouncing_ball/shader.lua
parentc5fc66ee58f2c60f2d226868bb1cf5b91badaf53 (diff)
add basic bouncing ball
Diffstat (limited to 'demo/bouncing_ball/shader.lua')
-rw-r--r--demo/bouncing_ball/shader.lua65
1 files changed, 65 insertions, 0 deletions
diff --git a/demo/bouncing_ball/shader.lua b/demo/bouncing_ball/shader.lua
new file mode 100644
index 0000000..50d27ff
--- /dev/null
+++ b/demo/bouncing_ball/shader.lua
@@ -0,0 +1,65 @@
+local Shader = {}
+local gl = honey.gl
+setmetatable(Shader, {__index=_G})
+setfenv(1, Shader)
+
+
+--===== creation =====--
+
+function Create(vertexSource, fragmentSource)
+ local self = {}
+ setmetatable(self, {__index=Shader})
+
+ -- vertex shader
+ local vertexShader = gl.CreateShader(gl.VERTEX_SHADER)
+ gl.ShaderSource(vertexShader, vertexSource)
+ gl.CompileShader(vertexShader)
+
+ -- fragment shader
+ local fragmentShader = gl.CreateShader(gl.FRAGMENT_SHADER)
+ gl.ShaderSource(fragmentShader, fragmentSource)
+ gl.CompileShader(fragmentShader)
+
+ -- link
+ self.program = gl.CreateProgram()
+ gl.AttachShader(self.program, vertexShader)
+ gl.AttachShader(self.program, fragmentShader)
+ gl.LinkProgram(self.program)
+
+ -- clean up
+ gl.DeleteShader(vertexShader)
+ gl.DeleteShader(fragmentShader)
+
+ return self
+end
+
+
+function Load(vertexFile, fragmentFile)
+ local vf = io.open(vertexFile)
+ local vsource = vf:read('*a')
+ vf:close()
+
+ local ff = io.open(fragmentFile)
+ local fsource = ff:read('*a')
+ ff:close()
+
+ return Create(vsource, fsource)
+end
+
+
+--===== methods =====--
+
+function Use(self)
+ gl.UseProgram(self.program)
+end
+
+function SetMatrix(self, name, matrix)
+ gl.UseProgram(self.program)
+ local location = gl.GetUniformLocation(self.program, name)
+ gl.UniformMatrix4fv(location, false, matrix)
+end
+
+
+--===== fin =====--
+
+return Shader