chiark / gitweb /
initial cut of X client
[moebius2.git] / project.c
1 /*
2  * Displays a conformation
3  */
4
5 #include <X11/Xlib.h>
6 #include <X11/Xutil.h>
7
8 #include "mgraph.h"
9
10 #define MAXTRIS (N*2)
11
12 typedef struct { double vertex[3][D3]; } Triangle;
13
14 static Triangle trisbuffer[MAXTRIS], *displaylist[MAXTRIS];
15 static int ntris;
16 static Vertices conformation;
17
18 const char *input_filename;
19
20 static void read_input(void) {
21   FILE *f;
22   int r;
23   
24   f= fopen(input_filename, "rb");  if (!f) diee("input file");
25   errno= 0;
26   r= fread(&conformation,1,sizeof(conformation),f);  if (r!=1) diee("fread");
27   fclose(f);
28 }
29
30 static void transform_coordinates(void) {
31   int v;
32
33   FOR_VERTEX(v) {
34   }
35 }
36
37 static void addtriangle(int va, int vb, int vc) {
38   Triangle *t= &trisbuffer[ntris];
39   int k;
40   
41   assert(ntris < MAXTRIS);
42   K {
43     t->vertex[0][k]= conformation[va][k];
44     t->vertex[1][k]= conformation[vb][k];
45     t->vertex[2][k]= conformation[vc][k];
46   }
47   displaylist[ntris++]= t;
48 }
49
50 static void generate_display_list(void) {
51   int vb, ve[3], e;
52   
53   FOR_VERTEX(vb) {
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).
57      */
58     for (e=0; e<3; e++) ve[e]= EDGE_END2(vb,e);
59     if (ve[0]>=0) {
60       if (ve[1]>=0) addtriangle(vb,ve[0],ve[1]);
61       if (ve[2]>=0) addtriangle(vb,ve[2],ve[0]);
62     }
63   }
64 }    
65
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];
71   return za > zb ? +1 :
72          za < zb ? -1 : 0;
73 }
74
75 static void sort_display_list(void) {
76   qsort(displaylist, ntris, sizeof(*displaylist), dl_compare);
77 }
78
79 /*---------- X stuff ----------*/
80
81 #define WSZ 400
82
83 static Display *display;
84 static Pixmap pixmap, doublebuffers[2];
85 static Window window;
86 static GC linegc, fillgc;
87 static int wwidth=WSZ, wheight=WSZ, currentbuffer;
88
89 static double scale= WSZ * 0.3;
90 static double eye_z= -10, eye_x= 0;
91 static double cut_z= -9;
92
93 static void xdie(int l, const char *str) {
94   fprintf(stderr,"X library call failed, line %d: %s\n", l, str);
95 }
96
97 #define XA(w) ((w) ? (void)0 : xdie(__LINE__, #w))
98
99 static void drawtriangle(const Triangle *t) {
100   XPoint points[4];
101   int i;
102
103   for (i=0; i<3; i++) {
104     double *v= t->vertex[i];
105     double x= v[0];
106     double y= v[1];
107     double z= v[2];
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                 );
112   }
113   points[3]= points[0];
114   
115   XA(! XFillPolygon(display,pixmap,fillgc, points,3,Convex,CoordModeOrigin) );
116   XA(! XDrawLines(display,pixmap,linegc,points, 4,CoordModeOrigin) );
117 }
118
119 static void display_prepare(void) {
120   XGCValues gcv;
121   XSetWindowAttributes wa;
122   int screen, depth;
123   XVisualInfo vinfo;
124   
125   XA( display= XOpenDisplay(0) );
126   screen= DefaultScreen(display);
127   depth= DefaultDepth(display,screen);
128   XA(! XMatchVisualInfo(display,screen,depth, TrueColor,&vinfo) );
129   
130   wa.event_mask= ButtonPress|ConfigureNotify;
131   XA( window= XCreateWindow(display, DefaultRootWindow(display),
132                             10,10, wwidth,wheight, 0,depth,
133                             InputOutput, vinfo.visual,
134                             CWEventMask, &wa) );
135
136   for (currentbuffer=0; currentbuffer<2; currentbuffer++) {
137     XA( pixmap= XCreatePixmap(display,window,wwidth,wheight,depth) );
138     doublebuffers[currentbuffer]= pixmap;
139   }
140
141   gcv.function= GXcopy;
142   gcv.plane_mask= ~0UL;
143   gcv.foreground= WhitePixel(display,screen);
144   linegc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask|GCForeground, &gcv);
145   
146   gcv.function= GXclear;
147   gcv.plane_mask= ~0UL;
148   fillgc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask, &gcv);
149
150   currentbuffer= 0;
151 }
152
153 static void display_conformation(void) {
154   Triangle *const *t;
155   int i;
156   
157   pixmap= doublebuffers[currentbuffer];
158   XA(! XFillRectangle(display,pixmap,fillgc,0,0,wwidth,wheight) );
159   for (i=0, t=displaylist; i<ntris; i++, t++)
160     drawtriangle(*t);
161   XA(! XSetWindowBackgroundPixmap(display,window,pixmap) );
162   XA(! XClearWindow(display,window) );
163   currentbuffer= !currentbuffer;
164 }
165
166 static void show(void) {
167   read_input();
168   transform_coordinates();
169   generate_display_list();
170   sort_display_list();
171   display_conformation();
172 }
173
174   
175 int main(int argc, const char *const *argv) {
176   if (argc != 2 || argv[1][0]=='-') {
177     fputs("need filename\n",stderr); exit(8);
178   }
179   input_filename= argv[1];
180
181   read_input();
182   display_prepare();
183   show();
184   XSync(display,0);
185   for (;;);
186 }
187