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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
local gl = honey.gl
local window = honey.window
gl.errorName = function(errorCode)
for name, code in pairs(gl.errorType) do
if code == errorCode then return name end
end
return 'unknown'
end
gl.init()
window.setHint(window.hintType.contextVersionMajor, 3)
window.setHint(window.hintType.contextVersionMinor, 3)
window.setHint(window.hintType.openGlProfile, window.profileType.openGlCoreProfile)
local w = window.create(640, 480, 'hello, world!')
window.makeContextCurrent(w)
gl.initGlad()
window.setFramebufferSizeCallback(w, function(_, width, height)
print(string.format("resize: (%d, %d)", width, height))
gl.setViewport(0, 0, width, height)
end)
local vertexShaderSource = [[
#version 330 core
layout (location = 0) in vec3 aPos;
out vec3 pos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
pos = aPos;
}
]]
local fragmentShaderSource = [[
#version 330 core
in vec3 pos;
out vec4 FragColor;
void main()
{
FragColor = vec4(pos.x+0.5, 0.0f, 2*(pos.y+pos.x), 1.0f);
}
]]
local vertexShader = gl.shader.create(gl.shader.type.vertexShader)
gl.shader.setSource(vertexShader, vertexShaderSource)
gl.shader.compile(vertexShader)
local fragmentShader = gl.shader.create(gl.shader.type.fragmentShader)
gl.shader.setSource(fragmentShader, fragmentShaderSource)
gl.shader.compile(fragmentShader)
local shader = gl.shader.createProgram()
gl.shader.attachShader(shader, vertexShader)
gl.shader.attachShader(shader, fragmentShader)
gl.shader.link(shader)
gl.shader.delete(vertexShader)
gl.shader.delete(fragmentShader)
local vertices = {
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0
}
local vertexArray = gl.createVertexArray()
local vertexBuffer = gl.createBuffer()
gl.bindVertexArray(vertexArray)
gl.bindBuffer(gl.bufferTarget.arrayBuffer, vertexBuffer)
local err = gl.getError()
if err ~= gl.errorType.noError then error(gl.errorName(err)) end
gl.bufferData(gl.bufferTarget.arrayBuffer, gl.dataType.float, vertices, gl.bufferUsage.staticDraw)
if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end
gl.vertexAttribPointer(0, 3, false, 3, 0)
if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end
gl.vertexArrayEnableAttrib(0)
if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end
gl.bindBuffer(gl.bufferTarget.arrayBuffer, 0)
if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end
while not window.shouldClose(w) do
gl.setClearColor(0.2, 0.3, 0.3, 1.0)
gl.clear(gl.bufferMask.colorBuffer);
gl.shader.use(shader)
gl.bindVertexArray(vertexArray)
gl.drawArrays(gl.primitiveType.triangles, 0, 3)
window.swapBuffers(w)
window.pollEvents()
end
window.destroy(w)
gl.terminate()
|