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.

auxbuffer.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Test AUX buffer rendering
  3. * Use GLX since GLUT doesn't support AUX buffers
  4. */
  5. /*
  6. * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. /*
  26. * This is a port of the infamous "gears" demo to straight GLX (i.e. no GLUT)
  27. * Port by Brian Paul 23 March 2001
  28. *
  29. * Command line options:
  30. * -info print GL implementation information
  31. *
  32. */
  33. #include <math.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <X11/Xlib.h>
  38. #include <X11/keysym.h>
  39. #include <GL/gl.h>
  40. #include <GL/glx.h>
  41. static int
  42. current_time(void)
  43. {
  44. return 0;
  45. }
  46. #ifndef M_PI
  47. #define M_PI 3.14159265
  48. #endif
  49. static int WinWidth = 300, WinHeight = 300;
  50. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  51. static GLint gear1, gear2, gear3;
  52. static GLfloat angle = 0.0;
  53. /*
  54. *
  55. * Draw a gear wheel. You'll probably want to call this function when
  56. * building a display list since we do a lot of trig here.
  57. *
  58. * Input: inner_radius - radius of hole at center
  59. * outer_radius - radius at center of teeth
  60. * width - width of gear
  61. * teeth - number of teeth
  62. * tooth_depth - depth of tooth
  63. */
  64. static void
  65. gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  66. GLint teeth, GLfloat tooth_depth)
  67. {
  68. GLint i;
  69. GLfloat r0, r1, r2;
  70. GLfloat angle, da;
  71. GLfloat u, v, len;
  72. r0 = inner_radius;
  73. r1 = outer_radius - tooth_depth / 2.0;
  74. r2 = outer_radius + tooth_depth / 2.0;
  75. da = 2.0 * M_PI / teeth / 4.0;
  76. glShadeModel(GL_FLAT);
  77. glNormal3f(0.0, 0.0, 1.0);
  78. /* draw front face */
  79. glBegin(GL_QUAD_STRIP);
  80. for (i = 0; i <= teeth; i++) {
  81. angle = i * 2.0 * M_PI / teeth;
  82. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  83. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  84. if (i < teeth) {
  85. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  86. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  87. width * 0.5);
  88. }
  89. }
  90. glEnd();
  91. /* draw front sides of teeth */
  92. glBegin(GL_QUADS);
  93. da = 2.0 * M_PI / teeth / 4.0;
  94. for (i = 0; i < teeth; i++) {
  95. angle = i * 2.0 * M_PI / teeth;
  96. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  97. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  98. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  99. width * 0.5);
  100. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  101. width * 0.5);
  102. }
  103. glEnd();
  104. glNormal3f(0.0, 0.0, -1.0);
  105. /* draw back face */
  106. glBegin(GL_QUAD_STRIP);
  107. for (i = 0; i <= teeth; i++) {
  108. angle = i * 2.0 * M_PI / teeth;
  109. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  110. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  111. if (i < teeth) {
  112. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  113. -width * 0.5);
  114. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  115. }
  116. }
  117. glEnd();
  118. /* draw back sides of teeth */
  119. glBegin(GL_QUADS);
  120. da = 2.0 * M_PI / teeth / 4.0;
  121. for (i = 0; i < teeth; i++) {
  122. angle = i * 2.0 * M_PI / teeth;
  123. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  124. -width * 0.5);
  125. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  126. -width * 0.5);
  127. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  128. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  129. }
  130. glEnd();
  131. /* draw outward faces of teeth */
  132. glBegin(GL_QUAD_STRIP);
  133. for (i = 0; i < teeth; i++) {
  134. angle = i * 2.0 * M_PI / teeth;
  135. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  136. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  137. u = r2 * cos(angle + da) - r1 * cos(angle);
  138. v = r2 * sin(angle + da) - r1 * sin(angle);
  139. len = sqrt(u * u + v * v);
  140. u /= len;
  141. v /= len;
  142. glNormal3f(v, -u, 0.0);
  143. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  144. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  145. glNormal3f(cos(angle), sin(angle), 0.0);
  146. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  147. width * 0.5);
  148. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  149. -width * 0.5);
  150. u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
  151. v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
  152. glNormal3f(v, -u, 0.0);
  153. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  154. width * 0.5);
  155. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  156. -width * 0.5);
  157. glNormal3f(cos(angle), sin(angle), 0.0);
  158. }
  159. glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
  160. glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
  161. glEnd();
  162. glShadeModel(GL_SMOOTH);
  163. /* draw inside radius cylinder */
  164. glBegin(GL_QUAD_STRIP);
  165. for (i = 0; i <= teeth; i++) {
  166. angle = i * 2.0 * M_PI / teeth;
  167. glNormal3f(-cos(angle), -sin(angle), 0.0);
  168. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  169. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  170. }
  171. glEnd();
  172. }
  173. static void
  174. draw(void)
  175. {
  176. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  177. glPushMatrix();
  178. glRotatef(view_rotx, 1.0, 0.0, 0.0);
  179. glRotatef(view_roty, 0.0, 1.0, 0.0);
  180. glRotatef(view_rotz, 0.0, 0.0, 1.0);
  181. glPushMatrix();
  182. glTranslatef(-3.0, -2.0, 0.0);
  183. glRotatef(angle, 0.0, 0.0, 1.0);
  184. glCallList(gear1);
  185. glPopMatrix();
  186. glPushMatrix();
  187. glTranslatef(3.1, -2.0, 0.0);
  188. glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
  189. glCallList(gear2);
  190. glPopMatrix();
  191. glPushMatrix();
  192. glTranslatef(-3.1, 4.2, 0.0);
  193. glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
  194. glCallList(gear3);
  195. glPopMatrix();
  196. glPopMatrix();
  197. }
  198. /* new window size or exposure */
  199. static void
  200. reshape(int width, int height)
  201. {
  202. GLfloat h = (GLfloat) height / (GLfloat) width;
  203. WinWidth = width;
  204. WinHeight = height;
  205. glViewport(0, 0, (GLint) width, (GLint) height);
  206. glMatrixMode(GL_PROJECTION);
  207. glLoadIdentity();
  208. glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
  209. glMatrixMode(GL_MODELVIEW);
  210. glLoadIdentity();
  211. glTranslatef(0.0, 0.0, -40.0);
  212. }
  213. static void
  214. init(void)
  215. {
  216. static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
  217. static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
  218. static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
  219. static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
  220. int i;
  221. glGetIntegerv(GL_AUX_BUFFERS, &i);
  222. printf("AUX BUFFERS: %d\n", i);
  223. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  224. glEnable(GL_CULL_FACE);
  225. glEnable(GL_LIGHTING);
  226. glEnable(GL_LIGHT0);
  227. glEnable(GL_DEPTH_TEST);
  228. /* make the gears */
  229. gear1 = glGenLists(1);
  230. glNewList(gear1, GL_COMPILE);
  231. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  232. gear(1.0, 4.0, 1.0, 20, 0.7);
  233. glEndList();
  234. gear2 = glGenLists(1);
  235. glNewList(gear2, GL_COMPILE);
  236. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  237. gear(0.5, 2.0, 2.0, 10, 0.7);
  238. glEndList();
  239. gear3 = glGenLists(1);
  240. glNewList(gear3, GL_COMPILE);
  241. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  242. gear(1.3, 2.0, 0.5, 10, 0.7);
  243. glEndList();
  244. glEnable(GL_NORMALIZE);
  245. }
  246. /*
  247. * Create an RGB, double-buffered window.
  248. * Return the window and context handles.
  249. */
  250. static void
  251. make_window( Display *dpy, const char *name,
  252. int x, int y, int width, int height,
  253. Window *winRet, GLXContext *ctxRet)
  254. {
  255. int attrib[] = { GLX_RGBA,
  256. GLX_RED_SIZE, 1,
  257. GLX_GREEN_SIZE, 1,
  258. GLX_BLUE_SIZE, 1,
  259. GLX_DOUBLEBUFFER,
  260. GLX_DEPTH_SIZE, 1,
  261. GLX_AUX_BUFFERS, 1,
  262. None };
  263. int scrnum;
  264. XSetWindowAttributes attr;
  265. unsigned long mask;
  266. Window root;
  267. Window win;
  268. GLXContext ctx;
  269. XVisualInfo *visinfo;
  270. scrnum = DefaultScreen( dpy );
  271. root = RootWindow( dpy, scrnum );
  272. visinfo = glXChooseVisual( dpy, scrnum, attrib );
  273. if (!visinfo) {
  274. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  275. exit(1);
  276. }
  277. /* window attributes */
  278. attr.background_pixel = 0;
  279. attr.border_pixel = 0;
  280. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  281. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  282. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  283. win = XCreateWindow( dpy, root, 0, 0, width, height,
  284. 0, visinfo->depth, InputOutput,
  285. visinfo->visual, mask, &attr );
  286. /* set hints and properties */
  287. {
  288. XSizeHints sizehints;
  289. sizehints.x = x;
  290. sizehints.y = y;
  291. sizehints.width = width;
  292. sizehints.height = height;
  293. sizehints.flags = USSize | USPosition;
  294. XSetNormalHints(dpy, win, &sizehints);
  295. XSetStandardProperties(dpy, win, name, name,
  296. None, (char **)NULL, 0, &sizehints);
  297. }
  298. ctx = glXCreateContext( dpy, visinfo, NULL, True );
  299. if (!ctx) {
  300. printf("Error: glXCreateContext failed\n");
  301. exit(1);
  302. }
  303. XFree(visinfo);
  304. *winRet = win;
  305. *ctxRet = ctx;
  306. }
  307. static void
  308. event_loop(Display *dpy, Window win)
  309. {
  310. while (1) {
  311. while (XPending(dpy) > 0) {
  312. XEvent event;
  313. XNextEvent(dpy, &event);
  314. switch (event.type) {
  315. case Expose:
  316. /* we'll redraw below */
  317. break;
  318. case ConfigureNotify:
  319. reshape(event.xconfigure.width, event.xconfigure.height);
  320. break;
  321. case KeyPress:
  322. {
  323. char buffer[10];
  324. int r, code;
  325. code = XLookupKeysym(&event.xkey, 0);
  326. if (code == XK_Left) {
  327. view_roty += 5.0;
  328. }
  329. else if (code == XK_Right) {
  330. view_roty -= 5.0;
  331. }
  332. else if (code == XK_Up) {
  333. view_rotx += 5.0;
  334. }
  335. else if (code == XK_Down) {
  336. view_rotx -= 5.0;
  337. }
  338. else {
  339. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  340. NULL, NULL);
  341. if (buffer[0] == 27) {
  342. /* escape */
  343. return;
  344. }
  345. }
  346. }
  347. }
  348. }
  349. /* next frame */
  350. angle += 2.0;
  351. /* draw to aux buffer */
  352. glDrawBuffer(GL_AUX0);
  353. draw();
  354. /* Copy aux buffer image to back color buffer */
  355. glReadBuffer(GL_AUX0);
  356. glDrawBuffer(GL_BACK);
  357. glWindowPos2iARB(0, 0);
  358. glDisable(GL_DEPTH_TEST);
  359. glCopyPixels(0, 0, WinWidth, WinHeight, GL_COLOR);
  360. glEnable(GL_DEPTH_TEST);
  361. glXSwapBuffers(dpy, win);
  362. /* calc framerate */
  363. {
  364. static int t0 = -1;
  365. static int frames = 0;
  366. int t = current_time();
  367. if (t0 < 0)
  368. t0 = t;
  369. frames++;
  370. if (t - t0 >= 5.0) {
  371. GLfloat seconds = t - t0;
  372. GLfloat fps = frames / seconds;
  373. printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
  374. fps);
  375. t0 = t;
  376. frames = 0;
  377. }
  378. }
  379. }
  380. }
  381. int
  382. main(int argc, char *argv[])
  383. {
  384. Display *dpy;
  385. Window win;
  386. GLXContext ctx;
  387. char *dpyName = ":0";
  388. GLboolean printInfo = GL_FALSE;
  389. int i;
  390. for (i = 1; i < argc; i++) {
  391. if (strcmp(argv[i], "-display") == 0) {
  392. dpyName = argv[i+1];
  393. i++;
  394. }
  395. else if (strcmp(argv[i], "-info") == 0) {
  396. printInfo = GL_TRUE;
  397. }
  398. }
  399. dpy = XOpenDisplay(dpyName);
  400. if (!dpy) {
  401. printf("Error: couldn't open display %s\n", dpyName);
  402. return -1;
  403. }
  404. make_window(dpy, "glxgears", 0, 0, WinWidth, WinHeight, &win, &ctx);
  405. XMapWindow(dpy, win);
  406. glXMakeCurrent(dpy, win, ctx);
  407. if (printInfo) {
  408. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  409. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  410. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  411. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  412. }
  413. init();
  414. event_loop(dpy, win);
  415. glXDestroyContext(dpy, ctx);
  416. XDestroyWindow(dpy, win);
  417. XCloseDisplay(dpy);
  418. return 0;
  419. }