File: glut_bind.cpp

package info (click to toggle)
luabind 0.9.1%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,844 kB
  • ctags: 2,833
  • sloc: cpp: 13,796; makefile: 126; sh: 24; ansic: 11
file content (182 lines) | stat: -rw-r--r-- 3,712 bytes parent folder | download | duplicates (2)
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
extern "C"
{
	#include "lua.h"
	#include "lauxlib.h"
	#include "lualib.h"
}

#include <luabind/luabind.hpp>
#include <luabind/class.hpp>
#include <luabind/function.hpp>
#include <luabind/object.hpp>


#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>

struct glut_constants {};
struct gl_constants {};

using luabind::object;

namespace glut_bindings
{
	object displayfunc;

	void displayfunc_callback()
	{
		displayfunc();
	}

	void set_displayfunc(object const& fun)
	{
		glutDisplayFunc(&displayfunc_callback);
		displayfunc = fun;
	}

	object idlefunc;

	void idlefunc_callback()
	{
		idlefunc();
	}

	void set_idlefunc(object const& fun)
	{
		glutIdleFunc(&idlefunc_callback);
		idlefunc = fun;
	}


	object reshapefunc;

	void reshapefunc_callback(int w, int h)
	{
		reshapefunc(w, h);
	}

	void set_reshapefunc(object const& fun)
	{
		reshapefunc = fun;
	}

	object keyboardfunc;

	void keyboardfunc_callback(unsigned char key, int x, int y)
	{
		keyboardfunc(key, x, y);
	}

	void set_keyboardfunc(object const& fun)
	{
		glutKeyboardFunc(&keyboardfunc_callback);
		keyboardfunc = fun;
	}

	object mousefunc;

	void mousefunc_callback(int button, int state, int x, int y)
	{
		mousefunc(button, state, x, y);
	}

	void set_mousefunc(object const& fun)
	{
		mousefunc = fun;
	}
}

void bind_glut(lua_State* L)
{
	using namespace luabind;
	using namespace glut_bindings;

	open(L);

	module(L)
	[	
		def("glutInitWindowSize", &glutInitWindowSize),
		def("glutInitWindowPosition", &glutInitWindowPosition),
		def("glutInitDisplayMode", &glutInitDisplayMode),

		class_<glut_constants>("glut")
			.enum_("constants")
			[
				value("RGB", GLUT_RGB),
				value("RGBA", GLUT_RGBA),
				value("INDEX", GLUT_INDEX),
				value("SINGLE", GLUT_SINGLE),
				value("DOUBLE", GLUT_DOUBLE),
				value("DEPTH", GLUT_DEPTH),
				value("STENCIL", GLUT_STENCIL),
				value("LEFT_BUTTON", GLUT_LEFT_BUTTON),
				value("MIDDLE_BUTTON", GLUT_MIDDLE_BUTTON),
				value("RIGHT_BUTTON", GLUT_RIGHT_BUTTON),
				value("UP", GLUT_UP),
				value("DOWN", GLUT_DOWN),
				value("ELAPSED_TIME", GLUT_ELAPSED_TIME)
			],

		def("glutCreateWindow", &glutCreateWindow),
		def("glutDestroyWindow", &glutDestroyWindow),
		def("glutFullScreen", &glutFullScreen),
		def("glutDisplayFunc", &set_displayfunc),
		def("glutKeyboardFunc", &set_keyboardfunc),
		def("glutReshapeFunc", &set_reshapefunc),
		def("glutIdleFunc", &set_idlefunc),
		def("glutMainLoop", &glutMainLoop),
		def("glutSwapBuffers", &glutSwapBuffers),
		def("glutGet", &glutGet),
		def("glutSolidSphere", &glutSolidSphere),
		def("glutWireSphere", &glutWireSphere),
		def("glutWireTeapot", &glutWireTeapot),
		def("glutSolidTeapot", &glutSolidTeapot),

		// -- opengl

		class_<gl_constants>("gl")
			.enum_("constants")
			[
				value("COLOR_BUFFER_BIT", GL_COLOR_BUFFER_BIT),
				value("DEPTH_BUFFER_BIT", GL_DEPTH_BUFFER_BIT),
				value("TRIANGLES", GL_TRIANGLES),
				value("MODELVIEW", GL_MODELVIEW),
				value("PROJECTION", GL_PROJECTION)
			],

		def("glBegin", &glBegin),
		def("glVertex3", &glVertex3f),
		def("glEnd", &glEnd),
		def("glClear", &glClear),
		def("glPushMatrix", &glPushMatrix),
		def("glPopMatrix", &glPopMatrix),
		def("glRotate", &glRotatef),
		def("glColor3", &glColor3f),
		def("glColor4", &glColor4f),
		def("glMatrixMode", &glMatrixMode),
		def("glLoadIdentity", &glLoadIdentity),
		def("glViewport", &glViewport),
		def("glTranslate", &glTranslatef),

		// -- glu

		def("gluPerspective", &gluPerspective)
	];
}

int main(int argc, char* argv[])
{
	lua_State* L = luaL_newstate();
	lua_baselibopen(L);
	lua_mathlibopen(L);
	bind_glut(L);

	glutInit (&argc, argv);

	lua_dofile(L, "./glut_bindings.lua");

	lua_close(L);
	return 0;
}