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


int gl_texture_create(lua_State *L);
int gl_texture_bind(lua_State *L);
int gl_texture_image_2d(lua_State *L);
int gl_texture_generate_mipmaps(lua_State *L);
int gl_texture_set_active(lua_State *L);
int gl_tex_parameter_i(lua_State *L);


void setup_texture(lua_State *L, int gl_index)
{
	struct honey_tbl_t tbl[] = {
		/* functions */
		H_FUNC("GenTextures", gl_texture_create),
		H_FUNC("BindTexture", gl_texture_bind),
		H_FUNC("TexImage2D", gl_texture_image_2d),
		H_FUNC("GenerateMipmap", gl_texture_generate_mipmaps),
		H_FUNC("ActiveTexture", gl_texture_set_active),
		H_FUNC("TexParameteri", gl_tex_parameter_i),

		/******** enums ********/
		/* texture binding targets */
		H_INT("TEXTURE_2D", GL_TEXTURE_2D),

		/* texture data formats */
		H_INT("RGB", GL_RGB),
		H_INT("RGBA", GL_RGBA),

		/* texture parameters */
		H_INT("TEXTURE_WRAP_S", GL_TEXTURE_WRAP_S),
		H_INT("TEXTURE_WRAP_T", GL_TEXTURE_WRAP_T),
		H_INT("TEXTURE_MIN_FILTER", GL_TEXTURE_MIN_FILTER),
		H_INT("TEXTURE_MAG_FILTER", GL_TEXTURE_MAG_FILTER),
		H_INT("TEXTURE_SWIZZLE_R", GL_TEXTURE_SWIZZLE_R),
		H_INT("TEXTURE_SWIZZLE_G", GL_TEXTURE_SWIZZLE_G),
		H_INT("TEXTURE_SWIZZLE_B", GL_TEXTURE_SWIZZLE_B),
		H_INT("TEXTURE_SWIZZLE_A", GL_TEXTURE_SWIZZLE_A),

		/* wrapping types */
		H_INT("REPEAT", GL_REPEAT),

		/* filter types */
		H_INT("NEAREST", GL_NEAREST),
		H_INT("LINEAR", GL_LINEAR),

		/* swizzle targets */
		H_INT("RED", GL_RED),
		H_INT("GREEN", GL_GREEN),
		H_INT("BLUE", GL_BLUE),
		H_INT("ALPHA", GL_ALPHA),
		H_INT("ZERO", GL_ZERO),
		H_INT("ONE", GL_ONE),

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


int gl_texture_create(lua_State *L)
{
	unsigned int texture;
	glGenTextures(1, &texture);
	lua_pushinteger(L, texture);
	return 1;
}


int gl_texture_bind(lua_State *L)
{
	lua_Integer target, texture;
	target = luaL_checkinteger(L, 1);
	texture = luaL_checkinteger(L, 2);
	glBindTexture(target, texture);
	return 0;
}


int gl_texture_image_2d(lua_State *L)
{
	lua_Integer target, mipmap_level,
	            internal_format,
	            width, height,
	            format, type;
	target = luaL_checkinteger(L, 1);
	mipmap_level = luaL_checkinteger(L, 2);
	internal_format = luaL_checkinteger(L, 3);
	width = luaL_checkinteger(L, 4);
	height = luaL_checkinteger(L, 5);
	format = luaL_checkinteger(L, 6);
	type = luaL_checkinteger(L, 7);
	void *data = lua_touserdata(L, 8);

	glTexImage2D(target, mipmap_level, internal_format, width, height, 0, format, type, data);
	return 0;
}


int gl_texture_generate_mipmaps(lua_State *L)
{
	lua_Integer target = luaL_checkinteger(L, 1);
	glGenerateMipmap(target);
	return 0;
}


int gl_texture_set_active(lua_State *L)
{
	lua_Integer unit = luaL_checkinteger(L, 1);
	glActiveTexture(GL_TEXTURE0 + unit);
	return 0;
}


int gl_tex_parameter_i(lua_State *L)
{
	lua_Integer target, pname, param;
	target = luaL_checkinteger(L, 1);
	pname = luaL_checkinteger(L, 2);
	param = luaL_checkinteger(L, 3);
	glTexParameteri(target, pname, param);
	return 0;
}