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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. *
  28. */
  29. #include <math.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <X11/Xlib.h>
  34. #include <X11/keysym.h>
  35. #include <GL/gl.h>
  36. #include <GL/glx.h>
  37. #define BENCHMARK
  38. #ifdef BENCHMARK
  39. /* XXX this probably isn't very portable */
  40. #include <sys/time.h>
  41. #include <unistd.h>
  42. /* return current time (in seconds) */
  43. static int
  44. current_time(void)
  45. {
  46. struct timeval tv;
  47. struct timezone tz;
  48. (void) gettimeofday(&tv, &tz);
  49. return (int) tv.tv_sec;
  50. }
  51. #else /*BENCHMARK*/
  52. /* dummy */
  53. static int
  54. current_time(void)
  55. {
  56. return 0;
  57. }
  58. #endif /*BENCHMARK*/
  59. #ifndef M_PI
  60. #define M_PI 3.14159265
  61. #endif
  62. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  63. static GLint gear1, gear2, gear3;
  64. static GLfloat angle = 0.0;
  65. /*
  66. *
  67. * Draw a gear wheel. You'll probably want to call this function when
  68. * building a display list since we do a lot of trig here.
  69. *
  70. * Input: inner_radius - radius of hole at center
  71. * outer_radius - radius at center of teeth
  72. * width - width of gear
  73. * teeth - number of teeth
  74. * tooth_depth - depth of tooth
  75. */
  76. static void
  77. gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  78. GLint teeth, GLfloat tooth_depth)
  79. {
  80. GLint i;
  81. GLfloat r0, r1, r2;
  82. GLfloat angle, da;
  83. GLfloat u, v, len;
  84. r0 = inner_radius;
  85. r1 = outer_radius - tooth_depth / 2.0;
  86. r2 = outer_radius + tooth_depth / 2.0;
  87. da = 2.0 * M_PI / teeth / 4.0;
  88. glShadeModel(GL_FLAT);
  89. glNormal3f(0.0, 0.0, 1.0);
  90. /* draw front face */
  91. glBegin(GL_QUAD_STRIP);
  92. for (i = 0; i <= teeth; i++) {
  93. angle = i * 2.0 * M_PI / teeth;
  94. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  95. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  96. if (i < teeth) {
  97. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  98. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  99. width * 0.5);
  100. }
  101. }
  102. glEnd();
  103. /* draw front sides of teeth */
  104. glBegin(GL_QUADS);
  105. da = 2.0 * M_PI / teeth / 4.0;
  106. for (i = 0; i < teeth; i++) {
  107. angle = i * 2.0 * M_PI / teeth;
  108. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  109. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  110. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  111. width * 0.5);
  112. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  113. width * 0.5);
  114. }
  115. glEnd();
  116. glNormal3f(0.0, 0.0, -1.0);
  117. /* draw back face */
  118. glBegin(GL_QUAD_STRIP);
  119. for (i = 0; i <= teeth; i++) {
  120. angle = i * 2.0 * M_PI / teeth;
  121. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  122. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  123. if (i < teeth) {
  124. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  125. -width * 0.5);
  126. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  127. }
  128. }
  129. glEnd();
  130. /* draw back sides of teeth */
  131. glBegin(GL_QUADS);
  132. da = 2.0 * M_PI / teeth / 4.0;
  133. for (i = 0; i < teeth; i++) {
  134. angle = i * 2.0 * M_PI / teeth;
  135. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  136. -width * 0.5);
  137. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  138. -width * 0.5);
  139. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  140. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  141. }
  142. glEnd();
  143. /* draw outward faces of teeth */
  144. glBegin(GL_QUAD_STRIP);
  145. for (i = 0; i < teeth; i++) {
  146. angle = i * 2.0 * M_PI / teeth;
  147. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  148. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  149. u = r2 * cos(angle + da) - r1 * cos(angle);
  150. v = r2 * sin(angle + da) - r1 * sin(angle);
  151. len = sqrt(u * u + v * v);
  152. u /= len;
  153. v /= len;
  154. glNormal3f(v, -u, 0.0);
  155. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  156. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  157. glNormal3f(cos(angle), sin(angle), 0.0);
  158. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  159. width * 0.5);
  160. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  161. -width * 0.5);
  162. u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
  163. v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
  164. glNormal3f(v, -u, 0.0);
  165. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  166. width * 0.5);
  167. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  168. -width * 0.5);
  169. glNormal3f(cos(angle), sin(angle), 0.0);
  170. }
  171. glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
  172. glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
  173. glEnd();
  174. glShadeModel(GL_SMOOTH);
  175. /* draw inside radius cylinder */
  176. glBegin(GL_QUAD_STRIP);
  177. for (i = 0; i <= teeth; i++) {
  178. angle = i * 2.0 * M_PI / teeth;
  179. glNormal3f(-cos(angle), -sin(angle), 0.0);
  180. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  181. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  182. }
  183. glEnd();
  184. }
  185. static void
  186. draw(void)
  187. {
  188. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  189. glPushMatrix();
  190. glRotatef(view_rotx, 1.0, 0.0, 0.0);
  191. glRotatef(view_roty, 0.0, 1.0, 0.0);
  192. glRotatef(view_rotz, 0.0, 0.0, 1.0);
  193. glPushMatrix();
  194. glTranslatef(-3.0, -2.0, 0.0);
  195. glRotatef(angle, 0.0, 0.0, 1.0);
  196. glCallList(gear1);
  197. glPopMatrix();
  198. glPushMatrix();
  199. glTranslatef(3.1, -2.0, 0.0);
  200. glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
  201. glCallList(gear2);
  202. glPopMatrix();
  203. glPushMatrix();
  204. glTranslatef(-3.1, 4.2, 0.0);
  205. glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
  206. glCallList(gear3);
  207. glPopMatrix();
  208. glPopMatrix();
  209. }
  210. /* new window size or exposure */
  211. static void
  212. reshape(int width, int height)
  213. {
  214. GLfloat h = (GLfloat) height / (GLfloat) width;
  215. glViewport(0, 0, (GLint) width, (GLint) height);
  216. glMatrixMode(GL_PROJECTION);
  217. glLoadIdentity();
  218. glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
  219. glMatrixMode(GL_MODELVIEW);
  220. glLoadIdentity();
  221. glTranslatef(0.0, 0.0, -40.0);
  222. }
  223. static void
  224. init(void)
  225. {
  226. static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
  227. static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
  228. static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
  229. static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
  230. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  231. glEnable(GL_CULL_FACE);
  232. glEnable(GL_LIGHTING);
  233. glEnable(GL_LIGHT0);
  234. glEnable(GL_DEPTH_TEST);
  235. /* make the gears */
  236. gear1 = glGenLists(1);
  237. glNewList(gear1, GL_COMPILE);
  238. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  239. gear(1.0, 4.0, 1.0, 20, 0.7);
  240. glEndList();
  241. gear2 = glGenLists(1);
  242. glNewList(gear2, GL_COMPILE);
  243. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  244. gear(0.5, 2.0, 2.0, 10, 0.7);
  245. glEndList();
  246. gear3 = glGenLists(1);
  247. glNewList(gear3, GL_COMPILE);
  248. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  249. gear(1.3, 2.0, 0.5, 10, 0.7);
  250. glEndList();
  251. glEnable(GL_NORMALIZE);
  252. }
  253. /*
  254. * Create an RGB, double-buffered window.
  255. * Return the window and context handles.
  256. */
  257. static void
  258. make_window( Display *dpy, const char *name,
  259. int x, int y, int width, int height,
  260. Window *winRet, GLXContext *ctxRet)
  261. {
  262. int attrib[] = { GLX_RGBA,
  263. GLX_RED_SIZE, 1,
  264. GLX_GREEN_SIZE, 1,
  265. GLX_BLUE_SIZE, 1,
  266. GLX_DOUBLEBUFFER,
  267. GLX_DEPTH_SIZE, 1,
  268. None };
  269. int scrnum;
  270. XSetWindowAttributes attr;
  271. unsigned long mask;
  272. Window root;
  273. Window win;
  274. GLXContext ctx;
  275. XVisualInfo *visinfo;
  276. scrnum = DefaultScreen( dpy );
  277. root = RootWindow( dpy, scrnum );
  278. visinfo = glXChooseVisual( dpy, scrnum, attrib );
  279. if (!visinfo) {
  280. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  281. exit(1);
  282. }
  283. /* window attributes */
  284. attr.background_pixel = 0;
  285. attr.border_pixel = 0;
  286. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  287. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  288. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  289. win = XCreateWindow( dpy, root, 0, 0, width, height,
  290. 0, visinfo->depth, InputOutput,
  291. visinfo->visual, mask, &attr );
  292. /* set hints and properties */
  293. {
  294. XSizeHints sizehints;
  295. sizehints.x = x;
  296. sizehints.y = y;
  297. sizehints.width = width;
  298. sizehints.height = height;
  299. sizehints.flags = USSize | USPosition;
  300. XSetNormalHints(dpy, win, &sizehints);
  301. XSetStandardProperties(dpy, win, name, name,
  302. None, (char **)NULL, 0, &sizehints);
  303. }
  304. ctx = glXCreateContext( dpy, visinfo, NULL, True );
  305. if (!ctx) {
  306. printf("Error: glXCreateContext failed\n");
  307. exit(1);
  308. }
  309. XFree(visinfo);
  310. *winRet = win;
  311. *ctxRet = ctx;
  312. }
  313. static void
  314. event_loop(Display *dpy, Window win)
  315. {
  316. while (1) {
  317. while (XPending(dpy) > 0) {
  318. XEvent event;
  319. XNextEvent(dpy, &event);
  320. switch (event.type) {
  321. case Expose:
  322. /* we'll redraw below */
  323. break;
  324. case ConfigureNotify:
  325. reshape(event.xconfigure.width, event.xconfigure.height);
  326. break;
  327. case KeyPress:
  328. {
  329. char buffer[10];
  330. int r, code;
  331. code = XLookupKeysym(&event.xkey, 0);
  332. if (code == XK_Left) {
  333. view_roty += 5.0;
  334. }
  335. else if (code == XK_Right) {
  336. view_roty -= 5.0;
  337. }
  338. else if (code == XK_Up) {
  339. view_rotx += 5.0;
  340. }
  341. else if (code == XK_Down) {
  342. view_rotx -= 5.0;
  343. }
  344. else {
  345. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  346. NULL, NULL);
  347. if (buffer[0] == 27) {
  348. /* escape */
  349. return;
  350. }
  351. }
  352. }
  353. }
  354. }
  355. /* next frame */
  356. angle += 2.0;
  357. draw();
  358. glXSwapBuffers(dpy, win);
  359. /* calc framerate */
  360. {
  361. static int t0 = -1;
  362. static int frames = 0;
  363. int t = current_time();
  364. if (t0 < 0)
  365. t0 = t;
  366. frames++;
  367. if (t - t0 >= 5.0) {
  368. GLfloat seconds = t - t0;
  369. GLfloat fps = frames / seconds;
  370. printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
  371. fps);
  372. t0 = t;
  373. frames = 0;
  374. }
  375. }
  376. }
  377. }
  378. int
  379. main(int argc, char *argv[])
  380. {
  381. Display *dpy;
  382. Window win;
  383. GLXContext ctx;
  384. char *dpyName = ":0";
  385. GLboolean printInfo = GL_FALSE;
  386. int i;
  387. for (i = 1; i < argc; i++) {
  388. if (strcmp(argv[i], "-display") == 0) {
  389. dpyName = argv[i+1];
  390. i++;
  391. }
  392. else if (strcmp(argv[i], "-info") == 0) {
  393. printInfo = GL_TRUE;
  394. }
  395. }
  396. dpy = XOpenDisplay(dpyName);
  397. if (!dpy) {
  398. printf("Error: couldn't open display %s\n", dpyName);
  399. return -1;
  400. }
  401. make_window(dpy, "glxgears", 0, 0, 300, 300, &win, &ctx);
  402. XMapWindow(dpy, win);
  403. glXMakeCurrent(dpy, win, ctx);
  404. if (printInfo) {
  405. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  406. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  407. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  408. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  409. }
  410. init();
  411. event_loop(dpy, win);
  412. glXDestroyContext(dpy, ctx);
  413. XDestroyWindow(dpy, win);
  414. XCloseDisplay(dpy);
  415. return 0;
  416. }