00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef _DISPLAY_WIN_H
00010 #define _DISPLAY_WIN_H
00011
00012 #define nUSE_X
00013
00014 #define _DISPLAY_VERSION 1.05
00015
00016 #ifdef NO_GENERAL
00017 #define bool int
00018 #define true 1
00019 #define false 0
00020 #endif
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024
00025
00026 #ifdef USE_X
00027 #include <X11/Xlib.h>
00028 #include <X11/Xutil.h>
00029 #include <X11/keysym.h>
00030 #include <X11/cursorfont.h>
00031
00032 #ifndef NO_XPM
00033 #include <X11/xpm.h>
00034 #endif
00035
00036 #else
00037
00038 #define GLUT_STATIC
00039 #include "glut.h"
00040
00041
00042
00043
00044
00045 #define KEY_ESCAPE 27
00046 #define KEY_RETURN 13
00047 #define KEY_BACKSPACE 8
00048
00049 #define KEY_ARROW_LEFT 100
00050 #define KEY_ARROW_UP 101
00051 #define KEY_ARROW_RIGHT 102
00052 #define KEY_ARROW_DOWN 103
00053
00054 typedef enum {
00055 WINDOW_ACTION_NONE,
00056 WINDOW_ACTION_SCALE,
00057 WINDOW_ACTION_SCALE_X,
00058 WINDOW_ACTION_SCALE_Y,
00059 WINDOW_ACTION_SCALE_Z,
00060 WINDOW_ACTION_ROTATE_XY,
00061 WINDOW_ACTION_ROTATE_X,
00062 WINDOW_ACTION_ROTATE_Y,
00063 WINDOW_ACTION_ROTATE_Z,
00064 WINDOW_ACTION_TRANSLATE_XY,
00065 WINDOW_ACTION_TRANSLATE_X,
00066 WINDOW_ACTION_TRANSLATE_Y,
00067 WINDOW_ACTION_TRANSLATE_Z,
00068 WINDOW_ACTION_LOOKAT,
00069 WINDOW_ACTION_PICK,
00070 WINDOW_ACTION_KEY_PRESS,
00071 WINDOW_ACTION_MAX
00072 } WindowAction;
00073
00074 typedef enum {
00075 BUTTON_UNKNOWN,
00076 BUTTON_LEFT,
00077 BUTTON_MIDDLE,
00078 BUTTON_RIGHT,
00079 BUTTON_SHIFT_LEFT,
00080 BUTTON_SHIFT_MIDDLE,
00081 BUTTON_SHIFT_RIGHT,
00082 BUTTON_CTRL_LEFT,
00083 BUTTON_CTRL_MIDDLE,
00084 BUTTON_CTRL_RIGHT,
00085 BUTTON_MAX
00086 } EventButtonId;
00087
00088 typedef enum {
00089 EVENT_BUTTON_STATE_NONE,
00090 EVENT_BUTTON_STATE_SHIFT,
00091 EVENT_BUTTON_STATE_CONTROL,
00092 EVENT_BUTTON_STATE_CONTROL_SHIFT
00093 } EventButtonState;
00094
00095 typedef enum {
00096 EVENT_NONE,
00097 EVENT_BUTTON_PRESS,
00098 EVENT_BUTTON_RELEASE,
00099 EVENT_MOUSE_MOTION,
00100 EVENT_WINDOW_EXPOSE,
00101 EVENT_KEY_PRESS,
00102 EVENT_KEY_RELEASE
00103 } EventType;
00104
00105 typedef enum {
00106 SPHERE_REP_DISC,
00107 SPHERE_REP_DOT,
00108 SPHERE_REP_POINT,
00109 SPHERE_REP_LINE,
00110 SPHERE_REP_SOLID,
00111 } SphereRep;
00112
00113 typedef struct GlutXmotion {
00114 int x, y;
00115 } GlutXmotion;
00116
00117 typedef struct XEvent {
00118 int mx, my;
00119 int mouse_pos_set;
00120 EventType type;
00121 int button_id;
00122 EventButtonState button_state;
00123 WindowAction action;
00124 void (*action_func)();
00125 GlutXmotion xmotion;
00126 } XEvent;
00127
00128 typedef char KeySym;
00129
00130 void display (void);
00131
00132 void event_Butp (int button, int state, int x, int y);
00133
00134 void event_Butr (int mx, int my, EventButtonId button_id);
00135
00136 void event_IdleProc ();
00137
00138 void event_KeyboardProc (unsigned char ch, int x, int y);
00139
00140 void event_Motion (int x, int y);
00141
00142 void event_SpecialProc (int key, int x, int y);
00143
00144 void event_WinObjGet (int win, int *p);
00145
00146 void event_WinReshape (int width, int height);
00147
00148 void opengl_SurfGenSphere (SphereRep dtype, float radius, int slices, int stacks);
00149
00150 void viewport_Set (int win, int w, int h);
00151
00152 #endif
00153
00154
00155
00156 #ifndef NO_THREAD
00157 #include <pthread.h>
00158 #endif
00159
00160 #include <sys/ipc.h>
00161 #include <sys/sem.h>
00162 #if !defined(_SEM_SEMUN_UNDEFINED)
00163
00164 #else
00165
00166
00167 union semun
00168 {
00169 int val;
00170 struct semid_ds *buf;
00171 unsigned short int *array;
00172 struct seminfo *__buf;
00173 };
00174 #endif
00175
00176 #ifndef NO_GENERAL
00177 #include "general.h"
00178 #endif
00179
00180
00181 #include <math.h>
00182 #include <errno.h>
00183 #include <string.h>
00184 #include <unistd.h>
00185
00186 #ifndef DEPTH
00187
00188 #define DEPTH 32
00189 #endif
00190
00191 #define DEPTH_REAL 8
00192 #if DEPTH == 16
00193 #define RGB RGB16
00194 #define GETRED RED16
00195 #define GETGREEN GREEN16
00196 #define GETBLUE BLUE16
00197 #elif DEPTH == 32
00198 #define RGB RGB32
00199 #define GETRED RED32
00200 #define GETGREEN GREEN32
00201 #define GETBLUE BLUE32
00202 #endif
00203
00204 #define RGB32(R, G, B) ((unsigned)(R)<<16)+((unsigned)(G)<<8)+((unsigned)(B))
00205 #define RGB16(R, G, B) ((unsigned)((R)&0xF8)<<8)+((unsigned)((G)&0xF8)<<3)+((unsigned)((B)>>3))
00206
00207 #define RGBany(R, G, B) ((((unsigned)(R)&CCT[0][0])<<CCT[0][1])>>CCT[0][2])\
00208 +((((unsigned)(G)&CCT[1][0])<<CCT[1][1])>>CCT[1][2])\
00209 +((((unsigned)(B)&CCT[2][0])<<CCT[2][1])>>CCT[2][2])
00210
00211 #define RED32(C) (((C&0xFF0000)>>16)*1.0/(0x00FF))
00212 #define GREEN32(C) (((C&0xFF00)>>8)*1.0/(0x00FF))
00213 #define BLUE32(C) (((C&0x00FF)*1.0/(0x00FF)))
00214
00215 #define RED16(C) (((C&0xF800)>>11)*1.0/(0x001F))
00216 #define GREEN16(C) (((C&0x07C0)>>6)*1.0/(0x001F))
00217 #define BLUE16(C) (((C&0x001F)*1.0/(0x001F)))
00218
00219 #define REDany(C) (((C&CCT[0][3])>>CCT[0][4])*1.0/(CCT[0][5]))
00220 #define GREENany(C) (((C&CCT[1][3])>>CCT[1][4])*1.0/(CCT[1][5]))
00221 #define BLUEany(C) (((C&CCT[2][3])>>CCT[2][4])*1.0/(CCT[2][5]))
00222
00223
00224
00225
00226 enum { MaxPoints=40000, MaxLines=5000, rInterval=50, DSCLEN=30};
00227
00228
00229
00230 inline void delay(long sec, long usec)
00231 {
00232
00233 struct {time_t tv_sec, tv_usec;} tv;
00234 tv.tv_sec=sec;
00235 tv.tv_usec=usec;
00236 select(0,NULL,NULL,NULL,(timeval *)&tv);
00237 }
00238
00239 extern unsigned long colorconvert_16[3][6], colorconvert_32[3][6];
00240
00241 class SYWindow
00242 {
00243 public:
00244 enum{ ROTATION=0,TRANSLATION=1,SCALING=2,PROJECTION=3,
00245 PBCTRANSLATION=4,ASPECTRATIO=5,PBCX=6,PBCY=7,PBCZ=8,PBCGLIDE=9};
00246 struct YPoint
00247 {
00248 double x, y, z, r;
00249 unsigned long c;
00250 unsigned long attr;
00251 char dscrpt[DSCLEN];
00252 }Points[MaxPoints];
00253 struct YLine
00254 {
00255 double x0, y0, z0, x1, y1, z1, r;
00256 unsigned long c;
00257 unsigned long attr;
00258 }Lines[MaxLines];
00259 struct ZInd
00260 {
00261 double Z;
00262 int index;
00263 }Z_ind[MaxLines+MaxPoints];
00264
00265 static int cmp(const void *p1, const void *p2);
00266
00267 unsigned long CCT[3][6];
00268
00269 int nP, nL;
00270 int semID;
00271 int semID2;
00272 bool alive,pause,drawframe;
00273 int loop;
00274 int rinterval;
00275 int msrsp;
00276
00277 KeySym ksfeedback;
00278
00279 #ifdef USE_X
00280
00281 Display *theDisplay;
00282 int theScreen;
00283 XVisualInfo visinfo;
00284 Visual *vis;
00285 XSetWindowAttributes attr;
00286 int PixDepth;
00287 GC gc;
00288 Pixmap pixmap;
00289 Window theWindow, Root;
00290
00291 #else
00292
00293 int theWindow, Root;
00294
00295 struct EventInfo {
00296 int mx, my;
00297 int mouse_pos_set;
00298 EventType type;
00299 int button_id;
00300 EventButtonState button_state;
00301 WindowAction action;
00302 void (*action_func)(int, int, int, int, int );
00303 } Event_info;
00304
00305 struct Extent {
00306 float xmin, ymin, zmin;
00307 float xmax, ymax, zmax;
00308 } VpExtent;
00309
00310 struct XformMap {
00311 float rot;
00312 float translate[3];
00313 float scale;
00314 } SceneXformMap;
00315
00316 struct Xform {
00317 float rot[3], trans[3], scale[3], center[3];
00318 } SceneXform;
00319
00320 struct Dlists {
00321 int disc_dlist, point_dlist, surf_dlist, line_dlist;
00322 } SceneDlist;
00323
00324 int sphere_render;
00325
00326 #endif
00327
00328 unsigned int width, height;
00329 unsigned int depth;
00330
00331 #ifdef USE_X
00332 Colormap cmap;
00333 #endif
00334
00335 unsigned long cblack, cwhite, bgcolor, framecolor;
00336
00337 double A11,A12,A13,A21,A22,A23,A31,A32,A33;
00338 double a11,a12,a13,a21,a22,a23,a31,a32,a33;
00339 double B11,B12,B13,B21,B22,B23,B31,B32,B33;
00340
00341 int R, B;
00342
00343 int Xp,Yp;
00344 int gifcount,pscount;
00345 unsigned long lastDrag;
00346
00347 bool dirty;
00348 int X0,Y0,X00,Y00;
00349 double Scale,Scale0,Aspr,Aspr0;
00350 double D,D0;
00351 bool square, sort;
00352
00353 bool autowritegif;
00354
00355 int scalepoints;
00356 int enable_pbc;
00357 double pbcshift[3];
00358 double maxpointradius, maxlinewidth;
00359
00360 double CXs(double x, double z)
00361 { return (width/2+(B*x/(D==0?1:(1-z/D)))*Scale+X0); }
00362 double CYs(double y, double z)
00363 { return (height/2-(B*y/(D==0?1:(1-z/D)))*Scale*Aspr+Y0); }
00364 double CXr(double x, double z)
00365 { return (width/2+(width/4*x/(D==0?1:(1-z/D)))*Scale+X0); }
00366 double CYr(double y, double z)
00367 { return (height/2-(height/4*y/(D==0?1:(1-z/D)))*Scale*Aspr+Y0); }
00368 double CX(double x, double z) {return (square)?CXs(x,z):CXr(x,z); }
00369 double CY(double y, double z) {return (square)?CYs(y,z):CYr(y,z); }
00370 double CR(double r, double z) {if(scalepoints) return CRs(r,z); else return CRf(r,z); }
00371 double CRs(double r, double z) { return (B*r*Scale/(D==0?1:(1-z/D))); }
00372 double CRf(double r, double z) { return r; }
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 double DEG(double a) { return (M_PI*a/180); }
00385
00386 void initRot()
00387 {
00388 a11=a22=a33=B11=B22=B33=1;
00389 a12=a13=a21=a23=a31=a32=B12=B13=B21=B23=B31=B32=0;
00390 }
00391 void copyRot()
00392 { A11=a11;A12=a12;A13=a13;
00393 A21=a21;A22=a22;A23=a23;
00394 A31=a31;A32=a32;A33=a33; }
00395 void saveRot()
00396 { a11=A11;a12=A12;a13=A13;
00397 a21=A21;a22=A22;a23=A23;
00398 a31=A31;a32=A32;a33=A33; }
00399 void saveScale()
00400 { Scale0=Scale;}
00401 void saveView()
00402 { saveRot();Scale0=Scale;X00=X0;Y00=Y0;D0=D;Aspr0=Aspr;}
00403
00404 SYWindow(const SYWindow &){}
00405 const SYWindow &operator =(const SYWindow &yw){ return yw;}
00406 public:
00407 SYWindow(int width_hint, int height_hint,
00408 const char *winname, bool s=true, bool so=false,bool fm=false);
00409 virtual ~SYWindow();
00410 void setinterval(int i) {if (i>0) rinterval=i; }
00411 bool IsAlive() { return alive; }
00412 bool TogglePause() { pause=!pause; return pause;}
00413 bool IsPaused() { return pause; }
00414 #ifdef _NOLOCK
00415 void Lock()
00416 {
00417 }
00418 void Unlock()
00419 {
00420 }
00421 void InitSem()
00422 {
00423 }
00424 void InitSem2()
00425 {
00426 }
00427 void LockWritegif()
00428 {
00429 }
00430 void UnlockWritegif()
00431 {
00432 }
00433 #else
00434 #ifndef _MYOWNLOCK
00435 void Lock()
00436 {
00437 static sembuf sbl={0, -1, 0};
00438 semop(semID,&sbl,1);
00439
00440 }
00441 void Unlock()
00442 {
00443 static sembuf sbu={0, 1, 0};
00444 semop(semID,&sbu,1);
00445
00446 }
00447 void InitSem()
00448 {
00449 semID=semget(IPC_PRIVATE, 1, IPC_CREAT|0777);
00450
00451 if(semID==-1) printf("semget failure! use -D_MYOWNLOCK\n");
00452 }
00453 void InitSem2()
00454 {
00455 semID2=semget(IPC_PRIVATE, 1, IPC_CREAT|0777);
00456
00457 if(semID2==-1) printf("semget failure! use -D_MYOWNLOCK\n");
00458 }
00459 void LockWritegif()
00460 {
00461 static sembuf sbl={0, -1, 0};
00462 autowritegif=true;
00463 semop(semID2,&sbl,1);
00464
00465 }
00466 void UnlockWritegif()
00467 {
00468 static sembuf sbu={0, 1, 0};
00469 autowritegif=false;
00470 semop(semID2,&sbu,1);
00471 }
00472 #else
00473 void Lock()
00474 {
00475
00476 semID--;
00477 while(semID<0) sleep(1);
00478
00479 }
00480 void Unlock()
00481 {
00482
00483 semID++;
00484
00485 }
00486 void InitSem()
00487 {
00488 printf("-D_MYOWNLOCK defined, use semaphore mimic\n");
00489 semID=0;
00490 }
00491 void InitSem2()
00492 {
00493 printf("-D_MYOWNLOCK defined, use semaphore mimic\n");
00494 semID2=0;
00495 }
00496 void LockWritegif()
00497 {
00498
00499 semID2--;
00500 while(semID2<0) sleep(1);
00501 }
00502 void UnlockWritegif()
00503 {
00504
00505 semID2++;
00506 }
00507 #endif
00508 #endif
00509 void DrawPoint(double x, double y, double z, double r, unsigned long c,
00510 unsigned long attr=0);
00511 void DrawPoint(double x, double y, double z, double r, unsigned long c,
00512 char *ds, unsigned long attr=0);
00513 void DrawLine(double x0, double y0, double z0,
00514 double x1, double y1, double z1, unsigned long c,
00515 double r=0.01, unsigned long attr=0);
00516 void Draw3DLine(YLine);
00517 void Draw3DPixel(YPoint);
00518 void Draw3DLinetoPS(FILE *,YLine);
00519 void Draw3DPixeltoPS(FILE *,YPoint);
00520 unsigned long AllocNamedColor(char *name);
00521 unsigned long AllocRGBColor(unsigned r, unsigned g, unsigned b);
00522 unsigned long AllocShortRGBColor(unsigned r, unsigned g, unsigned b);
00523 unsigned long StdColor(int c)
00524 {
00525 return AllocShortRGBColor(c&1?0xff:0, c&2?0xff:0, c&4?0xff:0);
00526 }
00527
00528 void Clear() { nP=nL=0; }
00529 void Refresh() { dirty=true; }
00530
00531 virtual void paint();
00532 virtual void update();
00533 virtual void newGraph();
00534
00535 virtual void Evolve();
00536 virtual int identify(int px, int py);
00537
00538 virtual void Routine()
00539 {
00540
00541 #ifdef USE_X
00542 while(alive)
00543 {
00544 if(rinterval==0) delay(0,1000*rInterval);
00545 else delay(0,1000*rinterval);
00546
00547 Evolve();
00548 }
00549
00550 #endif
00551 }
00552
00553 virtual int ExtKeyHandler(KeySym ks)
00554 {
00555 ksfeedback=ks;
00556 if((int)ks)return 0;else return 1;
00557 };
00558 virtual void FreeResource()
00559 {
00560 union semun su={0};
00561 if(alive)
00562 {
00563
00564 #ifdef USE_X
00565 XFreeGC(theDisplay, gc);
00566 XFreePixmap(theDisplay, pixmap);
00567 XFreeColormap(theDisplay, cmap);
00568 XDestroyWindow(theDisplay, theWindow);
00569 XCloseDisplay(theDisplay);
00570 #endif
00571
00572 semctl(semID, 0, IPC_RMID, su);
00573 semctl(semID2, 0, IPC_RMID, su);
00574 }
00575 alive=false;
00576 pause=false;
00577
00578 }
00579 #ifndef NO_THREAD
00580 static void *thread_routine(void *p) ;
00581 void Run() ;
00582 #endif
00583
00584 #ifndef NO_XPM
00585 void writeXpm(char *name)
00586 {
00587 XpmWriteFileFromPixmap(theDisplay,name,pixmap,0,NULL);
00588 }
00589 void writegif();
00590 #endif
00591 void importgif();
00592 void writeps();
00593 void testcolor();
00594 void reversergb();
00595 };
00596
00597 class YWindow: public SYWindow
00598 {
00599
00600 public:
00601 bool enableRot;
00602 void update();
00603 void applyRot();
00604 void horizontalRot(double);
00605 void verticalRot(double);
00606 void spinRot(double);
00607 void zoom(double);
00608 void rotateTo(XEvent);
00609 void translateTo(XEvent);
00610 void pbcshiftTo(XEvent,int);
00611 void pbcglideTo(XEvent);
00612 void pbcglide(double,double);
00613 void scaleTo(XEvent);
00614 void projectTo(XEvent);
00615 void applyRotate();
00616 void setWinSpec(int x0,int y0,double s,double d,double a[3][3]);
00617 YWindow(int w,int h,const char *n,bool s=true,bool so=true,bool fm=true):
00618 SYWindow(w,h,n,s,so,fm),enableRot(false){};
00619 void printWinSpec();
00620 virtual void Evolve();
00621 virtual void help();
00622 virtual void drawBoxFrame();
00623 };
00624
00625
00626
00627 #endif // _DISPLAY_WIN_H
00628