0c28e9fb7a735097f26a22ff6606aedbfe5a87f3
[xscreensaver] / hacks / glx / circuit.c
1 /*
2  * circuit - Random electronic components floating around
3  *
4  * version 1.4
5  *
6  * Since version 1.1: added to-220 transistor, added fuse
7  * Since version 1.2: random display digits, LED improvements (flickering)
8  * Since version 1.3: ICs look better, font textures, improved normals to
9  *                    eliminate segmenting on curved surfaces, speedups
10  *
11  * Copyright (C) 2001 Ben Buxton (bb@cactii.net)
12  *
13  * Permission to use, copy, modify, distribute, and sell this software and its
14  * documentation for any purpose is hereby granted without fee, provided that
15  * the above copyright notice appear in all copies and that both that
16  * copyright notice and this permission notice appear in supporting
17  * documentation.  No representations are made about the suitability of this
18  * software for any purpose.  It is provided "as is" without express or
19  * implied warranty.
20  */
21
22 /* Written over a few days in a (successful) bid to learn GL coding 
23  *
24  * -seven option is dedicated to all the Slarkeners
25  *
26  * try "-rotate -rotate-speed 0"
27  *
28  * This hack uses lookup tables for sin, cos and tan - it can do a lot
29  */
30
31
32 #include <X11/Intrinsic.h>
33
34 #ifdef STANDALONE
35 # define PROGCLASS                                      "Circuit"
36 # define HACK_INIT                                      init_circuit
37 # define HACK_DRAW                                      draw_circuit
38 # define HACK_RESHAPE                           reshape_circuit
39 # define circuit_opts                                     xlockmore_opts
40 /* insert defaults here */
41
42 #define DEF_SPIN        "True"
43 #define DEF_SEVEN       "False"
44 #define DEF_PARTS       "10"
45
46
47 #define DEFAULTS        "*parts:      " DEF_PARTS " \n" \
48                         "*spin:       " DEF_SPIN   "\n" \
49                         "*delay:       20000       \n" \
50                         "*showFPS:       False       \n" \
51                         "*seven:      " DEF_SEVEN  "\n" \
52                         "*light:      True  \n" \
53                         "*rotate:      False\n" \
54                         "*font:      fixed\n" \
55                         "*rotatespeed:      1\n" \
56
57 # include "xlockmore.h"                         /* from the xscreensaver distribution */
58 #else  /* !STANDALONE */
59 # include "xlock.h"                                     /* from the xlockmore distribution */
60 #endif /* !STANDALONE */
61
62 /* lifted from lament.c */
63 #define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
64 #define RANDSIGN() ((random() & 1) ? 1 : -1)
65
66
67 #ifdef USE_GL
68
69 #include <GL/glu.h>
70 #include "font-ximage.h"
71
72 #undef countof
73 #define countof(x) (sizeof((x))/sizeof((*x)))
74
75 static int maxparts;
76 static int spin;
77 static int seven;
78 static int rotate;
79 static int rotatespeed;
80 static int uselight;
81 static char *font;
82 int def_parts = 10;
83
84 #undef countof
85 #define countof(x) (sizeof((x))/sizeof((*x)))
86
87 static XrmOptionDescRec opts[] = {
88   {"-parts", ".circuit.parts", XrmoptionSepArg, "10" },
89   {"-font", ".circuit.font", XrmoptionSepArg, "fixed" },
90   {"-rotate-speed", ".circuit.rotatespeed", XrmoptionSepArg, "1" },
91   {"+spin", ".circuit.spin", XrmoptionNoArg, (caddr_t) "false" },
92   {"-spin", ".circuit.spin", XrmoptionNoArg, (caddr_t) "true" },
93   {"+light", ".circuit.light", XrmoptionNoArg, (caddr_t) "false" },
94   {"-light", ".circuit.light", XrmoptionNoArg, (caddr_t) "true" },
95   {"+seven", ".circuit.seven", XrmoptionNoArg, (caddr_t) "false" },
96   {"-seven", ".circuit.seven", XrmoptionNoArg, (caddr_t) "true" },
97   {"+rotate", ".circuit.rotate", XrmoptionNoArg, (caddr_t) "false" },
98   {"-rotate", ".circuit.rotate", XrmoptionNoArg, (caddr_t) "true" },
99 };
100
101 static argtype vars[] = {
102   {(caddr_t *) &maxparts, "parts", "Parts", DEF_PARTS, t_Int},
103   {(caddr_t *) &font, "font", "Font", "fixed", t_String},
104   {(caddr_t *) &rotatespeed, "rotatespeed", "Rotatespeed", "1", t_Int},
105   {(caddr_t *) &spin, "spin", "Spin", DEF_SPIN, t_Bool},
106   {(caddr_t *) &rotate, "rotate", "Rotate", "False", t_Bool},
107   {(caddr_t *) &uselight, "light", "Light", "True", t_Bool},
108   {(caddr_t *) &seven, "seven", "Seven", DEF_SEVEN, t_Bool},
109 };
110
111 ModeSpecOpt circuit_opts = {countof(opts), opts, countof(vars), vars, NULL};
112
113 #ifdef USE_MODULES
114 ModStruct   circuit_description =
115 {"circuit", "init_circuit", "draw_circuit", "release_circuit",
116  "draw_circuit", "init_circuit", NULL, &circuit_opts,
117  1000, 1, 2, 1, 4, 1.0, "",
118  "Flying electronic components", 0, NULL};
119
120 #endif
121
122
123 typedef struct {
124   GLXContext *glx_context;
125   Window window;
126 } Circuit;
127
128 static Circuit *circuit = NULL;
129
130 #include <math.h>
131 #include <sys/time.h>
132 #include <stdio.h>
133 #include <stdlib.h>
134
135 #ifndef M_PI
136 #define M_PI 3.14159265
137 #endif
138
139 /* window width, height */
140 int win_w, win_h;
141
142 /* width and height of viewport */
143
144 #define XMAX 30
145 #define YMAX 30
146
147 #define MAX_COMPONENTS 30
148
149 #define MOVE_MULT 0.05
150
151 static float f_rand(void) {
152    return ((float)RAND(10000)/(float)10000);
153 }
154
155 #define RAND_RANGE(min, max) ((min) + (max - min) * f_rand())
156
157 /* one lucky led gets to be a light source , unless -no-light*/
158 int light = 0;
159 int lighton = 0;
160
161 static GLfloat viewer[] = {0.0, 0.0, 14.0};
162 static GLfloat lightpos[] = {7.0, 7.0, 15, 1.0};
163
164 float sin_table[720];
165 float cos_table[720];
166 float tan_table[720];
167
168 ModeInfo *modeinfo;
169
170 /* used for allocating font textures */
171 typedef struct {
172   int num; /* index number */
173   int w;   /* width */
174   int h;   /* height */
175 } TexNum;
176
177 /* Represents a band on a resistor/diode/etc */
178 typedef struct {
179   float pos;     /* relative position from start/previous band */
180   GLfloat r, g, b; /* colour of the band */
181   float len;     /* length as a fraction of total length */
182 } Band;
183
184 typedef struct {
185   Band *b1, *b2, *b3, *b4; /* bands */
186   int b[4];
187 } Resistor;
188
189 typedef struct {
190   Band *band;
191   GLfloat r, g, b; /* body colour */
192 } Diode;
193
194 static const char * transistortypes[] = {
195   "TIP2955",
196   "TIP32C",
197   "LM 350T",
198   "IRF730",
199   "ULN2577",
200   "7805T",
201   "7912T",
202   "TIP120",
203   "2N6401",
204   "BD239",
205   "2SC1590",
206   "MRF485",
207   "SC141D"
208 };
209
210 typedef struct {
211   int type; /* package type. 0 = to-92, 1 = to-220 */
212   GLfloat tw, th; /* texture dimensions */
213   int tnum; /* texture binding */
214 } Transistor;
215
216 typedef struct {
217   GLfloat r,g,b; /* LED colour */
218   int light; /* are we the light source? */
219 } LED;
220
221 typedef struct {
222   int type; /* 0 = electro, 1 = ceramic */
223   float width; /* width of an electro/ceramic */
224   float length; /* length of an electro */
225 } Capacitor;
226
227 typedef struct {
228   int pins;
229   const char *val;
230 } ICTypes;
231
232 static const ICTypes ictypes[] = {
233   {8, "NE 555"},
234   {8, "LM 386N"},
235   {8, "ADC0831"},
236   {8, "LM 383T"},
237   {8, "TL071"},
238   {8, "LM 311"},
239   {8, "LM393"},
240   {8, "LM 3909"},
241
242   {14, "LM 380N"},
243   {14, "NE 556"},
244   {14, "TL074"},
245   {14, "LM324"},
246   {14, "LM339"},
247   {14, "MC1488"},
248   {14, "MC1489"},
249   {14, "LM1877-9"},
250   {14, "4011"},
251   {14, "4017"},
252   {14, "4013"},
253   {14, "4024"},
254   {14, "4066"},
255
256   {16, "4076"},
257   {16, "4049"},
258   {16, "4094"},
259   {16, "4043"},
260   {16, "4510"},
261   {16, "4511"},
262   {16, "4035"},
263   {16, "RS232"},
264   {16, "MC1800"},
265   {16, "ULN2081"},
266   {16, "UDN2953"},
267
268   {24, "ISD1416P"},
269   {24, "4515"},
270   {24, "TMS6264L"},
271   {24, "MC146818"}
272 };
273
274 typedef struct {
275   int type; /* 0 = DIL, 1 = flat square */
276   int pins; 
277   float tw, th; /* texture dimensions for markings */
278   int tnum; /* texture number */
279 } IC;
280
281 /* 7 segment display */
282
283 typedef struct {
284   int value; /* displayed number */
285 } Disp;
286
287 typedef struct {
288   GLfloat l, w;
289 } Fuse;
290
291 typedef struct {
292   GLfloat x, y, z; /* current co-ordinates */
293   GLfloat dx, dy, dz; /* current direction */
294   GLfloat rotx, roty, rotz; /* rotation vector */
295   GLfloat drot; /* rotation velocity (degrees per frame) */
296   int norm; /* Normalize this component (for shine) */
297   int rdeg; /* current rotation degrees */
298   int angle; /* angle about the z axis */
299   int alpha; /* 0 if not a transparent component */
300   int type;  /* 0 = resistor, 1 = diode, 2 = transistor, 3 = LED, 4 = cap, 5=IC,
301                 6 = 7 seg disp */
302   void * c; /* pointer to the component */
303 } Component;
304
305 static int band_list[12];
306   
307 /* standard colour codes */
308
309 static GLfloat colorcodes [12][3] = {
310   {0.0,0.0,0.0},    /* black  0 */
311   {0.49,0.25,0.08}, /* brown  1 */
312   {1.0,0.0,0.0},    /* red    2 */
313   {1.0,0.5,0.0},    /* orange 3 */
314   {1.0,1.0,0.0},    /* yellow 4 */
315   {0.0,1.0,0.0},    /* green  5 */
316   {0.0,0.5,1.0},    /* blue   6 */
317   {0.7,0.2,1.0},    /* violet 7 */
318   {0.5,0.5,0.5},    /* grey   8 */
319   {1.0,1.0,1.0},    /* white  9 */
320   {0.66,0.56,0.2},    /* gold  10 */
321   {0.8,0.8,0.8},    /* silver 11 */
322 };
323
324 /* base values for components - we can multiply by 0 - 1M */
325 static int values [9][2] = {
326   {1,0},
327   {2,2},
328   {3,3},
329   {4,7},
330   {5,6},
331   {6,8},
332   {7,5},
333   {8,2},
334   {9,1}
335 };
336
337 void DrawResistor(Resistor *);
338 void DrawDiode(Diode *);
339 void DrawTransistor(Transistor *);
340 void DrawLED(LED *);
341 void DrawIC(IC *);
342 void DrawCapacitor(Capacitor *);
343 void DrawDisp(Disp *);
344 void DrawFuse(Fuse *);
345
346 void reorder(Component *[]);
347 void circle(float, int,int);
348 void bandedCylinder(float, float , GLfloat, GLfloat , GLfloat,  Band **, int);
349 TexNum *fonttexturealloc(const char *, float *, float *);
350 Resistor *NewResistor(void);
351 Diode *NewDiode(void);
352 Transistor *NewTransistor(void);
353 LED * NewLED(void);
354 Capacitor *NewCapacitor(void);
355 IC* NewIC(void);
356 Disp* NewDisp(void);
357 Fuse *NewFuse(void);
358
359 /* we use trig tables to speed things up - 200 calls to sin()
360  in one frame can be a bit harsh..
361 */
362
363 void make_tables(void) {
364 int i;
365 float f;
366
367   f = 360 / (M_PI * 2);
368   for (i = 0 ; i < 720 ; i++) {
369     sin_table[i] = sin(i/f);
370   }
371   for (i = 0 ; i < 720 ; i++) {
372     cos_table[i] = cos(i/f);
373   }
374   for (i = 0 ; i < 720 ; i++) {
375     tan_table[i] = tan(i/f);
376   }
377 }
378
379
380 void createCylinder (float length, float radius, int endcaps, int half) 
381 {
382   int a; /* current angle around cylinder */
383   int angle, norm;
384   float z1, y1, z2, y2,ex;
385   int step;
386   int nsegs;
387
388   glPushMatrix();
389   nsegs = radius*MAX(win_w, win_h)/20;
390   nsegs = MAX(nsegs, 4);
391   if (nsegs % 2)
392      nsegs += 1;
393   angle = (half) ? (180 - 90/nsegs) : 374;
394   step = angle/nsegs;
395   z1 = radius; y1 = 0;
396   glBegin(GL_QUADS);
397   for (a = 0 ; a <= angle ; a+= angle/nsegs) {
398     y2=radius*(float)sin_table[(int)a];
399     z2=radius*(float)cos_table[(int)a];
400       glNormal3f(0, y1, z1);
401       glVertex3f(0,y1,z1);
402       glVertex3f(length,y1,z1);
403       glNormal3f(0, y2, z2);
404       glVertex3f(length,y2,z2);
405       glVertex3f(0,y2,z2);
406     z1=z2;
407     y1=y2;
408   }
409   glEnd();
410   if (half) {
411     glBegin(GL_POLYGON);
412       glNormal3f(0, 1, 0);
413       glVertex3f(0, 0, radius);
414       glVertex3f(length, 0, radius);
415       glVertex3f(length, 0, 0 - radius);
416       glVertex3f(0, 0, 0 - radius);
417     glEnd();
418   }
419   if (endcaps) {
420     for(ex = 0 ; ex <= length ; ex += length) {
421       z1 = radius; y1 = 0;
422       norm = (ex == length) ? 1 : -1;
423       glBegin(GL_TRIANGLES);
424       glNormal3f(norm, 0, 0);
425       for (a = 0 ; a <= angle ; a+= angle/nsegs) {
426         y2=radius*(float)sin_table[(int)a];
427         z2=radius*(float)cos_table[(int)a];
428           glVertex3f(ex,0, 0);
429           glVertex3f(ex,y1,z1);
430           glVertex3f(ex,y2,z2);
431         z1=z2;
432         y1=y2;
433       }
434       glEnd();
435     }
436   }
437   glPopMatrix();
438 }
439
440 void circle(float radius, int segments, int half)
441 {
442   float x1 = 0, x2 = 0;
443   float y1 = 0, y2 = 0;
444   int i, t, s;
445
446   if (half) {
447     t = 270; s = 90;
448     x1 = radius, y1 = 0;
449   } else {
450     t = 360, s = 0;
451   }
452   glBegin(GL_TRIANGLES);
453   glNormal3f(1, 0, 0);
454   for(i=s;i<=t;i+=10)
455   {
456     float angle=i;
457     x2=radius*(float)cos_table[(int)angle];
458     y2=radius*(float)sin_table[(int)angle];
459     glVertex3f(0,0,0);
460     glVertex3f(0,y1,x1);
461     glVertex3f(0,y2,x2);
462     x1=x2;
463     y1=y2;
464   }
465   glEnd();
466 }
467
468 void wire(float len)
469 {
470   static GLfloat col[] = {0.3, 0.3, 0.3, 1.0};
471   static GLfloat spec[] = {0.9, 0.9, 0.9, 1.0};
472   static GLfloat nospec[] = {0.4, 0.4, 0.4, 1.0};
473   GLfloat shin = 30;
474   int n;
475
476   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
477   glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
478   glMaterialfv(GL_FRONT, GL_SHININESS, &shin);
479   n = glIsEnabled(GL_NORMALIZE);
480   if (!n) glEnable(GL_NORMALIZE);
481   createCylinder(len, 0.05, 1, 0);
482   if (!n) glDisable(GL_NORMALIZE);
483   glMaterialfv(GL_FRONT, GL_SPECULAR, nospec);
484 }
485
486 void ring(GLfloat inner, GLfloat outer, int nsegs)
487 {
488   GLfloat z1, z2, y1, y2;
489   GLfloat Z1, Z2, Y1, Y2;
490   int i;
491
492   z1 = inner; y1 = 0;
493   Z1 = outer; Y1 = 0;
494   glBegin(GL_QUADS);
495   glNormal3f(1, 0, 0);
496   for(i=0; i <=360 ; i+= 360/nsegs)
497   {
498     float angle=i;
499     z2=inner*(float)sin_table[(int)angle];
500     y2=inner*(float)cos_table[(int)angle];
501     Z2=outer*(float)sin_table[(int)angle];
502     Y2=outer*(float)cos_table[(int)angle];
503     glVertex3f(0, Y1, Z1);
504     glVertex3f(0, y1, z1);
505     glVertex3f(0, y2, z2);
506     glVertex3f(0, Y2, Z2);
507     z1=z2;
508     y1=y2;
509     Z1=Z2;
510     Y1=Y2;
511   }
512   glEnd();
513 }
514
515 void sphere(GLfloat r, float stacks, float slices,
516              int startstack, int endstack, int startslice,
517              int endslice)
518 {
519   GLfloat d, d1, dr, dr1, Dr, Dr1, D, D1, z1, z2, y1, y2, Y1, Z1, Y2, Z2;
520   int a, a1, b, b1, c, c1;
521   GLfloat step, sstep;
522
523   step = 180/stacks;
524   sstep = 360/slices;
525   a1 = startstack * step;
526   b1 = startslice * sstep;
527   y1 = z1 = Y1 = Z1 = 0;
528   c = (endslice / slices) * 360;
529   c1 = (endstack/stacks)*180;
530   glBegin(GL_QUADS);
531   for (a = startstack * step ; a <= c1 ; a+= step) {
532     d=sin_table[a];
533     d1=sin_table[a1];
534     D=cos_table[a];
535     D1=cos_table[a1];
536     dr = d * r;
537     dr1 = d1 * r;
538     Dr = D * r;
539     Dr1 = D1 * r;
540     for (b = b1 ; b <= c ; b+= sstep) {
541       y2=dr*sin_table[b];
542       z2=dr*cos_table[b];
543       Y2=dr1*sin_table[b];
544       Z2=dr1*cos_table[b];
545         glNormal3f(Dr, y1, z1);
546         glVertex3f(Dr,y1,z1);
547         glNormal3f(Dr, y2, z2);
548         glVertex3f(Dr,y2,z2);
549         glNormal3f(Dr1, Y2, Z2);
550         glVertex3f(Dr1,Y2,Z2);
551         glNormal3f(Dr1, Y1, Z1);
552         glVertex3f(Dr1,Y1,Z1);
553       z1=z2;
554       y1=y2;
555       Z1=Z2;
556       Y1=Y2;
557     }
558     a1 = a;
559   }
560   glEnd();
561 }
562
563 int DrawComponent(Component *c)
564 {
565   int ret = 0; /* return 1 if component is freed */
566
567    glPushMatrix();
568    glTranslatef(c->x, c->y, c->z);
569      if (c->angle > 0) {
570         glRotatef(c->angle, c->rotx, c->roty, c->rotz);
571      }
572    if (spin) {
573      glRotatef(c->rdeg, c->rotx, c->roty, c->rotz);
574      c->rdeg += c->drot;
575    }
576
577    if (c->norm)
578      glEnable(GL_NORMALIZE);
579    else
580      glDisable(GL_NORMALIZE);
581
582  /* call object draw routine here */
583    if (c->type == 0) {
584      DrawResistor(c->c);
585    } else if (c->type == 1) {
586      DrawDiode(c->c);
587    } else if (c->type == 2) {
588      DrawTransistor(c->c);
589    } else if (c->type == 3) {
590      if (((LED *)c->c)->light && light) {
591        GLfloat lp[] = {0.1, 0, 0, 1};
592        glEnable(GL_LIGHT1);
593        glLightfv(GL_LIGHT1, GL_POSITION, lp);
594      }
595      DrawLED(c->c);
596    } else if (c->type == 4) {
597      DrawCapacitor(c->c);
598    } else if (c->type == 5) {
599      DrawIC(c->c);
600    } else if (c->type == 6) {
601      DrawDisp(c->c);
602    } else if (c->type == 7) {
603      DrawFuse(c->c);
604    }
605    c->x += c->dx * MOVE_MULT;
606    c->y += c->dy * MOVE_MULT;
607    if (c->x > XMAX/2 || c->x < 0 - XMAX/2 ||
608        c->y > YMAX/2 || c->y < 0 - YMAX/2) {
609         if (c->type == 3 && ((LED *)c->c)->light && light) {
610           glDisable(GL_LIGHT1);
611           light = 0; lighton = 0;
612         }
613         if (c->type == 1)
614           free(((Diode *)c->c)->band); /* remember to free diode band */
615         free(c->c);
616         ret = 1;
617    }
618
619    glPopMatrix();
620    glDisable(GL_NORMALIZE);
621    return ret;
622 }
623
624 /* draw a resistor */
625
626 void DrawResistor(Resistor *r)
627 {
628   int i;
629   GLfloat col[] = {0.74, 0.62, 0.46, 1.0};
630   GLfloat spec[] = {0.8, 0.8, 0.8, 1.0};
631   GLfloat shine = 30;
632
633    glTranslatef(-4, 0, 0);
634    wire(3);
635    glTranslatef(3, 0, 0);
636    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
637    glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
638    glMaterialfv(GL_FRONT, GL_SHININESS, &shine);
639    createCylinder(1.8, 0.4, 1, 0);
640    glPushMatrix();
641    for (i = 0 ; i < 4 ; i++) {
642      glTranslatef(0.35, 0, 0);
643      glCallList(band_list[r->b[i]]);
644    }
645    glPopMatrix();
646    glTranslatef(1.8, 0, 0);
647    wire(3);
648 }
649
650 void DrawFuse(Fuse *f)
651 {
652   static GLfloat col[] = {0.5, 0.5, 0.5, 1.0}; /* endcaps */
653   static GLfloat glass[] = {0.4, 0.4, 0.4, 0.3}; /* glass */
654   static GLfloat spec[] = {1, 1, 1, 1}; /* glass */
655
656    glPushMatrix();
657    glTranslatef(-1.8, 0, 0);
658    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
659    glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
660    glMateriali(GL_FRONT, GL_SHININESS, 40);
661    createCylinder(0.8, 0.45, 1, 0);
662    glTranslatef(0.8, 0, 0);
663    glEnable(GL_BLEND);
664    glDepthMask(GL_FALSE);
665    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glass);
666    glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 40);
667    createCylinder(2, 0.4, 0, 0);
668    createCylinder(2, 0.3, 0, 0);
669    glDisable(GL_BLEND);
670    glDepthMask(GL_TRUE);
671    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
672    glMateriali(GL_FRONT, GL_SHININESS, 40);
673    glBegin(GL_LINES);
674    glVertex3f(0, 0, 0);
675    glVertex3f(2, 0. ,0);
676    glEnd();
677    glTranslatef(2, 0, 0);
678    createCylinder(0.8, 0.45, 1, 0);
679    glPopMatrix();
680 }
681
682
683 void DrawCapacitor(Capacitor *c)
684 {
685   static GLfloat col[] = {0, 0, 0, 0};
686   static GLfloat spec[] = {0.8, 0.8, 0.8, 0};
687   GLfloat brown[] = {0.84, 0.5, 0};
688   static GLfloat shine = 40;
689
690   glPushMatrix();
691   if (c->type) {
692     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, brown);
693     sphere(c->width, 15, 15, 0, 4 ,0, 15);
694     glTranslatef(1.35*c->width, 0, 0);
695     sphere(c->width, 15, 15, 11, 15, 0, 15);
696     glRotatef(90, 0, 0, 1);
697     glTranslatef(0, 0.7*c->width, 0.3*c->width);
698     wire(3*c->width);
699     glTranslatef(0, 0, -0.6*c->width);
700     wire(3*c->width);
701   } else {
702     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
703     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
704     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &shine);
705     glBegin(GL_POLYGON);
706      glVertex3f(0, 0.82*c->width, -0.1);
707      glVertex3f(3*c->length, 0.82*c->width, -0.1);
708      glVertex3f(3*c->length, 0.82*c->width, 0.1);
709      glVertex3f(0, 0.82*c->width, 0.1);
710     glEnd();
711     col[0] = 0.7;
712     col[1] = 0.7;
713     col[2] = 0.7;
714     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
715     circle(0.6*c->width, 30, 0);
716     col[0] = 0.0;
717     col[1] = 0.2;
718     col[2] = 0.9;
719     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
720     ring(0.6*c->width, 0.8*c->width, 30);
721     glTranslatef(0.01, 0.0, 0);
722     createCylinder(3.0*c->length, 0.8*c->width, 1, 0);
723     col[0] = 0;
724     col[1] = 0;
725     col[2] = 0;
726     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
727     glTranslatef(3.01*c->length, 0.0, 0);
728     circle(0.6*c->width, 30, 0);
729     glTranslatef(0, 0.4*c->width, 0);
730     wire(3*c->length);
731     glTranslatef(0.0, -0.8*c->width, 0);
732     wire(3.3*c->length);
733   }
734   glPopMatrix();
735 }
736
737 void DrawLED(LED *l)
738 {
739   GLfloat col[] = {0, 0, 0, 0.6};
740
741   col[0] = l->r; col[1] = l->g; col[2] = l->b;
742   if (l->light && light) {
743     GLfloat dir[] = {-1, 0, 0};
744     glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, dir);
745     if (!lighton) {
746       glLightfv(GL_LIGHT1, GL_SPECULAR, col);
747       glLightfv(GL_LIGHT1, GL_AMBIENT, col);
748       glLightfv(GL_LIGHT1, GL_DIFFUSE, col);
749       glLighti(GL_LIGHT1, GL_SPOT_CUTOFF, (GLint) 90);
750       glLighti(GL_LIGHT1, GL_CONSTANT_ATTENUATION, (GLfloat)1); 
751       glLighti(GL_LIGHT1, GL_LINEAR_ATTENUATION, (GLfloat)0); 
752       glLighti(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, (GLfloat)0); 
753       glLighti(GL_LIGHT1, GL_SPOT_EXPONENT, (GLint) 20);
754       lighton = 1;
755     }
756   }
757   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
758   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col);
759   /* no transparency when LED is lit */
760   if (!l->light) { 
761     glEnable(GL_BLEND);
762     glDepthMask(GL_FALSE);
763     glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
764   }
765   createCylinder(1.2, 0.3, 0, 0);
766   if (l->light && light) {
767     glDisable(GL_LIGHTING);
768     glColor3fv(col);
769   }
770   sphere(0.3, 7, 7, 3, 7, 0, 7);
771   if (l->light && light) {
772     glEnable(GL_LIGHTING);
773   } else {
774     glDepthMask(GL_TRUE);
775     glDisable(GL_BLEND);
776   }
777
778   glTranslatef(1.2, 0, 0);
779   createCylinder(0.1, 0.38, 1, 0);
780   glTranslatef(-0.3, 0.15, 0);
781   wire(3);
782   glTranslatef(0, -0.3, 0);
783   wire(3.3);
784   if (random() % 50 == 25) {
785     if (l->light) {
786       l->light = 0; light = 0; lighton = 0;
787       glDisable(GL_LIGHT1);
788     } else if (!light) {
789       l->light = 1; light = 1;
790     }
791   }
792 }
793
794
795
796 void DrawDiode(Diode *d)
797 {
798   GLfloat shine = 40;
799   GLfloat col[] = {0.3, 0.3, 0.3, 0};
800   GLfloat spec[] = {0.7, 0.7, 0.7, 0};
801
802    glPushMatrix();
803    glMaterialfv(GL_FRONT, GL_SHININESS, &shine);
804    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
805    glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
806    glTranslatef(-4, 0, 0);
807    wire(3);
808    glTranslatef(3, 0, 0);
809    bandedCylinder(0.3, 1.5, d->r, d->g, d->b, &(d->band), 1);
810    glTranslatef(1.5, 0, 0);
811    wire(3);
812    glPopMatrix();
813 }
814
815 void Rect(GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h,
816             GLfloat t)
817 {
818   GLfloat yh;
819   GLfloat xw;
820   GLfloat zt;
821
822   yh = y+h; xw = x+w; zt = z - t;
823
824   glBegin(GL_QUADS); /* front */
825     glNormal3f(0, 0, 1);
826     glVertex3f(x, y, z);
827     glVertex3f(x, yh, z);
828     glVertex3f(xw, yh, z);
829     glVertex3f(xw, y, z);
830   /* back */
831     glNormal3f(0, 0, -1);
832     glVertex3f(x, y, zt);
833     glVertex3f(x, yh, zt);
834     glVertex3f(xw, yh, zt);
835     glVertex3f(xw, y, zt);
836   /* top */
837     glNormal3f(0, 1, 0);
838     glVertex3f(x, yh, z);
839     glVertex3f(x, yh, zt);
840     glVertex3f(xw, yh, zt);
841     glVertex3f(xw, yh, z);
842   /* bottom */
843     glNormal3f(0, -1, 0);
844     glVertex3f(x, y, z);
845     glVertex3f(x, y, zt);
846     glVertex3f(xw, y, zt);
847     glVertex3f(xw, y, z);
848   /* left */
849     glNormal3f(-1, 0, 0);
850     glVertex3f(x, y, z);
851     glVertex3f(x, y, zt);
852     glVertex3f(x, yh, zt);
853     glVertex3f(x, yh, z);
854   /* right */
855     glNormal3f(1, 0, 0);
856     glVertex3f(xw, y, z);
857     glVertex3f(xw, y, zt);
858     glVertex3f(xw, yh, zt);
859     glVertex3f(xw, yh, z);
860   glEnd();
861 }
862
863 /* IC pins */
864
865 void ICLeg(GLfloat x, GLfloat y, GLfloat z, int dir)
866 {
867   if (dir) {
868     Rect(x-0.1, y, z, 0.1, 0.1, 0.02);
869     Rect(x-0.1, y, z, 0.02, 0.1, 0.1);
870     Rect(x-0.1, y+0.03, z-0.1, 0.02, 0.05, 0.3);
871   } else {
872     Rect(x, y, z, 0.1, 0.1, 0.02);
873     Rect(x+0.8*0.1, y, z, 0.02, 0.1, 0.1);
874     Rect(x+0.8*0.1, y+0.03, z-0.1, 0.02, 0.05, 0.3);
875   }
876
877 }
878
879
880 void DrawIC(IC *c)
881 {
882   GLfloat w, h, d;
883   int z;
884   GLfloat col[] = {0.1, 0.1, 0.1, 0};
885   GLfloat col2[] = {0.2, 0.2, 0.2, 0};
886   GLfloat spec[] = {0.6, 0.6, 0.6, 0};
887   GLfloat shine = 40;
888   GLfloat lspec[] = {0.6, 0.6, 0.6, 0};
889   GLfloat lcol[] = {0.4, 0.4, 0.4, 0};
890   GLfloat lshine = 40;
891   float mult, th, size;
892
893   glPushMatrix();
894   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
895   glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
896   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
897   glMaterialfv(GL_FRONT, GL_SHININESS, &shine);
898   switch(c->pins) {
899     case 8:
900       w = 1.0; h = 1.5;
901       break;
902     case 14:
903       w = 1.0; h = 3;
904       break;
905     case 16:
906       w = 1.0; h = 3;
907       break;
908     case 24:
909     default:
910       w = 1.5; h = 3.5;
911       break;
912   }
913   w = w/2; h = h/2;
914     glBegin(GL_QUADS);
915       glNormal3f(0, 0, 1);
916       glVertex3f(w, h, 0.1);
917       glVertex3f(w, -h, 0.1);
918       glVertex3f(-w, -h, 0.1);
919       glVertex3f(-w, h, 0.1);
920       glNormal3f(0, 0, -1);
921       glVertex3f(w, h, -0.1);
922       glVertex3f(w, -h, -0.1);
923       glVertex3f(-w, -h, -0.1);
924       glVertex3f(-w, h, -0.1);
925       glNormal3f(1, 0, 0);
926       glVertex3f(w, h, -0.1);
927       glVertex3f(w, -h, -0.1);
928       glVertex3f(w, -h, 0.1);
929       glVertex3f(w, h, 0.1);
930       glNormal3f(0, -1, 0);
931       glVertex3f(w, -h, -0.1);
932       glVertex3f(w, -h, 0.1);
933       glVertex3f(-w, -h, 0.1);
934       glVertex3f(-w, -h, -0.1);
935       glNormal3f(-1, 0, 0);
936       glVertex3f(-w, h, -0.1);
937       glVertex3f(-w, h, 0.1);
938       glVertex3f(-w, -h, 0.1);
939       glVertex3f(-w, -h, -0.1);
940       glNormal3f(0, -1, 0);
941       glVertex3f(-w, h, -0.1);
942       glVertex3f(w, h, -0.1);
943       glVertex3f(w, h, 0.1);
944       glVertex3f(-w, h, 0.1);
945     glEnd();
946     glBindTexture(GL_TEXTURE_2D, c->tnum);
947     glEnable(GL_TEXTURE_2D);
948     glEnable(GL_BLEND);
949     glDepthMask(GL_FALSE);
950     if (c->pins == 8)
951       size = 0.4;
952     else
953       size = 0.6;
954     th = size/2;
955     mult = size*c->tw / c->th;
956     mult /= 2;
957     glBegin(GL_QUADS); /* text markings */
958      glNormal3f(0, 0, 1);
959      glTexCoord2f(0, 1);
960      glVertex3f(th, mult, 0.11);
961      glTexCoord2f(1, 1);
962      glVertex3f(th, -mult, 0.11);
963      glTexCoord2f(1, 0);
964      glVertex3f(-th, -mult, 0.11);
965      glTexCoord2f(0, 0);
966      glVertex3f(-th, mult, 0.11);
967     glEnd();
968     glDisable(GL_TEXTURE_2D);
969     glDisable(GL_BLEND);
970     glDepthMask(GL_TRUE);
971     d = (h*2-0.1) / c->pins;
972     d*=2;
973     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, lcol);
974     glMaterialfv(GL_FRONT, GL_SPECULAR, lspec);
975     glMaterialfv(GL_FRONT, GL_SHININESS, &lshine);
976     for (z = 0 ; z < c->pins/2 ; z++) {
977       ICLeg(w, -h + z*d + d/2, 0, 0);
978     }
979     for (z = 0 ; z < c->pins/2 ; z++) {
980       ICLeg(-w, -h + z*d + d/2, 0, 1);
981     }
982     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col2);
983     glTranslatef(-w+0.3, h-0.3, 0.11);
984     glRotatef(90, 0, 1, 0);
985     circle(0.1, 7, 0);
986     glPopMatrix();
987 }
988
989 void DrawDisp(Disp *d)
990 {
991   GLfloat col[] = {0.8, 0.8, 0.8, 1.0}; /* body colour */
992   GLfloat front[] = {0.2, 0.2, 0.2, 1.0}; /* front colour */
993   GLfloat on[] = {0.9, 0, 0, 1}; /* 'on' segment */
994   GLfloat off[] = {0.3, 0, 0, 1}; /* 'off' segment */
995   int i, j, k;
996   GLfloat x, y; /* for the pins */
997   GLfloat spec[] = {0.6, 0.6, 0.6, 0};
998   GLfloat lcol[] = {0.4, 0.4, 0.4, 0};
999   GLfloat shine = 40;
1000   static GLfloat vdata_h[6][2] = {
1001     {0, 0},
1002     {0.1, 0.1},
1003     {0.9, 0.1},
1004     {1, 0},
1005     {0.9, -0.1},
1006     {0.1, -0.1}
1007   };
1008   static GLfloat vdata_v[6][2] = {
1009     {0.27, 0},
1010     {0.35, -0.1},
1011     {0.2, -0.9},
1012     {0.1, -1},
1013     {0, -0.9},
1014     {0.15, -0.15}
1015   };
1016
1017   static GLfloat seg_start[7][2] = {
1018     {0.55, 2.26},
1019     {1.35, 2.26},
1020     {1.2, 1.27},
1021     {0.25, 0.25},
1022     {0.06, 1.25},
1023     {0.25, 2.25},
1024     {0.39, 1.24}
1025   };
1026
1027   static int nums[10][7] = {
1028     {1, 1, 1, 1, 1, 1, 0}, /* 0 */
1029     {0, 1, 1, 0, 0, 0, 0}, /* 1 */
1030     {1, 1, 0, 1, 1, 0, 1}, /* 2 */
1031     {1, 1, 1, 1, 0, 0, 1}, /* 3 */
1032     {0, 1, 1, 0, 0, 1, 1}, /* 4 */
1033     {1, 0, 1, 1, 0, 1, 1}, /* 5 */
1034     {1, 0, 1, 1, 1, 1, 1}, /* 6 */
1035     {1, 1, 1, 0, 0, 0, 0}, /* 7 */
1036     {1, 1, 1, 1, 1, 1, 1}, /* 8 */
1037     {1, 1, 1, 0, 0, 1, 1}  /* 9 */
1038   };
1039
1040    glTranslatef(-0.9, -1.8, 0);
1041    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
1042    Rect(0, 0, -0.01, 1.8, 2.6, 0.7);
1043    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, front);
1044    glBegin(GL_QUADS);
1045      glVertex2f(-0.05, -0.05);
1046      glVertex2f(-0.05, 2.65);
1047      glVertex2f(1.85, 2.65);
1048      glVertex2f(1.85, -0.05);
1049    glEnd();
1050    glDisable(GL_LIGHTING); /* lit segments dont need light */
1051    if (!seven && (random() % 30) == 19) { /* randomly change value */
1052      d->value = random() % 10;
1053    }
1054    for (j = 0 ; j < 7 ; j++) { /* draw the segments */
1055      GLfloat xx[6], yy[6];
1056      if (nums[d->value][j])
1057        glColor3fv(on);
1058      else
1059        glColor3fv(off);
1060      for (k = 0 ; k < 6 ; k++) {
1061        if (j == 0 || j == 3 || j == 6) {
1062          xx[k] = seg_start[j][0] + vdata_h[k][0];
1063          yy[k] = seg_start[j][1] + vdata_h[k][1];
1064        } else {
1065          xx[k] = seg_start[j][0] + vdata_v[k][0];
1066          yy[k] = seg_start[j][1] + vdata_v[k][1];
1067        }
1068      }
1069      glBegin(GL_POLYGON);
1070      for(i = 0 ; i < 6 ; i++) {
1071        glVertex3f(xx[i], yy[i], 0.01);
1072      }
1073      glEnd();
1074    }
1075    glColor3fv(on);
1076    glPointSize(4);
1077    glBegin(GL_POINTS);
1078     glVertex3f(1.5, 0.2, 0.01);
1079    glEnd();
1080    glEnable(GL_LIGHTING);
1081    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, lcol);
1082    glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
1083    glMaterialfv(GL_FRONT, GL_SHININESS, &shine);
1084    for (x = 0.35 ; x <= 1.5 ; x+= 1.15) {
1085      for ( y = 0.2 ; y <= 2.4 ; y += 0.3) {
1086        ICLeg(x, y, -0.7, 1);
1087      }
1088    }
1089 }
1090
1091 void HoledRectangle(GLfloat w, GLfloat h, GLfloat d, GLfloat radius, int p)
1092 {
1093   int step, a;
1094   GLfloat x1, y1, x2, y2;
1095   GLfloat yr, yr1, xr, xr1, side, side1;
1096   GLfloat nx, ny;
1097
1098   step = 360 / p;
1099   x1 = radius; y1 = 0;
1100   xr1 = w/2; yr1 = 0;
1101   side = w/2;
1102   side1 = h/2;
1103   glBegin(GL_QUADS);
1104   for (a = 0 ; a <= 360 ; a+= step) {
1105     y2=radius*(float)sin_table[(int)a];
1106     x2=radius*(float)cos_table[(int)a];
1107
1108     if (a < 45 || a > 315) {
1109       xr = side;
1110       yr = side1 * tan_table[a];
1111       nx = 1; ny = 0;
1112     } else if (a <= 135 || a >= 225) {
1113       xr = side/tan_table[a];
1114       if (a >= 225) {
1115        yr = -side1;
1116        xr = 0 - xr;
1117        nx = 0; ny = -1;
1118       } else {
1119        yr = side1;
1120        nx = 0; ny = 1;
1121      }
1122     } else {
1123       xr = -side;
1124       yr = -side1 * tan_table[a];
1125       nx = -1; ny = 0;
1126     }
1127
1128       glNormal3f(-x1, -y1, 0); /* cylinder */
1129       glVertex3f(x1,y1,0);
1130       glVertex3f(x1,y1,-d);
1131       glVertex3f(x2,y2,-d);
1132       glVertex3f(x2,y2,0);
1133
1134       glNormal3f(0, 0, 1); /* front face */
1135       glVertex3f(x1,y1,0);
1136       glVertex3f(xr1, yr1, 0);
1137       glVertex3f(xr, yr, 0);
1138       glVertex3f(x2, y2, 0);
1139
1140       glNormal3f(nx, ny, 0); /* side */
1141       glVertex3f(xr, yr, 0);
1142       glVertex3f(xr, yr, -d);
1143       glVertex3f(xr1, yr1, -d);
1144       glVertex3f(xr1, yr1, 0);
1145
1146       glNormal3f(0, 0, -1); /* back */
1147       glVertex3f(xr, yr, -d);
1148       glVertex3f(x2, y2, -d);
1149       glVertex3f(x1, y1, -d);
1150       glVertex3f(xr1, yr1, -d);
1151
1152     x1=x2;
1153     y1=y2;
1154     xr1 = xr; yr1 = yr;
1155   }
1156   glEnd();
1157 }
1158
1159 void DrawTransistor(Transistor *t)
1160 {
1161   static GLfloat col[] = {0.3, 0.3, 0.3, 1.0};
1162   static GLfloat spec[] = {0.9, 0.9, 0.9, 1.0};
1163   static GLfloat nospec[] = {0.4, 0.4, 0.4, 1.0};
1164   GLfloat shin = 30;
1165
1166   glPushMatrix();
1167   glMaterialfv(GL_FRONT, GL_SHININESS, &shin);
1168   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col);
1169   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1170   if (t->type == 1) {
1171     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, col);
1172     glRotatef(90, 0, 1, 0);
1173     glRotatef(90, 0, 0, 1);
1174     createCylinder(1.0, 0.4, 1, 1);
1175     Rect(0, -0.2, 0.4, 1, 0.2, 0.8);
1176     glTranslatef(-2, 0, -0.2);
1177     wire(2);
1178     glTranslatef(0, 0, 0.2);
1179     wire(2);
1180     glTranslatef(0, 0, 0.2);
1181     wire(2);
1182   } else {
1183     float mult, y1, y2;
1184     mult = 1.5*t->th/t->tw;
1185     y1 = 0.75+mult/2;
1186     y2 = 0.75-mult/2;
1187     Rect(0, 0, 0, 1.5, 1.5, 0.5);
1188     glEnable(GL_TEXTURE_2D);
1189     glBindTexture(GL_TEXTURE_2D, t->tnum);
1190     glEnable(GL_BLEND);
1191     glDepthMask(GL_FALSE);
1192     glBegin (GL_QUADS);
1193      glNormal3f(0, 0, 1);
1194      glTexCoord2f(0, 1);
1195      glVertex3f(0, y1, 0.01);
1196      glTexCoord2f(1, 1);
1197      glVertex3f(1.5, y1, 0.01);
1198      glTexCoord2f(1, 0);
1199      glVertex3f(1.5, y2, 0.01);
1200      glTexCoord2f(0, 0);
1201      glVertex3f(0, y2, 0.01);
1202     glEnd();
1203     glDisable(GL_TEXTURE_2D);
1204     glDisable(GL_BLEND);
1205     glDepthMask(GL_TRUE);
1206     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
1207     glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
1208     glMaterialfv(GL_FRONT, GL_SHININESS, &shin);
1209     Rect(0, 0, -0.5, 1.5, 1.5, 0.30);
1210     if (!glIsEnabled(GL_NORMALIZE)) glEnable(GL_NORMALIZE);
1211     glTranslatef(0.75, 1.875, -0.55);
1212     HoledRectangle(1.5, 0.75, 0.25, 0.2, 8);
1213     glMaterialfv(GL_FRONT, GL_SPECULAR, nospec);
1214     glTranslatef(-0.375, -1.875, 0);
1215     glRotatef(90, 0, 0, -1);
1216     wire(2);
1217     glTranslatef(0, 0.375, 0);
1218     wire(2);
1219     glTranslatef(0, 0.375, 0);
1220     wire(2);
1221   }
1222   glPopMatrix();
1223 }
1224
1225 Component * NewComponent(void)
1226 {
1227   Component *c;
1228   float rnd;
1229
1230   c = malloc(sizeof(Component));
1231   c->angle = RAND_RANGE(0,360);
1232   rnd = f_rand();
1233   if (rnd < 0.25) { /* come from the top */
1234      c->y = YMAX/2;
1235      c->x = RAND_RANGE(0, XMAX) - XMAX/2;
1236      if (c->x > 0)
1237        c->dx = 0 - RAND_RANGE(0.5, 2);
1238      else 
1239        c->dx = RAND_RANGE(0.5, 2);
1240      c->dy = 0 - RAND_RANGE(0.5, 2);
1241   } else if (rnd < 0.5) { /* come from the bottom */
1242      c->y = 0 - YMAX/2;
1243      c->x = RAND_RANGE(0, XMAX) - XMAX/2;
1244      if (c->x > 0)
1245        c->dx = 0 - RAND_RANGE(0.5, 2);
1246      else 
1247        c->dx = RAND_RANGE(0.5, 2);
1248      c->dy = RAND_RANGE(0.5, 2);
1249   } else if (rnd < 0.75) { /* come from the left */
1250      c->x = 0 - XMAX/2;
1251      c->y = RAND_RANGE(0, YMAX) - YMAX/2;
1252      c->dx = RAND_RANGE(0.5, 2);
1253      if (c->y > 0)
1254        c->dy = 0 - RAND_RANGE(0.5, 2);
1255      else 
1256        c->dy = RAND_RANGE(0.5, 2);
1257   } else { /* come from the right */
1258      c->x = XMAX/2;
1259      c->y = RAND_RANGE(0, YMAX) - YMAX/2;
1260      c->dx =  0 - RAND_RANGE(0.5, 2);
1261      if (c->y > 0)
1262        c->dy = 0 - RAND_RANGE(0.5, 2);
1263      else 
1264        c->dy = RAND_RANGE(0.5, 2);
1265   }
1266   c->z = RAND_RANGE(0, 7) - 9;
1267   c->rotx = f_rand();
1268   c->roty = f_rand();
1269   c->rotz = f_rand();
1270   c->drot = f_rand() * 7;
1271   c->rdeg = 0;
1272   c->dz = f_rand()*2 - 1;
1273   c->norm = 0;
1274   c->alpha = 0; /* explicitly set to 1 later */
1275   rnd = f_rand();
1276   if (rnd < 0.1) {
1277     c->c = NewResistor();
1278     c->type = 0;
1279     if (f_rand() < 0.4)
1280       c->norm = 1; /* some resistors shine */
1281   } else if (rnd < 0.2) {
1282     c->c = NewDiode();
1283     if (f_rand() < 0.4)
1284       c->norm = 1; /* some diodes shine */
1285     c->type = 1;
1286   } else if (rnd < 0.3) {
1287     c->c = NewTransistor();
1288     c->norm = 1;
1289     c->type = 2;
1290   } else if (rnd < 0.4) {
1291     c->c = NewCapacitor();
1292     c->norm = 1;
1293     c->type = 4;
1294   } else if (rnd < 0.6) {
1295     c->c = NewIC();
1296     c->type = 5;
1297     c->norm = 1;
1298   } else if (rnd < 0.7) {
1299     c->c = NewLED();
1300     c->type = 3;
1301     c->norm = 1;
1302     c->alpha = 1;
1303   } else if (rnd < 0.8) {
1304     c->c = NewFuse();
1305     c->norm = 1;
1306     c->type = 7;
1307     c->alpha = 1;
1308   } else {
1309     c->c = NewDisp();
1310     c->type = 6;
1311   }
1312   return c;
1313 }
1314
1315 Transistor *NewTransistor(void)
1316 {
1317   Transistor *t;
1318   float texfg[] = {0.7, 0.7, 0.7, 1.0};
1319   float texbg[] = {0.3, 0.3, 0.3, 0.1};
1320   TexNum *tn;
1321   const char *val;
1322
1323   t = malloc(sizeof(Transistor));
1324   t->type = (f_rand() < 0.5);
1325   if (t->type == 0) {
1326     val = transistortypes[random() % countof(transistortypes)];
1327     tn = fonttexturealloc(val, texfg, texbg);
1328     if (tn == NULL) {
1329       fprintf(stderr, "Error getting a texture for a string!\n");
1330     } else {
1331       t->tnum = tn->num;
1332       t->tw = tn->w; t->th = tn->h;
1333       free(tn);
1334     }
1335   }
1336   return t;
1337 }
1338
1339 Capacitor *NewCapacitor(void)
1340 {
1341   Capacitor *c;
1342
1343   c = malloc(sizeof(Capacitor));
1344   c->type = (f_rand() < 0.5);
1345   if (!c->type) {
1346     c->length = RAND_RANGE(0.5, 1);
1347     c->width = RAND_RANGE(0.5, 1);
1348   } else {
1349     c->width = RAND_RANGE(0.3, 1);
1350   }
1351   return c;
1352 }
1353
1354 /* 7 segment display */
1355
1356 Disp *NewDisp(void)
1357 {
1358   Disp *d;
1359
1360   d = malloc(sizeof(Disp));
1361   if (seven)
1362     d->value = 7;
1363   else
1364     d->value = RAND_RANGE(0, 10);
1365   return d;
1366 }
1367
1368
1369 IC *NewIC(void)
1370 {
1371   IC *c;
1372   int pins;
1373   TexNum *tn;
1374   float texfg[] = {0.7, 0.7, 0.7, 1.0};
1375   float texbg[] = {0.1, 0.1, 0.1, 0};
1376   const char *val;
1377   int types[countof(ictypes)], i, n = 0;
1378
1379   c = malloc(sizeof(IC));
1380   c->type = 0;
1381   switch((int)RAND_RANGE(0,4)) {
1382     case 0:
1383       pins = 8;
1384       break;
1385     case 1:
1386       pins = 14;
1387       break;
1388     case 2:
1389       pins = 16;
1390       break;
1391     case 3:
1392     default:
1393       pins = 24;
1394       break;
1395   }
1396   for (i = 0 ; i < countof(ictypes) ; i++) {
1397     if (ictypes[i].pins == pins) {
1398        types[n] = i;
1399        n++;
1400     }
1401   }
1402
1403   if (n > countof(types)) abort();
1404   val = ictypes[types[random() % n]].val;
1405   tn = fonttexturealloc(val, texfg, texbg);
1406   if (tn == NULL) {
1407     fprintf(stderr, "Error allocating font texture for '%s'\n", val);
1408   } else {
1409     c->tw = tn->w; c->th = tn->h;
1410     c->tnum = tn->num;
1411     free(tn);
1412   }
1413   c->pins = pins;
1414   return c;
1415 }
1416
1417 LED *NewLED(void)
1418 {
1419   LED *l;
1420   float r;
1421
1422   l = malloc(sizeof(LED));
1423   r = f_rand();
1424   l->light = 0;
1425   if (!light && (f_rand() < 0.4)) {
1426      light = 1;
1427      l->light = 1;
1428   }
1429   if (r < 0.2) {
1430     l->r = 0.9; l->g = 0; l->b = 0;
1431   } else if (r < 0.4) {
1432     l->r = 0.3; l->g = 0.9; l->b = 0;
1433   } else if (r < 0.6) {
1434     l->r = 0.8; l->g = 0.9; l->b = 0;
1435   } else if (r < 0.8) {
1436     l->r = 0.0; l->g = 0.2; l->b = 0.8;
1437   } else {
1438     l->r = 0.9, l->g = 0.55, l->b = 0;
1439   }
1440   return l;
1441 }
1442
1443 Fuse *NewFuse(void)
1444 {
1445   Fuse *f;
1446
1447   f = malloc(sizeof(Fuse));
1448   return f;
1449 }
1450
1451 Diode *NewDiode(void)
1452 {
1453   Band *b;
1454   Diode *ret;
1455
1456   ret = malloc(sizeof(Diode));
1457   b = malloc(sizeof(Band));
1458   b->pos = 0.8;
1459   b->len = 0.1;
1460   if (f_rand() < 0.5) {
1461     b->r = 1;
1462     b->g = 1;
1463     b->b = 1;
1464     ret->r = 0.7; ret->g = 0.1 ; ret->b = 0.1;
1465   } else {
1466     b->r = 1;
1467     b->g = 1;
1468     b->b = 1;
1469     ret->r = 0.2; ret->g = 0.2 ; ret->b = 0.2;
1470   }
1471   ret->band = b;
1472   return ret;
1473 }
1474
1475
1476 Resistor  * NewResistor(void)
1477 {
1478   int v, m, t; /* value, multiplier, tolerance */
1479   Resistor *ret;
1480
1481   v = RAND(9);
1482   m = RAND(5);
1483   t = (RAND(10) < 5) ? 10 : 11; 
1484   ret = malloc(sizeof(Resistor));
1485
1486   if (seven) {
1487     ret->b[0] = ret->b[1] = ret->b[2] = 7;
1488   } else {
1489     ret->b[0] = values[v][0];
1490     ret->b[1] = values[v][1];
1491     ret->b[2] = m;
1492   }
1493   ret->b[3] = t;
1494
1495   return ret;
1496 }
1497
1498 void makebandlist(void)
1499 {
1500   int i;
1501   GLfloat col[] = {0,0,0,0};
1502   GLfloat spec[] = {0.8,0.8,0.8,0};
1503   GLfloat shine = 40;
1504
1505    for (i = 0 ; i < 12 ; i++) {
1506      band_list[i] = glGenLists(i);
1507      glNewList(band_list[i], GL_COMPILE);
1508      col[0] = colorcodes[i][0];
1509      col[1] = colorcodes[i][1];
1510      col[2] = colorcodes[i][2];
1511      glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
1512      glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
1513      glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &shine);
1514      createCylinder(0.1, 0.42, 0, 0);
1515      glEndList();
1516   }
1517 }
1518   
1519
1520 void bandedCylinder(float radius, float l, GLfloat r, GLfloat g, GLfloat bl, 
1521                         Band **b, int nbands)
1522 {
1523   int n; /* band number */
1524   int p = 0; /* prev number + 1; */
1525   GLfloat col[] = {0,0,0,0};
1526
1527    col[0] = r; col[1] = g; col[2] = bl;
1528    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
1529    createCylinder(l, radius, 1, 0); /* body */
1530    for (n = 0 ; n < nbands ; n++) {
1531      glPushMatrix();
1532      glTranslatef(b[n]->pos*l, 0, 0);
1533      col[0] = b[n]->r; col[1] = b[n]->g; col[2] = b[n]->b;
1534      glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col);
1535      createCylinder(b[n]->len*l, radius*1.05, 0, 0); /* band */
1536      glPopMatrix();
1537      p = n+1;
1538    }
1539 }
1540
1541 void drawgrid(void)
1542 {
1543   GLfloat x, y;
1544   static GLfloat col[] = {0, 0.25, 0.05};
1545   static GLfloat col2[] = {0, 0.125, 0.05};
1546   GLfloat col3[] = {0, 0.8, 0};
1547   static GLfloat sx, sy; /* bright spot co-ords */
1548   static int sdir; /* 0 = left-right, 1 = right-left, 2 = up->dn, 3 = dn->up */
1549   static int s = 0; /* if spot is enabled */
1550   static float ds; /* speed of spot */
1551
1552   if (!s) {
1553      if (f_rand() < ((rotate) ? 0.05 : 0.01)) {
1554        sdir = RAND_RANGE(0, 4);
1555        ds = RAND_RANGE(0.4, 0.8);
1556        switch (sdir) {
1557           case 0:
1558            sx = -XMAX/2;
1559            sy = ((int)RAND_RANGE(0, YMAX/2))*2 - YMAX/2;
1560            break;
1561           case 1:
1562            sx = XMAX/2;
1563            sy = ((int)RAND_RANGE(0, YMAX/2))*2 - YMAX/2;
1564            break;
1565           case 2:
1566            sy = YMAX/2;
1567            sx = ((int)RAND_RANGE(0, XMAX/2))*2 - XMAX/2;
1568            break;
1569           case 3:
1570            sy = -YMAX/2;
1571            sx = ((int)RAND_RANGE(0, XMAX/2))*2 - XMAX/2;
1572            break;
1573         }
1574         s = 1;
1575       }
1576   } else if (!rotate) {
1577     if (col[1] < 0.25) {
1578       col[1] += 0.025; col[2] += 0.005;
1579       col2[1] += 0.015 ; col2[2] += 0.005;
1580     }
1581   }
1582
1583   glDisable(GL_LIGHTING);
1584   if (s) {
1585     glColor3fv(col3);
1586     glPushMatrix();
1587     glTranslatef(sx, sy, -10);
1588     sphere(0.1, 10, 10, 0, 10, 0, 10);
1589     if (sdir == 0) 
1590       glTranslatef(-ds, 0, 0);
1591     if (sdir == 1) 
1592       glTranslatef(ds, 0, 0);
1593     if (sdir == 2) 
1594       glTranslatef(0, ds, 0);
1595     if (sdir == 3) 
1596       glTranslatef(0, -ds, 0);
1597     sphere(0.05, 10, 10, 0, 10, 0, 10);
1598     glPopMatrix();
1599     if (sdir == 0) {
1600        sx += ds;
1601        if (sx > XMAX/2)
1602          s = 0;
1603     }
1604     if (sdir == 1) {
1605        sx -= ds;
1606        if (sx < -XMAX/2)
1607          s = 0;
1608     }
1609     if (sdir == 2) {
1610        sy -= ds;
1611        if (sy < YMAX/2)
1612          s = 0;
1613     }
1614     if (sdir == 3) {
1615        sy += ds;
1616        if (sy > YMAX/2)
1617          s = 0;
1618     }
1619   } else if (!rotate) {
1620     if (col[1] > 0) {
1621       col[1] -= 0.0025; col[2] -= 0.0005;
1622       col2[1] -= 0.0015 ; col2[2] -= 0.0005;
1623     }
1624   }
1625   for (x = -XMAX/2 ; x <= XMAX/2 ; x+= 2) {
1626     glColor3fv(col);
1627     glBegin(GL_LINES);
1628      glVertex3f(x, YMAX/2, -10);
1629      glVertex3f(x, -YMAX/2, -10);
1630      glColor3fv(col2);
1631      glVertex3f(x-0.02, YMAX/2, -10);
1632      glVertex3f(x-0.02, -YMAX/2, -10);
1633      glVertex3f(x+0.02, YMAX/2, -10);
1634      glVertex3f(x+0.02, -YMAX/2, -10);
1635     glEnd();
1636   }
1637   for (y = -YMAX/2 ; y <= YMAX/2 ; y+= 2) {
1638     glColor3fv(col);
1639     glBegin(GL_LINES);
1640      glVertex3f(-XMAX/2, y, -10);
1641      glVertex3f(XMAX/2, y, -10);
1642      glColor3fv(col2);
1643      glVertex3f(-XMAX/2, y-0.02, -10);
1644      glVertex3f(XMAX/2, y-0.02, -10);
1645      glVertex3f(-XMAX/2, y+0.02, -10);
1646      glVertex3f(XMAX/2, y+0.02, -10);
1647     glEnd();
1648   }
1649   glEnable(GL_LIGHTING);
1650 }
1651
1652 void display(void)
1653 {
1654   static Component *c[MAX_COMPONENTS];
1655   static int i = 0;
1656   GLfloat light_sp[] = {0.8, 0.8, 0.8, 1.0};
1657   GLfloat black[] = {0, 0, 0, 1.0};
1658   static GLfloat rotate_angle = 0; /*  when 'rotate' is enabled */
1659   int j;
1660
1661   if (i == 0) {
1662     for (i = 0 ; i < maxparts ; i++) {
1663       c[i] = NULL;
1664     }
1665   } 
1666   glEnable(GL_LIGHTING);
1667   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1668   glLoadIdentity();
1669   gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
1670   glPushMatrix();
1671   if (rotate) {
1672      glRotatef(rotate_angle, 0, 0, 1);
1673      rotate_angle += 0.01 * (float)rotatespeed;
1674      if (rotate_angle >= 360) rotate_angle = 0;
1675   }
1676   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
1677   glLightfv(GL_LIGHT0, GL_SPECULAR, light_sp);
1678   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_sp);
1679   glLighti(GL_LIGHT0, GL_CONSTANT_ATTENUATION, (GLfloat)1); 
1680   glLighti(GL_LIGHT0, GL_LINEAR_ATTENUATION, (GLfloat)0.5); 
1681   glLighti(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, (GLfloat)0); 
1682   drawgrid();
1683   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, light_sp);
1684   if (f_rand() < 0.05) {
1685     for (j = 0 ; j < maxparts ; j++) {
1686       if (c[j] == NULL) {
1687         c[j] = NewComponent();
1688         j = maxparts;
1689       }
1690     }
1691     reorder(&c[0]);
1692   }
1693   for (j = 0 ; j < maxparts ; j++) {
1694      glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, black);
1695      glMaterialfv(GL_FRONT, GL_EMISSION, black);
1696      glMaterialfv(GL_FRONT, GL_SPECULAR, black);
1697      if (c[j] != NULL) {
1698        if (DrawComponent(c[j])) {
1699         free(c[j]); c[j] = NULL;
1700        }
1701      }
1702   }
1703   glPopMatrix();
1704   glFlush();
1705 }
1706
1707 TexNum * fonttexturealloc (const char *str, float *fg, float *bg)
1708 {
1709   static char *strings[50]; /* max of 40 textures */
1710   static int w[50], h[50];
1711   int i;
1712   static int init;
1713   XImage *ximage;
1714   char *c;
1715   TexNum *t;
1716
1717   if (init == 0) {
1718     for (i = 1 ; i < 50 ; i++) {
1719       strings[i] = NULL;
1720       w[i] = 0; h[i] = 0;
1721     }
1722     init++;
1723   }
1724   for (i = 1 ; i < 50 ; i++) {
1725     if (strings[i] && !strcmp(str, strings[i])) { /* if one matches */
1726       t = malloc(sizeof(TexNum));
1727       t->w = w[i]; t->h = h[i];
1728       t->num = i;
1729       return t;
1730     }
1731   }
1732   /* at this point we need to make the new texture */
1733   ximage = text_to_ximage (modeinfo->xgwa.screen,
1734                            modeinfo->xgwa.visual,
1735                            font, str,
1736                            fg, bg);
1737   for (i = 1 ; strings[i] != NULL ; i++); /* set i to the next unused value */
1738   glBindTexture(GL_TEXTURE_2D, i);
1739   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1740   gluBuild2DMipmaps(GL_TEXTURE_2D, 4, ximage->width, ximage->height, GL_RGBA,
1741                     GL_UNSIGNED_BYTE, ximage->data);
1742   t = malloc(sizeof(TexNum));
1743   t->w = ximage->width;
1744   t->h = ximage->height;
1745   w[i] = t->w; h[i] = t->h;
1746   free(ximage->data);
1747   ximage->data = 0;
1748   XFree (ximage);
1749   c = malloc(strlen(str)+1);
1750   strncpy(c, str, strlen(str)+1);
1751   strings[i] = c;
1752   t->num = i;
1753   return t;
1754 }
1755
1756 /* ensure transparent components are at the end */
1757 void reorder(Component *c[])
1758 {
1759   int i, j, k;
1760   Component *c1[MAX_COMPONENTS];
1761   Component *c2[MAX_COMPONENTS];
1762
1763   j = 0;
1764   for (i = 0 ; i < maxparts ; i++) { /* clear old matrix */
1765     c1[i] = NULL;
1766     c2[i] = NULL;
1767   }
1768   for (i = 0 ; i < maxparts ; i++) {
1769     if (c[i] == NULL) continue;
1770     if (c[i]->alpha) { /* transparent parts go to c1 */
1771       c1[j] = c[i];
1772       j++;
1773     } else { /* opaque parts go to c2 */
1774       c2[i] = c[i];
1775     }
1776   }
1777   for (i = 0 ; i < maxparts ; i++) { /* clear old matrix */
1778     c[i] = NULL;
1779   }
1780   k = 0;
1781   for (i = 0 ; i < maxparts ; i++) { /* insert opaque part */
1782     if (c2[i] != NULL) {
1783       c[k] = c2[i];
1784       k++;
1785     }
1786   }
1787   for (i = 0 ; i < j ; i++) { /* insert transparent parts */
1788     c[k] = c1[i];
1789     k++;
1790   }
1791 }
1792
1793 void reshape_circuit(ModeInfo *mi, int width, int height)
1794 {
1795  glViewport(0,0,(GLint)width, (GLint) height);
1796  glMatrixMode(GL_PROJECTION);
1797  glLoadIdentity();
1798  glFrustum(-1.0,1.0,-1.0,1.0,1.5,35.0);
1799  glMatrixMode(GL_MODELVIEW);
1800  win_h = height; win_w = width;
1801 }
1802
1803
1804 void init_circuit(ModeInfo *mi)
1805 {
1806 int screen = MI_SCREEN(mi);
1807 Circuit *c;
1808
1809  if (circuit == NULL) {
1810    if ((circuit = (Circuit *) calloc(MI_NUM_SCREENS(mi),
1811                                         sizeof(Circuit))) == NULL)
1812           return;
1813  }
1814  c = &circuit[screen];
1815  c->window = MI_WINDOW(mi);
1816
1817
1818  if ((c->glx_context = init_GL(mi)) != NULL) {
1819       reshape_circuit(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1820  } else {
1821      MI_CLEARWINDOW(mi);
1822  }
1823  if (uselight == 0)
1824     light = 1;
1825  glClearColor(0.0,0.0,0.0,0.0);
1826  glShadeModel(GL_SMOOTH);
1827  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
1828  glEnable(GL_DEPTH_TEST);
1829  glEnable(GL_LIGHTING);
1830  glEnable(GL_LIGHT0);
1831  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1832  make_tables();
1833  makebandlist();
1834  
1835 }
1836
1837 void draw_circuit(ModeInfo *mi)
1838 {
1839   Circuit *c = &circuit[MI_SCREEN(mi)];
1840   Window w = MI_WINDOW(mi);
1841   Display *disp = MI_DISPLAY(mi);
1842
1843   if (!c->glx_context)
1844       return;
1845  modeinfo = mi;
1846
1847  glXMakeCurrent(disp, w, *(c->glx_context));
1848
1849   display();
1850
1851   if(mi->fps_p) do_fps(mi);
1852   glFinish(); 
1853   glXSwapBuffers(disp, w);
1854 }
1855
1856 void release_circuit(ModeInfo *mi)
1857 {
1858   if (circuit != NULL) {
1859    (void) free((void *) circuit);
1860    circuit = NULL;
1861   }
1862   FreeAllGL(MI);
1863 }
1864
1865 #endif