1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
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
function SetTexture(self, name, texture)
self:Use()
gl.BindTexture(gl.TEXTURE_2D, texture)
local location = gl.GetUniformLocation(self.program, name)
gl.Uniform1i(location, 0)
end
--===== fin =====--
return Shader
|