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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
#include "shader.h"
int honey_shader_mt_ref = LUA_NOREF;
void honey_setup_shader(lua_State* L)
{
honey_lua_create_table
(L, 2,
HONEY_TABLE, "__index", 7,
/* honey.shader.prototype */
HONEY_FUNCTION, "use", honey_shader_use,
HONEY_FUNCTION, "setInteger", honey_shader_set_int,
HONEY_FUNCTION, "setFloat", honey_shader_set_float,
HONEY_FUNCTION, "setVec3", honey_shader_set_vec3,
HONEY_FUNCTION, "setVec4", honey_shader_set_vec4,
HONEY_FUNCTION, "setMat3", honey_shader_set_mat3,
HONEY_FUNCTION, "setMat4", honey_shader_set_mat4,
HONEY_FUNCTION, "__gc", honey_shader_delete);
honey_shader_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, LUA_REGISTRYINDEX, honey_shader_mt_ref);
lua_setfield(L, -2, "shaderMetatable");
lua_pushcfunction(L, honey_shader_new);
lua_setfield(L, -2, "shader");
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_shader_new(lua_State* L)
{
char* vertex_shader_source, *fragment_shader_source;
honey_lua_parse_arguments
(L, 1,
2, HONEY_STRING, &vertex_shader_source, HONEY_STRING, &fragment_shader_source);
int success;
char error[1024];
int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1,
(const char* const*) &vertex_shader_source, NULL);
glCompileShader(vertex_shader);
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertex_shader, 1024, NULL, error);
honey_lua_throw_error(L, "error compiling vertex shader: %s",
error);
}
int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1,
(const char* const*) &fragment_shader_source, NULL);
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragment_shader, 1024, NULL, error);
honey_lua_throw_error(L, "error compiling fragment shader: %s",
error);
}
int program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glGetShaderiv(program, GL_LINK_STATUS, &success);
if (!success) {
glGetShaderInfoLog(program, 1024, NULL, error);
honey_lua_throw_error(L, "error linking shader program: %s",
error);
}
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
int* shader = lua_newuserdata(L, sizeof(int));
*shader = program;
if (honey_shader_mt_ref == LUA_NOREF ||
honey_shader_mt_ref == LUA_REFNIL)
honey_lua_throw_error
(L, "cannot create shader as there is no shader metatable set up.");
lua_rawgeti(L, LUA_REGISTRYINDEX, honey_shader_mt_ref);
lua_setmetatable(L, -2);
return 1;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_shader_use(lua_State* L)
{
int *shader;
honey_lua_parse_arguments
(L, 1,
1, HONEY_USERDATA, &shader);
glUseProgram(*shader);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_shader_set_int(lua_State* L)
{
int *shader, value;
char* name;
honey_lua_parse_arguments
(L, 1,
3,
HONEY_USERDATA, &shader,
HONEY_STRING, &name,
HONEY_INTEGER, &value);
glUseProgram(*shader);
unsigned int location = glGetUniformLocation(*shader, name);
glUniform1i(location, value);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_shader_set_float(lua_State* L)
{
int *shader; char* name; float value;
honey_lua_parse_arguments
(L, 1, 3,
HONEY_USERDATA, &shader,
HONEY_STRING, &name,
HONEY_NUMBER, &value);
glUseProgram(*shader);
unsigned int location = glGetUniformLocation(*shader, name);
glUniform1f(location, value);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_shader_set_vec3(lua_State* L)
{
int *shader; char* name; honey_glm_array* array;
honey_lua_parse_arguments
(L, 1, 3,
HONEY_USERDATA, &shader,
HONEY_STRING, &name,
HONEY_USERDATA, &array);
if (array->type != VEC3)
honey_lua_throw_error(L,
"expected glm array of type VEC3 (%d), but got %d instead",
VEC3,
array->type);
glUseProgram(*shader);
unsigned int location = glGetUniformLocation(*shader, name);
glUniform3fv(location, 1, array->data);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_shader_set_vec4(lua_State* L)
{
int *shader; char* name; honey_glm_array* array;
honey_lua_parse_arguments
(L, 1, 3,
HONEY_USERDATA, &shader,
HONEY_STRING, &name,
HONEY_USERDATA, &array);
if (array->type != VEC4)
honey_lua_throw_error(L,
"expected glm array of type VEC4 (%d), but got %d instead",
VEC4,
array->type);
glUseProgram(*shader);
unsigned int location = glGetUniformLocation(*shader, name);
glUniform4fv(location, 1, array->data);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_shader_set_mat3(lua_State* L)
{
int *shader; char* name; honey_glm_array* array;
honey_lua_parse_arguments
(L, 1, 3,
HONEY_USERDATA, &shader,
HONEY_STRING, &name,
HONEY_USERDATA, &array);
if (array->type != MAT3)
honey_lua_throw_error(L,
"expected glm array of type MAT3 (%d), but got %d instead",
MAT3,
array->type);
glUseProgram(*shader);
unsigned int location = glGetUniformLocation(*shader, name);
glUniformMatrix3fv(location, 1, GL_FALSE, array->data);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_shader_set_mat4(lua_State* L)
{
int *shader; char* name; honey_glm_array* array;
honey_lua_parse_arguments
(L, 1, 3,
HONEY_USERDATA, &shader,
HONEY_STRING, &name,
HONEY_USERDATA, &array);
if (array->type != MAT4)
honey_lua_throw_error(L,
"expected glm array of type MAT4 (%d), but got %d instead",
MAT4,
array->type);
glUseProgram(*shader);
unsigned int location = glGetUniformLocation(*shader, name);
glUniformMatrix4fv(location, 1, GL_FALSE, array->data);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_shader_delete(lua_State* L)
{
int *shader;
honey_lua_parse_arguments
(L, 1, 1, HONEY_USERDATA, &shader);
glDeleteProgram(*shader);
return 0;
}
|