util_view.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "util/util_opengl.h"
  19. #include "util/util_string.h"
  20. #include "util/util_time.h"
  21. #include "util/util_version.h"
  22. #include "util/util_view.h"
  23. #ifdef __APPLE__
  24. # include <GLUT/glut.h>
  25. #else
  26. # include <GL/glut.h>
  27. #endif
  28. CCL_NAMESPACE_BEGIN
  29. /* structs */
  30. struct View {
  31. ViewInitFunc initf;
  32. ViewExitFunc exitf;
  33. ViewResizeFunc resize;
  34. ViewDisplayFunc display;
  35. ViewKeyboardFunc keyboard;
  36. ViewMotionFunc motion;
  37. bool first_display;
  38. bool redraw;
  39. int mouseX, mouseY;
  40. int mouseBut0, mouseBut2;
  41. int width, height;
  42. } V;
  43. /* public */
  44. static void view_display_text(int x, int y, const char *text)
  45. {
  46. const char *c;
  47. glRasterPos3f(x, y, 0);
  48. for (c = text; *c != '\0'; c++)
  49. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *c);
  50. }
  51. void view_display_info(const char *info)
  52. {
  53. const int height = 20;
  54. glEnable(GL_BLEND);
  55. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  56. glColor4f(0.1f, 0.1f, 0.1f, 0.8f);
  57. glRectf(0.0f, V.height - height, V.width, V.height);
  58. glDisable(GL_BLEND);
  59. glColor3f(0.5f, 0.5f, 0.5f);
  60. view_display_text(10, 7 + V.height - height, info);
  61. glColor3f(1.0f, 1.0f, 1.0f);
  62. }
  63. void view_display_help()
  64. {
  65. const int w = (int)((float)V.width / 1.15f);
  66. const int h = (int)((float)V.height / 1.15f);
  67. const int x1 = (V.width - w) / 2;
  68. const int x2 = x1 + w;
  69. const int y1 = (V.height - h) / 2;
  70. const int y2 = y1 + h;
  71. glEnable(GL_BLEND);
  72. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  73. glColor4f(0.5f, 0.5f, 0.5f, 0.8f);
  74. glRectf(x1, y1, x2, y2);
  75. glDisable(GL_BLEND);
  76. glColor3f(0.8f, 0.8f, 0.8f);
  77. string info = string("Cycles Renderer ") + CYCLES_VERSION_STRING;
  78. view_display_text(x1 + 20, y2 - 20, info.c_str());
  79. view_display_text(x1 + 20, y2 - 40, "(C) 2011-2016 Blender Foundation");
  80. view_display_text(x1 + 20, y2 - 80, "Controls:");
  81. view_display_text(x1 + 20, y2 - 100, "h: Info/Help");
  82. view_display_text(x1 + 20, y2 - 120, "r: Reset");
  83. view_display_text(x1 + 20, y2 - 140, "p: Pause");
  84. view_display_text(x1 + 20, y2 - 160, "esc: Cancel");
  85. view_display_text(x1 + 20, y2 - 180, "q: Quit program");
  86. view_display_text(x1 + 20, y2 - 210, "i: Interactive mode");
  87. view_display_text(x1 + 20, y2 - 230, "Left mouse: Move camera");
  88. view_display_text(x1 + 20, y2 - 250, "Right mouse: Rotate camera");
  89. view_display_text(x1 + 20, y2 - 270, "W/A/S/D: Move camera");
  90. view_display_text(x1 + 20, y2 - 290, "0/1/2/3: Set max bounces");
  91. glColor3f(1.0f, 1.0f, 1.0f);
  92. }
  93. static void view_display()
  94. {
  95. if (V.first_display) {
  96. if (V.initf)
  97. V.initf();
  98. if (V.exitf)
  99. atexit(V.exitf);
  100. V.first_display = false;
  101. }
  102. glClearColor(0.05f, 0.05f, 0.05f, 0.0f);
  103. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  104. glMatrixMode(GL_PROJECTION);
  105. glLoadIdentity();
  106. gluOrtho2D(0, V.width, 0, V.height);
  107. glMatrixMode(GL_MODELVIEW);
  108. glLoadIdentity();
  109. glRasterPos3f(0, 0, 0);
  110. if (V.display)
  111. V.display();
  112. glutSwapBuffers();
  113. }
  114. static void view_reshape(int width, int height)
  115. {
  116. if (width <= 0 || height <= 0)
  117. return;
  118. V.width = width;
  119. V.height = height;
  120. glViewport(0, 0, width, height);
  121. glMatrixMode(GL_PROJECTION);
  122. glLoadIdentity();
  123. glMatrixMode(GL_MODELVIEW);
  124. glLoadIdentity();
  125. if (V.resize)
  126. V.resize(width, height);
  127. }
  128. static void view_keyboard(unsigned char key, int x, int y)
  129. {
  130. if (V.keyboard)
  131. V.keyboard(key);
  132. if (key == 'm')
  133. printf("mouse %d %d\n", x, y);
  134. if (key == 'q') {
  135. if (V.exitf)
  136. V.exitf();
  137. exit(0);
  138. }
  139. }
  140. static void view_mouse(int button, int state, int x, int y)
  141. {
  142. if (button == 0) {
  143. if (state == GLUT_DOWN) {
  144. V.mouseX = x;
  145. V.mouseY = y;
  146. V.mouseBut0 = 1;
  147. }
  148. else if (state == GLUT_UP) {
  149. V.mouseBut0 = 0;
  150. }
  151. }
  152. else if (button == 2) {
  153. if (state == GLUT_DOWN) {
  154. V.mouseX = x;
  155. V.mouseY = y;
  156. V.mouseBut2 = 1;
  157. }
  158. else if (state == GLUT_UP) {
  159. V.mouseBut2 = 0;
  160. }
  161. }
  162. }
  163. static void view_motion(int x, int y)
  164. {
  165. const int but = V.mouseBut0 ? 0 : 2;
  166. const int distX = x - V.mouseX;
  167. const int distY = y - V.mouseY;
  168. if (V.motion)
  169. V.motion(distX, distY, but);
  170. V.mouseX = x;
  171. V.mouseY = y;
  172. }
  173. static void view_idle()
  174. {
  175. if (V.redraw) {
  176. V.redraw = false;
  177. glutPostRedisplay();
  178. }
  179. time_sleep(0.1);
  180. }
  181. void view_main_loop(const char *title,
  182. int width,
  183. int height,
  184. ViewInitFunc initf,
  185. ViewExitFunc exitf,
  186. ViewResizeFunc resize,
  187. ViewDisplayFunc display,
  188. ViewKeyboardFunc keyboard,
  189. ViewMotionFunc motion)
  190. {
  191. const char *name = "app";
  192. char *argv = (char *)name;
  193. int argc = 1;
  194. memset(&V, 0, sizeof(V));
  195. V.width = width;
  196. V.height = height;
  197. V.first_display = true;
  198. V.redraw = false;
  199. V.initf = initf;
  200. V.exitf = exitf;
  201. V.resize = resize;
  202. V.display = display;
  203. V.keyboard = keyboard;
  204. V.motion = motion;
  205. glutInit(&argc, &argv);
  206. glutInitWindowSize(width, height);
  207. glutInitWindowPosition(0, 0);
  208. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  209. glutCreateWindow(title);
  210. glewInit();
  211. view_reshape(width, height);
  212. glutDisplayFunc(view_display);
  213. glutIdleFunc(view_idle);
  214. glutReshapeFunc(view_reshape);
  215. glutKeyboardFunc(view_keyboard);
  216. glutMouseFunc(view_mouse);
  217. glutMotionFunc(view_motion);
  218. glutMainLoop();
  219. }
  220. void view_redraw()
  221. {
  222. V.redraw = true;
  223. }
  224. CCL_NAMESPACE_END