glutrender.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include "glutrender.h"
  11. #include "viewport.h"
  12. #include "arcball.h"
  13. #include "appmouse.h"
  14. #include "glexpose.h"
  15. #include "glcompui.h"
  16. #include <util/exit.h>
  17. /*call backs */
  18. static float begin_x = 0.0;
  19. static float begin_y = 0.0;
  20. static float dx = 0.0;
  21. static float dy = 0.0;
  22. /*mouse mode mapping funvtion from glut to glcomp*/
  23. static glMouseButtonType getGlCompMouseType(int n)
  24. {
  25. switch (n) {
  26. case GLUT_LEFT_BUTTON:
  27. return glMouseLeftButton;
  28. case GLUT_RIGHT_BUTTON:
  29. return glMouseMiddleButton;
  30. case GLUT_MIDDLE_BUTTON:
  31. return glMouseRightButton;
  32. default:
  33. return glMouseLeftButton;
  34. }
  35. }
  36. static void cb_reshape(int width, int height)
  37. {
  38. int vPort[4];
  39. float aspect;
  40. view->w = width;
  41. view->h = height;
  42. if (view->widgets)
  43. glcompsetUpdateBorder(view->widgets, view->w, view->h);
  44. glViewport(0, 0, view->w, view->h);
  45. /* get current viewport */
  46. glGetIntegerv(GL_VIEWPORT, vPort);
  47. /* setup various opengl things that we need */
  48. glMatrixMode(GL_PROJECTION);
  49. glLoadIdentity();
  50. init_arcBall(view->arcball, view->w, view->h);
  51. if (view->w > view->h) {
  52. aspect = (float) view->w / (float) view->h;
  53. glOrtho(-aspect * GL_VIEWPORT_FACTOR, aspect * GL_VIEWPORT_FACTOR,
  54. GL_VIEWPORT_FACTOR * -1, GL_VIEWPORT_FACTOR, -1500, 1500);
  55. } else {
  56. aspect = (float) view->h / (float) view->w;
  57. glOrtho(GL_VIEWPORT_FACTOR * -1, GL_VIEWPORT_FACTOR,
  58. -aspect * GL_VIEWPORT_FACTOR, aspect * GL_VIEWPORT_FACTOR,
  59. -1500, 1500);
  60. }
  61. glMatrixMode(GL_MODELVIEW);
  62. glLoadIdentity();
  63. /*** OpenGL END ***/
  64. }
  65. static void cb_display(void)
  66. {
  67. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  68. glLoadIdentity();
  69. glexpose_main(view); //draw all stuff
  70. glutSwapBuffers();
  71. if (view->initFile) {
  72. view->initFile = 0;
  73. if (view->activeGraph == 0)
  74. close_graph(view);
  75. add_graph_to_viewport_from_file(view->initFileName);
  76. }
  77. }
  78. static void cb_mouseclick(int button, int state, int x, int y)
  79. {
  80. if (view->g == 0) return;
  81. begin_x = (float) x;
  82. begin_y = (float) y;
  83. if(state==GLUT_DOWN)
  84. {
  85. view->widgets->base.common.functions.mousedown(&view->widgets->base,
  86. (float)x, (float)y,
  87. getGlCompMouseType(button));
  88. if (button == GLUT_LEFT_BUTTON)
  89. appmouse_left_click_down(view,x,y);
  90. if (button == GLUT_RIGHT_BUTTON)
  91. appmouse_right_click_down(view,x,y);
  92. if (button == GLUT_MIDDLE_BUTTON)
  93. appmouse_middle_click_down(view,x,y);
  94. }
  95. else
  96. {
  97. view->arcball->isDragging = 0;
  98. view->widgets->base.common.functions.mouseup(&view->widgets->base,
  99. (float)x, (float)y,
  100. getGlCompMouseType(button));
  101. if (button == GLUT_LEFT_BUTTON || button == GLUT_RIGHT_BUTTON ||
  102. button == GLUT_MIDDLE_BUTTON)
  103. appmouse_up(view, x, y);
  104. dx = 0.0;
  105. dy = 0.0;
  106. }
  107. cb_display();
  108. }
  109. // mouse moving with a button clicked (dragging)
  110. static void cb_drag(int X, int Y)
  111. {
  112. float x = (float) X;
  113. float y = (float) Y;
  114. if (view->widgets)
  115. view->widgets->base.common.functions.mouseover(&view->widgets->base, x, y);
  116. dx = x - begin_x;
  117. dy = y - begin_y;
  118. view->mouse.dragX = dx;
  119. view->mouse.dragY = dy;
  120. appmouse_move(view,x,y);
  121. if (view->mouse.down)
  122. appmouse_drag(view, x, y);
  123. begin_x = x;
  124. begin_y = y;
  125. cb_display();
  126. }
  127. static void cb_keyboard(unsigned char key, int x, int y)
  128. {
  129. (void)x;
  130. (void)y;
  131. if (key==27) /*ESC*/
  132. graphviz_exit(1);
  133. if(key=='3')
  134. switch2D3D(NULL, 0, 0,glMouseLeftButton);
  135. if(key=='c')
  136. menu_click_center(NULL, 0, 0,glMouseLeftButton);
  137. if(key=='+')
  138. menu_click_zoom_plus(NULL, 0, 0,glMouseLeftButton);
  139. if(key=='-')
  140. menu_click_zoom_minus(NULL, 0, 0,glMouseLeftButton);
  141. if(key=='p')
  142. menu_click_pan(NULL, 0, 0,glMouseLeftButton);
  143. appmouse_key_press(view,key);
  144. }
  145. static void cb_keyboard_up(unsigned char key, int x, int y)
  146. {
  147. (void)key;
  148. (void)x;
  149. (void)y;
  150. appmouse_key_release(view);
  151. }
  152. static void cb_special_key(int key, int x, int y)
  153. {
  154. (void)x;
  155. (void)y;
  156. if(key==GLUT_KEY_F1)
  157. {
  158. printf("Currently help is not available\n");
  159. }
  160. appmouse_key_press(view,key);
  161. }
  162. static void cb_special_key_up(int key, int x, int y)
  163. {
  164. (void)x;
  165. (void)y;
  166. if(key==GLUT_KEY_F1)
  167. {
  168. printf("Currently help is not available\n");
  169. }
  170. appmouse_key_release(view);
  171. }
  172. static void cb_game_mode(char* optArg)
  173. {
  174. glutGameModeString(optArg);
  175. if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
  176. {
  177. glutEnterGameMode();
  178. }
  179. else
  180. {
  181. printf("smyrna cannot initialize requested screen resolution and rate!\n");
  182. graphviz_exit(-1);
  183. }
  184. }
  185. int cb_glutinit(int w, int h, int *argcp, char *argv[], char *optArg) {
  186. /*
  187. w,h: width and height of the window in pixels
  188. argcp argv: main function's parameters, required for glutinit
  189. */
  190. glutInit(argcp,argv); //this is required by some OS.
  191. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  192. // The Type Of Depth Testing To Do
  193. glDisable(GL_DEPTH);
  194. glClearDepth(1.0f); // Depth Buffer Setup
  195. glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  196. glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To
  197. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
  198. cb_game_mode(optArg);
  199. /*register callbacks here*/
  200. glutDisplayFunc(cb_display);
  201. glutReshapeFunc(cb_reshape);
  202. glutKeyboardFunc(cb_keyboard);
  203. glutKeyboardUpFunc( cb_keyboard_up);
  204. glutMouseFunc(cb_mouseclick);
  205. glutMotionFunc(cb_drag);
  206. glutPassiveMotionFunc(NULL);
  207. glutVisibilityFunc(NULL);
  208. glutEntryFunc(NULL);
  209. glutSpecialFunc(cb_special_key);
  210. glutSpecialUpFunc(cb_special_key_up);
  211. //Attach extra call backs if needed in the future
  212. /* glutOverlayDisplayFunc
  213. glutSpaceballMotionFunc
  214. glutSpaceballRotateFunc
  215. glutSpaceballButtonFunc
  216. glutButtonBoxFunc
  217. glutDialsFunc
  218. glutTabletMotionFunc
  219. glutTabletButtonFunc
  220. glutMenuStatusFunc
  221. glutIdleFunc
  222. glutTimerFunc
  223. */
  224. //pass control to glut
  225. cb_reshape(w,h);
  226. glutMainLoop ();
  227. return 0; //we should never reach here
  228. }