summaryrefslogtreecommitdiff
path: root/src/opengl/drawing.c
blob: e3a2d87b5feb881f58523cedfb5fe1e46cf68e36 (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
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <lua.h>
#include <lauxlib.h>
#include "util/util.h"

#define GL_DRAWING_FUNCTIONS \
	X("ClearColor", glClearColor_bind) \
	X("Clear", glClear_bind) \
	X("DrawArrays", glDrawArrays_bind) \
	X("DrawElements", glDrawElements_bind) \
	X("Viewport", glViewport_bind) \
	X("GenFramebuffers", glGenFramebuffers_bind) \
	X("BindFramebuffer", glBindFramebuffer_bind) \
	X("FramebufferTexture2D", glFramebufferTexture2D_bind) \
	X("CullFace", glCullFace_bind) \
	X("BlendFunc", glBlendFunc_bind) \
	X("PolygonMode", glPolygonMode_bind) \


#define X(name, func) int func(lua_State *L);
GL_DRAWING_FUNCTIONS
#undef X

void setup_drawing(lua_State *L, int gl_index)
{
	struct honey_tbl_t tbl[] = {
		/* functions */
		#define X(name, func) H_FUNC(name, func),
		GL_DRAWING_FUNCTIONS
		#undef X

		/******** enums ********/
		/* rendering primitives */
		H_INT("POINTS", GL_POINTS),
		H_INT("LINES", GL_LINES),
		H_INT("TRIANGLES", GL_TRIANGLES),

		/* clear bitmasks */
		H_INT("COLOR_BUFFER_BIT", GL_COLOR_BUFFER_BIT),
		H_INT("DEPTH_BUFFER_BIT", GL_DEPTH_BUFFER_BIT),
		H_INT("STENCIL_BUFFER_BIT", GL_STENCIL_BUFFER_BIT),

		H_INT("FRONT", GL_FRONT),
		H_INT("BACK", GL_BACK),
		H_INT("FRONT_AND_BACK", GL_FRONT_AND_BACK),

		/* BlendFunc factors */
		H_INT("ZERO", GL_ZERO),
		H_INT("ONE", GL_ONE),
		H_INT("SRC_COLOR", GL_SRC_COLOR),
		H_INT("ONE_MINUS_SRC_COLOR", GL_ONE_MINUS_SRC_COLOR),
		H_INT("DST_COLOR", GL_DST_COLOR),
		H_INT("ONE_MINUS_DST_COLOR", GL_ONE_MINUS_DST_COLOR),
		H_INT("SRC_ALPHA", GL_SRC_ALPHA),
		H_INT("ONE_MINUS_SRC_ALPHA", GL_ONE_MINUS_SRC_ALPHA),
		H_INT("DST_ALPHA", GL_DST_ALPHA),
		H_INT("ONE_MINUS_DST_ALPHA", GL_ONE_MINUS_DST_ALPHA), 
		H_INT("CONSTANT_COLOR", GL_CONSTANT_COLOR),
		H_INT("ONE_MINUS_CONSTANT_COLOR", GL_ONE_MINUS_CONSTANT_COLOR),
		H_INT("CONSTANT_ALPHA", GL_CONSTANT_ALPHA),
		H_INT("ONE_MINUS_CONSTANT_ALPHA", GL_ONE_MINUS_CONSTANT_ALPHA),

		/* PolygonMode faces & modes */
		H_INT("FRONT_AND_BACK", GL_FRONT_AND_BACK),
		H_INT("POINT", GL_POINT),
		H_INT("LINE", GL_LINE),
		H_INT("FILL", GL_FILL),

		H_INT("FRAMEBUFFER", GL_FRAMEBUFFER),

		H_END
	};
	create_table(L, tbl);
	append_table(L, gl_index, lua_gettop(L));
	lua_pop(L, 1);
}

int glClearColor_bind(lua_State *L)
{
	lua_Number r, g, b, a;
	r = luaL_checknumber(L, 1);
	g = luaL_checknumber(L, 2);
	b = luaL_checknumber(L, 3);
	a = luaL_checknumber(L, 4);
	glClearColor(r, g, b, a);
	return 0;
}


int glClear_bind(lua_State *L)
{
	lua_Integer mask = luaL_checkinteger(L, 1);
	glClear(mask);
	return 0;
}


int glDrawArrays_bind(lua_State *L)
{
	lua_Integer mode, first, count;
	mode = luaL_checkinteger(L, 1);
	first = luaL_checkinteger(L, 2);
	count = luaL_checkinteger(L, 3);
	glDrawArrays(mode, first, count);
	return 0;
}


int glDrawElements_bind(lua_State *L)
{
	lua_Integer mode, count, type, offset;
	mode = luaL_checkinteger(L, 1);
	count = luaL_checkinteger(L, 2);
	type = luaL_checkinteger(L, 3);
	offset = luaL_checkinteger(L, 4);
	glDrawElements(mode, count, type, (const void*)offset);
	return 0;
}


int glViewport_bind(lua_State *L)
{
	lua_Integer x, y, w, h;
	x = luaL_checkinteger(L, 1);
	y = luaL_checkinteger(L, 2);
	w = luaL_checkinteger(L, 3);
	h = luaL_checkinteger(L, 4);
	glViewport(x, y, w, h);
	return 0;
}


int glGenFramebuffers_bind(lua_State *L)
{
	int framebuffer;
	glGenFramebuffers(1, &framebuffer);
	lua_pushinteger(L, framebuffer);
	return 1;
}


int glBindFramebuffer_bind(lua_State *L)
{
	int target = luaL_checkinteger(L, 1);
	int framebuffer = luaL_checkinteger(L, 2);
	glBindFramebuffer(target, framebuffer);
	return 0;
}


int glFramebufferTexture2D_bind(lua_State *L)
{
	int target = luaL_checkinteger(L, 1);
	int attachment = luaL_checkinteger(L, 2);
	int textarget = luaL_checkinteger(L, 3);
	int texture = luaL_checkinteger(L, 4);
	int level = luaL_checkinteger(L, 5);

	glFramebufferTexture2D(target, attachment, textarget, texture, level);
	return 0;
}


int glCullFace_bind(lua_State *L)
{
	int mode = luaL_checkinteger(L, 1);
	glCullFace(mode);
	return 0;
}


int glBlendFunc_bind(lua_State *L)
{
	int sfactor = luaL_checkinteger(L, 1);
	int dfactor = luaL_checkinteger(L, 2);
	glBlendFunc(sfactor, dfactor);
	return 0;
}


int glPolygonMode_bind(lua_State *L)
{
	int face = luaL_checkinteger(L, 1);
	int mode = luaL_checkinteger(L, 2);
	glPolygonMode(face, mode);
	return 0;
}