#include #include #include #include #include #include "vec.h" #define TIME_SPAN 0.1 #define SCR_DIS 1.0 #define SCR_MAG 200.0 Vector points[] = { {0,0,0},{100,0,0},{100,100,0}, {0,100,0} , {0,0,100},{100,0,100},{100,100,100},{0,100,100}, {100,-100,0},{0,-100,0},{50,-50,200}, }; Line lines[] = { { &points[0], &points[1] }, { &points[1], &points[2] }, /* ΩÊýÂÎ */ { &points[2], &points[3] }, { &points[3], &points[0] }, { &points[4], &points[5] }, { &points[5], &points[6] }, { &points[6], &points[7] }, { &points[7], &points[4] }, { &points[0], &points[4] }, { &points[1], &points[5] }, { &points[2], &points[6] }, { &points[3], &points[7] }, { &points[1], &points[8] }, { &points[8], &points[9] }, /* »Í³Ñ¿í */ { &points[9], &points[0] }, { &points[10], &points[0] }, { &points[10], &points[1] }, { &points[10], &points[8] }, { &points[10], &points[9] }, }; #define WIDTH 320 #define HEIGHT 240 #define NPOINTS (sizeof(points)/sizeof(Vector)) #define NLINES (sizeof(lines)/sizeof(Line)) Vector eyepos = { 800, 800, 200 }; Vector eyedir = { -4, -4, -1 }; Display *dpy; int scr; Window win; GC gc; int init_xwin(int width, int height) { XGCValues gcv; XSetWindowAttributes xswa; if ((dpy = XOpenDisplay(NULL)) == NULL) { fprintf(stderr, "cant open display\n"); exit(1); } scr = DefaultScreen(dpy); xswa.background_pixel = BlackPixel(dpy,scr); xswa.border_pixel = BlackPixel(dpy,scr); xswa.event_mask = ExposureMask | ButtonPressMask; win=XCreateWindow(dpy,RootWindow(dpy,scr),0,0,width,height,1, CopyFromParent,CopyFromParent,CopyFromParent, (CWBackPixel|CWBorderPixel|CWEventMask),&xswa); gcv.foreground = WhitePixel(dpy,scr); gcv.background = BlackPixel(dpy,scr); gcv.function = GXcopy; /* GXxor; */ gc = XCreateGC(dpy,win,(GCForeground|GCBackground|GCFunction),&gcv); XMapWindow(dpy,win); } void drawview(Display *dpy, Window win, GC gc, Line *lines, int n_lines, Vector *eyepos, Vector *eyedir, int w, int h) { double m[NMAT][NMAT]; Vector p, q; int i; getmatrix(m,eyepos,eyedir); for (i=0; i< n_lines; ++i) { vmul(m,lines[i].start,&p); vmul(m,lines[i].end,&q); project(&p,SCR_MAG/SCR_DIS,&p); project(&q,SCR_MAG/SCR_DIS,&q); XDrawLine(dpy,win,gc,(int)p.x+w/2,(int)-p.y+h/2,(int)q.x+w/2,(int)-q.y+h/2); } XFlush(dpy); } double gettime() { struct timeval t; gettimeofday(&t,0); return(t.tv_sec + t.tv_usec * 1e-6); } int main() { double t1,t2,t3; init_xwin(WIDTH,HEIGHT); XMapWindow(dpy,win); for (;;) { fd_set readfds, writefds, exceptfds; struct timeval timeout; int i; XEvent ev; FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); FD_SET(ConnectionNumber(dpy),&readfds); t2 = gettime(); t3 = TIME_SPAN - (t2 - t1); if (t3 < 0) t3 = 0; timeout.tv_sec = t3; timeout.tv_usec = (t3 - timeout.tv_sec) * 1e6; i = select(getdtablesize(),&readfds,&writefds,&exceptfds,&timeout); t2 = gettime(); if (t2 - t1 > TIME_SPAN) { vadd(&eyepos,&eyedir,&eyepos); XClearWindow(dpy,win); drawview(dpy,win,gc,lines,NLINES,&eyepos,&eyedir,WIDTH,HEIGHT); t1 = gettime(); } while (XCheckMaskEvent(dpy,-1,&ev)==True) { switch (ev.type) { case Expose: while (XCheckTypedEvent(dpy,Expose,&ev)); XClearWindow(dpy,win); drawview(dpy,win,gc,lines,NLINES,&eyepos,&eyedir,WIDTH,HEIGHT); break; case ButtonPress: switch (ev.xbutton.button) { case 3: exit(0); break; } break; default: break; } } XFlush(dpy); } }