From 344d6e68bee7f286f7c4b4b25518367c595b4619 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 13 Mar 2023 22:42:40 -0500 Subject: add shader.lua --- main.lua | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index 0ff081c..831aa54 100644 --- a/main.lua +++ b/main.lua @@ -64,25 +64,10 @@ void main() } ]] --- vertex shader -local vertexShader = gl.CreateShader(gl.VERTEX_SHADER) -gl.ShaderSource(vertexShader, vertexShaderSource) -gl.CompileShader(vertexShader) - --- fragment shader -local fragmentShader = gl.CreateShader(gl.FRAGMENT_SHADER) -gl.ShaderSource(fragmentShader, fragmentShaderSource) -gl.CompileShader(fragmentShader) - --- link -local shader = gl.CreateProgram() -gl.AttachShader(shader, vertexShader) -gl.AttachShader(shader, fragmentShader) -gl.LinkProgram(shader) --- clean up -gl.DeleteShader(vertexShader) -gl.DeleteShader(fragmentShader) - +local shader = honey.Shader{ + vertex = vertexShaderSource, + fragment = fragmentShaderSource, +} local tetra = honey.mesh.loadFile("assets/tetrahedron.obj")[1] local cube = honey.mesh.loadFile("assets/cube.obj")[1] @@ -117,15 +102,10 @@ while not window:shouldClose() do gl.ClearColor(0.2, 0.4, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT) - gl.UseProgram(shader) - - local modelLocation = gl.GetUniformLocation(shader, "model") - local viewLocation = gl.GetUniformLocation(shader, "view") - local projectionLocation = gl.GetUniformLocation(shader, "projection") - - gl.UniformMatrix4fv(modelLocation, false, model.data) - gl.UniformMatrix4fv(viewLocation, false, view.data) - gl.UniformMatrix4fv(projectionLocation, false, projection.data) + shader:use() + shader:setMatrix('model', model) + shader:setMatrix('view', view) + shader:setMatrix('projection', projection) mesh:drawElements() -- cgit v1.2.1