Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
17 KiB

  1. extern "C" {
  2. #include <windows.h>
  3. #include <GL/glu.h>
  4. #include <GL/gl.h>
  5. #include <GL/glaux.h>
  6. };
  7. #ifdef GLX_MOTIF
  8. #include <GL/glx.h>
  9. #include <X11/Intrinsic.h>
  10. #include <Xm/Xm.h>
  11. #include <Xm/CascadeB.h>
  12. #include <Xm/Frame.h>
  13. #include <Xm/MainW.h>
  14. #include <Xm/PushB.h>
  15. #include <Xm/PushBG.h>
  16. #include <Xm/RowColumn.h>
  17. #include <Xm/ToggleB.h>
  18. #include <GLwMDrawA.h>
  19. #include <sys/time.h>
  20. #endif
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. #include "scene.hxx"
  26. #include "cbacks.hxx"
  27. #include "menu.h"
  28. LRESULT APIENTRY MyWndProc(HWND, UINT, WPARAM, LPARAM);
  29. VOID SubclassWindow (HWND, WNDPROC);
  30. void vCustomizeWnd(void);
  31. extern light lights[];
  32. extern GLboolean bAutoMotion(void);
  33. extern void draw(void);
  34. #ifdef GLX_MOTIF
  35. static Display *display;
  36. XtAppContext app_context;
  37. Widget glw;
  38. const int max_args = 20;
  39. #endif
  40. int quick_moves = 0;
  41. int auto_motion = 0;
  42. #ifdef GLX_MOTIF
  43. XVisualInfo *find_visual()
  44. {
  45. int attr[256], i, stencil, dbuffer;
  46. XVisualInfo *vi;
  47. i = 0;
  48. attr[i++] = GLX_RGBA;
  49. attr[i++] = GLX_RED_SIZE;
  50. attr[i++] = 1;
  51. attr[i++] = GLX_GREEN_SIZE;
  52. attr[i++] = 1;
  53. attr[i++] = GLX_BLUE_SIZE;
  54. attr[i++] = 1;
  55. attr[i++] = GLX_DEPTH_SIZE;
  56. attr[i++] = 1;
  57. dbuffer = i;
  58. attr[i++] = GLX_DOUBLEBUFFER;
  59. stencil = i;
  60. attr[i++] = GLX_STENCIL_SIZE;
  61. attr[i++] = 1;
  62. attr[i++] = (int)None;
  63. vi = glXChooseVisual(display, DefaultScreen(display), attr);
  64. if (vi == NULL) {
  65. fprintf(stderr, "Unable to find visual with stencil buffer.\n");
  66. fprintf(stderr, "(Things won't look quite as good).\n");
  67. attr[stencil] = (int)None;
  68. vi = glXChooseVisual(display, DefaultScreen(display), attr);
  69. if (vi == NULL) {
  70. fprintf(stderr, "Unable to find double-buffered visual.\n");
  71. fprintf(stderr, "(Things will look even worse).\n");
  72. attr[dbuffer] = (int)None;
  73. vi = glXChooseVisual(display, DefaultScreen(display), attr);
  74. if (vi == NULL) fprintf(stderr, "Can't find visual at all.\n");
  75. }
  76. }
  77. return vi;
  78. }
  79. #endif
  80. #ifdef GLX_MOTIF
  81. Widget create_widgets(Widget parent)
  82. {
  83. Widget main_window;
  84. Widget menu_bar;
  85. Widget menu_pane;
  86. Widget button;
  87. Widget cascade;
  88. Widget frame;
  89. XVisualInfo *vi;
  90. Arg args[max_args];
  91. int argcount;
  92. char buffer[128];
  93. int i;
  94. main_window = XmCreateMainWindow(parent, "main1", NULL, 0);
  95. XtManageChild(main_window);
  96. menu_bar = XmCreateMenuBar(main_window, "menu_bar", NULL, 0);
  97. XtManageChild(menu_bar);
  98. menu_pane = XmCreatePulldownMenu(menu_bar, "menu_pane", NULL, 0);
  99. button = XmCreatePushButton(menu_pane, "Exit", NULL, 0);
  100. XtManageChild(button);
  101. XtAddCallback(button, XmNactivateCallback, (XtCallbackProc)exitCB, NULL);
  102. XtSetArg(args[0], XmNsubMenuId, menu_pane);
  103. cascade = XmCreateCascadeButton(menu_bar, "File", args, 1);
  104. XtManageChild(cascade);
  105. argcount = 0;
  106. XtSetArg(args[argcount], XmNradioBehavior, True); argcount++;
  107. menu_pane = XmCreatePulldownMenu(menu_bar, "menu_pane", args, argcount);
  108. XtSetArg(args[0], XmNset, TRUE);
  109. for (i = 0; i < nindices; i++) {
  110. if (i == def_refraction_index) argcount = 1;
  111. else argcount = 0;
  112. button = XmCreateToggleButton(menu_pane, (char *)indices[i].name,
  113. args, argcount);
  114. XtManageChild(button);
  115. XtAddCallback(button, XmNvalueChangedCallback,
  116. (XtCallbackProc)refractionCB,
  117. (XtPointer)(&indices[i].index));
  118. }
  119. XtSetArg(args[0], XmNsubMenuId, menu_pane);
  120. cascade = XmCreateCascadeButton(menu_bar, "Material", args, 1);
  121. XtManageChild(cascade);
  122. menu_pane = XmCreatePulldownMenu(menu_bar, "menu_pane", NULL, 0);
  123. XtSetArg(args[0], XmNset, draw_square);
  124. button = XmCreateToggleButton(menu_pane, "Draw Square", args, 1);
  125. XtManageChild(button);
  126. XtAddCallback(button, XmNvalueChangedCallback,
  127. (XtCallbackProc)drawSomethingCB, &draw_square);
  128. XtSetArg(args[0], XmNset, draw_shadows);
  129. button = XmCreateToggleButton(menu_pane, "Draw Shadows", args, 1);
  130. XtManageChild(button);
  131. XtAddCallback(button, XmNvalueChangedCallback,
  132. (XtCallbackProc)drawSomethingCB, &draw_shadows);
  133. XtSetArg(args[0], XmNset, draw_refraction);
  134. button = XmCreateToggleButton(menu_pane, "Draw Refraction", args, 1);
  135. XtManageChild(button);
  136. XtAddCallback(button, XmNvalueChangedCallback,
  137. (XtCallbackProc)drawSomethingCB, &draw_refraction);
  138. XtSetArg(args[0], XmNset, draw_sphere);
  139. button = XmCreateToggleButton(menu_pane, "Draw Sphere", args, 1);
  140. XtManageChild(button);
  141. XtAddCallback(button, XmNvalueChangedCallback,
  142. (XtCallbackProc)drawSomethingCB, &draw_sphere);
  143. XtSetArg(args[0], XmNset, draw_lights);
  144. button = XmCreateToggleButton(menu_pane, "Draw Lights", args, 1);
  145. XtManageChild(button);
  146. XtAddCallback(button, XmNvalueChangedCallback,
  147. (XtCallbackProc)drawSomethingCB, &draw_lights);
  148. #ifdef TEXTURE
  149. XtSetArg(args[0], XmNset, draw_texture);
  150. button = XmCreateToggleButton(menu_pane, "Texture Map", args, 1);
  151. XtManageChild(button);
  152. XtAddCallback(button, XmNvalueChangedCallback,
  153. (XtCallbackProc)drawSomethingCB, &draw_texture);
  154. #endif
  155. XtSetArg(args[0], XmNsubMenuId, menu_pane);
  156. cascade = XmCreateCascadeButton(menu_bar, "Draw", args, 1);
  157. XtManageChild(cascade);
  158. argcount = 0;
  159. XtSetArg(args[argcount], XmNradioBehavior, True); argcount++;
  160. menu_pane = XmCreatePulldownMenu(menu_bar, "menu_pane", args, argcount);
  161. XtSetArg(args[0], XmNset, TRUE);
  162. for (i = 0; i < npossible_divisions; i++) {
  163. if (i == def_divisions_index) argcount = 1;
  164. else argcount = 0;
  165. sprintf(buffer, "%d", possible_divisions[i]);
  166. button = XmCreateToggleButton(menu_pane, buffer, args, argcount);
  167. XtManageChild(button);
  168. XtAddCallback(button, XmNvalueChangedCallback,
  169. (XtCallbackProc)subdivisionCB, &possible_divisions[i]);
  170. }
  171. XtSetArg(args[0], XmNsubMenuId, menu_pane);
  172. cascade = XmCreateCascadeButton(menu_bar, "Subdivision", args, 1);
  173. XtManageChild(cascade);
  174. menu_pane = XmCreatePulldownMenu(menu_bar, "menu_pane", args, argcount);
  175. button = XmCreatePushButton(menu_pane, "Reset Position", NULL, 0);
  176. XtManageChild(button);
  177. XtAddCallback(button, XmNactivateCallback,
  178. (XtCallbackProc)resetLightsCB, NULL);
  179. XtSetArg(args[0], XmNset, TRUE);
  180. for (i = 0; i < nlights; i++) {
  181. button = XmCreateToggleButton(menu_pane, lights[i].name, args,
  182. lights[i].on ? 1 : 0);
  183. XtManageChild(button);
  184. XtAddCallback(button, XmNvalueChangedCallback,
  185. (XtCallbackProc)light_onCB, &lights[i]);
  186. }
  187. XtSetArg(args[0], XmNsubMenuId, menu_pane);
  188. cascade = XmCreateCascadeButton(menu_bar, "Lights", args, 1);
  189. XtManageChild(cascade);
  190. menu_pane = XmCreatePulldownMenu(menu_bar, "menu_pane", args,
  191. argcount);
  192. XtSetArg(args[0], XmNset, quick_moves);
  193. button = XmCreateToggleButton(menu_pane, "Quick Motion",
  194. args, 1);
  195. XtManageChild(button);
  196. XtAddCallback(button, XmNvalueChangedCallback,
  197. (XtCallbackProc)intToggleCB, &quick_moves);
  198. XtSetArg(args[0], XmNset, auto_motion);
  199. button = XmCreateToggleButton(menu_pane, "Rotate Automatically",
  200. args, 1);
  201. XtManageChild(button);
  202. XtAddCallback(button, XmNvalueChangedCallback,
  203. (XtCallbackProc)autoMotionCB, NULL);
  204. XtSetArg(args[0], XmNsubMenuId, menu_pane);
  205. cascade = XmCreateCascadeButton(menu_bar, "Motion", args, 1);
  206. XtManageChild(cascade);
  207. argcount = 0;
  208. XtSetArg(args[argcount], XmNmarginWidth, 0); argcount++;
  209. XtSetArg(args[argcount], XmNmarginHeight, 0); argcount++;
  210. XtSetArg(args[argcount], XmNshadowThickness, 1); argcount++;
  211. XtSetArg(args[argcount], XmNshadowType, XmSHADOW_OUT); argcount++;
  212. frame = XmCreateFrame(main_window, "frame", args, argcount);
  213. XtManageChild(frame);
  214. argcount = 0;
  215. vi = find_visual();
  216. if (vi) {
  217. XtSetArg(args[argcount], GLwNvisualInfo, vi); argcount++;
  218. }
  219. else {
  220. XtSetArg(args[argcount], GLwNrgba, TRUE); argcount++;
  221. XtSetArg(args[argcount], GLwNdepthSize, 1); argcount++;
  222. XtSetArg(args[argcount], GLwNdoublebuffer, TRUE); argcount++;
  223. }
  224. XtSetArg(args[argcount], XmNbottomAttachment, XmATTACH_FORM); argcount++;
  225. XtSetArg(args[argcount], XmNtopAttachment, XmATTACH_FORM); argcount++;
  226. XtSetArg(args[argcount], XmNleftAttachment, XmATTACH_FORM); argcount++;
  227. XtSetArg(args[argcount], XmNrightAttachment, XmATTACH_FORM); argcount++;
  228. glw = GLwCreateMDrawingArea(frame, "glwidget", args, argcount);
  229. XtManageChild(glw);
  230. XtAddCallback(glw, GLwNginitCallback, (XtCallbackProc)initCB, 0);
  231. XtAddCallback(glw, GLwNexposeCallback, (XtCallbackProc)exposeCB, 0);
  232. XtAddCallback(glw, GLwNresizeCallback, (XtCallbackProc)resizeCB, 0);
  233. XtAddCallback(glw, GLwNinputCallback, (XtCallbackProc)inputCB, 0);
  234. return main_window;
  235. }
  236. #endif
  237. void main(int argc, char **argv)
  238. {
  239. auxInitDisplayMode(AUX_RGBA | AUX_STENCIL | AUX_DOUBLE );
  240. auxInitPosition(100, 100, 300, 300);
  241. auxInitWindow("BackTrace");
  242. vCustomizeWnd();
  243. scene_load_texture((char *)def_texfile);
  244. vInit();
  245. auxReshapeFunc(vResize);
  246. //
  247. // we've a choice of using accelerator or auxKeyFunc
  248. // the use of auxKeyFunc here is solely for demonstration purpose
  249. //
  250. auxKeyFunc(AUX_SPACE, vQuickMove);
  251. auxKeyFunc(AUX_RETURN, vResetLights);
  252. auxKeyFunc(AUX_p, vAutoMotion);
  253. auxKeyFunc(AUX_q, vDrawAll);
  254. auxKeyFunc(AUX_w, vDrawSquare);
  255. auxKeyFunc(AUX_e, vDrawShadow);
  256. auxKeyFunc(AUX_r, vDrawRefraction);
  257. auxKeyFunc(AUX_t, vDrawSphere);
  258. auxKeyFunc(AUX_y, vDrawLight);
  259. auxKeyFunc(AUX_u, vDrawTexture);
  260. auxKeyFunc(AUX_a, vRefractionAIR);
  261. auxKeyFunc(AUX_s, vRefractionICE);
  262. auxKeyFunc(AUX_d, vRefractionWATER);
  263. auxKeyFunc(AUX_f, vRefractionZincGLASS);
  264. auxKeyFunc(AUX_g, vRefractionLightGLASS);
  265. auxKeyFunc(AUX_h, vRefractionHeavyGLASS);
  266. auxKeyFunc(AUX_1, vSubdivision10);
  267. auxKeyFunc(AUX_2, vSubdivision20);
  268. auxKeyFunc(AUX_3, vSubdivision30);
  269. auxKeyFunc(AUX_4, vSubdivision40);
  270. auxKeyFunc(AUX_R, vRLight_on);
  271. auxKeyFunc(AUX_G, vGLight_on);
  272. auxKeyFunc(AUX_B, vBLight_on);
  273. auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, vMouseDown);
  274. auxMouseFunc(AUX_MIDDLEBUTTON, AUX_MOUSEDOWN, vMouseDown);
  275. auxMouseFunc(AUX_RIGHTBUTTON, AUX_MOUSEDOWN, vMouseDown);
  276. auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEUP, vLeftMouseUp);
  277. auxMouseFunc(AUX_MIDDLEBUTTON, AUX_MOUSEUP, vMiddleMouseUp);
  278. auxMouseFunc(AUX_RIGHTBUTTON, AUX_MOUSEUP, vRightMouseUp);
  279. auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSELOC, vMouseMove);
  280. auxMouseFunc(AUX_MIDDLEBUTTON, AUX_MOUSELOC, vMouseMove);
  281. auxMouseFunc(AUX_RIGHTBUTTON, AUX_MOUSELOC, vMouseMove);
  282. auxMainLoop(draw);
  283. return;
  284. }
  285. #ifdef GLX_MOTIF
  286. void main(int argc, char **argv)
  287. {
  288. Widget app_shell;
  289. Arg args[max_args];
  290. int argcount;
  291. scene_load_texture((char *)def_texfile);
  292. XtToolkitInitialize();
  293. app_context = XtCreateApplicationContext();
  294. display = XtOpenDisplay(app_context, NULL, argv[0],
  295. "XMdemos", NULL, 0, &argc, argv);
  296. if (!display) {
  297. XtWarning("Can't open display.");
  298. exit(0);
  299. }
  300. argcount = 0;
  301. XtSetArg(args[argcount], XmNmaxAspectX, 1); argcount++;
  302. XtSetArg(args[argcount], XmNmaxAspectY, 1); argcount++;
  303. XtSetArg(args[argcount], XmNminAspectX, 1); argcount++;
  304. XtSetArg(args[argcount], XmNminAspectY, 1); argcount++;
  305. app_shell =
  306. XtAppCreateShell(argv[0], "XMdemos", applicationShellWidgetClass,
  307. display, args, argcount);
  308. create_widgets(app_shell);
  309. XtRealizeWidget(app_shell);
  310. XtAppMainLoop(app_context);
  311. }
  312. #endif
  313. /**************************************************************************\
  314. *
  315. * function: MyWndProc
  316. *
  317. * input parameters: normal window procedure parameters.
  318. *
  319. \**************************************************************************/
  320. LRESULT APIENTRY MyWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  321. {
  322. WNDPROC pfnOldProc;
  323. static UINT_PTR uiTmID = 0;
  324. int wmId, wmEvent;
  325. pfnOldProc = (WNDPROC) GetWindowLongPtr (hwnd, GWLP_USERDATA);
  326. switch (message) {
  327. case WM_COMMAND: {
  328. HMENU hmenu;
  329. hmenu = GetMenu(auxGetHWND());
  330. wmId = LOWORD(wParam);
  331. wmEvent = HIWORD(wParam);
  332. switch (wmId) {
  333. case IDM_EXIT : vExit(); break;
  334. case IDM_AIR :
  335. case IDM_ICE :
  336. case IDM_WATER :
  337. case IDM_ZINC :
  338. case IDM_LIGHT :
  339. case IDM_HEAVY : vRefraction(wmId - IDM_AIR); break;
  340. case IDM_SQUARE :
  341. {
  342. if (draw_square)
  343. CheckMenuItem(hmenu, IDM_SQUARE, MF_BYCOMMAND | MF_UNCHECKED);
  344. else
  345. CheckMenuItem(hmenu, IDM_SQUARE, MF_BYCOMMAND | MF_CHECKED);
  346. vDrawStuff(&draw_square);
  347. break;
  348. }
  349. case IDM_SHADOW :
  350. {
  351. if (draw_shadows)
  352. CheckMenuItem(hmenu, IDM_SHADOW, MF_BYCOMMAND | MF_UNCHECKED);
  353. else
  354. CheckMenuItem(hmenu, IDM_SHADOW, MF_BYCOMMAND | MF_CHECKED);
  355. vDrawStuff(&draw_shadows);
  356. break;
  357. }
  358. case IDM_REFRACTION:
  359. {
  360. if (draw_refraction)
  361. CheckMenuItem(hmenu, IDM_REFRACTION, MF_BYCOMMAND | MF_UNCHECKED);
  362. else
  363. CheckMenuItem(hmenu, IDM_REFRACTION, MF_BYCOMMAND | MF_CHECKED);
  364. vDrawStuff(&draw_refraction);
  365. break;
  366. }
  367. case IDM_SPHERE :
  368. {
  369. if (draw_sphere)
  370. CheckMenuItem(hmenu, IDM_SPHERE, MF_BYCOMMAND | MF_UNCHECKED);
  371. else
  372. CheckMenuItem(hmenu, IDM_SPHERE, MF_BYCOMMAND | MF_CHECKED);
  373. vDrawStuff(&draw_sphere);
  374. break;
  375. }
  376. case IDM_LIGHTS :
  377. {
  378. if (draw_lights)
  379. CheckMenuItem(hmenu, IDM_LIGHTS, MF_BYCOMMAND | MF_UNCHECKED);
  380. else
  381. CheckMenuItem(hmenu, IDM_LIGHTS, MF_BYCOMMAND | MF_CHECKED);
  382. vDrawStuff(&draw_lights);
  383. break;
  384. }
  385. case IDM_TEXTURE :
  386. {
  387. if (draw_texture)
  388. CheckMenuItem(hmenu, IDM_TEXTURE, MF_BYCOMMAND | MF_UNCHECKED);
  389. else
  390. CheckMenuItem(hmenu, IDM_TEXTURE, MF_BYCOMMAND | MF_CHECKED);
  391. vDrawStuff(&draw_texture);
  392. break;
  393. }
  394. case IDM_ALL : vDrawAll(); break;
  395. case IDM_10 :
  396. case IDM_20 :
  397. case IDM_30 :
  398. case IDM_40 : vSubdivision(wmId - IDM_10); break;
  399. case IDM_RESETLGT : vResetLights(); break;
  400. case IDM_RED :
  401. case IDM_GREEN :
  402. case IDM_BLUE : vLight_on(wmId - IDM_RED); break;
  403. case IDM_QUICK : vQuickMove(); break;
  404. case IDM_AUTO : vAutoMotion(); break;
  405. default:
  406. break;
  407. }
  408. }
  409. case WM_USER:
  410. if (auto_motion) {
  411. uiTmID = SetTimer(hwnd, 1, 1, NULL);
  412. if (uiTmID == 0)
  413. OutputDebugString("failed to create timer\n");
  414. } else {
  415. if (uiTmID != 0) {
  416. KillTimer(hwnd, uiTmID);
  417. uiTmID = 0;
  418. }
  419. }
  420. return 0;
  421. case WM_TIMER: {
  422. //static int cnt=0;
  423. //char text[128];
  424. bAutoMotion();
  425. //wsprintf(text, "WM_TIMER %d\n", cnt++);
  426. //OutputDebugString(text);
  427. return 0;
  428. }
  429. case WM_DESTROY:
  430. if (uiTmID != 0)
  431. KillTimer(hwnd, uiTmID);
  432. return CallWindowProc(pfnOldProc, hwnd, message, wParam, lParam);
  433. default:
  434. return CallWindowProc(pfnOldProc, hwnd, message, wParam, lParam);
  435. } /* end switch */
  436. return 0;
  437. }
  438. /**************************************************************************\
  439. * function: SubclassWindow
  440. *
  441. * input parameters:
  442. * hwnd - window handle to be subclassed,
  443. * SubclassWndProc - the new window procedure.
  444. *
  445. \**************************************************************************/
  446. VOID SubclassWindow (HWND hwnd, WNDPROC SubclassWndProc)
  447. {
  448. LONG_PTR pfnOldProc;
  449. pfnOldProc = GetWindowLongPtr (hwnd, GWLP_WNDPROC);
  450. SetWindowLongPtr (hwnd, GWLP_USERDATA, (LONG_PTR) pfnOldProc);
  451. SetWindowLongPtr (hwnd, GWLP_WNDPROC, (LONG_PTR) SubclassWndProc);
  452. }
  453. /******************************Public*Routine******************************\
  454. *
  455. * vCustomizeWnd
  456. *
  457. * Effects: Subclass the window created by the toolkit
  458. * Add menu bar to the window
  459. * Setup timer
  460. *
  461. * History:
  462. * 01-Dec-1993
  463. *
  464. \**************************************************************************/
  465. void vCustomizeWnd(void)
  466. {
  467. HWND hWnd;
  468. if ((hWnd = auxGetHWND()) == NULL) {
  469. OutputDebugString("auxGetHWND() failed\n");
  470. return;
  471. }
  472. SubclassWindow (hWnd, MyWndProc);
  473. SendMessage(hWnd, WM_USER, 0L, 0L);
  474. SetMenu(hWnd, LoadMenu(GetModuleHandle(NULL), "Backtrac"));
  475. DrawMenuBar(hWnd);
  476. return;
  477. }