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.

glxcontexts.c 16KB

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