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.

gears2.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* gears.c */
  2. /*
  3. * 3-D gear wheels. This program is in the public domain.
  4. *
  5. * Brian Paul
  6. * modified by Uwe Maurer (uwe_maurer@t-online.de)
  7. */
  8. #include <string.h>
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <ggi/ggi.h>
  12. #include <GL/ggimesa.h>
  13. #ifndef M_PI
  14. # define M_PI 3.14159265
  15. #endif
  16. ggi_visual_t vis;
  17. char text[100];
  18. /*
  19. * Draw a gear wheel. You'll probably want to call this function when
  20. * building a display list since we do a lot of trig here.
  21. *
  22. * Input: inner_radius - radius of hole at center
  23. * outer_radius - radius at center of teeth
  24. * width - width of gear
  25. * teeth - number of teeth
  26. * tooth_depth - depth of tooth
  27. */
  28. static void gear( GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  29. GLint teeth, GLfloat tooth_depth )
  30. {
  31. GLint i;
  32. GLfloat r0, r1, r2;
  33. GLfloat angle, da;
  34. GLfloat u, v, len;
  35. r0 = inner_radius;
  36. r1 = outer_radius - tooth_depth/2.0;
  37. r2 = outer_radius + tooth_depth/2.0;
  38. da = 2.0*M_PI / teeth / 4.0;
  39. glShadeModel( GL_FLAT );
  40. glNormal3f( 0.0, 0.0, 1.0 );
  41. /* draw front face */
  42. glBegin( GL_QUAD_STRIP );
  43. for (i=0;i<=teeth;i++) {
  44. angle = i * 2.0*M_PI / teeth;
  45. glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  46. glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
  47. glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  48. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  49. }
  50. glEnd();
  51. /* draw front sides of teeth */
  52. glBegin( GL_QUADS );
  53. da = 2.0*M_PI / teeth / 4.0;
  54. for (i=0;i<teeth;i++) {
  55. angle = i * 2.0*M_PI / teeth;
  56. glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
  57. glVertex3f( r2*cos(angle+da), r2*sin(angle+da), width*0.5 );
  58. glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 );
  59. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  60. }
  61. glEnd();
  62. glNormal3f( 0.0, 0.0, -1.0 );
  63. /* draw back face */
  64. glBegin( GL_QUAD_STRIP );
  65. for (i=0;i<=teeth;i++) {
  66. angle = i * 2.0*M_PI / teeth;
  67. glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
  68. glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  69. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  70. glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  71. }
  72. glEnd();
  73. /* draw back sides of teeth */
  74. glBegin( GL_QUADS );
  75. da = 2.0*M_PI / teeth / 4.0;
  76. for (i=0;i<teeth;i++) {
  77. angle = i * 2.0*M_PI / teeth;
  78. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  79. glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  80. glVertex3f( r2*cos(angle+da), r2*sin(angle+da), -width*0.5 );
  81. glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
  82. }
  83. glEnd();
  84. /* draw outward faces of teeth */
  85. glBegin( GL_QUAD_STRIP );
  86. for (i=0;i<teeth;i++) {
  87. angle = i * 2.0*M_PI / teeth;
  88. glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
  89. glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
  90. u = r2*cos(angle+da) - r1*cos(angle);
  91. v = r2*sin(angle+da) - r1*sin(angle);
  92. len = sqrt( u*u + v*v );
  93. u /= len;
  94. v /= len;
  95. glNormal3f( v, -u, 0.0 );
  96. glVertex3f( r2*cos(angle+da), r2*sin(angle+da), width*0.5 );
  97. glVertex3f( r2*cos(angle+da), r2*sin(angle+da), -width*0.5 );
  98. glNormal3f( cos(angle), sin(angle), 0.0 );
  99. glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 );
  100. glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  101. u = r1*cos(angle+3*da) - r2*cos(angle+2*da);
  102. v = r1*sin(angle+3*da) - r2*sin(angle+2*da);
  103. glNormal3f( v, -u, 0.0 );
  104. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  105. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  106. glNormal3f( cos(angle), sin(angle), 0.0 );
  107. }
  108. glVertex3f( r1*cos(0), r1*sin(0), width*0.5 );
  109. glVertex3f( r1*cos(0), r1*sin(0), -width*0.5 );
  110. glEnd();
  111. glShadeModel( GL_SMOOTH );
  112. /* draw inside radius cylinder */
  113. glBegin( GL_QUAD_STRIP );
  114. for (i=0;i<=teeth;i++) {
  115. angle = i * 2.0*M_PI / teeth;
  116. glNormal3f( -cos(angle), -sin(angle), 0.0 );
  117. glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  118. glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  119. }
  120. glEnd();
  121. }
  122. static GLfloat view_rotx=20.0, view_roty=30.0, view_rotz=0.0;
  123. static GLint gear1, gear2, gear3;
  124. static GLfloat angle = 0.0;
  125. static GLuint limit;
  126. static GLuint count = 1;
  127. static void draw( void )
  128. {
  129. glClearColor(0,0,0,0);
  130. glClearIndex(0);
  131. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  132. glPushMatrix();
  133. glRotatef( view_rotx, 1.0, 0.0, 0.0 );
  134. glRotatef( view_roty, 0.0, 1.0, 0.0 );
  135. glRotatef( view_rotz, 0.0, 0.0, 1.0 );
  136. glPushMatrix();
  137. glTranslatef( -3.0, -2.0, 0.0 );
  138. glRotatef( angle, 0.0, 0.0, 1.0 );
  139. glCallList(gear1);
  140. glPopMatrix();
  141. glPushMatrix();
  142. glTranslatef( 3.1, -2.0, 0.0 );
  143. glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 );
  144. glCallList(gear2);
  145. glPopMatrix();
  146. glPushMatrix();
  147. glTranslatef( -3.1, 4.2, 0.0 );
  148. glRotatef( -2.0*angle-25.0, 0.0, 0.0, 1.0 );
  149. glCallList(gear3);
  150. glPopMatrix();
  151. glPopMatrix();
  152. glFlush();
  153. glFinish();
  154. #if 0
  155. ggiSetGCForeground(vis,255);
  156. ggiPuts(vis,0,0,"Mesa -> GGI");
  157. ggiPuts(vis,0,ggiGetInfo(vis)->mode->visible.y," Mesa -> GGI");
  158. ggiPuts(vis,0,16,text);
  159. ggiPuts(vis,0,ggiGetInfo(vis)->mode->visible.y+16,text);
  160. #endif
  161. GGIMesaSwapBuffers();
  162. count++;
  163. if (count==limit) {
  164. exit(1);
  165. }
  166. }
  167. static void idle( void )
  168. {
  169. angle += 2.0;
  170. draw();
  171. }
  172. /* new window size or exposure */
  173. static void reshape( int width, int height )
  174. {
  175. GLfloat h = (GLfloat) height / (GLfloat) width;
  176. glViewport(0, 0, (GLint)width, (GLint)height);
  177. glMatrixMode(GL_PROJECTION);
  178. glLoadIdentity();
  179. glFrustum( -1.0, 1.0, -h, h, 5.0, 60.0 );
  180. glMatrixMode(GL_MODELVIEW);
  181. glLoadIdentity();
  182. glTranslatef( 0.0, 0.0, -40.0 );
  183. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  184. }
  185. static void init( void )
  186. {
  187. static GLfloat pos[4] = {5.0, 5.0, 10.0, 0.0 };
  188. static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0 };
  189. static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0 };
  190. static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0 };
  191. glLightfv( GL_LIGHT0, GL_POSITION, pos );
  192. glEnable( GL_CULL_FACE );
  193. glEnable( GL_LIGHTING );
  194. glEnable( GL_LIGHT0 );
  195. glEnable( GL_DEPTH_TEST );
  196. /* make the gears */
  197. gear1 = glGenLists(1);
  198. glNewList(gear1, GL_COMPILE);
  199. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red );
  200. glIndexi(1);
  201. gear( 1.0, 4.0, 1.0, 20, 0.7 );
  202. glEndList();
  203. gear2 = glGenLists(1);
  204. glNewList(gear2, GL_COMPILE);
  205. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
  206. glIndexi(2);
  207. gear( 0.5, 2.0, 2.0, 10, 0.7 );
  208. glEndList();
  209. gear3 = glGenLists(1);
  210. glNewList(gear3, GL_COMPILE);
  211. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue );
  212. glIndexi(3);
  213. gear( 1.3, 2.0, 0.5, 10, 0.7 );
  214. glEndList();
  215. glEnable( GL_NORMALIZE );
  216. }
  217. static void usage(char *s)
  218. {
  219. printf("%s visible_x visible_y virtual_x virtual_y bpp db_flag\n",s);
  220. printf("example:\n");
  221. printf("%s 320 200 320 400 8 1\n",s);
  222. exit(1);
  223. }
  224. int main( int argc, char *argv[] )
  225. {
  226. GGIMesaContext ctx;
  227. int vis_x,vis_y,vir_x,vir_y,bpp,db_flag,gt;
  228. ggi_mode mode;
  229. limit=0;
  230. if (argc<7) usage(argv[0]);
  231. vis_x=atoi(argv[1]);
  232. vis_y=atoi(argv[2]);
  233. vir_x=atoi(argv[3]);
  234. vir_y=atoi(argv[4]);
  235. bpp=atoi(argv[5]);
  236. db_flag=atoi(argv[6]);
  237. switch(bpp)
  238. {
  239. case 4: gt=GT_4BIT;break;
  240. case 8: gt=GT_8BIT;break;
  241. case 15:gt=GT_15BIT;break;
  242. case 16:gt=GT_16BIT;break;
  243. case 24:gt=GT_24BIT;break;
  244. case 32:gt=GT_32BIT;break;
  245. default:
  246. printf("%i Bits per Pixel ???\n",bpp);
  247. exit(1);
  248. }
  249. sprintf(text,"%sx%s %i colors, RGB mode, %s",
  250. argv[1],argv[2],1<<bpp,
  251. (db_flag) ? "doublebuffer" : "no doublebuffer");
  252. if (ggiInit()<0)
  253. {
  254. printf("ggiInit() failed\n");
  255. exit(1);
  256. }
  257. ctx=GGIMesaCreateContext();
  258. if (ctx==NULL)
  259. {
  260. printf("GGIMesaCreateContext() failed\n");
  261. exit(1);
  262. }
  263. vis=ggiOpen(NULL);
  264. if (vis==NULL)
  265. {
  266. printf("ggiOpen() failed\n");
  267. exit(1);
  268. }
  269. if (ggiSetGraphMode(vis,vis_x,vis_y,vir_x,vir_y,gt)<0)
  270. {
  271. printf("%s: can't set graphmode (%i %i %i %i) %i BPP\n",
  272. argv[0],vis_x,vis_y,vir_x,vir_y,bpp);
  273. exit(1);
  274. }
  275. if (GGIMesaSetVisual(ctx,vis,GL_TRUE,db_flag)<0)
  276. {
  277. printf ("GGIMesaSetVisual() failed\n");
  278. exit(1);
  279. }
  280. GGIMesaMakeCurrent(ctx);
  281. ggiGetMode(vis,&mode);
  282. reshape(mode.visible.x,mode.visible.y);
  283. init();
  284. while (!ggiKbhit(vis)) idle();
  285. GGIMesaDestroyContext(ctx);
  286. ggiClose(vis);
  287. printf("%s\n",text);
  288. ggiExit();
  289. return 0;
  290. }