From 4e53c82487592a5a490e3ac6daaae979e9812338 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 30 Dec 2007 19:05:52 +0000 Subject: [PATCH] initial cut of X client --- .bzrignore | 1 + Makefile | 5 +- common.h | 1 + project.c | 187 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 project.c diff --git a/.bzrignore b/.bzrignore index 51d35b4..edc617c 100644 --- a/.bzrignore +++ b/.bzrignore @@ -1,3 +1,4 @@ minimise primer initial +project diff --git a/Makefile b/Makefile index fc74681..cf0bbd0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -TARGETS= minimise primer initial +TARGETS= minimise primer initial project CWARNS= -Wall -Wwrite-strings -Wpointer-arith -Werror @@ -17,6 +17,9 @@ minimise: energy.o bgl.o common.o mgraph.o primer: primer.o common.o $(CC) $(CFLAGS) -o $@ $^ $(LIBGSL) +project: project.o common.o mgraph.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBGSL) -L/usr/X11R6/lib -lX11 + initial: generator primer sgtatham/z.typescript ./$^ -o$@ diff --git a/common.h b/common.h index a642213..d6d1e58 100644 --- a/common.h +++ b/common.h @@ -15,6 +15,7 @@ #include #include #include +#include #define D3 3 diff --git a/project.c b/project.c new file mode 100644 index 0000000..2d24f08 --- /dev/null +++ b/project.c @@ -0,0 +1,187 @@ +/* + * Displays a conformation + */ + +#include +#include + +#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