Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

eglgears.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 "glxgears" demo to straight EGL
  23. * Port by Dane Rushton 10 July 2005
  24. *
  25. * No command line options.
  26. * Program runs for 5 seconds then exits, outputing framerate to console
  27. */
  28. #include <math.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <GL/gl.h>
  33. #include <GLES/egl.h>
  34. #include <assert.h>
  35. #define MAX_CONFIGS 10
  36. #define MAX_MODES 100
  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 double
  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 (double) tv.tv_sec + tv.tv_usec / 1000000.0;
  54. }
  55. #else /*BENCHMARK*/
  56. /* dummy */
  57. static double
  58. current_time(void)
  59. {
  60. /* update this function for other platforms! */
  61. static double t = 0.0;
  62. static int warn = 1;
  63. if (warn) {
  64. fprintf(stderr, "Warning: current_time() not implemented!!\n");
  65. warn = 0;
  66. }
  67. return t += 1.0;
  68. }
  69. #endif /*BENCHMARK*/
  70. #ifndef M_PI
  71. #define M_PI 3.14159265
  72. #endif
  73. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  74. static GLint gear1, gear2, gear3;
  75. static GLfloat angle = 0.0;
  76. #if 0
  77. static GLfloat eyesep = 5.0; /* Eye separation. */
  78. static GLfloat fix_point = 40.0; /* Fixation point distance. */
  79. static GLfloat left, right, asp; /* Stereo frustum params. */
  80. #endif
  81. /*
  82. *
  83. * Draw a gear wheel. You'll probably want to call this function when
  84. * building a display list since we do a lot of trig here.
  85. *
  86. * Input: inner_radius - radius of hole at center
  87. * outer_radius - radius at center of teeth
  88. * width - width of gear
  89. * teeth - number of teeth
  90. * tooth_depth - depth of tooth
  91. */
  92. static void
  93. gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  94. GLint teeth, GLfloat tooth_depth)
  95. {
  96. GLint i;
  97. GLfloat r0, r1, r2;
  98. GLfloat angle, da;
  99. GLfloat u, v, len;
  100. r0 = inner_radius;
  101. r1 = outer_radius - tooth_depth / 2.0;
  102. r2 = outer_radius + tooth_depth / 2.0;
  103. da = 2.0 * M_PI / teeth / 4.0;
  104. glShadeModel(GL_FLAT);
  105. glNormal3f(0.0, 0.0, 1.0);
  106. /* draw front face */
  107. glBegin(GL_QUAD_STRIP);
  108. for (i = 0; i <= teeth; i++) {
  109. angle = i * 2.0 * M_PI / teeth;
  110. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  111. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  112. if (i < teeth) {
  113. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  114. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  115. width * 0.5);
  116. }
  117. }
  118. glEnd();
  119. /* draw front sides of teeth */
  120. glBegin(GL_QUADS);
  121. da = 2.0 * M_PI / teeth / 4.0;
  122. for (i = 0; i < teeth; i++) {
  123. angle = i * 2.0 * M_PI / teeth;
  124. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  125. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  126. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  127. width * 0.5);
  128. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  129. width * 0.5);
  130. }
  131. glEnd();
  132. glNormal3f(0.0, 0.0, -1.0);
  133. /* draw back face */
  134. glBegin(GL_QUAD_STRIP);
  135. for (i = 0; i <= teeth; i++) {
  136. angle = i * 2.0 * M_PI / teeth;
  137. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  138. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  139. if (i < teeth) {
  140. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  141. -width * 0.5);
  142. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  143. }
  144. }
  145. glEnd();
  146. /* draw back sides of teeth */
  147. glBegin(GL_QUADS);
  148. da = 2.0 * M_PI / teeth / 4.0;
  149. for (i = 0; i < teeth; i++) {
  150. angle = i * 2.0 * M_PI / teeth;
  151. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  152. -width * 0.5);
  153. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  154. -width * 0.5);
  155. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  156. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  157. }
  158. glEnd();
  159. /* draw outward faces of teeth */
  160. glBegin(GL_QUAD_STRIP);
  161. for (i = 0; i < teeth; i++) {
  162. angle = i * 2.0 * M_PI / teeth;
  163. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  164. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  165. u = r2 * cos(angle + da) - r1 * cos(angle);
  166. v = r2 * sin(angle + da) - r1 * sin(angle);
  167. len = sqrt(u * u + v * v);
  168. u /= len;
  169. v /= len;
  170. glNormal3f(v, -u, 0.0);
  171. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  172. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  173. glNormal3f(cos(angle), sin(angle), 0.0);
  174. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  175. width * 0.5);
  176. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  177. -width * 0.5);
  178. u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
  179. v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
  180. glNormal3f(v, -u, 0.0);
  181. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  182. width * 0.5);
  183. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  184. -width * 0.5);
  185. glNormal3f(cos(angle), sin(angle), 0.0);
  186. }
  187. glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
  188. glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
  189. glEnd();
  190. glShadeModel(GL_SMOOTH);
  191. /* draw inside radius cylinder */
  192. glBegin(GL_QUAD_STRIP);
  193. for (i = 0; i <= teeth; i++) {
  194. angle = i * 2.0 * M_PI / teeth;
  195. glNormal3f(-cos(angle), -sin(angle), 0.0);
  196. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  197. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  198. }
  199. glEnd();
  200. }
  201. static void
  202. draw(void)
  203. {
  204. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  205. glPushMatrix();
  206. glRotatef(view_rotx, 1.0, 0.0, 0.0);
  207. glRotatef(view_roty, 0.0, 1.0, 0.0);
  208. glRotatef(view_rotz, 0.0, 0.0, 1.0);
  209. glPushMatrix();
  210. glTranslatef(-3.0, -2.0, 0.0);
  211. glRotatef(angle, 0.0, 0.0, 1.0);
  212. glCallList(gear1);
  213. glPopMatrix();
  214. glPushMatrix();
  215. glTranslatef(3.1, -2.0, 0.0);
  216. glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
  217. glCallList(gear2);
  218. glPopMatrix();
  219. glPushMatrix();
  220. glTranslatef(-3.1, 4.2, 0.0);
  221. glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
  222. glCallList(gear3);
  223. glPopMatrix();
  224. glPopMatrix();
  225. }
  226. /* new window size or exposure */
  227. static void
  228. reshape(int width, int height)
  229. {
  230. glViewport(0, 0, (GLint) width, (GLint) height);
  231. GLfloat h = (GLfloat) height / (GLfloat) width;
  232. glMatrixMode(GL_PROJECTION);
  233. glLoadIdentity();
  234. glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
  235. glMatrixMode(GL_MODELVIEW);
  236. glLoadIdentity();
  237. glTranslatef(0.0, 0.0, -40.0);
  238. }
  239. static void
  240. init(void)
  241. {
  242. static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
  243. static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
  244. static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
  245. static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
  246. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  247. glEnable(GL_CULL_FACE);
  248. glEnable(GL_LIGHTING);
  249. glEnable(GL_LIGHT0);
  250. glEnable(GL_DEPTH_TEST);
  251. /* make the gears */
  252. gear1 = glGenLists(1);
  253. glNewList(gear1, GL_COMPILE);
  254. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  255. gear(1.0, 4.0, 1.0, 20, 0.7);
  256. glEndList();
  257. gear2 = glGenLists(1);
  258. glNewList(gear2, GL_COMPILE);
  259. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  260. gear(0.5, 2.0, 2.0, 10, 0.7);
  261. glEndList();
  262. gear3 = glGenLists(1);
  263. glNewList(gear3, GL_COMPILE);
  264. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  265. gear(1.3, 2.0, 0.5, 10, 0.7);
  266. glEndList();
  267. glEnable(GL_NORMALIZE);
  268. }
  269. static void run_gears(EGLDisplay dpy, EGLSurface surf, int ttr)
  270. {
  271. double st = current_time();
  272. double ct = st;
  273. int frames = 0;
  274. while (ct - st < ttr)
  275. {
  276. double tt = current_time();
  277. double dt = tt - ct;
  278. ct = tt;
  279. /* advance rotation for next frame */
  280. angle += 70.0 * dt; /* 70 degrees per second */
  281. if (angle > 3600.0)
  282. angle -= 3600.0;
  283. draw();
  284. eglSwapBuffers(dpy, surf);
  285. frames++;
  286. }
  287. GLfloat seconds = ct - st;
  288. GLfloat fps = frames / seconds;
  289. printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, fps);
  290. }
  291. int
  292. main(int argc, char *argv[])
  293. {
  294. int maj, min;
  295. EGLContext ctx;
  296. EGLSurface screen_surf;
  297. EGLConfig configs[MAX_CONFIGS];
  298. EGLint numConfigs, i;
  299. EGLBoolean b;
  300. EGLDisplay d;
  301. EGLint screenAttribs[10];
  302. EGLModeMESA mode[MAX_MODES];
  303. EGLScreenMESA screen;
  304. EGLint count, chosenMode;
  305. GLboolean printInfo = GL_FALSE;
  306. EGLint width = 0, height = 0;
  307. /* parse cmd line args */
  308. for (i = 1; i < argc; i++)
  309. {
  310. if (strcmp(argv[i], "-info") == 0)
  311. {
  312. printInfo = GL_TRUE;
  313. }
  314. else
  315. printf("Warning: unknown parameter: %s\n", argv[i]);
  316. }
  317. /* DBR : Create EGL context/surface etc */
  318. d = eglGetDisplay(":0");
  319. assert(d);
  320. if (!eglInitialize(d, &maj, &min)) {
  321. printf("eglgears: eglInitialize failed\n");
  322. exit(1);
  323. }
  324. printf("eglgears: EGL version = %d.%d\n", maj, min);
  325. printf("eglgears: EGL_VENDOR = %s\n", eglQueryString(d, EGL_VENDOR));
  326. /* XXX use ChooseConfig */
  327. eglGetConfigs(d, configs, MAX_CONFIGS, &numConfigs);
  328. eglGetScreensMESA(d, &screen, 1, &count);
  329. if (!eglGetModesMESA(d, screen, mode, MAX_MODES, &count) || count == 0) {
  330. printf("eglgears: eglGetModesMESA failed!\n");
  331. return 0;
  332. }
  333. /* Print list of modes, and find the one to use */
  334. printf("eglgears: Found %d modes:\n", count);
  335. for (i = 0; i < count; i++) {
  336. EGLint w, h;
  337. eglGetModeAttribMESA(d, mode[i], EGL_WIDTH, &w);
  338. eglGetModeAttribMESA(d, mode[i], EGL_HEIGHT, &h);
  339. printf("%3d: %d x %d\n", i, w, h);
  340. if (w > width && h > height && w <= 1280 && h <= 1024) {
  341. width = w;
  342. height = h;
  343. chosenMode = i;
  344. }
  345. }
  346. printf("eglgears: Using screen mode/size %d: %d x %d\n", chosenMode, width, height);
  347. ctx = eglCreateContext(d, configs[0], EGL_NO_CONTEXT, NULL);
  348. if (ctx == EGL_NO_CONTEXT) {
  349. printf("eglgears: failed to create context\n");
  350. return 0;
  351. }
  352. /* build up screenAttribs array */
  353. i = 0;
  354. screenAttribs[i++] = EGL_WIDTH;
  355. screenAttribs[i++] = width;
  356. screenAttribs[i++] = EGL_HEIGHT;
  357. screenAttribs[i++] = height;
  358. screenAttribs[i++] = EGL_NONE;
  359. screen_surf = eglCreateScreenSurfaceMESA(d, configs[0], screenAttribs);
  360. if (screen_surf == EGL_NO_SURFACE) {
  361. printf("eglgears: failed to create screen surface\n");
  362. return 0;
  363. }
  364. b = eglShowScreenSurfaceMESA(d, screen, screen_surf, mode[chosenMode]);
  365. if (!b) {
  366. printf("eglgears: show surface failed\n");
  367. return 0;
  368. }
  369. b = eglMakeCurrent(d, screen_surf, screen_surf, ctx);
  370. if (!b) {
  371. printf("eglgears: make current failed\n");
  372. return 0;
  373. }
  374. if (printInfo)
  375. {
  376. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  377. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  378. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  379. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  380. }
  381. init();
  382. reshape(width, height);
  383. glDrawBuffer( GL_BACK );
  384. run_gears(d, screen_surf, 5.0);
  385. eglDestroySurface(d, screen_surf);
  386. eglDestroyContext(d, ctx);
  387. eglTerminate(d);
  388. return 0;
  389. }