37 |
|
#include "x11icon.h" |
38 |
|
|
39 |
|
#ifndef RAYQLEN |
40 |
< |
#define RAYQLEN 10240 /* max. rays to queue before flush */ |
40 |
> |
#define RAYQLEN 50000 /* max. rays to queue before flush */ |
41 |
|
#endif |
42 |
|
|
43 |
– |
#ifndef PORTALP |
44 |
– |
#define PORTRED 2 /* portal red color */ |
45 |
– |
#define PORTGRN -1 /* portal green left alone */ |
46 |
– |
#define PORTBLU 128 /* portal blue color */ |
47 |
– |
#define PORTALP -1 /* don't use alpha channel */ |
48 |
– |
#endif |
49 |
– |
#define isportal(c) ((PORTRED<0 || (c)[0]==PORTRED) && \ |
50 |
– |
(PORTGRN<0 || (c)[1]==PORTGRN) && \ |
51 |
– |
(PORTBLU<0 || (c)[2]==PORTBLU) && \ |
52 |
– |
(PORTALP<0 || (c)[3]==PORTALP)) |
53 |
– |
|
43 |
|
#ifndef FEQ |
44 |
|
#define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) |
45 |
|
#endif |
46 |
|
|
47 |
+ |
#define VWHEADLOCK 01 /* head position is locked flag */ |
48 |
+ |
#define VWPERSP 02 /* perspective view is set */ |
49 |
+ |
#define VWORTHO 04 /* orthographic view is set */ |
50 |
+ |
#define VWCHANGE 010 /* view has changed */ |
51 |
+ |
#define VWSTEADY 020 /* view is now steady */ |
52 |
+ |
#define VWMAPPED 040 /* window is mapped */ |
53 |
+ |
|
54 |
|
#define GAMMA 1.4 /* default gamma correction */ |
55 |
|
|
56 |
|
#define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) |
93 |
|
|
94 |
|
static XEvent currentevent; /* current event */ |
95 |
|
|
100 |
– |
static int mapped = 0; /* window is mapped? */ |
96 |
|
static unsigned long ourblack=0, ourwhite=~0; |
97 |
|
|
98 |
|
static Display *ourdisplay = NULL; /* our display */ |
110 |
|
|
111 |
|
static int inpresflags; /* input result flags */ |
112 |
|
|
113 |
< |
static int headlocked; /* lock vertical motion */ |
113 |
> |
static int viewflags; /* what's happening with view */ |
114 |
|
|
120 |
– |
static int isperspective; /* perspective/ortho view */ |
121 |
– |
|
122 |
– |
static int viewsteady; /* is view steady? */ |
123 |
– |
|
115 |
|
static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(), |
116 |
|
xferdepth(), freedepth(), setglortho(), |
117 |
|
setglpersp(), getframe(), getmove(), fixwindow(), mytmflags(); |
132 |
|
{ |
133 |
|
extern char *getenv(); |
134 |
|
static RGBPRIMS myprims = STDPRIMS; |
135 |
< |
#if (PORTALP<0) |
136 |
< |
static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8, |
137 |
< |
GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8, |
138 |
< |
GLX_DEPTH_SIZE,15, None}; |
139 |
< |
#else |
140 |
< |
static int atlBest[] = {GLX_RGBA, GLX_RED_SIZE,8, |
150 |
< |
GLX_GREEN_SIZE,8, GLX_BLUE_SIZE,8, |
151 |
< |
GLX_ALPHA_SIZE,2, GLX_DEPTH_SIZE,15, None}; |
152 |
< |
#endif |
135 |
> |
static int atlBest[] = {GLX_RGBA, GLX_DOUBLEBUFFER, |
136 |
> |
GLX_RED_SIZE,8, GLX_GREEN_SIZE,8, |
137 |
> |
GLX_BLUE_SIZE,8, GLX_DEPTH_SIZE,15, None}; |
138 |
> |
static int atlOK[] = {GLX_RGBA, GLX_DOUBLEBUFFER, |
139 |
> |
GLX_RED_SIZE,4, GLX_GREEN_SIZE,4, |
140 |
> |
GLX_BLUE_SIZE,4, GLX_DEPTH_SIZE,15, None}; |
141 |
|
char *ev; |
142 |
|
double gamval = GAMMA; |
143 |
|
RGBPRIMP dpri = stdprims; |
168 |
|
#endif |
169 |
|
/* find a usable visual */ |
170 |
|
ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest); |
171 |
+ |
if (ourvinf == NULL) |
172 |
+ |
ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK); |
173 |
|
CHECK(ourvinf==NULL, USER, "no suitable visuals available"); |
174 |
|
/* get a context */ |
175 |
|
gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE); |
226 |
|
glXMakeCurrent(ourdisplay, gwind, gctx); |
227 |
|
glEnable(GL_DEPTH_TEST); |
228 |
|
glDepthFunc(GL_LEQUAL); |
229 |
< |
glDisable(GL_DITHER); |
229 |
> |
glClearColor(0, 0, 0, 0); |
230 |
|
glFrontFace(GL_CCW); |
231 |
|
glDisable(GL_CULL_FACE); |
232 |
|
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); |
243 |
|
checkglerr("setting rendering parameters"); |
244 |
|
copystruct(&odev.v, &stdview); |
245 |
|
odev.v.type = VT_PER; |
246 |
< |
headlocked = 0; /* free up head movement */ |
257 |
< |
viewsteady = 1; /* view starts static */ |
258 |
< |
isperspective = -1; /* but no view set, yet */ |
246 |
> |
viewflags = VWSTEADY; /* view starts static */ |
247 |
|
/* map the window */ |
248 |
|
XMapWindow(ourdisplay, gwind); |
249 |
|
dev_input(); /* sets size and view angles */ |
333 |
|
VSUM(vwright.vp, nv->vp, nv->hvec, d); |
334 |
|
/* setview(&vwright); -- Unnecessary */ |
335 |
|
#endif |
336 |
< |
} else |
337 |
< |
viewsteady = 1; |
336 |
> |
viewflags |= VWCHANGE; |
337 |
> |
} |
338 |
|
wipeclean(); |
339 |
|
return(1); |
340 |
|
} |
430 |
|
{ |
431 |
|
int ndrawn; |
432 |
|
|
433 |
< |
if (mapped && isperspective > 0) { |
433 |
> |
if ((viewflags&(VWMAPPED|VWPERSP)) == (VWMAPPED|VWPERSP)) { |
434 |
|
#ifdef STEREO |
435 |
|
pushright(); /* draw right eye */ |
436 |
|
ndrawn = gmDrawGeom(); |
437 |
|
#ifdef DOBJ |
438 |
|
ndrawn += dobj_render(); |
439 |
|
#endif |
452 |
– |
if (ndrawn) |
453 |
– |
gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); |
440 |
|
checkglerr("rendering right eye"); |
441 |
|
popright(); /* draw left eye */ |
442 |
|
#endif |
444 |
|
#ifdef DOBJ |
445 |
|
ndrawn += dobj_render(); |
446 |
|
#endif |
447 |
< |
if (ndrawn) |
462 |
< |
gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); |
447 |
> |
glXSwapBuffers(ourdisplay, gwind); |
448 |
|
checkglerr("rendering base view"); |
449 |
|
} |
450 |
< |
if (mapped && viewsteady) |
451 |
< |
if (isperspective > 0) { /* first time after steady */ |
452 |
< |
if (ndrawn) |
453 |
< |
xferdepth(); /* transfer and clear depth */ |
454 |
< |
setglortho(); /* set orthographic view */ |
455 |
< |
} else if (!isperspective) { |
450 |
> |
if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) == |
451 |
> |
(VWMAPPED|VWSTEADY|VWPERSP)) { |
452 |
> |
/* first time after steady */ |
453 |
> |
if (ndrawn) |
454 |
> |
xferdepth(); /* transfer and clear depth */ |
455 |
> |
setglortho(); /* set orthographic view */ |
456 |
> |
|
457 |
> |
} |
458 |
> |
if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) == |
459 |
> |
(VWMAPPED|VWSTEADY|VWORTHO)) { |
460 |
> |
/* else update cones */ |
461 |
|
#ifdef STEREO |
462 |
< |
pushright(); |
463 |
< |
odUpdate(1); /* draw right eye */ |
464 |
< |
popright(); |
462 |
> |
pushright(); |
463 |
> |
odUpdate(1); /* draw right eye */ |
464 |
> |
popright(); |
465 |
|
#endif |
466 |
< |
odUpdate(0); /* draw left eye */ |
467 |
< |
} |
468 |
< |
glFlush(); /* flush OpenGL */ |
466 |
> |
odUpdate(0); /* draw left eye */ |
467 |
> |
glFlush(); /* flush OpenGL */ |
468 |
> |
} |
469 |
|
rayqleft = RAYQLEN; |
470 |
|
/* flush X11 and return # pending */ |
471 |
|
return(odev.inpready = XPending(ourdisplay)); |
489 |
|
xferdepth() /* load and clear depth buffer */ |
490 |
|
{ |
491 |
|
register GLfloat *dbp; |
492 |
< |
register GLubyte *cbuf; |
492 |
> |
register GLubyte *pbuf; |
493 |
|
|
494 |
|
if (depthbuffer == NULL) { /* allocate private depth buffer */ |
495 |
|
#ifdef STEREO |
502 |
|
} |
503 |
|
/* allocate alpha buffer for portals */ |
504 |
|
if (gmPortals) |
505 |
< |
cbuf = (GLubyte *)malloc(odev.hres*odev.vres* |
506 |
< |
(4*sizeof(GLubyte))); |
505 |
> |
pbuf = (GLubyte *)malloc(odev.hres*odev.vres * |
506 |
> |
(4*sizeof(GLubyte))); |
507 |
|
else |
508 |
< |
cbuf = NULL; |
508 |
> |
pbuf = NULL; |
509 |
|
#ifdef STEREO |
510 |
|
setstereobuf(STEREO_BUFFER_RIGHT); |
511 |
|
glReadPixels(0, 0, odev.hres, odev.vres, |
512 |
|
GL_DEPTH_COMPONENT, GL_FLOAT, depthright); |
513 |
< |
if (cbuf != NULL) |
513 |
> |
if (pbuf != NULL) { |
514 |
> |
glClear(GL_COLOR_BUFFER_BIT); |
515 |
> |
gmDrawPortals(0xff, -1, -1, -1); |
516 |
|
glReadPixels(0, 0, odev.hres, odev.vres, |
517 |
< |
GL_RGBA, GL_UNSIGNED_BYTE, cbuf); |
517 |
> |
GL_RGBA, GL_UNSIGNED_BYTE, pbuf); |
518 |
> |
} |
519 |
|
for (dbp = depthright + odev.hres*odev.vres; dbp-- > depthright; ) |
520 |
< |
if (cbuf != NULL && isportal(cbuf+4*(dbp-depthright))) |
520 |
> |
if (pbuf != NULL && pbuf[4*(dbp-depthright)]&0x40) |
521 |
|
*dbp = FHUGE; |
522 |
|
else |
523 |
|
*dbp = mapdepth(*dbp); |
528 |
|
/* read back depth buffer */ |
529 |
|
glReadPixels(0, 0, odev.hres, odev.vres, |
530 |
|
GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); |
531 |
< |
if (cbuf != NULL) |
531 |
> |
if (pbuf != NULL) { |
532 |
> |
glClear(GL_COLOR_BUFFER_BIT); /* find portals */ |
533 |
> |
gmDrawPortals(0xff, -1, -1, -1); |
534 |
|
glReadPixels(0, 0, odev.hres, odev.vres, |
535 |
< |
GL_RGBA, GL_UNSIGNED_BYTE, cbuf); |
535 |
> |
GL_RGBA, GL_UNSIGNED_BYTE, pbuf); |
536 |
> |
} |
537 |
|
for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; ) |
538 |
< |
if (cbuf != NULL && isportal(cbuf+4*(dbp-depthbuffer))) |
538 |
> |
if (pbuf != NULL && pbuf[4*(dbp-depthbuffer)]&0x40) |
539 |
|
*dbp = FHUGE; |
540 |
|
else |
541 |
|
*dbp = mapdepth(*dbp); |
542 |
|
glClear(GL_DEPTH_BUFFER_BIT); /* clear system depth buffer */ |
547 |
– |
if (cbuf != NULL) |
548 |
– |
free((char *)cbuf); /* free our color buffer */ |
543 |
|
odDepthMap(0, depthbuffer); /* transfer depth data */ |
544 |
+ |
if (pbuf != NULL) |
545 |
+ |
free((char *)pbuf); /* free our portal buffer */ |
546 |
|
} |
547 |
|
|
548 |
|
|
568 |
|
FVECT direc; |
569 |
|
{ |
570 |
|
GLfloat gldepth; |
575 |
– |
GLubyte glcolor[4]; |
571 |
|
double dist; |
572 |
|
|
573 |
|
if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres) |
577 |
|
else { |
578 |
|
glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT, |
579 |
|
GL_FLOAT, &gldepth); |
585 |
– |
if (gmPortals) { |
586 |
– |
glReadPixels(dx,dy, 1,1, GL_RGBA, |
587 |
– |
GL_UNSIGNED_BYTE, glcolor); |
588 |
– |
if (isportal(glcolor)) |
589 |
– |
return(FHUGE); |
590 |
– |
} |
580 |
|
dist = mapdepth(gldepth); |
581 |
|
} |
582 |
|
if (dist >= .99*FHUGE) |
592 |
|
double d; |
593 |
|
|
594 |
|
setstereobuf(STEREO_BUFFER_RIGHT); |
595 |
< |
if (isperspective > 0) { |
595 |
> |
if (viewflags & VWPERSP) { |
596 |
|
glMatrixMode(GL_MODELVIEW); |
597 |
|
glPushMatrix(); |
598 |
|
d = -eyesepdist / sqrt(odev.v.hn2); |
606 |
|
static |
607 |
|
popright() /* pop off right view & buffer */ |
608 |
|
{ |
609 |
< |
if (isperspective > 0) { |
609 |
> |
if (viewflags & VWPERSP) { |
610 |
|
glMatrixMode(GL_MODELVIEW); |
611 |
|
glPopMatrix(); |
612 |
|
} |
649 |
|
resizewindow(levptr(XConfigureEvent)); |
650 |
|
break; |
651 |
|
case UnmapNotify: |
652 |
< |
mapped = 0; |
652 |
> |
viewflags &= ~VWMAPPED; |
653 |
|
break; |
654 |
|
case MapNotify: |
655 |
|
odRemap(0); |
656 |
< |
mapped = 1; |
656 |
> |
viewflags |= VWMAPPED; |
657 |
|
break; |
658 |
|
case Expose: |
659 |
|
fixwindow(levptr(XExposeEvent)); |
687 |
|
glPushAttrib(GL_LIGHTING_BIT|GL_ENABLE_BIT); |
688 |
|
glDisable(GL_LIGHTING); |
689 |
|
if (fore) |
690 |
< |
glColor3ub(0, 255, 255); |
690 |
> |
glColor3ub(4, 250, 250); |
691 |
|
else |
692 |
|
glColor3ub(0, 0, 0); |
693 |
|
glBegin(GL_LINES); /* draw each grid line */ |
740 |
|
d = MOVPCT/100. * mov; |
741 |
|
VSUM(nv.vp, nv.vp, odir, d); |
742 |
|
} |
743 |
< |
if (!mov ^ !orb && headlocked) { /* restore head height */ |
743 |
> |
if (!mov ^ !orb && viewflags&VWHEADLOCK) { /* restore height */ |
744 |
|
VSUM(v1, odev.v.vp, nv.vp, -1.); |
745 |
|
d = DOT(v1, nv.vup); |
746 |
|
VSUM(nv.vp, nv.vp, odev.v.vup, d); |
789 |
|
|
790 |
|
XNoOp(ourdisplay); /* makes sure we're not idle */ |
791 |
|
|
792 |
< |
viewsteady = 0; /* flag moving view */ |
792 |
> |
viewflags &= ~VWSTEADY; /* flag moving view */ |
793 |
|
setglpersp(&odev.v); /* start us off in perspective */ |
794 |
|
while (!XCheckMaskEvent(ourdisplay, |
795 |
|
ButtonReleaseMask, levptr(XEvent))) { |
810 |
|
#ifdef DOBJ |
811 |
|
ndrawn += dobj_render(); |
812 |
|
#endif |
824 |
– |
if (ndrawn) |
825 |
– |
gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); |
813 |
|
popright(); |
814 |
|
#endif |
815 |
|
/* redraw octrees */ |
817 |
|
#ifdef DOBJ |
818 |
|
ndrawn += dobj_render(); /* redraw objects */ |
819 |
|
#endif |
820 |
< |
if (ndrawn) |
821 |
< |
gmDrawPortals(PORTRED, PORTGRN, PORTBLU, PORTALP); |
835 |
< |
glFlush(); |
836 |
< |
if (!ndrawn) { |
820 |
> |
glXSwapBuffers(ourdisplay, gwind); |
821 |
> |
if (!ndrawn) |
822 |
|
sleep(1); /* for reasonable interaction */ |
838 |
– |
#ifdef STEREO |
839 |
– |
pushright(); |
840 |
– |
draw_grids(0); |
841 |
– |
popright(); |
842 |
– |
#endif |
843 |
– |
draw_grids(0); |
844 |
– |
} |
823 |
|
} |
824 |
|
if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ |
825 |
|
movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); |
827 |
|
wy = levptr(XButtonReleasedEvent)->y; |
828 |
|
moveview(wx, odev.vres-1-wy, movdir, movorb); |
829 |
|
} |
830 |
+ |
viewflags |= VWSTEADY; /* done goofing around */ |
831 |
|
} |
832 |
|
|
833 |
|
|
874 |
|
odev.v.vp[2] + odev.v.vdir[2], |
875 |
|
odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); |
876 |
|
checkglerr("setting perspective view"); |
898 |
– |
isperspective = 1; |
877 |
|
vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.; |
878 |
|
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec); |
879 |
|
vec[0] = -odev.v.vdir[0]; |
889 |
|
glEnable(GL_LIGHT0); |
890 |
|
glEnable(GL_LIGHTING); /* light our GL objects */ |
891 |
|
glShadeModel(GL_SMOOTH); |
892 |
+ |
viewflags &= ~VWORTHO; |
893 |
+ |
viewflags |= VWPERSP; |
894 |
|
} |
895 |
|
|
896 |
|
|
897 |
|
static |
898 |
|
setglortho() /* set up orthographic view for cone drawing */ |
899 |
|
{ |
900 |
+ |
glDrawBuffer(GL_FRONT); /* use single-buffer mode */ |
901 |
|
/* set view matrix */ |
902 |
|
glMatrixMode(GL_PROJECTION); |
903 |
|
glLoadIdentity(); |
904 |
|
glOrtho(0., (double)odev.hres, 0., (double)odev.vres, |
905 |
|
0.001*OMAXDEPTH, 1.001*(-OMAXDEPTH)); |
906 |
|
checkglerr("setting orthographic view"); |
926 |
– |
isperspective = 0; |
907 |
|
glDisable(GL_LIGHTING); /* cones are constant color */ |
908 |
|
glShadeModel(GL_FLAT); |
909 |
+ |
viewflags &= ~VWPERSP; |
910 |
+ |
viewflags |= VWORTHO; |
911 |
|
} |
912 |
|
|
913 |
|
|
914 |
|
static |
915 |
|
wipeclean() /* prepare for redraw */ |
916 |
|
{ |
917 |
< |
/* clear depth buffer */ |
917 |
> |
glDrawBuffer(GL_BACK); /* use double-buffer mode */ |
918 |
> |
glReadBuffer(GL_BACK); |
919 |
> |
/* clear buffers */ |
920 |
|
#ifdef STEREO |
921 |
|
setstereobuf(STEREO_BUFFER_RIGHT); |
922 |
< |
glClear(GL_DEPTH_BUFFER_BIT); |
922 |
> |
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
923 |
|
setstereobuf(STEREO_BUFFER_LEFT); |
924 |
|
#endif |
925 |
< |
glClear(GL_DEPTH_BUFFER_BIT); |
942 |
< |
if (viewsteady) /* clear samples if steady */ |
943 |
< |
odClean(); |
925 |
> |
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
926 |
|
freedepth(); |
927 |
+ |
if ((viewflags&(VWCHANGE|VWSTEADY)) == |
928 |
+ |
(VWCHANGE|VWSTEADY)) { /* clear samples if new */ |
929 |
+ |
odClean(); |
930 |
+ |
viewflags &= ~VWCHANGE; /* change noted */ |
931 |
+ |
} |
932 |
|
setglpersp(&odev.v); /* reset view & clipping planes */ |
933 |
|
} |
934 |
|
|
948 |
|
return; |
949 |
|
switch (buf[0]) { |
950 |
|
case 'h': /* turn on height motion lock */ |
951 |
< |
headlocked = 1; |
951 |
> |
viewflags |= VWHEADLOCK; |
952 |
|
return; |
953 |
|
case 'H': /* turn off height motion lock */ |
954 |
< |
headlocked = 0; |
954 |
> |
viewflags &= ~VWHEADLOCK; |
955 |
|
return; |
956 |
|
case 'l': /* retrieve last view */ |
957 |
|
inpresflags |= DFL(DC_LASTVIEW); |
980 |
|
return; |
981 |
|
case CTRL('R'): /* redraw screen */ |
982 |
|
odRemap(0); |
983 |
< |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
983 |
> |
glClear(GL_DEPTH_BUFFER_BIT); |
984 |
|
#ifdef STEREO |
985 |
|
setstereobuf(STEREO_BUFFER_RIGHT); |
986 |
< |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
986 |
> |
glClear(GL_DEPTH_BUFFER_BIT); |
987 |
|
setstereobuf(STEREO_BUFFER_LEFT); |
988 |
|
#endif |
989 |
|
return; |
1072 |
|
odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); |
1073 |
|
|
1074 |
|
inpresflags |= DFL(DC_SETVIEW); |
1075 |
+ |
viewflags |= VWCHANGE; |
1076 |
|
} |