chiark / gitweb /
initial cut of X client
[moebius2.git] / project.c
diff --git a/project.c b/project.c
new file mode 100644 (file)
index 0000000..2d24f08
--- /dev/null
+++ b/project.c
@@ -0,0 +1,187 @@
+/*
+ * Displays a conformation
+ */
+
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+
+#include "mgraph.h"
+
+#define MAXTRIS (N*2)
+
+typedef struct { double vertex[3][D3]; } Triangle;
+
+static Triangle trisbuffer[MAXTRIS], *displaylist[MAXTRIS];
+static int ntris;
+static Vertices conformation;
+
+const char *input_filename;
+
+static void read_input(void) {
+  FILE *f;
+  int r;
+  
+  f= fopen(input_filename, "rb");  if (!f) diee("input file");
+  errno= 0;
+  r= fread(&conformation,1,sizeof(conformation),f);  if (r!=1) diee("fread");
+  fclose(f);
+}
+
+static void transform_coordinates(void) {
+  int v;
+
+  FOR_VERTEX(v) {
+  }
+}
+
+static void addtriangle(int va, int vb, int vc) {
+  Triangle *t= &trisbuffer[ntris];
+  int k;
+  
+  assert(ntris < MAXTRIS);
+  K {
+    t->vertex[0][k]= conformation[va][k];
+    t->vertex[1][k]= conformation[vb][k];
+    t->vertex[2][k]= conformation[vc][k];
+  }
+  displaylist[ntris++]= t;
+}
+
+static void generate_display_list(void) {
+  int vb, ve[3], e;
+  
+  FOR_VERTEX(vb) {
+    /* We use the two triangles in the parallelogram vb, vb+e1, vb+e0, vb+e2.
+     * We go round each triangle clockwise (although our surface is non-
+     * orientable so it shouldn't matter).
+     */
+    for (e=0; e<3; e++) ve[e]= EDGE_END2(vb,e);
+    if (ve[0]>=0) {
+      if (ve[1]>=0) addtriangle(vb,ve[0],ve[1]);
+      if (ve[2]>=0) addtriangle(vb,ve[2],ve[0]);
+    }
+  }
+}    
+
+static int dl_compare(const void *tav, const void *tbv) {
+  const Triangle *const *tap= tav, *ta= *tap;
+  const Triangle *const *tbp= tbp, *tb= *tbp;
+  double za= ta->vertex[0][2];
+  double zb= tb->vertex[0][2];
+  return za > zb ? +1 :
+         za < zb ? -1 : 0;
+}
+
+static void sort_display_list(void) {
+  qsort(displaylist, ntris, sizeof(*displaylist), dl_compare);
+}
+
+/*---------- X stuff ----------*/
+
+#define WSZ 400
+
+static Display *display;
+static Pixmap pixmap, doublebuffers[2];
+static Window window;
+static GC linegc, fillgc;
+static int wwidth=WSZ, wheight=WSZ, currentbuffer;
+
+static double scale= WSZ * 0.3;
+static double eye_z= -10, eye_x= 0;
+static double cut_z= -9;
+
+static void xdie(int l, const char *str) {
+  fprintf(stderr,"X library call failed, line %d: %s\n", l, str);
+}
+
+#define XA(w) ((w) ? (void)0 : xdie(__LINE__, #w))
+
+static void drawtriangle(const Triangle *t) {
+  XPoint points[4];
+  int i;
+
+  for (i=0; i<3; i++) {
+    double *v= t->vertex[i];
+    double x= v[0];
+    double y= v[1];
+    double z= v[2];
+    if (z < cut_z) return;
+    double zezezp= eye_z / (eye_z - z);
+    points[i].x= scale * (zezezp * (x - eye_x) + eye_x);
+    points[i].y= scale * (zezezp *  y                 );
+  }
+  points[3]= points[0];
+  
+  XA(! XFillPolygon(display,pixmap,fillgc, points,3,Convex,CoordModeOrigin) );
+  XA(! XDrawLines(display,pixmap,linegc,points, 4,CoordModeOrigin) );
+}
+
+static void display_prepare(void) {
+  XGCValues gcv;
+  XSetWindowAttributes wa;
+  int screen, depth;
+  XVisualInfo vinfo;
+  
+  XA( display= XOpenDisplay(0) );
+  screen= DefaultScreen(display);
+  depth= DefaultDepth(display,screen);
+  XA(! XMatchVisualInfo(display,screen,depth, TrueColor,&vinfo) );
+  
+  wa.event_mask= ButtonPress|ConfigureNotify;
+  XA( window= XCreateWindow(display, DefaultRootWindow(display),
+                           10,10, wwidth,wheight, 0,depth,
+                           InputOutput, vinfo.visual,
+                           CWEventMask, &wa) );
+
+  for (currentbuffer=0; currentbuffer<2; currentbuffer++) {
+    XA( pixmap= XCreatePixmap(display,window,wwidth,wheight,depth) );
+    doublebuffers[currentbuffer]= pixmap;
+  }
+
+  gcv.function= GXcopy;
+  gcv.plane_mask= ~0UL;
+  gcv.foreground= WhitePixel(display,screen);
+  linegc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask|GCForeground, &gcv);
+  
+  gcv.function= GXclear;
+  gcv.plane_mask= ~0UL;
+  fillgc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask, &gcv);
+
+  currentbuffer= 0;
+}
+
+static void display_conformation(void) {
+  Triangle *const *t;
+  int i;
+  
+  pixmap= doublebuffers[currentbuffer];
+  XA(! XFillRectangle(display,pixmap,fillgc,0,0,wwidth,wheight) );
+  for (i=0, t=displaylist; i<ntris; i++, t++)
+    drawtriangle(*t);
+  XA(! XSetWindowBackgroundPixmap(display,window,pixmap) );
+  XA(! XClearWindow(display,window) );
+  currentbuffer= !currentbuffer;
+}
+
+static void show(void) {
+  read_input();
+  transform_coordinates();
+  generate_display_list();
+  sort_display_list();
+  display_conformation();
+}
+
+  
+int main(int argc, const char *const *argv) {
+  if (argc != 2 || argv[1][0]=='-') {
+    fputs("need filename\n",stderr); exit(8);
+  }
+  input_filename= argv[1];
+
+  read_input();
+  display_prepare();
+  show();
+  XSync(display,0);
+  for (;;);
+}
+