summaryrefslogtreecommitdiff
path: root/src/gl/gl.c
blob: d0fb33c9d901e415324ec20a22d3b69fea3af09d (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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <stdlib.h>
#include <stdbool.h>
#include "gl/glad/glad.h"
#include <GLFW/glfw3.h>
#include <lua.h>
#include <honeysuckle.h>
#include "gl.h"

/* needs to be here because glad uses macros to define glBufferData */
#ifdef HONEY_TEST_H
#undef glBufferData
#define glBufferData mock_glBufferData_
#endif


int gl_init(lua_State *L);
int glad_init(lua_State *L);
int gl_terminate(lua_State *L);
int gl_get_error(lua_State *L);

/* buffers */
int gl_create_buffer(lua_State *L);
int gl_bind_buffer(lua_State *L);
int gl_buffer_data(lua_State *L);

int gl_vertex_array_create(lua_State *L);
int gl_vertex_array_bind(lua_State *L);
int gl_vertex_attrib_pointer(lua_State *L);
int gl_vertex_array_enable_attrib(lua_State *L);

/* drawing */
int gl_set_viewport(lua_State *L);
int gl_draw_arrays(lua_State *L);
int gl_set_clear_color(lua_State *L);
int gl_clear(lua_State *L);


void setup_gl(lua_State *L, int honey_index)
{
	int data_types = hs_create_table(L,
		hs_str_int("integer", GL_INT),
		hs_str_int("float", GL_FLOAT),
	);

	int error_types = hs_create_table(L,
		hs_str_int("noError", GL_NO_ERROR),
		hs_str_int("invalidEnum", GL_INVALID_ENUM),
		hs_str_int("invalidValue", GL_INVALID_VALUE),
		hs_str_int("invalidOperation", GL_INVALID_OPERATION),
		hs_str_int("invalidFramebufferOperation", GL_INVALID_FRAMEBUFFER_OPERATION),
		hs_str_int("outOfMemory", GL_OUT_OF_MEMORY),
	);

	int buffer_binding_targets = hs_create_table(L,
		hs_str_int("arrayBuffer", GL_ARRAY_BUFFER),
		hs_str_int("elementArrayBuffer", GL_ELEMENT_ARRAY_BUFFER),
	);

	int buffer_usage_patterns = hs_create_table(L,
		hs_str_int("streamDraw", GL_STREAM_DRAW),
		hs_str_int("staticDraw", GL_STATIC_DRAW),
		hs_str_int("dynamicDraw", GL_DYNAMIC_DRAW),
	);

	int primitive_types = hs_create_table(L,
		hs_str_int("points", GL_POINTS),
		hs_str_int("lines", GL_LINES),
		hs_str_int("triangles", GL_TRIANGLES),
	);

	int buffer_masks = hs_create_table(L,
		hs_str_int("colorBuffer", GL_COLOR_BUFFER_BIT),
		hs_str_int("depthBuffer", GL_DEPTH_BUFFER_BIT),
		hs_str_int("stencilBuffer", GL_STENCIL_BUFFER_BIT),
	);

	int gl_index = hs_create_table(L,
		hs_str_cfunc("init", gl_init),
		hs_str_cfunc("initGlad", glad_init),
		hs_str_cfunc("terminate", gl_terminate),
		hs_str_cfunc("getError", gl_get_error),

		hs_str_tbl("dataType", data_types),
		hs_str_tbl("errorType", error_types),

		/* buffer */
		hs_str_cfunc("createBuffer", gl_create_buffer),
		hs_str_cfunc("bindBuffer", gl_bind_buffer),
		hs_str_cfunc("bufferData", gl_buffer_data),

		hs_str_cfunc("createVertexArray", gl_vertex_array_create),
		hs_str_cfunc("bindVertexArray", gl_vertex_array_bind),
		hs_str_cfunc("vertexAttribPointer", gl_vertex_attrib_pointer),
		hs_str_cfunc("vertexArrayEnableAttrib", gl_vertex_array_enable_attrib),

		hs_str_tbl("bufferTarget", buffer_binding_targets),
		hs_str_tbl("bufferUsage", buffer_usage_patterns),

		/* drawing */
		hs_str_cfunc("drawArrays", gl_draw_arrays),
		hs_str_cfunc("setClearColor", gl_set_clear_color),
		hs_str_cfunc("clear", gl_clear),
		hs_str_cfunc("setViewport", gl_set_viewport),

		hs_str_tbl("primitiveType", primitive_types),
		hs_str_tbl("bufferMask", buffer_masks),
	);

	setup_shader(L, gl_index);
	lua_setfield(L, honey_index, "gl");
}


int gl_init(lua_State *L)
{
	if (!glfwInit()) {
		hs_throw_error(L, "failed to initialize GLFW");
	}
	return 0;
}


int glad_init(lua_State *L)
{
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		hs_throw_error(L, "failed to initialize GLAD");
	}
}


