summaryrefslogtreecommitdiff
path: root/demo/honey.lua
blob: 6d2cdfc6c0a1c411b138824dae0df4fdb2ab639b (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;

out vec3 ourColor;
out vec2 TexCoord;

void main()
{
    gl_Position = vec4(aPos, 1.0);
    ourColor = aColor;
    TexCoord = aTexCoord;
}
]]

local fragmentShaderSource = [[
#version 330 core
out vec4 FragColor;
  
in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D ourTexture;

void main()
{
    FragColor = texture(ourTexture, TexCoord);
}
]]

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 texture = gl.texture.create()
gl.texture.bind(gl.texture.bindTarget.texture2d, texture)
err = gl.getError()
if err ~= gl.errorType.noError then error(gl.errorName(err)) end


local image, width, height, channels = honey.image.load('container.jpg', 0)
gl.texture.bufferImage2d(
	gl.texture.bindTarget.texture2d, 0,
	gl.texture.format.rgb,
	width, height,
	gl.texture.format.rgb, gl.dataType.uchar,
	image
)
gl.texture.generateMipmaps(gl.texture.bindTarget.texture2d)
err = gl.getError()
if err ~= gl.errorType.noError then error(gl.errorName(err)) end

honey.image.destroy(image)


local vertices = {
	-- position          color      uvs
	 0.5,  0.5, 0.0,    0, 0, 0,    1, 1,
	 0.5, -0.5, 0.0,    1, 0, 0,    1, 0,
	-0.5, -0.5, 0.0,    0, 1, 0,    0, 0,
	-0.5,  0.5, 0.0,    0, 0, 1,    0, 1
}
local indices = {
        0, 1, 3,
        1, 2, 3
}

local vertexArray = gl.data.createVertexArray()

local vertexBuffer = gl.data.createBuffer()
local elementBuffer = gl.data.createBuffer()
gl.data.bindVertexArray(vertexArray)

gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, vertexBuffer)
local err = gl.getError()
if err ~= gl.errorType.noError then error(gl.errorName(err)) end
gl.data.bufferData(gl.data.bufferTarget.arrayBuffer, gl.dataType.float, vertices, gl.data.bufferUsage.staticDraw)
if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end

gl.data.bindBuffer(gl.data.bufferTarget.elementArrayBuffer, elementBuffer)
gl.data.bufferData(gl.data.bufferTarget.elementArrayBuffer, gl.dataType.uint, indices, gl.data.bufferUsage.staticDraw)

gl.data.vertexAttribPointer(0, 3, false, 8, 0)
gl.data.vertexArrayEnableAttrib(0)
gl.data.vertexAttribPointer(1, 3, false, 8, 3)
gl.data.vertexArrayEnableAttrib(1)
gl.data.vertexAttribPointer(2, 2, false, 8, 6)
gl.data.vertexArrayEnableAttrib(2)

gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, 0)
if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end

gl.shader.use(shader)
local textureLocation = gl.shader.getUniformLocation(shader, "ourTexture")
err = gl.getError()
if err ~= gl.errorType.noError then error(gl.errorName(err)) end
gl.shader.uniform1i(textureLocation, 0)
err = gl.getError()
if err ~= gl.errorType.noError then error(gl.errorName(err)) end


while not window.shouldClose(w) do
	gl.draw.setClearColor(0.2, 0.3, 0.3, 1.0)
	gl.draw.clear(gl.draw.bufferMask.colorBuffer);

	gl.texture.setActiveUnit(0)
	err = gl.getError()
	if err ~= gl.errorType.noError then error(gl.errorName(err)) end
	gl.texture.bind(gl.texture.bindTarget.texture2d, texture)
	err = gl.getError()
	if err ~= gl.errorType.noError then error(gl.errorName(err)) end


	gl.shader.use(shader)
	gl.data.bindVertexArray(vertexArray)
	gl.draw.drawElements(gl.draw.primitiveType.triangles, 6, gl.dataType.uint, 0)

	window.swapBuffers(w)
	window.pollEvents()
end
window.destroy(w)
gl.terminate()