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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
local gl = honey.gl
local window = honey.window
--===== initialize audio =====--
local engine = honey.audio.engine_init()
honey.audio.engine_play_sound(engine, 'example_sound.ogg')
--====== initialize opengl ======--
gl.Init()
window.setHint(window.hintType.contextVersionMajor, 3)
window.setHint(window.hintType.contextVersionMinor, 3)
window.setHint(window.hintType.openGlProfile, window.profileType.openGlCoreProfile)
--====== create window ======--
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)
--====== compile shaders ======--
local vertexShaderSource = [[
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec3 ourColor;
out vec2 TexCoord;
void main()
{
gl_Position = projection * view * model * 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);
}
]]
-- 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)
--====== set up mesh data manually ======--
local vertices = {
-- positions colors uvs
0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, -- top right
0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, -- bottom right
-0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -- bottom let
-0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0 -- top let
}
local indices = {
0, 1, 3, -- first triangle
1, 2, 3 -- second triangle
}
-- buffers
local vertexArray = gl.GenVertexArrays()
local vertexBuffer = gl.GenBuffers()
local elementBuffer = gl.GenBuffers()
gl.BindVertexArray(vertexArray)
gl.BindBuffer(gl.ARRAY_BUFFER, vertexBuffer)
gl.BufferData(gl.ARRAY_BUFFER, gl.FLOAT, vertices, gl.STATIC_DRAW)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.UNSIGNED_INT, indices, gl.STATIC_DRAW)
-- position
gl.VertexAttribPointer(0, 3, false, 8, 0)
gl.EnableVertexAttribArray(0)
-- color
gl.VertexAttribPointer(1, 3, false, 8, 3)
gl.EnableVertexAttribArray(1)
-- uv
gl.VertexAttribPointer(2, 2, false, 8, 6)
gl.EnableVertexAttribArray(2)
--===== load mesh from file =====--
local scene = honey.import.importFile('suzanne.dae')
print(#scene.meshes)
local suzanne = {}
suzanne.vertexData = {}
--====== load texture ======--
local texture = gl.GenTextures()
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
local image, width, height = honey.image.load('container.jpg', 3)
gl.TexImage2D(
gl.TEXTURE_2D, 0,
gl.RGB, width, height,
gl.RGB, gl.UNSIGNED_BYTE,
image
)
gl.GenerateMipmap(gl.TEXTURE_2D)
honey.image.destroy(image)
-- connect shader samplers to texture units
gl.UseProgram(shader)
gl.Uniform1i(gl.GetUniformLocation(shader, 'ourTexture'), 0)
--====== matrices ======--
local model = honey.glm.mat4()
honey.glm.mat4_identity(model)
local axis = honey.glm.vec3()
honey.glm.vec3_set(axis, 0, 1.0)
honey.glm.vec3_set(axis, 1, 0.0)
honey.glm.vec3_set(axis, 2, 0.0)
honey.glm.rotate(model, math.rad(-55), axis)
local view = honey.glm.mat4()
honey.glm.mat4_identity(view)
local translation = honey.glm.vec3()
honey.glm.vec3_set(translation, 0, 0.0)
honey.glm.vec3_set(translation, 1, 0.0)
honey.glm.vec3_set(translation, 2, -3.0)
honey.glm.translate(view, translation)
local projection = honey.glm.mat4()
honey.glm.perspective(math.rad(45), 800/600, 0.1, 100, projection)
--====== main loop ======--
local transform = honey.glm.mat4()
while not window.shouldClose(w) do
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.ActiveTexture(0)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.UseProgram(shader)
local modelL = gl.GetUniformLocation(shader, 'model')
local viewL = gl.GetUniformLocation(shader, 'view')
local projectionL = gl.GetUniformLocation(shader, 'projection')
gl.UniformMatrix4fv(modelL, false, model)
gl.UniformMatrix4fv(viewL, false, view)
gl.UniformMatrix4fv(projectionL, false, projection)
gl.BindVertexArray(vertexArray)
gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0)
window.swapBuffers(w)
window.pollEvents()
end
--===== shut down =====--
window.destroy(w)
gl.Terminate()
honey.audio.engine_uninit(engine)
|