int gl_terminate(lua_State *L)
{
	glfwTerminate();
	return 0;
}


int gl_get_error(lua_State *L)
{
	lua_pushinteger(L, glGetError());
	return 1;
}


int gl_create_buffer(lua_State *L)
{
	int buf;
	glGenBuffers(1, &buf);
	lua_pushinteger(L, buf);
	return 1;
}


int gl_bind_buffer(lua_State *L)
{
	lua_Integer target, buf;
	hs_parse_args(L, hs_int(target), hs_int(buf));
	glBindBuffer(target, buf);
	return 0;
}


int gl_buffer_data(lua_State *L)
{
	lua_Integer target, type, usage;
	int table;
	hs_parse_args(L, hs_int(target), hs_int(type), hs_tbl(table), hs_int(usage));

	if (type != GL_INT && type != GL_FLOAT) {
		hs_throw_error(L, "invalid type");
	}

	/* build raw buffer */
	size_t len = lua_objlen(L, table);
	void *buf;
	if (type == GL_FLOAT) {
		float *fbuf = malloc(len * sizeof(float));
		if (fbuf == NULL)
			hs_throw_error(L, "failed to allocate intermediary fbuffer");
		for (int i=0; i<len; i++) {
			lua_rawgeti(L, table, i+1);
			if (!lua_isnumber(L, -1)) {
				hs_throw_error(L, "all table items must be numbers (failed at index %d)", i);
			}
			fbuf[i] = lua_tonumber(L, -1);
			lua_pop(L, 1);
		}
		len = len * sizeof(float);
		buf = fbuf;
	}
	else {
		int *ibuf = malloc(len * sizeof(int));
		if (ibuf == NULL)
			hs_throw_error(L, "failed to allocate intermediary ibuffer");
		for (int i=0; i<len; i++) {
			lua_rawgeti(L, table, i+1);
			if (!lua_isnumber(L, -1)) {
				hs_throw_error(L, "all table items must be integers (failed at index %d)", i);
			}
			ibuf[i] = lua_tointeger(L, -1);
			lua_pop(L, 1);
		}
		len = len * sizeof(int);
		buf = ibuf;
	}

	/* call */
	glBufferData(target, len, buf, usage);
	free(buf);
	return 0;
}


int gl_vertex_array_create(lua_State *L)
{
	int array;
	glGenVertexArrays(1, &array);
	lua_pushinteger(L, array);
	return 1;
}


int gl_vertex_array_bind(lua_State *L)
{
	lua_Integer array;
	hs_parse_args(L, hs_int(array));
	glBindVertexArray(array);
	return 0;
}


int gl_vertex_attrib_pointer(lua_State *L)
{
	lua_Integer index, size, stride, offset;
	bool normalized;
	hs_parse_args(L, hs_int(index), hs_int(size), hs_bool(normalized), hs_int(stride), hs_int(offset));
	glVertexAttribPointer(index, size, GL_FLOAT, 
	                      normalized, stride*sizeof(float), 
	                      (void*) (offset*sizeof(float)));
	return 0;
}


int gl_vertex_array_enable_attrib(lua_State *L)
{
	lua_Integer index;
	hs_parse_args(L, hs_int(index));
	glEnableVertexAttribArray(index);
	return 0;
}


int gl_set_clear_color(lua_State *L)
{
	lua_Number r, g, b, a;
	hs_parse_args(L, hs_num(r), hs_num(g), hs_num(b), hs_num(a));
	glClearColor(r, g, b, a);
	return 0;
}


int gl_clear(lua_State *L)
{
	lua_Integer mask;
	hs_parse_args(L, hs_int(mask));
	glClear(mask);
	return 0;
}


int gl_draw_arrays(lua_State *L)
{
	lua_Integer mode, first, count;
	hs_parse_args(L, hs_int(mode), hs_int(first), hs_int(count));
	glDrawArrays(mode, first, count);
	return 0;
}


int gl_set_viewport(lua_State *L)
{
	lua_Integer x, y, w, h;
	hs_parse_args(L, hs_int(x), hs_int(y), hs_int(w), hs_int(h));
	glViewport(x, y, w, h);
	return 0;
}