1 extern "C"
2 {
3 #include "lua.h"
4 #include "lauxlib.h"
5 #include "lualib.h"
6 }
7
8 #include <luabind/luabind.hpp>
9 #include <luabind/class.hpp>
10 #include <luabind/function.hpp>
11 #include <luabind/object.hpp>
12
13
14 #include <GL/glut.h>
15 #include <GL/gl.h>
16 #include <GL/glu.h>
17
18 struct glut_constants {};
19 struct gl_constants {};
20
21 using luabind::object;
22
23 namespace glut_bindings
24 {
25 object displayfunc;
26
displayfunc_callback()27 void displayfunc_callback()
28 {
29 displayfunc();
30 }
31
set_displayfunc(object const & fun)32 void set_displayfunc(object const& fun)
33 {
34 glutDisplayFunc(&displayfunc_callback);
35 displayfunc = fun;
36 }
37
38 object idlefunc;
39
idlefunc_callback()40 void idlefunc_callback()
41 {
42 idlefunc();
43 }
44
set_idlefunc(object const & fun)45 void set_idlefunc(object const& fun)
46 {
47 glutIdleFunc(&idlefunc_callback);
48 idlefunc = fun;
49 }
50
51
52 object reshapefunc;
53
reshapefunc_callback(int w,int h)54 void reshapefunc_callback(int w, int h)
55 {
56 reshapefunc(w, h);
57 }
58
set_reshapefunc(object const & fun)59 void set_reshapefunc(object const& fun)
60 {
61 reshapefunc = fun;
62 }
63
64 object keyboardfunc;
65
keyboardfunc_callback(unsigned char key,int x,int y)66 void keyboardfunc_callback(unsigned char key, int x, int y)
67 {
68 keyboardfunc(key, x, y);
69 }
70
set_keyboardfunc(object const & fun)71 void set_keyboardfunc(object const& fun)
72 {
73 glutKeyboardFunc(&keyboardfunc_callback);
74 keyboardfunc = fun;
75 }
76
77 object mousefunc;
78
mousefunc_callback(int button,int state,int x,int y)79 void mousefunc_callback(int button, int state, int x, int y)
80 {
81 mousefunc(button, state, x, y);
82 }
83
set_mousefunc(object const & fun)84 void set_mousefunc(object const& fun)
85 {
86 mousefunc = fun;
87 }
88 }
89
bind_glut(lua_State * L)90 void bind_glut(lua_State* L)
91 {
92 using namespace luabind;
93 using namespace glut_bindings;
94
95 open(L);
96
97 module(L)
98 [
99 def("glutInitWindowSize", &glutInitWindowSize),
100 def("glutInitWindowPosition", &glutInitWindowPosition),
101 def("glutInitDisplayMode", &glutInitDisplayMode),
102
103 class_<glut_constants>("glut")
104 .enum_("constants")
105 [
106 value("RGB", GLUT_RGB),
107 value("RGBA", GLUT_RGBA),
108 value("INDEX", GLUT_INDEX),
109 value("SINGLE", GLUT_SINGLE),
110 value("DOUBLE", GLUT_DOUBLE),
111 value("DEPTH", GLUT_DEPTH),
112 value("STENCIL", GLUT_STENCIL),
113 value("LEFT_BUTTON", GLUT_LEFT_BUTTON),
114 value("MIDDLE_BUTTON", GLUT_MIDDLE_BUTTON),
115 value("RIGHT_BUTTON", GLUT_RIGHT_BUTTON),
116 value("UP", GLUT_UP),
117 value("DOWN", GLUT_DOWN),
118 value("ELAPSED_TIME", GLUT_ELAPSED_TIME)
119 ],
120
121 def("glutCreateWindow", &glutCreateWindow),
122 def("glutDestroyWindow", &glutDestroyWindow),
123 def("glutFullScreen", &glutFullScreen),
124 def("glutDisplayFunc", &set_displayfunc),
125 def("glutKeyboardFunc", &set_keyboardfunc),
126 def("glutReshapeFunc", &set_reshapefunc),
127 def("glutIdleFunc", &set_idlefunc),
128 def("glutMainLoop", &glutMainLoop),
129 def("glutSwapBuffers", &glutSwapBuffers),
130 def("glutGet", &glutGet),
131 def("glutSolidSphere", &glutSolidSphere),
132 def("glutWireSphere", &glutWireSphere),
133 def("glutWireTeapot", &glutWireTeapot),
134 def("glutSolidTeapot", &glutSolidTeapot),
135
136 // -- opengl
137
138 class_<gl_constants>("gl")
139 .enum_("constants")
140 [
141 value("COLOR_BUFFER_BIT", GL_COLOR_BUFFER_BIT),
142 value("DEPTH_BUFFER_BIT", GL_DEPTH_BUFFER_BIT),
143 value("TRIANGLES", GL_TRIANGLES),
144 value("MODELVIEW", GL_MODELVIEW),
145 value("PROJECTION", GL_PROJECTION)
146 ],
147
148 def("glBegin", &glBegin),
149 def("glVertex3", &glVertex3f),
150 def("glEnd", &glEnd),
151 def("glClear", &glClear),
152 def("glPushMatrix", &glPushMatrix),
153 def("glPopMatrix", &glPopMatrix),
154 def("glRotate", &glRotatef),
155 def("glColor3", &glColor3f),
156 def("glColor4", &glColor4f),
157 def("glMatrixMode", &glMatrixMode),
158 def("glLoadIdentity", &glLoadIdentity),
159 def("glViewport", &glViewport),
160 def("glTranslate", &glTranslatef),
161
162 // -- glu
163
164 def("gluPerspective", &gluPerspective)
165 ];
166 }
167
main(int argc,char * argv[])168 int main(int argc, char* argv[])
169 {
170 lua_State* L = lua_open();
171 lua_baselibopen(L);
172 lua_mathlibopen(L);
173 bind_glut(L);
174
175 glutInit (&argc, argv);
176
177 lua_dofile(L, "glut_bindings.lua");
178
179 lua_close(L);
180 return 0;
181 }
182
183