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.

vgears.c 7.1KB

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