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

int gl_set_viewport(lua_State *L);
int gl_draw_arrays(lua_State *L);
int gl_draw_elements(lua_State *L);
int gl_set_clear_color(lua_State *L);
int gl_clear(lua_State *L);
int gl_gen_framebuffers(lua_State *L);
int gl_bind_framebuffer(lua_State *L);
int gl_framebuffer_texture_2d(lua_State *L);

void setup_drawing(lua_State *L, int gl_index)
{
	int tbl = hs_create_table(L,
		/* functions */
		hs_str_cfunc("DrawArrays", gl_draw_arrays),
		hs_str_cfunc("DrawElements", gl_draw_elements),
		hs_str_cfunc("ClearColor", gl_set_clear_color),
		hs_str_cfunc("Clear", gl_clear),
		hs_str_cfunc("Viewport", gl_set_viewport),

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

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

	append_table(L, gl_index, tbl);
	lua_pop(L, 1);
}

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_draw_elements(lua_State *L)
{
	lua_Integer mode, count, type, offset;
	hs_parse_args(L, hs_int(mode), hs_int(count), hs_int(type), hs_int(offset));
	glDrawElements(mode, count, type, (const void*)offset);
	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;
}


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


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


int gl_framebuffer_texture_2d(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;
}