2 * Displays a conformation
12 typedef struct { double vertex[3][D3]; } Triangle;
14 static Triangle trisbuffer[MAXTRIS], *displaylist[MAXTRIS];
16 static Vertices conformation;
18 const char *input_filename;
20 static void read_input(void) {
24 f= fopen(input_filename, "rb"); if (!f) diee("input file");
26 r= fread(&conformation,1,sizeof(conformation),f); if (r!=1) diee("fread");
30 static void transform_coordinates(void) {
37 static void addtriangle(int va, int vb, int vc) {
38 Triangle *t= &trisbuffer[ntris];
41 assert(ntris < MAXTRIS);
43 t->vertex[0][k]= conformation[va][k];
44 t->vertex[1][k]= conformation[vb][k];
45 t->vertex[2][k]= conformation[vc][k];
47 displaylist[ntris++]= t;
50 static void generate_display_list(void) {
54 /* We use the two triangles in the parallelogram vb, vb+e1, vb+e0, vb+e2.
55 * We go round each triangle clockwise (although our surface is non-
56 * orientable so it shouldn't matter).
58 for (e=0; e<3; e++) ve[e]= EDGE_END2(vb,e);
60 if (ve[1]>=0) addtriangle(vb,ve[0],ve[1]);
61 if (ve[2]>=0) addtriangle(vb,ve[2],ve[0]);
66 static int dl_compare(const void *tav, const void *tbv) {
67 const Triangle *const *tap= tav, *ta= *tap;
68 const Triangle *const *tbp= tbp, *tb= *tbp;
69 double za= ta->vertex[0][2];
70 double zb= tb->vertex[0][2];
75 static void sort_display_list(void) {
76 qsort(displaylist, ntris, sizeof(*displaylist), dl_compare);
79 /*---------- X stuff ----------*/
83 static Display *display;
84 static Pixmap pixmap, doublebuffers[2];
86 static GC linegc, fillgc;
87 static int wwidth=WSZ, wheight=WSZ, currentbuffer;
89 static double scale= WSZ * 0.3;
90 static double eye_z= -10, eye_x= 0;
91 static double cut_z= -9;
93 static void xdie(int l, const char *str) {
94 fprintf(stderr,"X library call failed, line %d: %s\n", l, str);
97 #define XA(w) ((w) ? (void)0 : xdie(__LINE__, #w))
99 static void drawtriangle(const Triangle *t) {
103 for (i=0; i<3; i++) {
104 double *v= t->vertex[i];
108 if (z < cut_z) return;
109 double zezezp= eye_z / (eye_z - z);
110 points[i].x= scale * (zezezp * (x - eye_x) + eye_x);
111 points[i].y= scale * (zezezp * y );
113 points[3]= points[0];
115 XA(! XFillPolygon(display,pixmap,fillgc, points,3,Convex,CoordModeOrigin) );
116 XA(! XDrawLines(display,pixmap,linegc,points, 4,CoordModeOrigin) );
119 static void display_prepare(void) {
121 XSetWindowAttributes wa;
125 XA( display= XOpenDisplay(0) );
126 screen= DefaultScreen(display);
127 depth= DefaultDepth(display,screen);
128 XA(! XMatchVisualInfo(display,screen,depth, TrueColor,&vinfo) );
130 wa.event_mask= ButtonPress|ConfigureNotify;
131 XA( window= XCreateWindow(display, DefaultRootWindow(display),
132 10,10, wwidth,wheight, 0,depth,
133 InputOutput, vinfo.visual,
136 for (currentbuffer=0; currentbuffer<2; currentbuffer++) {
137 XA( pixmap= XCreatePixmap(display,window,wwidth,wheight,depth) );
138 doublebuffers[currentbuffer]= pixmap;
141 gcv.function= GXcopy;
142 gcv.plane_mask= ~0UL;
143 gcv.foreground= WhitePixel(display,screen);
144 linegc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask|GCForeground, &gcv);
146 gcv.function= GXclear;
147 gcv.plane_mask= ~0UL;
148 fillgc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask, &gcv);
153 static void display_conformation(void) {
157 pixmap= doublebuffers[currentbuffer];
158 XA(! XFillRectangle(display,pixmap,fillgc,0,0,wwidth,wheight) );
159 for (i=0, t=displaylist; i<ntris; i++, t++)
161 XA(! XSetWindowBackgroundPixmap(display,window,pixmap) );
162 XA(! XClearWindow(display,window) );
163 currentbuffer= !currentbuffer;
166 static void show(void) {
168 transform_coordinates();
169 generate_display_list();
171 display_conformation();
175 int main(int argc, const char *const *argv) {
176 if (argc != 2 || argv[1][0]=='-') {
177 fputs("need filename\n",stderr); exit(8);
179 input_filename= argv[1];