Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* $ID$ */
  2. /*
  3. * Spinning gears demo for Linux SVGA/Mesa interface in 32K color mode.
  4. *
  5. * Compile with: gcc vgears.c -I../include -L../lib -lMesaGL -lX11 -lXext
  6. * -lvga -lm -o vgears
  7. *
  8. * This program is in the public domain.
  9. * Brian Paul, January 1996
  10. */
  11. #include <vga.h>
  12. #include <math.h>
  13. #include "GL/svgamesa.h"
  14. #include "GL/gl.h"
  15. int width = 800, height = 600;
  16. SVGAMesaContext vmc;
  17. /*
  18. * Draw a gear wheel. You'll probably want to call this function when
  19. * building a display list since we do a lot of trig here.
  20. *
  21. * Input: inner_radius - radius of hole at center
  22. * outer_radius - radius at center of teeth
  23. * width - width of gear
  24. * teeth - number of teeth
  25. * tooth_depth - depth of tooth
  26. */
  27. static void gear( GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  28. GLint teeth, GLfloat tooth_depth )
  29. {
  30. GLint i;
  31. GLfloat r0, r1, r2;
  32. GLfloat angle, da;
  33. GLfloat u, v, len;
  34. r0 = inner_radius;
  35. r1 = outer_radius - tooth_depth/2.0;
  36. r2 = outer_radius + tooth_depth/2.0;
  37. da = 2.0*M_PI / teeth / 4.0;
  38. glShadeModel( GL_FLAT );
  39. glNormal3f( 0.0, 0.0, 1.0 );
  40. /* draw front face */
  41. glBegin( GL_QUAD_STRIP );
  42. for (i=0;i<=teeth;i++) {
  43. angle = i * 2.0*M_PI / teeth;
  44. glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  45. glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
  46. glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  47. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  48. }
  49. glEnd();
  50. /* draw front sides of teeth */
  51. glBegin( GL_QUADS );
  52. da = 2.0*M_PI / teeth / 4.0;
  53. for (i=0;i<teeth;i++) {
  54. angle = i * 2.0*M_PI / teeth;
  55. glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
  56. glVertex3f( r2*cos(angle+da), r2*sin(angle+da), width*0.5 );
  57. glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 );
  58. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  59. }
  60. glEnd();
  61. glNormal3f( 0.0, 0.0, -1.0 );
  62. /* draw back face */
  63. glBegin( GL_QUAD_STRIP );
  64. for (i=0;i<=teeth;i++) {
  65. angle = i * 2.0*M_PI / teeth;
  66. glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
  67. glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  68. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  69. glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  70. }
  71. glEnd();
  72. /* draw back sides of teeth */
  73. glBegin( GL_QUADS );
  74. da = 2.0*M_PI / teeth / 4.0;
  75. for (i=0;i<teeth;i++) {
  76. angle = i * 2.0*M_PI / teeth;
  77. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  78. glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  79. glVertex3f( r2*cos(angle+da), r2*sin(angle+da), -width*0.5 );
  80. glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
  81. }
  82. glEnd();
  83. /* draw outward faces of teeth */
  84. glBegin( GL_QUAD_STRIP );
  85. for (i=0;i<teeth;i++) {
  86. angle = i * 2.0*M_PI / teeth;
  87. glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
  88. glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
  89. u = r2*cos(angle+da) - r1*cos(angle);
  90. v = r2*sin(angle+da) - r1*sin(angle);
  91. len = sqrt( u*u + v*v );
  92. u /= len;
  93. v /= len;
  94. glNormal3f( v, -u, 0.0 );
  95. glVertex3f( r2*cos(angle+da), r2*sin(angle+da), width*0.5 );
  96. glVertex3f( r2*cos(angle+da), r2*sin(angle+da), -width*0.5 );
  97. glNormal3f( cos(angle), sin(angle), 0.0 );
  98. glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 );
  99. glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  100. u = r1*cos(angle+3*da) - r2*cos(angle+2*da);
  101. v = r1*sin(angle+3*da) - r2*sin(angle+2*da);
  102. glNormal3f( v, -u, 0.0 );
  103. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  104. glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  105. glNormal3f( cos(angle), sin(angle), 0.0 );
  106. }
  107. glVertex3f( r1*cos(0), r1*sin(0), width*0.5 );
  108. glVertex3f( r1*cos(0), r1*sin(0), -width*0.5 );
  109. glEnd();
  110. glShadeModel( GL_SMOOTH );
  111. /* draw inside radius cylinder */
  112. glBegin( GL_QUAD_STRIP );
  113. for (i=0;i<=teeth;i++) {
  114. angle = i * 2.0*M_PI / teeth;
  115. glNormal3f( -cos(angle), -sin(angle), 0.0 );
  116. glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  117. glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  118. }
  119. glEnd();
  120. }
  121. static GLfloat view_rotx=20.0, view_roty=30.0, view_rotz=0.0;
  122. static GLint gear1, gear2, gear3;
  123. static GLfloat angle = 0.0;
  124. static GLuint limit;
  125. static GLuint count = 1;
  126. static void draw( void )
  127. {
  128. angle += 2.0;
  129. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  130. glPushMatrix();
  131. glRotatef( view_rotx, 1.0, 0.0, 0.0 );
  132. glRotatef( view_roty, 0.0, 1.0, 0.0 );
  133. glRotatef( view_rotz, 0.0, 0.0, 1.0 );
  134. glPushMatrix();
  135. glTranslatef( -3.0, -2.0, 0.0 );
  136. glRotatef( angle, 0.0, 0.0, 1.0 );
  137. glCallList(gear1);
  138. glPopMatrix();
  139. glPushMatrix();
  140. glTranslatef( 3.1, -2.0, 0.0 );
  141. glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 );
  142. glCallList(gear2);
  143. glPopMatrix();
  144. glPushMatrix();
  145. glTranslatef( -3.1, 4.2, 0.0 );
  146. glRotatef( -2.0*angle-25.0, 0.0, 0.0, 1.0 );
  147. glCallList(gear3);
  148. glPopMatrix();
  149. glPopMatrix();
  150. SVGAMesaSwapBuffers();
  151. }
  152. static void init( void )
  153. {
  154. static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0 };
  155. static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0 };
  156. static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0 };
  157. static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0 };
  158. GLfloat w = (float) width / (float) height;
  159. GLfloat h = 1.0;
  160. glLightfv( GL_LIGHT0, GL_POSITION, pos );
  161. glEnable( GL_CULL_FACE );
  162. glEnable( GL_LIGHTING );
  163. glEnable( GL_LIGHT0 );
  164. glEnable( GL_DEPTH_TEST );
  165. /* make the gears */
  166. gear1 = glGenLists(1);
  167. glNewList(gear1, GL_COMPILE);
  168. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red );
  169. gear( 1.0, 4.0, 1.0, 20, 0.7 );
  170. glEndList();
  171. gear2 = glGenLists(1);
  172. glNewList(gear2, GL_COMPILE);
  173. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
  174. gear( 0.5, 2.0, 2.0, 10, 0.7 );
  175. glEndList();
  176. gear3 = glGenLists(1);
  177. glNewList(gear3, GL_COMPILE);
  178. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue );
  179. gear( 1.3, 2.0, 0.5, 10, 0.7 );
  180. glEndList();
  181. glEnable( GL_NORMALIZE );
  182. glViewport( 0, 0, width, height );
  183. glMatrixMode(GL_PROJECTION);
  184. glLoadIdentity();
  185. if (width>height) {
  186. GLfloat w = (GLfloat) width / (GLfloat) height;
  187. glFrustum( -w, w, -1.0, 1.0, 5.0, 60.0 );
  188. }
  189. else {
  190. GLfloat h = (GLfloat) height / (GLfloat) width;
  191. glFrustum( -1.0, 1.0, -h, h, 5.0, 60.0 );
  192. }
  193. glMatrixMode(GL_MODELVIEW);
  194. glLoadIdentity();
  195. glTranslatef( 0.0, 0.0, -40.0 );
  196. }
  197. void setup( void )
  198. {
  199. vga_init();
  200. vga_setmode(G800x600x32K);
  201. /* gl_setcontextvga(G800x600x32K);*/
  202. vmc = SVGAMesaCreateContext(GL_TRUE);
  203. SVGAMesaMakeCurrent( vmc );
  204. }
  205. void end( void )
  206. {
  207. SVGAMesaDestroyContext( vmc );
  208. vga_setmode( TEXT );
  209. }
  210. int main( int argc, char *argv[] )
  211. {
  212. int i;
  213. setup();
  214. init();
  215. for (i=0;i<4;i++) {
  216. draw(); /*SVGAMesaSwapBuffers();*/
  217. }
  218. end();
  219. return 0;
  220. }