summaryrefslogtreecommitdiff
path: root/honey
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-03-13 22:42:40 -0500
committersanine <sanine.not@pm.me>2023-03-13 22:42:40 -0500
commit344d6e68bee7f286f7c4b4b25518367c595b4619 (patch)
tree9abb86b714fef18aef0d8048bd4931f6adcf9fd0 /honey
parent53b151b3fd6d4c2b953d8ed93261125577f1c123 (diff)
add shader.lua
Diffstat (limited to 'honey')
-rw-r--r--honey/shader.lua68
-rw-r--r--honey/std.lua3
2 files changed, 71 insertions, 0 deletions
diff --git a/honey/shader.lua b/honey/shader.lua
new file mode 100644
index 0000000..82d7e8a
--- /dev/null
+++ b/honey/shader.lua
@@ -0,0 +1,68 @@
+local gl = honey.gl
+
+local module = {}
+setmetatable(module, {__index=_G})
+setfenv(1, module)
+
+
+Shader = {}
+Shader.__index = Shader
+
+local function compileShader(source, type)
+ local shader = gl.CreateShader(type)
+ gl.ShaderSource(shader, source)
+ gl.CompileShader(shader)
+ return shader
+end
+
+function Shader.new(_, sources)
+ local self = {}
+ self.locations = {}
+ self.links = {}
+
+ local shaders = {}
+ if sources.vertex then
+ table.insert(shaders, compileShader(sources.vertex, gl.VERTEX_SHADER))
+ end
+ if sources.fragment then
+ table.insert(shaders, compileShader(sources.fragment, gl.FRAGMENT_SHADER))
+ end
+
+ self.program = gl.CreateProgram()
+ for _, shader in ipairs(shaders) do
+ gl.AttachShader(self.program, shader)
+ end
+ gl.LinkProgram(self.program)
+ for _, shader in ipairs(shaders) do
+ gl.DeleteShader(shader)
+ end
+
+ setmetatable(self, Shader)
+ return self
+end
+setmetatable(Shader, {__call=Shader.new})
+
+
+function Shader.getLocation(self, name)
+ if self.locations[name] then
+ return self.locations[name]
+ end
+
+ local location = gl.GetUniformLocation(self.program, name)
+ self.locations[name] = location
+ return location
+end
+
+
+function Shader.use(self)
+ gl.UseProgram(self.program)
+end
+
+
+function Shader.setMatrix(self, name, matrix)
+ local location = self:getLocation(name)
+ gl.UniformMatrix4fv(location, false, matrix.data)
+end
+
+
+return module.Shader
diff --git a/honey/std.lua b/honey/std.lua
index 78b2525..ba86eb0 100644
--- a/honey/std.lua
+++ b/honey/std.lua
@@ -3,6 +3,7 @@ local window = require 'honey.window'
local mesh = require 'honey.mesh'
local Vec3 = require 'honey.vec3'
local Mat4 = require 'honey.mat4'
+local Shader = require 'honey.shader'
honey.init = init.init
honey.terminate = init.terminate
@@ -11,3 +12,5 @@ honey.Window = window.Window
honey.mesh = mesh
honey.Vec3 = Vec3
honey.Mat4 = Mat4
+
+honey.Shader = Shader