glut_bind.cpp 3.6 KB

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