Clone of mesa.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

eglgears.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <GL/gl.h>
  30. #include <EGL/egl.h>
  31. #include "eglut.h"
  32. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  33. static GLint gear1, gear2, gear3;
  34. static GLfloat angle = 0.0;
  35. /*
  36. *
  37. * Draw a gear wheel. You'll probably want to call this function when
  38. * building a display list since we do a lot of trig here.
  39. *
  40. * Input: inner_radius - radius of hole at center
  41. * outer_radius - radius at center of teeth
  42. * width - width of gear
  43. * teeth - number of teeth
  44. * tooth_depth - depth of tooth
  45. */
  46. static void
  47. gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  48. GLint teeth, GLfloat tooth_depth)
  49. {
  50. GLint i;
  51. GLfloat r0, r1, r2;
  52. GLfloat angle, da;
  53. GLfloat u, v, len;
  54. r0 = inner_radius;
  55. r1 = outer_radius - tooth_depth / 2.0;
  56. r2 = outer_radius + tooth_depth / 2.0;
  57. da = 2.0 * M_PI / teeth / 4.0;
  58. glShadeModel(GL_FLAT);
  59. glNormal3f(0.0, 0.0, 1.0);
  60. /* draw front face */
  61. glBegin(GL_QUAD_STRIP);
  62. for (i = 0; i <= teeth; i++) {
  63. angle = i * 2.0 * M_PI / teeth;
  64. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  65. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  66. if (i < teeth) {
  67. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  68. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  69. width * 0.5);
  70. }
  71. }
  72. glEnd();
  73. /* draw front 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), r1 * sin(angle), width * 0.5);
  79. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  80. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  81. width * 0.5);
  82. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  83. width * 0.5);
  84. }
  85. glEnd();
  86. glNormal3f(0.0, 0.0, -1.0);
  87. /* draw back face */
  88. glBegin(GL_QUAD_STRIP);
  89. for (i = 0; i <= teeth; i++) {
  90. angle = i * 2.0 * M_PI / teeth;
  91. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  92. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  93. if (i < teeth) {
  94. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  95. -width * 0.5);
  96. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  97. }
  98. }
  99. glEnd();
  100. /* draw back sides of teeth */
  101. glBegin(GL_QUADS);
  102. da = 2.0 * M_PI / teeth / 4.0;
  103. for (i = 0; i < teeth; i++) {
  104. angle = i * 2.0 * M_PI / teeth;
  105. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  106. -width * 0.5);
  107. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  108. -width * 0.5);
  109. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  110. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  111. }
  112. glEnd();
  113. /* draw outward faces of teeth */
  114. glBegin(GL_QUAD_STRIP);
  115. for (i = 0; i < teeth; i++) {
  116. angle = i * 2.0 * M_PI / teeth;
  117. glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  118. glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  119. u = r2 * cos(angle + da) - r1 * cos(angle);
  120. v = r2 * sin(angle + da) - r1 * sin(angle);
  121. len = sqrt(u * u + v * v);
  122. u /= len;
  123. v /= len;
  124. glNormal3f(v, -u, 0.0);
  125. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  126. glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  127. glNormal3f(cos(angle), sin(angle), 0.0);
  128. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  129. width * 0.5);
  130. glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  131. -width * 0.5);
  132. u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
  133. v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
  134. glNormal3f(v, -u, 0.0);
  135. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  136. width * 0.5);
  137. glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  138. -width * 0.5);
  139. glNormal3f(cos(angle), sin(angle), 0.0);
  140. }
  141. glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
  142. glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
  143. glEnd();
  144. glShadeModel(GL_SMOOTH);
  145. /* draw inside radius cylinder */
  146. glBegin(GL_QUAD_STRIP);
  147. for (i = 0; i <= teeth; i++) {
  148. angle = i * 2.0 * M_PI / teeth;
  149. glNormal3f(-cos(angle), -sin(angle), 0.0);
  150. glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  151. glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  152. }
  153. glEnd();
  154. }
  155. static void
  156. draw(void)
  157. {
  158. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  159. glPushMatrix();
  160. glRotatef(view_rotx, 1.0, 0.0, 0.0);
  161. glRotatef(view_roty, 0.0, 1.0, 0.0);
  162. glRotatef(view_rotz, 0.0, 0.0, 1.0);
  163. glPushMatrix();
  164. glTranslatef(-3.0, -2.0, 0.0);
  165. glRotatef(angle, 0.0, 0.0, 1.0);
  166. glCallList(gear1);
  167. glPopMatrix();
  168. glPushMatrix();
  169. glTranslatef(3.1, -2.0, 0.0);
  170. glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
  171. glCallList(gear2);
  172. glPopMatrix();
  173. glPushMatrix();
  174. glTranslatef(-3.1, 4.2, 0.0);
  175. glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
  176. glCallList(gear3);
  177. glPopMatrix();
  178. glPopMatrix();
  179. }
  180. static void
  181. idle(void)
  182. {
  183. static double t0 = -1.;
  184. double dt, t = eglutGet(EGLUT_ELAPSED_TIME) / 1000.0;
  185. if (t0 < 0.0)
  186. t0 = t;
  187. dt = t - t0;
  188. t0 = t;
  189. angle += 70.0 * dt; /* 70 degrees per second */
  190. angle = fmod(angle, 360.0); /* prevents eventual overflow */
  191. eglutPostRedisplay();
  192. }
  193. /* new window size or exposure */
  194. static void
  195. reshape(int width, int height)
  196. {
  197. GLfloat h = (GLfloat) height / (GLfloat) width;
  198. glViewport(0, 0, (GLint) width, (GLint) height);
  199. glMatrixMode(GL_PROJECTION);
  200. glLoadIdentity();
  201. glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
  202. glMatrixMode(GL_MODELVIEW);
  203. glLoadIdentity();
  204. glTranslatef(0.0, 0.0, -40.0);
  205. }
  206. static void
  207. init(void)
  208. {
  209. static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
  210. static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
  211. static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
  212. static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
  213. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  214. glEnable(GL_CULL_FACE);
  215. glEnable(GL_LIGHTING);
  216. glEnable(GL_LIGHT0);
  217. glEnable(GL_DEPTH_TEST);
  218. /* make the gears */
  219. gear1 = glGenLists(1);
  220. glNewList(gear1, GL_COMPILE);
  221. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  222. gear(1.0, 4.0, 1.0, 20, 0.7);
  223. glEndList();
  224. gear2 = glGenLists(1);
  225. glNewList(gear2, GL_COMPILE);
  226. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  227. gear(0.5, 2.0, 2.0, 10, 0.7);
  228. glEndList();
  229. gear3 = glGenLists(1);
  230. glNewList(gear3, GL_COMPILE);
  231. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  232. gear(1.3, 2.0, 0.5, 10, 0.7);
  233. glEndList();
  234. glEnable(GL_NORMALIZE);
  235. }
  236. int
  237. main(int argc, char *argv[])
  238. {
  239. eglutInitWindowSize(300, 300);
  240. eglutInitAPIMask(EGLUT_OPENGL_BIT);
  241. eglutInit(argc, argv);
  242. eglutCreateWindow("eglgears");
  243. eglutIdleFunc(idle);
  244. eglutReshapeFunc(reshape);
  245. eglutDisplayFunc(draw);
  246. init();
  247. glDrawBuffer(GL_BACK);
  248. eglutMainLoop();
  249. return 0;
  250. }