diff options
-rw-r--r-- | assets/cube.mtl | 14 | ||||
-rw-r--r-- | assets/cube.obj | 84 | ||||
-rw-r--r-- | honey/shader.lua | 68 | ||||
-rw-r--r-- | honey/std.lua | 3 | ||||
-rw-r--r-- | main.lua | 36 |
5 files changed, 129 insertions, 76 deletions
diff --git a/assets/cube.mtl b/assets/cube.mtl index f231bdf..d6e6205 100644 --- a/assets/cube.mtl +++ b/assets/cube.mtl @@ -1,10 +1,12 @@ # Blender MTL File: 'None' # Material Count: 1 -newmtl None -Ns 500 -Ka 0.8 0.8 0.8 -Kd 0.8 0.8 0.8 -Ks 0.8 0.8 0.8 -d 1 +newmtl None.002 +Ns 499.999983 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.800000 0.800000 0.800000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 illum 2 diff --git a/assets/cube.obj b/assets/cube.obj index 01d923d..4943740 100644 --- a/assets/cube.obj +++ b/assets/cube.obj @@ -2,30 +2,30 @@ # www.blender.org mtllib cube.mtl o Cube_Cube.003 -v -1.000000 -1.000000 1.000000 -v -1.000000 1.000000 1.000000 -v -1.000000 -1.000000 -1.000000 -v -1.000000 1.000000 -1.000000 -v 1.000000 -1.000000 1.000000 -v 1.000000 1.000000 1.000000 -v 1.000000 -1.000000 -1.000000 -v 1.000000 1.000000 -1.000000 -v -1.000000 -1.000000 -1.000000 -v -1.000000 -1.000000 -1.000000 -v -1.000000 -1.000000 1.000000 -v -1.000000 -1.000000 1.000000 -v -1.000000 1.000000 1.000000 -v -1.000000 1.000000 1.000000 -v -1.000000 1.000000 -1.000000 -v -1.000000 1.000000 -1.000000 -v 1.000000 -1.000000 -1.000000 -v 1.000000 -1.000000 -1.000000 -v 1.000000 1.000000 -1.000000 -v 1.000000 1.000000 -1.000000 -v 1.000000 -1.000000 1.000000 -v 1.000000 -1.000000 1.000000 -v 1.000000 1.000000 1.000000 -v 1.000000 1.000000 1.000000 +v -0.500000 0.500000 0.500000 +v -0.500000 -0.500000 -0.500000 +v -0.500000 -0.500000 0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 -0.500000 -0.500000 +v -0.500000 -0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v 0.500000 -0.500000 0.500000 +v 0.500000 -0.500000 -0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 -0.500000 0.500000 +v 0.500000 -0.500000 0.500000 +v 0.500000 -0.500000 -0.500000 +v -0.500000 -0.500000 0.500000 +v -0.500000 -0.500000 -0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 0.500000 0.500000 +v 0.500000 0.500000 -0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v 0.500000 -0.500000 0.500000 +v -0.500000 0.500000 0.500000 vt 0.625000 0.000000 vt 0.375000 0.250000 vt 0.375000 0.000000 @@ -50,23 +50,23 @@ vt 0.625000 0.750000 vt 0.625000 1.000000 vt 0.375000 0.750000 vt 0.875000 0.750000 -vn -1.0000 0.0000 0.0000 +vn -1.0000 -0.0000 0.0000 vn 0.0000 0.0000 -1.0000 -vn 1.0000 0.0000 0.0000 -vn 0.0000 0.0000 1.0000 -vn 0.0000 -1.0000 0.0000 +vn 1.0000 -0.0000 0.0000 +vn 0.0000 -0.0000 1.0000 +vn 0.0000 -1.0000 -0.0000 vn 0.0000 1.0000 0.0000 -usemtl None -s off -f 13/1/1 9/2/1 11/3/1 -f 16/4/2 17/5/2 10/6/2 -f 20/7/3 21/8/3 18/9/3 -f 24/10/4 12/11/4 22/12/4 -f 7/13/5 1/14/5 3/15/5 -f 4/16/6 6/17/6 8/18/6 -f 13/1/1 15/19/1 9/2/1 -f 16/4/2 19/20/2 17/5/2 -f 20/7/3 23/21/3 21/8/3 -f 24/10/4 14/22/4 12/11/4 -f 7/13/5 5/23/5 1/14/5 -f 4/16/6 2/24/6 6/17/6 +usemtl None.002 +s 1 +f 1/1/1 2/2/1 3/3/1 +f 4/4/2 5/5/2 6/6/2 +f 7/7/3 8/8/3 9/9/3 +f 10/10/4 11/11/4 12/12/4 +f 13/13/5 14/14/5 15/15/5 +f 16/16/6 17/17/6 18/18/6 +f 1/1/1 19/19/1 2/2/1 +f 4/4/2 20/20/2 5/5/2 +f 7/7/3 21/21/3 8/8/3 +f 10/10/4 22/22/4 11/11/4 +f 13/13/5 23/23/5 14/14/5 +f 16/16/6 24/24/6 17/17/6 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 @@ -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() |