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.

df_gears.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. (c) Copyright 2001 convergence integrated media GmbH.
  3. All rights reserved.
  4. Written by Denis Oliver Kropp <dok@convergence.de> and
  5. Andreas Hundt <andi@convergence.de>.
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the
  16. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <directfb.h>
  24. #include <directfbgl.h>
  25. #include <GL/gl.h>
  26. /* the super interface */
  27. IDirectFB *dfb;
  28. /* the primary surface (surface of primary layer) */
  29. IDirectFBSurface *primary;
  30. /* the GL context */
  31. IDirectFBGL *primary_gl;
  32. /* our font */
  33. IDirectFBFont *font;
  34. /* event buffer */
  35. IDirectFBEventBuffer *events;
  36. /* macro for a safe call to DirectFB functions */
  37. #define DFBCHECK(x...) \
  38. { \
  39. err = x; \
  40. if (err != DFB_OK) { \
  41. fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \
  42. DirectFBErrorFatal( #x, err ); \
  43. } \
  44. }
  45. static int screen_width, screen_height;
  46. static unsigned long T0 = 0;
  47. static GLint Frames = 0;
  48. static GLfloat fps = 0;
  49. static inline unsigned long get_millis()
  50. {
  51. struct timeval tv;
  52. gettimeofday (&tv, NULL);
  53. return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
  54. }
  55. #ifndef M_PI
  56. #define M_PI 3.14159265
  57. #endif
  58. /**
  59. Draw a gear wheel. You'll probably want to call this function when
  60. building a display list since we do a lot of trig here.
  61. Input: inner_radius - radius of hole at center
  62. outer_radius - radius at center of teeth
  63. width - width of gear
  64. teeth - number of teeth
  65. tooth_depth - depth of tooth
  66. **/
  67. static void
  68. gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  69. GLint teeth, GLfloat tooth_depth)
  70. {
  71. GLint i;
  72. GLfloat r0, r1, r2;
  73. GLfloat angle, da;
  74. GLfloat u, v, len;
  75. r0 = inner_radius;
  76. r1 = outer_radius - tooth_depth / 2.0;
  77. r2 = outer_radius + tooth_depth / 2.0;
  78. da = 2.0 * M_PI / teeth / 4.0;
  79. glShadeModel(GL_FLAT);
  80. glNormal3f(0.0, 0.0, 1.0);
  81. /* draw front face */
  82. glBegin(GL_QUAD_STRIP);
  83. for (i = 0; i <= teeth; i++) {
  84. angle = i * 2.0 * M_PI / teeth;
  85. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  86. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  87. if (i < teeth) {
  88. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  89. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
  90. }
  91. }
  92. glEnd();
  93. /* draw front sides of teeth */
  94. glBegin(GL_QUADS);
  95. da = 2.0 * M_PI / teeth / 4.0;
  96. for (i = 0; i < teeth; i++) {
  97. angle = i * 2.0 * M_PI / teeth;
  98. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  99. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  100. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
  101. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), 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), -width * 0.5);
  113. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  114. }
  115. }
  116. glEnd();
  117. /* draw back sides of teeth */
  118. glBegin(GL_QUADS);
  119. da = 2.0 * M_PI / teeth / 4.0;
  120. for (i = 0; i < teeth; i++) {
  121. angle = i * 2.0 * M_PI / teeth;
  122. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
  123. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
  124. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  125. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  126. }
  127. glEnd();
  128. /* draw outward faces of teeth */
  129. glBegin(GL_QUAD_STRIP);
  130. for (i = 0; i < teeth; i++) {
  131. angle = i * 2.0 * M_PI / teeth;
  132. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  133. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  134. u = r2 * cos(angle + da) - r1 * cos(angle);
  135. v = r2 * sin(angle + da) - r1 * sin(angle);
  136. len = sqrt(u * u + v * v);
  137. u /= len;
  138. v /= len;
  139. glNormal3f(v, -u, 0.0);
  140. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  141. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  142. glNormal3f(cos(angle), sin(angle), 0.0);
  143. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
  144. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
  145. u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
  146. v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
  147. glNormal3f(v, -u, 0.0);
  148. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
  149. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
  150. glNormal3f(cos(angle), sin(angle), 0.0);
  151. }
  152. glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
  153. glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
  154. glEnd();
  155. glShadeModel(GL_SMOOTH);
  156. /* draw inside radius cylinder */
  157. glBegin(GL_QUAD_STRIP);
  158. for (i = 0; i <= teeth; i++) {
  159. angle = i * 2.0 * M_PI / teeth;
  160. glNormal3f(-cos(angle), -sin(angle), 0.0);
  161. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  162. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  163. }
  164. glEnd();
  165. }
  166. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  167. static GLfloat inc_rotx = 0, inc_roty = 0, inc_rotz = 0;
  168. static GLint gear1, gear2, gear3;
  169. static GLfloat angle = 0.0;
  170. static void
  171. draw(void)
  172. {
  173. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  174. glPushMatrix();
  175. glRotatef(view_rotx, 1.0, 0.0, 0.0);
  176. glRotatef(view_roty, 0.0, 1.0, 0.0);
  177. glRotatef(view_rotz, 0.0, 0.0, 1.0);
  178. glPushMatrix();
  179. glTranslatef(-3.0, -2.0, 0.0);
  180. glRotatef(angle, 0.0, 0.0, 1.0);
  181. glCallList(gear1);
  182. glPopMatrix();
  183. glPushMatrix();
  184. glTranslatef(3.1, -2.0, 0.0);
  185. glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
  186. glCallList(gear2);
  187. glPopMatrix();
  188. glPushMatrix();
  189. glTranslatef(-3.1, 4.2, 0.0);
  190. glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
  191. glCallList(gear3);
  192. glPopMatrix();
  193. glPopMatrix();
  194. }
  195. /* new window size or exposure */
  196. static void
  197. reshape(int width, int height)
  198. {
  199. GLfloat h = (GLfloat) height / (GLfloat) width;
  200. glViewport(0, 0, (GLint) width, (GLint) height);
  201. glMatrixMode(GL_PROJECTION);
  202. glLoadIdentity();
  203. glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
  204. glMatrixMode(GL_MODELVIEW);
  205. glLoadIdentity();
  206. glTranslatef(0.0, 0.0, -40.0);
  207. }
  208. static void
  209. init(int argc, char *argv[])
  210. {
  211. static GLfloat pos[4] = {5.0, 5.0, 10.0, 0.0};
  212. static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
  213. static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
  214. static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
  215. GLint i;
  216. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  217. glEnable(GL_CULL_FACE);
  218. glEnable(GL_LIGHTING);
  219. glEnable(GL_LIGHT0);
  220. glEnable(GL_DEPTH_TEST);
  221. /* make the gears */
  222. gear1 = glGenLists(1);
  223. glNewList(gear1, GL_COMPILE);
  224. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  225. gear(1.0, 4.0, 1.0, 20, 0.7);
  226. glEndList();
  227. gear2 = glGenLists(1);
  228. glNewList(gear2, GL_COMPILE);
  229. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  230. gear(0.5, 2.0, 2.0, 10, 0.7);
  231. glEndList();
  232. gear3 = glGenLists(1);
  233. glNewList(gear3, GL_COMPILE);
  234. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  235. gear(1.3, 2.0, 0.5, 10, 0.7);
  236. glEndList();
  237. glEnable(GL_NORMALIZE);
  238. for ( i=1; i<argc; i++ ) {
  239. if (strcmp(argv[i], "-info")==0) {
  240. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  241. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  242. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  243. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  244. }
  245. }
  246. }
  247. int main( int argc, char *argv[] )
  248. {
  249. int quit = 0;
  250. DFBResult err;
  251. DFBSurfaceDescription dsc;
  252. DFBCHECK(DirectFBInit( &argc, &argv ));
  253. /* create the super interface */
  254. DFBCHECK(DirectFBCreate( &dfb ));
  255. /* create an event buffer for all devices with these caps */
  256. DFBCHECK(dfb->CreateInputEventBuffer( dfb, DICAPS_KEYS | DICAPS_AXES,
  257. DFB_FALSE, &events ));
  258. /* set our cooperative level to DFSCL_FULLSCREEN
  259. for exclusive access to the primary layer */
  260. dfb->SetCooperativeLevel( dfb, DFSCL_FULLSCREEN );
  261. /* get the primary surface, i.e. the surface of the
  262. primary layer we have exclusive access to */
  263. dsc.flags = DSDESC_CAPS;
  264. dsc.caps = DSCAPS_PRIMARY | DSCAPS_DOUBLE;
  265. DFBCHECK(dfb->CreateSurface( dfb, &dsc, &primary ));
  266. /* get the size of the surface and fill it */
  267. DFBCHECK(primary->GetSize( primary, &screen_width, &screen_height ));
  268. DFBCHECK(primary->FillRectangle( primary, 0, 0,
  269. screen_width, screen_height ));
  270. primary->Flip( primary, NULL, 0 );
  271. /* create the default font and set it */
  272. DFBCHECK(dfb->CreateFont( dfb, NULL, NULL, &font ));
  273. DFBCHECK(primary->SetFont( primary, font ));
  274. /* get the GL context */
  275. DFBCHECK(primary->GetGL( primary, &primary_gl ));
  276. DFBCHECK(primary_gl->Lock( primary_gl ));
  277. init(argc, argv);
  278. reshape(screen_width, screen_height);
  279. DFBCHECK(primary_gl->Unlock( primary_gl ));
  280. T0 = get_millis();
  281. while (!quit) {
  282. DFBInputEvent evt;
  283. unsigned long t;
  284. DFBCHECK(primary_gl->Lock( primary_gl ));
  285. draw();
  286. DFBCHECK(primary_gl->Unlock( primary_gl ));
  287. if (fps) {
  288. char buf[64];
  289. snprintf(buf, 64, "%4.1f FPS\n", fps);
  290. primary->SetColor( primary, 0xff, 0, 0, 0xff );
  291. primary->DrawString( primary, buf, -1, screen_width - 5, 5, DSTF_TOPRIGHT );
  292. }
  293. primary->Flip( primary, NULL, 0 );
  294. Frames++;
  295. t = get_millis();
  296. if (t - T0 >= 2000) {
  297. GLfloat seconds = (t - T0) / 1000.0;
  298. fps = Frames / seconds;
  299. T0 = t;
  300. Frames = 0;
  301. }
  302. while (events->GetEvent( events, DFB_EVENT(&evt) ) == DFB_OK) {
  303. switch (evt.type) {
  304. case DIET_KEYPRESS:
  305. switch (evt.key_symbol) {
  306. case DIKS_ESCAPE:
  307. quit = 1;
  308. break;
  309. case DIKS_CURSOR_UP:
  310. inc_rotx = 5.0;
  311. break;
  312. case DIKS_CURSOR_DOWN:
  313. inc_rotx = -5.0;
  314. break;
  315. case DIKS_CURSOR_LEFT:
  316. inc_roty = 5.0;
  317. break;
  318. case DIKS_CURSOR_RIGHT:
  319. inc_roty = -5.0;
  320. break;
  321. case DIKS_PAGE_UP:
  322. inc_rotz = 5.0;
  323. break;
  324. case DIKS_PAGE_DOWN:
  325. inc_rotz = -5.0;
  326. break;
  327. default:
  328. ;
  329. }
  330. break;
  331. case DIET_KEYRELEASE:
  332. switch (evt.key_symbol) {
  333. case DIKS_CURSOR_UP:
  334. inc_rotx = 0;
  335. break;
  336. case DIKS_CURSOR_DOWN:
  337. inc_rotx = 0;
  338. break;
  339. case DIKS_CURSOR_LEFT:
  340. inc_roty = 0;
  341. break;
  342. case DIKS_CURSOR_RIGHT:
  343. inc_roty = 0;
  344. break;
  345. case DIKS_PAGE_UP:
  346. inc_rotz = 0;
  347. break;
  348. case DIKS_PAGE_DOWN:
  349. inc_rotz = 0;
  350. break;
  351. default:
  352. ;
  353. }
  354. break;
  355. case DIET_AXISMOTION:
  356. if (evt.flags & DIEF_AXISREL) {
  357. switch (evt.axis) {
  358. case DIAI_X:
  359. view_roty += evt.axisrel / 2.0;
  360. break;
  361. case DIAI_Y:
  362. view_rotx += evt.axisrel / 2.0;
  363. break;
  364. case DIAI_Z:
  365. view_rotz += evt.axisrel / 2.0;
  366. break;
  367. default:
  368. ;
  369. }
  370. }
  371. break;
  372. default:
  373. ;
  374. }
  375. }
  376. angle += 2.0;
  377. view_rotx += inc_rotx;
  378. view_roty += inc_roty;
  379. view_rotz += inc_rotz;
  380. }
  381. /* release our interfaces to shutdown DirectFB */
  382. primary_gl->Release( primary_gl );
  383. primary->Release( primary );
  384. font->Release( font );
  385. events->Release( events );
  386. dfb->Release( dfb );
  387. return 0;
  388. }