Clone of mesa.
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.

glxgears.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. /*
  22. * This is a port of the infamous "gears" demo to straight GLX (i.e. no GLUT)
  23. * Port by Brian Paul 23 March 2001
  24. *
  25. * See usage() below for command line options.
  26. */
  27. #include <math.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <X11/Xlib.h>
  32. #include <X11/keysym.h>
  33. #include <GL/gl.h>
  34. #include <GL/glx.h>
  35. #ifndef GLX_MESA_swap_control
  36. #define GLX_MESA_swap_control 1
  37. typedef int (*PFNGLXGETSWAPINTERVALMESAPROC)(void);
  38. #endif
  39. #define BENCHMARK
  40. #ifdef BENCHMARK
  41. /* XXX this probably isn't very portable */
  42. #include <sys/time.h>
  43. #include <unistd.h>
  44. /* return current time (in seconds) */
  45. static double
  46. current_time(void)
  47. {
  48. struct timeval tv;
  49. #ifdef __VMS
  50. (void) gettimeofday(&tv, NULL );
  51. #else
  52. struct timezone tz;
  53. (void) gettimeofday(&tv, &tz);
  54. #endif
  55. return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
  56. }
  57. #else /*BENCHMARK*/
  58. /* dummy */
  59. static double
  60. current_time(void)
  61. {
  62. /* update this function for other platforms! */
  63. static double t = 0.0;
  64. static int warn = 1;
  65. if (warn) {
  66. fprintf(stderr, "Warning: current_time() not implemented!!\n");
  67. warn = 0;
  68. }
  69. return t += 1.0;
  70. }
  71. #endif /*BENCHMARK*/
  72. #ifndef M_PI
  73. #define M_PI 3.14159265
  74. #endif
  75. /** Event handler results: */
  76. #define NOP 0
  77. #define EXIT 1
  78. #define DRAW 2
  79. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  80. static GLint gear1, gear2, gear3;
  81. static GLfloat angle = 0.0;
  82. static GLboolean fullscreen = GL_FALSE; /* Create a single fullscreen window */
  83. static GLboolean stereo = GL_FALSE; /* Enable stereo. */
  84. static GLboolean animate = GL_TRUE; /* Animation */
  85. static GLfloat eyesep = 5.0; /* Eye separation. */
  86. static GLfloat fix_point = 40.0; /* Fixation point distance. */
  87. static GLfloat left, right, asp; /* Stereo frustum params. */
  88. /*
  89. *
  90. * Draw a gear wheel. You'll probably want to call this function when
  91. * building a display list since we do a lot of trig here.
  92. *
  93. * Input: inner_radius - radius of hole at center
  94. * outer_radius - radius at center of teeth
  95. * width - width of gear
  96. * teeth - number of teeth
  97. * tooth_depth - depth of tooth
  98. */
  99. static void
  100. gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  101. GLint teeth, GLfloat tooth_depth)
  102. {
  103. GLint i;
  104. GLfloat r0, r1, r2;
  105. GLfloat angle, da;
  106. GLfloat u, v, len;
  107. r0 = inner_radius;
  108. r1 = outer_radius - tooth_depth / 2.0;
  109. r2 = outer_radius + tooth_depth / 2.0;
  110. da = 2.0 * M_PI / teeth / 4.0;
  111. glShadeModel(GL_FLAT);
  112. glNormal3f(0.0, 0.0, 1.0);
  113. /* draw front face */
  114. glBegin(GL_QUAD_STRIP);
  115. for (i = 0; i <= teeth; i++) {
  116. angle = i * 2.0 * M_PI / teeth;
  117. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  118. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  119. if (i < teeth) {
  120. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  121. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  122. width * 0.5);
  123. }
  124. }
  125. glEnd();
  126. /* draw front sides of teeth */
  127. glBegin(GL_QUADS);
  128. da = 2.0 * M_PI / teeth / 4.0;
  129. for (i = 0; i < teeth; i++) {
  130. angle = i * 2.0 * M_PI / teeth;
  131. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  132. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  133. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  134. width * 0.5);
  135. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  136. width * 0.5);
  137. }
  138. glEnd();
  139. glNormal3f(0.0, 0.0, -1.0);
  140. /* draw back face */
  141. glBegin(GL_QUAD_STRIP);
  142. for (i = 0; i <= teeth; i++) {
  143. angle = i * 2.0 * M_PI / teeth;
  144. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  145. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  146. if (i < teeth) {
  147. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  148. -width * 0.5);
  149. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  150. }
  151. }
  152. glEnd();
  153. /* draw back sides of teeth */
  154. glBegin(GL_QUADS);
  155. da = 2.0 * M_PI / teeth / 4.0;
  156. for (i = 0; i < teeth; i++) {
  157. angle = i * 2.0 * M_PI / teeth;
  158. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  159. -width * 0.5);
  160. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  161. -width * 0.5);
  162. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  163. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  164. }
  165. glEnd();
  166. /* draw outward faces of teeth */
  167. glBegin(GL_QUAD_STRIP);
  168. for (i = 0; i < teeth; i++) {
  169. angle = i * 2.0 * M_PI / teeth;
  170. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  171. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  172. u = r2 * cos(angle + da) - r1 * cos(angle);
  173. v = r2 * sin(angle + da) - r1 * sin(angle);
  174. len = sqrt(u * u + v * v);
  175. u /= len;
  176. v /= len;
  177. glNormal3f(v, -u, 0.0);
  178. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  179. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  180. glNormal3f(cos(angle), sin(angle), 0.0);
  181. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  182. width * 0.5);
  183. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  184. -width * 0.5);
  185. u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
  186. v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
  187. glNormal3f(v, -u, 0.0);
  188. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  189. width * 0.5);
  190. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  191. -width * 0.5);
  192. glNormal3f(cos(angle), sin(angle), 0.0);
  193. }
  194. glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
  195. glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
  196. glEnd();
  197. glShadeModel(GL_SMOOTH);
  198. /* draw inside radius cylinder */
  199. glBegin(GL_QUAD_STRIP);
  200. for (i = 0; i <= teeth; i++) {
  201. angle = i * 2.0 * M_PI / teeth;
  202. glNormal3f(-cos(angle), -sin(angle), 0.0);
  203. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  204. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  205. }
  206. glEnd();
  207. }
  208. static void
  209. draw(void)
  210. {
  211. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  212. glPushMatrix();
  213. glRotatef(view_rotx, 1.0, 0.0, 0.0);
  214. glRotatef(view_roty, 0.0, 1.0, 0.0);
  215. glRotatef(view_rotz, 0.0, 0.0, 1.0);
  216. glPushMatrix();
  217. glTranslatef(-3.0, -2.0, 0.0);
  218. glRotatef(angle, 0.0, 0.0, 1.0);
  219. glCallList(gear1);
  220. glPopMatrix();
  221. glPushMatrix();
  222. glTranslatef(3.1, -2.0, 0.0);
  223. glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
  224. glCallList(gear2);
  225. glPopMatrix();
  226. glPushMatrix();
  227. glTranslatef(-3.1, 4.2, 0.0);
  228. glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
  229. glCallList(gear3);
  230. glPopMatrix();
  231. glPopMatrix();
  232. }
  233. static void
  234. draw_gears(void)
  235. {
  236. if (stereo) {
  237. /* First left eye. */
  238. glDrawBuffer(GL_BACK_LEFT);
  239. glMatrixMode(GL_PROJECTION);
  240. glLoadIdentity();
  241. glFrustum(left, right, -asp, asp, 5.0, 60.0);
  242. glMatrixMode(GL_MODELVIEW);
  243. glPushMatrix();
  244. glTranslated(+0.5 * eyesep, 0.0, 0.0);
  245. draw();
  246. glPopMatrix();
  247. /* Then right eye. */
  248. glDrawBuffer(GL_BACK_RIGHT);
  249. glMatrixMode(GL_PROJECTION);
  250. glLoadIdentity();
  251. glFrustum(-right, -left, -asp, asp, 5.0, 60.0);
  252. glMatrixMode(GL_MODELVIEW);
  253. glPushMatrix();
  254. glTranslated(-0.5 * eyesep, 0.0, 0.0);
  255. draw();
  256. glPopMatrix();
  257. }
  258. else {
  259. draw();
  260. }
  261. }
  262. /** Draw single frame, do SwapBuffers, compute FPS */
  263. static void
  264. draw_frame(Display *dpy, Window win)
  265. {
  266. static int frames = 0;
  267. static double tRot0 = -1.0, tRate0 = -1.0;
  268. double dt, t = current_time();
  269. if (tRot0 < 0.0)
  270. tRot0 = t;
  271. dt = t - tRot0;
  272. tRot0 = t;
  273. if (animate) {
  274. /* advance rotation for next frame */
  275. angle += 70.0 * dt; /* 70 degrees per second */
  276. if (angle > 3600.0)
  277. angle -= 3600.0;
  278. }
  279. draw_gears();
  280. glXSwapBuffers(dpy, win);
  281. frames++;
  282. if (tRate0 < 0.0)
  283. tRate0 = t;
  284. if (t - tRate0 >= 5.0) {
  285. GLfloat seconds = t - tRate0;
  286. GLfloat fps = frames / seconds;
  287. printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
  288. fps);
  289. tRate0 = t;
  290. frames = 0;
  291. }
  292. }
  293. /* new window size or exposure */
  294. static void
  295. reshape(int width, int height)
  296. {
  297. glViewport(0, 0, (GLint) width, (GLint) height);
  298. if (stereo) {
  299. GLfloat w;
  300. asp = (GLfloat) height / (GLfloat) width;
  301. w = fix_point * (1.0 / 5.0);
  302. left = -5.0 * ((w - 0.5 * eyesep) / fix_point);
  303. right = 5.0 * ((w + 0.5 * eyesep) / fix_point);
  304. }
  305. else {
  306. GLfloat h = (GLfloat) height / (GLfloat) width;
  307. glMatrixMode(GL_PROJECTION);
  308. glLoadIdentity();
  309. glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
  310. }
  311. glMatrixMode(GL_MODELVIEW);
  312. glLoadIdentity();
  313. glTranslatef(0.0, 0.0, -40.0);
  314. }
  315. static void
  316. init(void)
  317. {
  318. static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
  319. static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
  320. static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
  321. static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
  322. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  323. glEnable(GL_CULL_FACE);
  324. glEnable(GL_LIGHTING);
  325. glEnable(GL_LIGHT0);
  326. glEnable(GL_DEPTH_TEST);
  327. /* make the gears */
  328. gear1 = glGenLists(1);
  329. glNewList(gear1, GL_COMPILE);
  330. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  331. gear(1.0, 4.0, 1.0, 20, 0.7);
  332. glEndList();
  333. gear2 = glGenLists(1);
  334. glNewList(gear2, GL_COMPILE);
  335. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  336. gear(0.5, 2.0, 2.0, 10, 0.7);
  337. glEndList();
  338. gear3 = glGenLists(1);
  339. glNewList(gear3, GL_COMPILE);
  340. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  341. gear(1.3, 2.0, 0.5, 10, 0.7);
  342. glEndList();
  343. glEnable(GL_NORMALIZE);
  344. }
  345. /**
  346. * Remove window border/decorations.
  347. */
  348. static void
  349. no_border( Display *dpy, Window w)
  350. {
  351. static const unsigned MWM_HINTS_DECORATIONS = (1 << 1);
  352. static const int PROP_MOTIF_WM_HINTS_ELEMENTS = 5;
  353. typedef struct
  354. {
  355. unsigned long flags;
  356. unsigned long functions;
  357. unsigned long decorations;
  358. long inputMode;
  359. unsigned long status;
  360. } PropMotifWmHints;
  361. PropMotifWmHints motif_hints;
  362. Atom prop, proptype;
  363. unsigned long flags = 0;
  364. /* setup the property */
  365. motif_hints.flags = MWM_HINTS_DECORATIONS;
  366. motif_hints.decorations = flags;
  367. /* get the atom for the property */
  368. prop = XInternAtom( dpy, "_MOTIF_WM_HINTS", True );
  369. if (!prop) {
  370. /* something went wrong! */
  371. return;
  372. }
  373. /* not sure this is correct, seems to work, XA_WM_HINTS didn't work */
  374. proptype = prop;
  375. XChangeProperty( dpy, w, /* display, window */
  376. prop, proptype, /* property, type */
  377. 32, /* format: 32-bit datums */
  378. PropModeReplace, /* mode */
  379. (unsigned char *) &motif_hints, /* data */
  380. PROP_MOTIF_WM_HINTS_ELEMENTS /* nelements */
  381. );
  382. }
  383. /*
  384. * Create an RGB, double-buffered window.
  385. * Return the window and context handles.
  386. */
  387. static void
  388. make_window( Display *dpy, const char *name,
  389. int x, int y, int width, int height,
  390. Window *winRet, GLXContext *ctxRet)
  391. {
  392. int attribs[] = { GLX_RGBA,
  393. GLX_RED_SIZE, 1,
  394. GLX_GREEN_SIZE, 1,
  395. GLX_BLUE_SIZE, 1,
  396. GLX_DOUBLEBUFFER,
  397. GLX_DEPTH_SIZE, 1,
  398. None };
  399. int stereoAttribs[] = { GLX_RGBA,
  400. GLX_RED_SIZE, 1,
  401. GLX_GREEN_SIZE, 1,
  402. GLX_BLUE_SIZE, 1,
  403. GLX_DOUBLEBUFFER,
  404. GLX_DEPTH_SIZE, 1,
  405. GLX_STEREO,
  406. None };
  407. int scrnum;
  408. XSetWindowAttributes attr;
  409. unsigned long mask;
  410. Window root;
  411. Window win;
  412. GLXContext ctx;
  413. XVisualInfo *visinfo;
  414. scrnum = DefaultScreen( dpy );
  415. root = RootWindow( dpy, scrnum );
  416. if (fullscreen) {
  417. x = 0; y = 0;
  418. width = DisplayWidth( dpy, scrnum );
  419. height = DisplayHeight( dpy, scrnum );
  420. }
  421. if (stereo)
  422. visinfo = glXChooseVisual( dpy, scrnum, stereoAttribs );
  423. else
  424. visinfo = glXChooseVisual( dpy, scrnum, attribs );
  425. if (!visinfo) {
  426. if (stereo) {
  427. printf("Error: couldn't get an RGB, "
  428. "Double-buffered, Stereo visual\n");
  429. } else
  430. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  431. exit(1);
  432. }
  433. /* window attributes */
  434. attr.background_pixel = 0;
  435. attr.border_pixel = 0;
  436. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  437. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  438. /* XXX this is a bad way to get a borderless window! */
  439. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  440. win = XCreateWindow( dpy, root, x, y, width, height,
  441. 0, visinfo->depth, InputOutput,
  442. visinfo->visual, mask, &attr );
  443. if (fullscreen)
  444. no_border(dpy, win);
  445. /* set hints and properties */
  446. {
  447. XSizeHints sizehints;
  448. sizehints.x = x;
  449. sizehints.y = y;
  450. sizehints.width = width;
  451. sizehints.height = height;
  452. sizehints.flags = USSize | USPosition;
  453. XSetNormalHints(dpy, win, &sizehints);
  454. XSetStandardProperties(dpy, win, name, name,
  455. None, (char **)NULL, 0, &sizehints);
  456. }
  457. ctx = glXCreateContext( dpy, visinfo, NULL, True );
  458. if (!ctx) {
  459. printf("Error: glXCreateContext failed\n");
  460. exit(1);
  461. }
  462. XFree(visinfo);
  463. *winRet = win;
  464. *ctxRet = ctx;
  465. }
  466. /**
  467. * Determine whether or not a GLX extension is supported.
  468. */
  469. static int
  470. is_glx_extension_supported(Display *dpy, const char *query)
  471. {
  472. const int scrnum = DefaultScreen(dpy);
  473. const char *glx_extensions = NULL;
  474. const size_t len = strlen(query);
  475. const char *ptr;
  476. if (glx_extensions == NULL) {
  477. glx_extensions = glXQueryExtensionsString(dpy, scrnum);
  478. }
  479. ptr = strstr(glx_extensions, query);
  480. return ((ptr != NULL) && ((ptr[len] == ' ') || (ptr[len] == '\0')));
  481. }
  482. /**
  483. * Attempt to determine whether or not the display is synched to vblank.
  484. */
  485. static void
  486. query_vsync(Display *dpy)
  487. {
  488. int interval = 0;
  489. if (is_glx_extension_supported(dpy, "GLX_MESA_swap_control")) {
  490. PFNGLXGETSWAPINTERVALMESAPROC pglXGetSwapIntervalMESA =
  491. (PFNGLXGETSWAPINTERVALMESAPROC)
  492. glXGetProcAddressARB((const GLubyte *) "glXGetSwapIntervalMESA");
  493. interval = (*pglXGetSwapIntervalMESA)();
  494. } else if (is_glx_extension_supported(dpy, "GLX_SGI_swap_control")) {
  495. /* The default swap interval with this extension is 1. Assume that it
  496. * is set to the default.
  497. *
  498. * Many Mesa-based drivers default to 0, but all of these drivers also
  499. * export GLX_MESA_swap_control. In that case, this branch will never
  500. * be taken, and the correct result should be reported.
  501. */
  502. interval = 1;
  503. }
  504. if (interval > 0) {
  505. printf("Running synchronized to the vertical refresh. The framerate should be\n");
  506. if (interval == 1) {
  507. printf("approximately the same as the monitor refresh rate.\n");
  508. } else if (interval > 1) {
  509. printf("approximately 1/%d the monitor refresh rate.\n",
  510. interval);
  511. }
  512. }
  513. }
  514. /**
  515. * Handle one X event.
  516. * \return NOP, EXIT or DRAW
  517. */
  518. static int
  519. handle_event(Display *dpy, Window win, XEvent *event)
  520. {
  521. (void) dpy;
  522. (void) win;
  523. switch (event->type) {
  524. case Expose:
  525. return DRAW;
  526. case ConfigureNotify:
  527. reshape(event->xconfigure.width, event->xconfigure.height);
  528. break;
  529. case KeyPress:
  530. {
  531. char buffer[10];
  532. int r, code;
  533. code = XLookupKeysym(&event->xkey, 0);
  534. if (code == XK_Left) {
  535. view_roty += 5.0;
  536. }
  537. else if (code == XK_Right) {
  538. view_roty -= 5.0;
  539. }
  540. else if (code == XK_Up) {
  541. view_rotx += 5.0;
  542. }
  543. else if (code == XK_Down) {
  544. view_rotx -= 5.0;
  545. }
  546. else {
  547. r = XLookupString(&event->xkey, buffer, sizeof(buffer),
  548. NULL, NULL);
  549. if (buffer[0] == 27) {
  550. /* escape */
  551. return EXIT;
  552. }
  553. else if (buffer[0] == 'a' || buffer[0] == 'A') {
  554. animate = !animate;
  555. }
  556. }
  557. return DRAW;
  558. }
  559. }
  560. return NOP;
  561. }
  562. static void
  563. event_loop(Display *dpy, Window win)
  564. {
  565. while (1) {
  566. int op;
  567. while (!animate || XPending(dpy) > 0) {
  568. XEvent event;
  569. XNextEvent(dpy, &event);
  570. op = handle_event(dpy, win, &event);
  571. if (op == EXIT)
  572. return;
  573. else if (op == DRAW)
  574. break;
  575. }
  576. draw_frame(dpy, win);
  577. }
  578. }
  579. static void
  580. usage(void)
  581. {
  582. printf("Usage:\n");
  583. printf(" -display <displayname> set the display to run on\n");
  584. printf(" -stereo run in stereo mode\n");
  585. printf(" -fullscreen run in fullscreen mode\n");
  586. printf(" -info display OpenGL renderer info\n");
  587. printf(" -geometry WxH+X+Y window geometry\n");
  588. }
  589. int
  590. main(int argc, char *argv[])
  591. {
  592. unsigned int winWidth = 300, winHeight = 300;
  593. int x = 0, y = 0;
  594. Display *dpy;
  595. Window win;
  596. GLXContext ctx;
  597. char *dpyName = NULL;
  598. GLboolean printInfo = GL_FALSE;
  599. int i;
  600. for (i = 1; i < argc; i++) {
  601. if (strcmp(argv[i], "-display") == 0) {
  602. dpyName = argv[i+1];
  603. i++;
  604. }
  605. else if (strcmp(argv[i], "-info") == 0) {
  606. printInfo = GL_TRUE;
  607. }
  608. else if (strcmp(argv[i], "-stereo") == 0) {
  609. stereo = GL_TRUE;
  610. }
  611. else if (strcmp(argv[i], "-fullscreen") == 0) {
  612. fullscreen = GL_TRUE;
  613. }
  614. else if (i < argc-1 && strcmp(argv[i], "-geometry") == 0) {
  615. XParseGeometry(argv[i+1], &x, &y, &winWidth, &winHeight);
  616. i++;
  617. }
  618. else {
  619. usage();
  620. return -1;
  621. }
  622. }
  623. dpy = XOpenDisplay(dpyName);
  624. if (!dpy) {
  625. printf("Error: couldn't open display %s\n",
  626. dpyName ? dpyName : getenv("DISPLAY"));
  627. return -1;
  628. }
  629. make_window(dpy, "glxgears", x, y, winWidth, winHeight, &win, &ctx);
  630. XMapWindow(dpy, win);
  631. glXMakeCurrent(dpy, win, ctx);
  632. query_vsync(dpy);
  633. if (printInfo) {
  634. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  635. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  636. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  637. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  638. }
  639. init();
  640. /* Set initial projection/viewing transformation.
  641. * We can't be sure we'll get a ConfigureNotify event when the window
  642. * first appears.
  643. */
  644. reshape(winWidth, winHeight);
  645. event_loop(dpy, win);
  646. glDeleteLists(gear1, 1);
  647. glDeleteLists(gear2, 1);
  648. glDeleteLists(gear3, 1);
  649. glXDestroyContext(dpy, ctx);
  650. XDestroyWindow(dpy, win);
  651. XCloseDisplay(dpy);
  652. return 0;
  653. }