Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

glxgears.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. * Command line options:
  26. * -info print GL implementation information
  27. * -stereo use stereo enabled GLX visual
  28. *
  29. */
  30. #include <math.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <X11/Xlib.h>
  35. #include <X11/keysym.h>
  36. #include <GL/gl.h>
  37. #include <GL/glx.h>
  38. #define BENCHMARK
  39. #ifdef BENCHMARK
  40. /* XXX this probably isn't very portable */
  41. #include <sys/time.h>
  42. #include <unistd.h>
  43. /* return current time (in seconds) */
  44. static double
  45. current_time(void)
  46. {
  47. struct timeval tv;
  48. #ifdef __VMS
  49. (void) gettimeofday(&tv, NULL );
  50. #else
  51. struct timezone tz;
  52. (void) gettimeofday(&tv, &tz);
  53. #endif
  54. return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
  55. }
  56. #else /*BENCHMARK*/
  57. /* dummy */
  58. static double
  59. current_time(void)
  60. {
  61. /* update this function for other platforms! */
  62. static double t = 0.0;
  63. static int warn = 1;
  64. if (warn) {
  65. fprintf(stderr, "Warning: current_time() not implemented!!\n");
  66. warn = 0;
  67. }
  68. return t += 1.0;
  69. }
  70. #endif /*BENCHMARK*/
  71. #ifndef M_PI
  72. #define M_PI 3.14159265
  73. #endif
  74. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  75. static GLint gear1, gear2, gear3;
  76. static GLfloat angle = 0.0;
  77. static GLboolean fullscreen = GL_FALSE; /* Create a single fullscreen window */
  78. static GLboolean stereo = GL_FALSE; /* Enable stereo. */
  79. static GLfloat eyesep = 5.0; /* Eye separation. */
  80. static GLfloat fix_point = 40.0; /* Fixation point distance. */
  81. static GLfloat left, right, asp; /* Stereo frustum params. */
  82. /*
  83. *
  84. * Draw a gear wheel. You'll probably want to call this function when
  85. * building a display list since we do a lot of trig here.
  86. *
  87. * Input: inner_radius - radius of hole at center
  88. * outer_radius - radius at center of teeth
  89. * width - width of gear
  90. * teeth - number of teeth
  91. * tooth_depth - depth of tooth
  92. */
  93. static void
  94. gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  95. GLint teeth, GLfloat tooth_depth)
  96. {
  97. GLint i;
  98. GLfloat r0, r1, r2;
  99. GLfloat angle, da;
  100. GLfloat u, v, len;
  101. r0 = inner_radius;
  102. r1 = outer_radius - tooth_depth / 2.0;
  103. r2 = outer_radius + tooth_depth / 2.0;
  104. da = 2.0 * M_PI / teeth / 4.0;
  105. glShadeModel(GL_FLAT);
  106. glNormal3f(0.0, 0.0, 1.0);
  107. /* draw front face */
  108. glBegin(GL_QUAD_STRIP);
  109. for (i = 0; i <= teeth; i++) {
  110. angle = i * 2.0 * M_PI / teeth;
  111. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  112. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  113. if (i < teeth) {
  114. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  115. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  116. width * 0.5);
  117. }
  118. }
  119. glEnd();
  120. /* draw front sides of teeth */
  121. glBegin(GL_QUADS);
  122. da = 2.0 * M_PI / teeth / 4.0;
  123. for (i = 0; i < teeth; i++) {
  124. angle = i * 2.0 * M_PI / teeth;
  125. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  126. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  127. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  128. width * 0.5);
  129. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  130. width * 0.5);
  131. }
  132. glEnd();
  133. glNormal3f(0.0, 0.0, -1.0);
  134. /* draw back face */
  135. glBegin(GL_QUAD_STRIP);
  136. for (i = 0; i <= teeth; i++) {
  137. angle = i * 2.0 * M_PI / teeth;
  138. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  139. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  140. if (i < teeth) {
  141. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  142. -width * 0.5);
  143. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  144. }
  145. }
  146. glEnd();
  147. /* draw back sides of teeth */
  148. glBegin(GL_QUADS);
  149. da = 2.0 * M_PI / teeth / 4.0;
  150. for (i = 0; i < teeth; i++) {
  151. angle = i * 2.0 * M_PI / teeth;
  152. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  153. -width * 0.5);
  154. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  155. -width * 0.5);
  156. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  157. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  158. }
  159. glEnd();
  160. /* draw outward faces of teeth */
  161. glBegin(GL_QUAD_STRIP);
  162. for (i = 0; i < teeth; i++) {
  163. angle = i * 2.0 * M_PI / teeth;
  164. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  165. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  166. u = r2 * cos(angle + da) - r1 * cos(angle);
  167. v = r2 * sin(angle + da) - r1 * sin(angle);
  168. len = sqrt(u * u + v * v);
  169. u /= len;
  170. v /= len;
  171. glNormal3f(v, -u, 0.0);
  172. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  173. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  174. glNormal3f(cos(angle), sin(angle), 0.0);
  175. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  176. width * 0.5);
  177. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  178. -width * 0.5);
  179. u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
  180. v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
  181. glNormal3f(v, -u, 0.0);
  182. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  183. width * 0.5);
  184. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  185. -width * 0.5);
  186. glNormal3f(cos(angle), sin(angle), 0.0);
  187. }
  188. glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
  189. glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
  190. glEnd();
  191. glShadeModel(GL_SMOOTH);
  192. /* draw inside radius cylinder */
  193. glBegin(GL_QUAD_STRIP);
  194. for (i = 0; i <= teeth; i++) {
  195. angle = i * 2.0 * M_PI / teeth;
  196. glNormal3f(-cos(angle), -sin(angle), 0.0);
  197. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  198. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  199. }
  200. glEnd();
  201. }
  202. static void
  203. do_draw(void)
  204. {
  205. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  206. glPushMatrix();
  207. glRotatef(view_rotx, 1.0, 0.0, 0.0);
  208. glRotatef(view_roty, 0.0, 1.0, 0.0);
  209. glRotatef(view_rotz, 0.0, 0.0, 1.0);
  210. glPushMatrix();
  211. glTranslatef(-3.0, -2.0, 0.0);
  212. glRotatef(angle, 0.0, 0.0, 1.0);
  213. glCallList(gear1);
  214. glPopMatrix();
  215. glPushMatrix();
  216. glTranslatef(3.1, -2.0, 0.0);
  217. glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
  218. glCallList(gear2);
  219. glPopMatrix();
  220. glPushMatrix();
  221. glTranslatef(-3.1, 4.2, 0.0);
  222. glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
  223. glCallList(gear3);
  224. glPopMatrix();
  225. glPopMatrix();
  226. }
  227. static void
  228. draw(void)
  229. {
  230. if (stereo) {
  231. /* First left eye. */
  232. glDrawBuffer(GL_BACK_LEFT);
  233. glMatrixMode(GL_PROJECTION);
  234. glLoadIdentity();
  235. glFrustum(left, right, -asp, asp, 5.0, 60.0);
  236. glMatrixMode(GL_MODELVIEW);
  237. glPushMatrix();
  238. glTranslated(+0.5 * eyesep, 0.0, 0.0);
  239. do_draw();
  240. glPopMatrix();
  241. /* Then right eye. */
  242. glDrawBuffer(GL_BACK_RIGHT);
  243. glMatrixMode(GL_PROJECTION);
  244. glLoadIdentity();
  245. glFrustum(-right, -left, -asp, asp, 5.0, 60.0);
  246. glMatrixMode(GL_MODELVIEW);
  247. glPushMatrix();
  248. glTranslated(-0.5 * eyesep, 0.0, 0.0);
  249. do_draw();
  250. glPopMatrix();
  251. } else
  252. do_draw();
  253. }
  254. /* new window size or exposure */
  255. static void
  256. reshape(int width, int height)
  257. {
  258. glViewport(0, 0, (GLint) width, (GLint) height);
  259. if (stereo) {
  260. GLfloat w;
  261. asp = (GLfloat) height / (GLfloat) width;
  262. w = fix_point * (1.0 / 5.0);
  263. left = -5.0 * ((w - 0.5 * eyesep) / fix_point);
  264. right = 5.0 * ((w + 0.5 * eyesep) / fix_point);
  265. } else {
  266. GLfloat h = (GLfloat) height / (GLfloat) width;
  267. glMatrixMode(GL_PROJECTION);
  268. glLoadIdentity();
  269. glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
  270. }
  271. glMatrixMode(GL_MODELVIEW);
  272. glLoadIdentity();
  273. glTranslatef(0.0, 0.0, -40.0);
  274. }
  275. static void
  276. init(void)
  277. {
  278. static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
  279. static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
  280. static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
  281. static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
  282. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  283. glEnable(GL_CULL_FACE);
  284. glEnable(GL_LIGHTING);
  285. glEnable(GL_LIGHT0);
  286. glEnable(GL_DEPTH_TEST);
  287. /* make the gears */
  288. gear1 = glGenLists(1);
  289. glNewList(gear1, GL_COMPILE);
  290. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  291. gear(1.0, 4.0, 1.0, 20, 0.7);
  292. glEndList();
  293. gear2 = glGenLists(1);
  294. glNewList(gear2, GL_COMPILE);
  295. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  296. gear(0.5, 2.0, 2.0, 10, 0.7);
  297. glEndList();
  298. gear3 = glGenLists(1);
  299. glNewList(gear3, GL_COMPILE);
  300. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  301. gear(1.3, 2.0, 0.5, 10, 0.7);
  302. glEndList();
  303. glEnable(GL_NORMALIZE);
  304. }
  305. /*
  306. * Create an RGB, double-buffered window.
  307. * Return the window and context handles.
  308. */
  309. static void
  310. make_window( Display *dpy, const char *name,
  311. int x, int y, int width, int height,
  312. Window *winRet, GLXContext *ctxRet)
  313. {
  314. int attribs[] = { GLX_RGBA,
  315. GLX_RED_SIZE, 1,
  316. GLX_GREEN_SIZE, 1,
  317. GLX_BLUE_SIZE, 1,
  318. GLX_DOUBLEBUFFER,
  319. GLX_DEPTH_SIZE, 1,
  320. None };
  321. int stereoAttribs[] = { GLX_RGBA,
  322. GLX_RED_SIZE, 1,
  323. GLX_GREEN_SIZE, 1,
  324. GLX_BLUE_SIZE, 1,
  325. GLX_DOUBLEBUFFER,
  326. GLX_DEPTH_SIZE, 1,
  327. GLX_STEREO,
  328. None };
  329. int scrnum;
  330. XSetWindowAttributes attr;
  331. unsigned long mask;
  332. Window root;
  333. Window win;
  334. GLXContext ctx;
  335. XVisualInfo *visinfo;
  336. scrnum = DefaultScreen( dpy );
  337. root = RootWindow( dpy, scrnum );
  338. if (fullscreen) {
  339. x = 0; y = 0;
  340. width = DisplayWidth( dpy, scrnum );
  341. height = DisplayHeight( dpy, scrnum );
  342. }
  343. if (stereo)
  344. visinfo = glXChooseVisual( dpy, scrnum, stereoAttribs );
  345. else
  346. visinfo = glXChooseVisual( dpy, scrnum, attribs );
  347. if (!visinfo) {
  348. if (stereo) {
  349. printf("Error: couldn't get an RGB, "
  350. "Double-buffered, Stereo visual\n");
  351. } else
  352. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  353. exit(1);
  354. }
  355. /* window attributes */
  356. attr.background_pixel = 0;
  357. attr.border_pixel = 0;
  358. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  359. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  360. attr.override_redirect = fullscreen;
  361. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
  362. win = XCreateWindow( dpy, root, 0, 0, width, height,
  363. 0, visinfo->depth, InputOutput,
  364. visinfo->visual, mask, &attr );
  365. /* set hints and properties */
  366. {
  367. XSizeHints sizehints;
  368. sizehints.x = x;
  369. sizehints.y = y;
  370. sizehints.width = width;
  371. sizehints.height = height;
  372. sizehints.flags = USSize | USPosition;
  373. XSetNormalHints(dpy, win, &sizehints);
  374. XSetStandardProperties(dpy, win, name, name,
  375. None, (char **)NULL, 0, &sizehints);
  376. }
  377. ctx = glXCreateContext( dpy, visinfo, NULL, True );
  378. if (!ctx) {
  379. printf("Error: glXCreateContext failed\n");
  380. exit(1);
  381. }
  382. XFree(visinfo);
  383. *winRet = win;
  384. *ctxRet = ctx;
  385. }
  386. static void
  387. event_loop(Display *dpy, Window win)
  388. {
  389. while (1) {
  390. while (XPending(dpy) > 0) {
  391. XEvent event;
  392. XNextEvent(dpy, &event);
  393. switch (event.type) {
  394. case Expose:
  395. /* we'll redraw below */
  396. break;
  397. case ConfigureNotify:
  398. reshape(event.xconfigure.width, event.xconfigure.height);
  399. break;
  400. case KeyPress:
  401. {
  402. char buffer[10];
  403. int r, code;
  404. code = XLookupKeysym(&event.xkey, 0);
  405. if (code == XK_Left) {
  406. view_roty += 5.0;
  407. }
  408. else if (code == XK_Right) {
  409. view_roty -= 5.0;
  410. }
  411. else if (code == XK_Up) {
  412. view_rotx += 5.0;
  413. }
  414. else if (code == XK_Down) {
  415. view_rotx -= 5.0;
  416. }
  417. else {
  418. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  419. NULL, NULL);
  420. if (buffer[0] == 27) {
  421. /* escape */
  422. return;
  423. }
  424. }
  425. }
  426. }
  427. }
  428. {
  429. static int frames = 0;
  430. static double tRot0 = -1.0, tRate0 = -1.0;
  431. double dt, t = current_time();
  432. if (tRot0 < 0.0)
  433. tRot0 = t;
  434. dt = t - tRot0;
  435. tRot0 = t;
  436. /* advance rotation for next frame */
  437. angle += 70.0 * dt; /* 70 degrees per second */
  438. if (angle > 3600.0)
  439. angle -= 3600.0;
  440. draw();
  441. glXSwapBuffers(dpy, win);
  442. frames++;
  443. if (tRate0 < 0.0)
  444. tRate0 = t;
  445. if (t - tRate0 >= 5.0) {
  446. GLfloat seconds = t - tRate0;
  447. GLfloat fps = frames / seconds;
  448. printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
  449. fps);
  450. tRate0 = t;
  451. frames = 0;
  452. }
  453. }
  454. }
  455. }
  456. static void
  457. usage(void)
  458. {
  459. printf("Usage:\n");
  460. printf(" -display <displayname> set the display to run on\n");
  461. printf(" -stereo run in stereo mode\n");
  462. printf(" -fullscreen run in fullscreen mode\n");
  463. printf(" -info display OpenGL renderer info\n");
  464. }
  465. int
  466. main(int argc, char *argv[])
  467. {
  468. const int winWidth = 300, winHeight = 300;
  469. Display *dpy;
  470. Window win;
  471. GLXContext ctx;
  472. char *dpyName = NULL;
  473. GLboolean printInfo = GL_FALSE;
  474. int i;
  475. for (i = 1; i < argc; i++) {
  476. if (strcmp(argv[i], "-display") == 0) {
  477. dpyName = argv[i+1];
  478. i++;
  479. }
  480. else if (strcmp(argv[i], "-info") == 0) {
  481. printInfo = GL_TRUE;
  482. }
  483. else if (strcmp(argv[i], "-stereo") == 0) {
  484. stereo = GL_TRUE;
  485. }
  486. else if (strcmp(argv[i], "-fullscreen") == 0) {
  487. fullscreen = GL_TRUE;
  488. }
  489. else {
  490. usage();
  491. return -1;
  492. }
  493. }
  494. dpy = XOpenDisplay(dpyName);
  495. if (!dpy) {
  496. printf("Error: couldn't open display %s\n",
  497. dpyName ? dpyName : getenv("DISPLAY"));
  498. return -1;
  499. }
  500. make_window(dpy, "glxgears", 0, 0, winWidth, winHeight, &win, &ctx);
  501. XMapWindow(dpy, win);
  502. glXMakeCurrent(dpy, win, ctx);
  503. if (printInfo) {
  504. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  505. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  506. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  507. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  508. }
  509. init();
  510. /* Set initial projection/viewing transformation.
  511. * We can't be sure we'll get a ConfigureNotify event when the window
  512. * first appears.
  513. */
  514. reshape(winWidth, winHeight);
  515. event_loop(dpy, win);
  516. glDeleteLists(gear1, 1);
  517. glDeleteLists(gear2, 1);
  518. glDeleteLists(gear3, 1);
  519. glXDestroyContext(dpy, ctx);
  520. XDestroyWindow(dpy, win);
  521. XCloseDisplay(dpy);
  522. return 0;
  523. }