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

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