glut.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * m3dview/glut.c
  3. *
  4. * Copyright (C) 2019 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief GLUT interface for the simple portable Model 3D viewer
  27. * https://gitlab.com/bztsrc/model3d
  28. *
  29. */
  30. #include "viewer.h"
  31. #include <glut.h>
  32. #include <gl.h>
  33. int px = 0, py = 0;
  34. /* window resize event callback */
  35. void glut_reshape(int w, int h)
  36. {
  37. screenw = w;
  38. screenh = h;
  39. glViewport(0, 0, w, h);
  40. }
  41. /* render scene callback */
  42. void glut_display(void)
  43. {
  44. display(glutGet(GLUT_ELAPSED_TIME));
  45. glutSwapBuffers();
  46. glutPostRedisplay();
  47. }
  48. /* mouse button press event callback */
  49. void glut_mouse(int button, int state, int x, int y)
  50. {
  51. int s = state == GLUT_DOWN;
  52. if (button == GLUT_LEFT_BUTTON) mousebtn = s;
  53. if (s && button == GLUT_MIDDLE_BUTTON) zoomin();
  54. if (s && button == GLUT_RIGHT_BUTTON) zoomout();
  55. if(px && py) {
  56. mousex = x - px;
  57. mousey = y - py;
  58. }
  59. px = x; py = y;
  60. glutPostRedisplay();
  61. }
  62. /* mouse motion event callback */
  63. void glut_motion(int x, int y)
  64. {
  65. mousex = x - px;
  66. mousey = y - py;
  67. px = x; py = y;
  68. mousemove = 1;
  69. glutPostRedisplay();
  70. }
  71. /* normal key event callback */
  72. void glut_keyboard(unsigned char key, __attribute__((unused)) int x, __attribute__((unused)) int y)
  73. {
  74. switch (key) {
  75. case 27: case 'q': exit(1); break;
  76. case 9: actionid++; break;
  77. case '+': case '=': zoomin(); break;
  78. case '-': zoomout(); break;
  79. case ',': prevframe(); break;
  80. case '.': nextframe(); break;
  81. case ' ': continous(); break;
  82. case 'm': domesh ^= 1; break;
  83. case 's': doskel ^= 1; break;
  84. case '0':
  85. case '1':
  86. case '2':
  87. case '3':
  88. case '4':
  89. case '5':
  90. case '6':
  91. case '7':
  92. case '8':
  93. case '9': fpsdiv(key-'0'); break;
  94. }
  95. glutPostRedisplay();
  96. }
  97. /* special key event callback */
  98. void glut_special(int key, __attribute__((unused)) int x, __attribute__((unused)) int y)
  99. {
  100. switch (key) {
  101. case GLUT_KEY_F4: exit(1); break;
  102. case GLUT_KEY_UP: mousez++; break;
  103. case GLUT_KEY_DOWN: mousez--; break;
  104. case GLUT_KEY_LEFT: mousex = -10; mousey = 0; mousemove = 1; break;
  105. case GLUT_KEY_RIGHT: mousex = 10; mousey = 0; mousemove = 1;; break;
  106. case GLUT_KEY_PAGE_UP: actionid--; break;
  107. case GLUT_KEY_PAGE_DOWN: actionid++; break;
  108. }
  109. glutPostRedisplay();
  110. }
  111. /**
  112. * Set window title
  113. */
  114. void set_title(char *title)
  115. {
  116. glutSetWindowTitle(title);
  117. }
  118. /**
  119. * Main procedure. Set up and main loop
  120. */
  121. int main(int argc, char **argv)
  122. {
  123. load(argc, argv);
  124. glutInitWindowPosition(50, 50);
  125. glutInitWindowSize(screenw, screenh);
  126. glutInit(&argc, argv);
  127. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  128. glutCreateWindow(wintitle);
  129. screenw = glutGet(GLUT_WINDOW_WIDTH);
  130. screenh = glutGet(GLUT_WINDOW_HEIGHT);
  131. glutReshapeFunc(glut_reshape);
  132. glutDisplayFunc(glut_display);
  133. glutMouseFunc(glut_mouse);
  134. glutMotionFunc(glut_motion);
  135. glutKeyboardFunc(glut_keyboard);
  136. glutSpecialFunc(glut_special);
  137. setupgl();
  138. glutMainLoop();
  139. cleanup();
  140. return 0;
  141. }