| 1 |
gwlarson |
3.13 |
/* Copyright (c) 1999 Silicon Graphics, Inc. */ |
| 2 |
gwlarson |
3.1 |
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ SGI"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* OpenGL driver for holodeck display. |
| 9 |
|
|
* Based on GLX driver using T-mesh. |
| 10 |
|
|
* |
| 11 |
|
|
* Define symbol STEREO for stereo viewing. |
| 12 |
|
|
* Define symbol DOBJ for display object viewing. |
| 13 |
|
|
*/ |
| 14 |
|
|
|
| 15 |
|
|
#ifdef NOSTEREO |
| 16 |
|
|
#ifdef STEREO |
| 17 |
|
|
#undef STEREO |
| 18 |
|
|
#else |
| 19 |
|
|
#undef NOSTEREO |
| 20 |
|
|
#endif |
| 21 |
|
|
#endif |
| 22 |
|
|
|
| 23 |
|
|
#include "standard.h" |
| 24 |
|
|
|
| 25 |
|
|
#include <sys/types.h> |
| 26 |
|
|
#include <GL/glx.h> |
| 27 |
|
|
#include <GL/glu.h> |
| 28 |
|
|
#ifdef STEREO |
| 29 |
|
|
#include <X11/extensions/SGIStereo.h> |
| 30 |
|
|
#endif |
| 31 |
|
|
|
| 32 |
|
|
#include "rhd_odraw.h" |
| 33 |
|
|
#ifdef DOBJ |
| 34 |
|
|
#include "rhdobj.h" |
| 35 |
|
|
#endif |
| 36 |
|
|
|
| 37 |
|
|
#include "x11icon.h" |
| 38 |
|
|
|
| 39 |
|
|
#ifndef RAYQLEN |
| 40 |
gwlarson |
3.11 |
#define RAYQLEN 0 /* max. rays to queue before flush */ |
| 41 |
gwlarson |
3.1 |
#endif |
| 42 |
gwlarson |
3.15 |
#ifndef MINWIDTH |
| 43 |
|
|
#define MINWIDTH 480 /* minimum graphics window width */ |
| 44 |
|
|
#define MINHEIGHT 400 /* minimum graphics window height */ |
| 45 |
|
|
#endif |
| 46 |
|
|
#ifndef VIEWDIST |
| 47 |
|
|
#define VIEWDIST 356 /* assumed viewing distance (mm) */ |
| 48 |
|
|
#endif |
| 49 |
|
|
#ifndef BORWIDTH |
| 50 |
|
|
#define BORWIDTH 5 /* border width */ |
| 51 |
|
|
#endif |
| 52 |
gwlarson |
3.1 |
|
| 53 |
|
|
#ifndef FEQ |
| 54 |
|
|
#define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) |
| 55 |
|
|
#endif |
| 56 |
|
|
|
| 57 |
gwlarson |
3.7 |
#define VWHEADLOCK 01 /* head position is locked flag */ |
| 58 |
|
|
#define VWPERSP 02 /* perspective view is set */ |
| 59 |
|
|
#define VWORTHO 04 /* orthographic view is set */ |
| 60 |
|
|
#define VWCHANGE 010 /* view has changed */ |
| 61 |
|
|
#define VWSTEADY 020 /* view is now steady */ |
| 62 |
|
|
#define VWMAPPED 040 /* window is mapped */ |
| 63 |
|
|
|
| 64 |
gwlarson |
3.1 |
#define GAMMA 1.4 /* default gamma correction */ |
| 65 |
|
|
|
| 66 |
|
|
#define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) |
| 67 |
|
|
|
| 68 |
|
|
#define MOVPCT 7 /* percent distance to move /frame */ |
| 69 |
|
|
#define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) |
| 70 |
|
|
#define MOVDEG (-5) /* degrees to orbit CW/down /frame */ |
| 71 |
|
|
#define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) |
| 72 |
|
|
|
| 73 |
|
|
#define setstereobuf(bid) (glXWaitGL(), \ |
| 74 |
|
|
XSGISetStereoBuffer(ourdisplay, gwind, bid), \ |
| 75 |
|
|
glXWaitX()) |
| 76 |
|
|
|
| 77 |
|
|
#define ourscreen DefaultScreen(ourdisplay) |
| 78 |
|
|
#define ourroot RootWindow(ourdisplay,ourscreen) |
| 79 |
|
|
#define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\ |
| 80 |
|
|
ButtonPressMask|ButtonReleaseMask) |
| 81 |
|
|
|
| 82 |
|
|
#define levptr(etype) ((etype *)¤tevent) |
| 83 |
|
|
|
| 84 |
|
|
struct driver odev; /* global device driver structure */ |
| 85 |
|
|
|
| 86 |
|
|
char odev_args[64]; /* command arguments */ |
| 87 |
|
|
|
| 88 |
|
|
static GLfloat *depthbuffer = NULL; /* depth buffer */ |
| 89 |
|
|
|
| 90 |
|
|
#ifdef STEREO |
| 91 |
|
|
static VIEW vwright; /* right eye view */ |
| 92 |
|
|
static GLfloat *depthright = NULL; /* right depth buffer */ |
| 93 |
|
|
#endif |
| 94 |
|
|
|
| 95 |
|
|
static int rayqleft = 0; /* rays left to queue before flush */ |
| 96 |
|
|
|
| 97 |
|
|
static XEvent currentevent; /* current event */ |
| 98 |
|
|
|
| 99 |
|
|
static unsigned long ourblack=0, ourwhite=~0; |
| 100 |
|
|
|
| 101 |
|
|
static Display *ourdisplay = NULL; /* our display */ |
| 102 |
|
|
static XVisualInfo *ourvinf; /* our visual information */ |
| 103 |
|
|
static Window gwind = 0; /* our graphics window */ |
| 104 |
|
|
static GLXContext gctx; /* our GLX context */ |
| 105 |
|
|
|
| 106 |
|
|
static double pwidth, pheight; /* pixel dimensions (mm) */ |
| 107 |
|
|
|
| 108 |
|
|
static double dev_zmin, dev_zmax; /* fore and aft clipping plane dist. */ |
| 109 |
|
|
static double dev_zrat; /* (1. - dev_zmin/dev_zmax) */ |
| 110 |
|
|
|
| 111 |
|
|
#define setzrat() (dev_zrat = 1. - dev_zmin/dev_zmax) |
| 112 |
|
|
#define mapdepth(d) ((d)>0.9995 ? FHUGE : dev_zmin/(1.-(d)*dev_zrat)) |
| 113 |
|
|
|
| 114 |
|
|
static int inpresflags; /* input result flags */ |
| 115 |
|
|
|
| 116 |
gwlarson |
3.7 |
static int viewflags; /* what's happening with view */ |
| 117 |
gwlarson |
3.1 |
|
| 118 |
|
|
static int resizewindow(), getevent(), getkey(), moveview(), wipeclean(), |
| 119 |
gwlarson |
3.2 |
xferdepth(), freedepth(), setglortho(), |
| 120 |
gwlarson |
3.1 |
setglpersp(), getframe(), getmove(), fixwindow(), mytmflags(); |
| 121 |
|
|
|
| 122 |
|
|
static double getdistance(); |
| 123 |
|
|
|
| 124 |
|
|
#ifdef STEREO |
| 125 |
|
|
static int pushright(), popright(); |
| 126 |
|
|
#endif |
| 127 |
|
|
|
| 128 |
gwlarson |
3.2 |
extern int gmPortals; /* GL portal list id */ |
| 129 |
|
|
|
| 130 |
gwlarson |
3.1 |
extern time_t time(); |
| 131 |
|
|
|
| 132 |
|
|
|
| 133 |
|
|
dev_open(id) /* initialize GLX driver */ |
| 134 |
|
|
char *id; |
| 135 |
|
|
{ |
| 136 |
|
|
extern char *getenv(); |
| 137 |
|
|
static RGBPRIMS myprims = STDPRIMS; |
| 138 |
gwlarson |
3.6 |
static int atlBest[] = {GLX_RGBA, GLX_DOUBLEBUFFER, |
| 139 |
|
|
GLX_RED_SIZE,8, GLX_GREEN_SIZE,8, |
| 140 |
|
|
GLX_BLUE_SIZE,8, GLX_DEPTH_SIZE,15, None}; |
| 141 |
|
|
static int atlOK[] = {GLX_RGBA, GLX_DOUBLEBUFFER, |
| 142 |
|
|
GLX_RED_SIZE,4, GLX_GREEN_SIZE,4, |
| 143 |
|
|
GLX_BLUE_SIZE,4, GLX_DEPTH_SIZE,15, None}; |
| 144 |
gwlarson |
3.1 |
char *ev; |
| 145 |
|
|
double gamval = GAMMA; |
| 146 |
|
|
RGBPRIMP dpri = stdprims; |
| 147 |
|
|
XSetWindowAttributes ourwinattr; |
| 148 |
|
|
XWMHints ourxwmhints; |
| 149 |
|
|
XSizeHints oursizhints; |
| 150 |
|
|
/* check for unsupported stereo */ |
| 151 |
|
|
#ifdef NOSTEREO |
| 152 |
|
|
error(INTERNAL, "stereo display driver unavailable"); |
| 153 |
|
|
#endif |
| 154 |
|
|
/* open display server */ |
| 155 |
|
|
ourdisplay = XOpenDisplay(NULL); |
| 156 |
gwlarson |
3.5 |
CHECK(ourdisplay==NULL, USER, |
| 157 |
|
|
"cannot open X-windows; DISPLAY variable set?"); |
| 158 |
gwlarson |
3.1 |
#ifdef STEREO |
| 159 |
|
|
switch (XSGIQueryStereoMode(ourdisplay, ourroot)) { |
| 160 |
|
|
case STEREO_TOP: |
| 161 |
|
|
case STEREO_BOTTOM: |
| 162 |
|
|
break; |
| 163 |
|
|
case STEREO_OFF: |
| 164 |
|
|
error(USER, |
| 165 |
gwlarson |
3.5 |
"wrong video mode: run \"/usr/gfx/setmon -n STR_TOP\" first"); |
| 166 |
gwlarson |
3.1 |
case X_STEREO_UNSUPPORTED: |
| 167 |
|
|
error(USER, "stereo mode not supported on this screen"); |
| 168 |
|
|
default: |
| 169 |
|
|
error(INTERNAL, "unknown stereo mode"); |
| 170 |
|
|
} |
| 171 |
|
|
#endif |
| 172 |
|
|
/* find a usable visual */ |
| 173 |
|
|
ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlBest); |
| 174 |
gwlarson |
3.6 |
if (ourvinf == NULL) |
| 175 |
|
|
ourvinf = glXChooseVisual(ourdisplay, ourscreen, atlOK); |
| 176 |
gwlarson |
3.5 |
CHECK(ourvinf==NULL, USER, "no suitable visuals available"); |
| 177 |
gwlarson |
3.1 |
/* get a context */ |
| 178 |
|
|
gctx = glXCreateContext(ourdisplay, ourvinf, NULL, GL_TRUE); |
| 179 |
|
|
/* set gamma and tone mapping */ |
| 180 |
|
|
if ((ev = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL |
| 181 |
|
|
|| (ev = getenv("DISPLAY_GAMMA")) != NULL) |
| 182 |
|
|
gamval = atof(ev); |
| 183 |
|
|
if ((ev = getenv("DISPLAY_PRIMARIES")) != NULL && |
| 184 |
|
|
sscanf(ev, "%f %f %f %f %f %f %f %f", |
| 185 |
|
|
&myprims[RED][CIEX],&myprims[RED][CIEY], |
| 186 |
|
|
&myprims[GRN][CIEX],&myprims[GRN][CIEY], |
| 187 |
|
|
&myprims[BLU][CIEX],&myprims[BLU][CIEY], |
| 188 |
|
|
&myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6) |
| 189 |
|
|
dpri = myprims; |
| 190 |
|
|
if (tmInit(mytmflags(), dpri, gamval) == NULL) |
| 191 |
|
|
error(SYSTEM, "not enough memory in dev_open"); |
| 192 |
|
|
/* open window */ |
| 193 |
|
|
ourwinattr.background_pixel = ourblack; |
| 194 |
|
|
ourwinattr.border_pixel = ourblack; |
| 195 |
|
|
ourwinattr.event_mask = ourmask; |
| 196 |
|
|
/* this is stupid */ |
| 197 |
|
|
ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot, |
| 198 |
|
|
ourvinf->visual, AllocNone); |
| 199 |
|
|
gwind = XCreateWindow(ourdisplay, ourroot, 0, 0, |
| 200 |
|
|
DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, |
| 201 |
|
|
#ifdef STEREO |
| 202 |
|
|
(DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH)/2, |
| 203 |
|
|
#else |
| 204 |
|
|
DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, |
| 205 |
|
|
#endif |
| 206 |
|
|
BORWIDTH, ourvinf->depth, InputOutput, ourvinf->visual, |
| 207 |
|
|
CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr); |
| 208 |
gwlarson |
3.5 |
CHECK(gwind==0, SYSTEM, "cannot create window"); |
| 209 |
gwlarson |
3.1 |
XStoreName(ourdisplay, gwind, id); |
| 210 |
|
|
/* set window manager hints */ |
| 211 |
|
|
ourxwmhints.flags = InputHint|IconPixmapHint; |
| 212 |
|
|
ourxwmhints.input = True; |
| 213 |
|
|
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, |
| 214 |
|
|
gwind, x11icon_bits, x11icon_width, x11icon_height); |
| 215 |
|
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
| 216 |
|
|
oursizhints.min_width = MINWIDTH; |
| 217 |
|
|
#ifdef STEREO |
| 218 |
|
|
oursizhints.min_height = MINHEIGHT/2; |
| 219 |
|
|
oursizhints.max_width = DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH; |
| 220 |
|
|
oursizhints.max_height = (DisplayHeight(ourdisplay,ourscreen) - |
| 221 |
|
|
2*BORWIDTH)/2; |
| 222 |
|
|
oursizhints.flags = PMinSize|PMaxSize; |
| 223 |
|
|
#else |
| 224 |
|
|
oursizhints.min_height = MINHEIGHT; |
| 225 |
|
|
oursizhints.flags = PMinSize; |
| 226 |
|
|
#endif |
| 227 |
|
|
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
| 228 |
|
|
/* set GLX context */ |
| 229 |
|
|
glXMakeCurrent(ourdisplay, gwind, gctx); |
| 230 |
|
|
glEnable(GL_DEPTH_TEST); |
| 231 |
|
|
glDepthFunc(GL_LEQUAL); |
| 232 |
gwlarson |
3.9 |
glClearColor(0, 0, 0, 0); |
| 233 |
gwlarson |
3.1 |
glFrontFace(GL_CCW); |
| 234 |
|
|
glDisable(GL_CULL_FACE); |
| 235 |
|
|
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); |
| 236 |
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 237 |
gwlarson |
3.10 |
glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 238 |
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 239 |
gwlarson |
3.1 |
/* figure out sensible view */ |
| 240 |
|
|
pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) / |
| 241 |
|
|
DisplayWidth(ourdisplay, ourscreen); |
| 242 |
|
|
pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / |
| 243 |
|
|
DisplayHeight(ourdisplay, ourscreen); |
| 244 |
|
|
#ifdef STEREO |
| 245 |
|
|
pheight *= 2.; |
| 246 |
|
|
setstereobuf(STEREO_BUFFER_LEFT); |
| 247 |
|
|
#endif |
| 248 |
|
|
checkglerr("setting rendering parameters"); |
| 249 |
|
|
copystruct(&odev.v, &stdview); |
| 250 |
|
|
odev.v.type = VT_PER; |
| 251 |
gwlarson |
3.7 |
viewflags = VWSTEADY; /* view starts static */ |
| 252 |
gwlarson |
3.1 |
/* map the window */ |
| 253 |
|
|
XMapWindow(ourdisplay, gwind); |
| 254 |
|
|
dev_input(); /* sets size and view angles */ |
| 255 |
|
|
if (!odInit(DisplayWidth(ourdisplay,ourscreen) * |
| 256 |
gwlarson |
3.11 |
DisplayHeight(ourdisplay,ourscreen) / 3)) |
| 257 |
gwlarson |
3.1 |
error(SYSTEM, "insufficient memory for value storage"); |
| 258 |
|
|
odev.name = id; |
| 259 |
|
|
odev.firstuse = 1; /* can't recycle samples */ |
| 260 |
|
|
odev.ifd = ConnectionNumber(ourdisplay); |
| 261 |
|
|
} |
| 262 |
|
|
|
| 263 |
|
|
|
| 264 |
|
|
dev_close() /* close our display and free resources */ |
| 265 |
|
|
{ |
| 266 |
|
|
#ifdef DOBJ |
| 267 |
|
|
dobj_cleanup(); |
| 268 |
|
|
#endif |
| 269 |
gwlarson |
3.2 |
freedepth(); |
| 270 |
|
|
gmEndGeom(); |
| 271 |
|
|
gmEndPortal(); |
| 272 |
gwlarson |
3.1 |
odDone(); |
| 273 |
|
|
glXMakeCurrent(ourdisplay, None, NULL); |
| 274 |
|
|
glXDestroyContext(ourdisplay, gctx); |
| 275 |
|
|
XDestroyWindow(ourdisplay, gwind); |
| 276 |
|
|
gwind = 0; |
| 277 |
|
|
XCloseDisplay(ourdisplay); |
| 278 |
|
|
ourdisplay = NULL; |
| 279 |
|
|
tmDone(NULL); |
| 280 |
|
|
odev.v.type = 0; |
| 281 |
|
|
odev.hres = odev.vres = 0; |
| 282 |
|
|
odev.ifd = -1; |
| 283 |
|
|
} |
| 284 |
|
|
|
| 285 |
|
|
|
| 286 |
|
|
dev_clear() /* clear our representation */ |
| 287 |
|
|
{ |
| 288 |
gwlarson |
3.10 |
viewflags |= VWCHANGE; /* pretend our view has changed */ |
| 289 |
|
|
wipeclean(); /* clean off display and samples */ |
| 290 |
gwlarson |
3.12 |
dev_flush(); /* redraw geometry & get depth */ |
| 291 |
gwlarson |
3.1 |
rayqleft = 0; /* hold off update */ |
| 292 |
|
|
} |
| 293 |
|
|
|
| 294 |
|
|
|
| 295 |
|
|
int |
| 296 |
|
|
dev_view(nv) /* assign new driver view */ |
| 297 |
|
|
register VIEW *nv; |
| 298 |
|
|
{ |
| 299 |
|
|
double d; |
| 300 |
|
|
|
| 301 |
|
|
if (nv->type != VT_PER || /* check view legality */ |
| 302 |
|
|
nv->horiz > 160. || nv->vert > 160.) { |
| 303 |
|
|
error(COMMAND, "illegal view type/angle"); |
| 304 |
|
|
nv->type = odev.v.type; |
| 305 |
|
|
nv->horiz = odev.v.horiz; |
| 306 |
|
|
nv->vert = odev.v.vert; |
| 307 |
|
|
return(0); |
| 308 |
|
|
} |
| 309 |
|
|
if (nv != &odev.v) { |
| 310 |
|
|
/* resize window? */ |
| 311 |
|
|
if (!FEQ(nv->horiz,odev.v.horiz) || |
| 312 |
|
|
!FEQ(nv->vert,odev.v.vert)) { |
| 313 |
|
|
int dw = DisplayWidth(ourdisplay,ourscreen); |
| 314 |
|
|
int dh = DisplayHeight(ourdisplay,ourscreen); |
| 315 |
|
|
|
| 316 |
|
|
dw -= 25; /* for window frame */ |
| 317 |
|
|
dh -= 50; |
| 318 |
|
|
#ifdef STEREO |
| 319 |
|
|
dh /= 2; |
| 320 |
|
|
#endif |
| 321 |
|
|
odev.hres = 2.*VIEWDIST/pwidth * |
| 322 |
|
|
tan(PI/180./2.*nv->horiz); |
| 323 |
|
|
odev.vres = 2.*VIEWDIST/pheight * |
| 324 |
|
|
tan(PI/180./2.*nv->vert); |
| 325 |
|
|
if (odev.hres > dw) { |
| 326 |
|
|
odev.vres = dw * odev.vres / odev.hres; |
| 327 |
|
|
odev.hres = dw; |
| 328 |
|
|
} |
| 329 |
|
|
if (odev.vres > dh) { |
| 330 |
|
|
odev.hres = dh * odev.hres / odev.vres; |
| 331 |
|
|
odev.vres = dh; |
| 332 |
|
|
} |
| 333 |
|
|
XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); |
| 334 |
|
|
dev_input(); /* get resize event */ |
| 335 |
|
|
} |
| 336 |
|
|
copystruct(&odev.v, nv); /* setview() already called */ |
| 337 |
gwlarson |
3.14 |
viewflags |= VWCHANGE; |
| 338 |
|
|
} |
| 339 |
gwlarson |
3.1 |
#ifdef STEREO |
| 340 |
gwlarson |
3.14 |
copystruct(&vwright, nv); |
| 341 |
|
|
d = eyesepdist / sqrt(nv->hn2); |
| 342 |
|
|
VSUM(vwright.vp, nv->vp, nv->hvec, d); |
| 343 |
|
|
/* setview(&vwright); -- Unnecessary */ |
| 344 |
gwlarson |
3.1 |
#endif |
| 345 |
|
|
wipeclean(); |
| 346 |
|
|
return(1); |
| 347 |
|
|
} |
| 348 |
|
|
|
| 349 |
|
|
|
| 350 |
gwlarson |
3.2 |
dev_section(gfn, pfn) /* add octree for geometry rendering */ |
| 351 |
|
|
char *gfn, *pfn; |
| 352 |
gwlarson |
3.1 |
{ |
| 353 |
|
|
extern char *index(); |
| 354 |
|
|
char *cp; |
| 355 |
|
|
|
| 356 |
gwlarson |
3.2 |
if (gfn == NULL) { |
| 357 |
|
|
gmEndGeom(); |
| 358 |
|
|
gmEndPortal(); |
| 359 |
gwlarson |
3.4 |
wipeclean(); /* new geometry, so redraw it */ |
| 360 |
gwlarson |
3.2 |
return; |
| 361 |
|
|
} |
| 362 |
|
|
if (access(gfn, R_OK) == 0) |
| 363 |
|
|
gmNewGeom(gfn); |
| 364 |
gwlarson |
3.1 |
#ifdef DEBUG |
| 365 |
|
|
else { |
| 366 |
gwlarson |
3.2 |
sprintf(errmsg, "cannot load octree \"%s\"", gfn); |
| 367 |
gwlarson |
3.1 |
error(WARNING, errmsg); |
| 368 |
|
|
} |
| 369 |
|
|
#endif |
| 370 |
gwlarson |
3.2 |
if (pfn != NULL) |
| 371 |
|
|
gmNewPortal(pfn); |
| 372 |
gwlarson |
3.1 |
} |
| 373 |
|
|
|
| 374 |
|
|
|
| 375 |
|
|
dev_auxcom(cmd, args) /* process an auxiliary command */ |
| 376 |
|
|
char *cmd, *args; |
| 377 |
|
|
{ |
| 378 |
|
|
#ifdef DOBJ |
| 379 |
gwlarson |
3.12 |
int vischange; |
| 380 |
|
|
|
| 381 |
|
|
if ((vischange = dobj_command(cmd, args)) >= 0) { |
| 382 |
|
|
if (vischange) { |
| 383 |
|
|
imm_mode = beam_sync(1) > 0; |
| 384 |
|
|
dev_clear(); |
| 385 |
|
|
} |
| 386 |
gwlarson |
3.1 |
return; |
| 387 |
gwlarson |
3.12 |
} |
| 388 |
gwlarson |
3.1 |
#endif |
| 389 |
|
|
sprintf(errmsg, "%s: unknown command", cmd); |
| 390 |
|
|
error(COMMAND, errmsg); |
| 391 |
|
|
} |
| 392 |
|
|
|
| 393 |
|
|
|
| 394 |
|
|
VIEW * |
| 395 |
|
|
dev_auxview(n, hvres) /* return nth auxiliary view */ |
| 396 |
|
|
int n; |
| 397 |
|
|
int hvres[2]; |
| 398 |
|
|
{ |
| 399 |
|
|
hvres[0] = odev.hres; hvres[1] = odev.vres; |
| 400 |
|
|
if (n == 0) |
| 401 |
|
|
return(&odev.v); |
| 402 |
|
|
#ifdef STEREO |
| 403 |
|
|
if (n == 1) |
| 404 |
|
|
return(&vwright); |
| 405 |
|
|
#endif |
| 406 |
|
|
return(NULL); |
| 407 |
|
|
} |
| 408 |
|
|
|
| 409 |
|
|
|
| 410 |
|
|
int |
| 411 |
|
|
dev_input() /* get X11 input */ |
| 412 |
|
|
{ |
| 413 |
|
|
inpresflags = 0; |
| 414 |
|
|
|
| 415 |
|
|
do |
| 416 |
|
|
getevent(); |
| 417 |
|
|
|
| 418 |
|
|
while (XPending(ourdisplay) > 0); |
| 419 |
|
|
|
| 420 |
|
|
odev.inpready = 0; |
| 421 |
|
|
|
| 422 |
|
|
return(inpresflags); |
| 423 |
|
|
} |
| 424 |
|
|
|
| 425 |
|
|
|
| 426 |
|
|
dev_value(c, d, p) /* add a pixel value to our texture */ |
| 427 |
|
|
COLR c; |
| 428 |
|
|
FVECT d, p; |
| 429 |
|
|
{ |
| 430 |
|
|
#ifdef DOBJ |
| 431 |
|
|
if (dobj_lightsamp != NULL) { /* in light source sampling */ |
| 432 |
|
|
(*dobj_lightsamp)(c, d, p); |
| 433 |
|
|
return; |
| 434 |
|
|
} |
| 435 |
|
|
#endif |
| 436 |
|
|
odSample(c, d, p); /* add to display representation */ |
| 437 |
|
|
if (!--rayqleft) |
| 438 |
|
|
dev_flush(); /* flush output */ |
| 439 |
|
|
} |
| 440 |
|
|
|
| 441 |
|
|
|
| 442 |
|
|
int |
| 443 |
|
|
dev_flush() /* flush output as appropriate */ |
| 444 |
|
|
{ |
| 445 |
|
|
int ndrawn; |
| 446 |
|
|
|
| 447 |
gwlarson |
3.7 |
if ((viewflags&(VWMAPPED|VWPERSP)) == (VWMAPPED|VWPERSP)) { |
| 448 |
gwlarson |
3.1 |
#ifdef STEREO |
| 449 |
|
|
pushright(); /* draw right eye */ |
| 450 |
gwlarson |
3.3 |
ndrawn = gmDrawGeom(); |
| 451 |
gwlarson |
3.1 |
#ifdef DOBJ |
| 452 |
gwlarson |
3.3 |
ndrawn += dobj_render(); |
| 453 |
gwlarson |
3.1 |
#endif |
| 454 |
|
|
checkglerr("rendering right eye"); |
| 455 |
|
|
popright(); /* draw left eye */ |
| 456 |
|
|
#endif |
| 457 |
gwlarson |
3.3 |
ndrawn = gmDrawGeom(); |
| 458 |
gwlarson |
3.1 |
#ifdef DOBJ |
| 459 |
|
|
ndrawn += dobj_render(); |
| 460 |
|
|
#endif |
| 461 |
gwlarson |
3.6 |
glXSwapBuffers(ourdisplay, gwind); |
| 462 |
gwlarson |
3.1 |
checkglerr("rendering base view"); |
| 463 |
|
|
} |
| 464 |
gwlarson |
3.7 |
if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) == |
| 465 |
|
|
(VWMAPPED|VWSTEADY|VWPERSP)) { |
| 466 |
|
|
/* first time after steady */ |
| 467 |
|
|
if (ndrawn) |
| 468 |
|
|
xferdepth(); /* transfer and clear depth */ |
| 469 |
|
|
setglortho(); /* set orthographic view */ |
| 470 |
|
|
|
| 471 |
|
|
} |
| 472 |
|
|
if ((viewflags&(VWMAPPED|VWSTEADY|VWPERSP|VWORTHO)) == |
| 473 |
|
|
(VWMAPPED|VWSTEADY|VWORTHO)) { |
| 474 |
|
|
/* else update cones */ |
| 475 |
gwlarson |
3.1 |
#ifdef STEREO |
| 476 |
gwlarson |
3.7 |
pushright(); |
| 477 |
|
|
odUpdate(1); /* draw right eye */ |
| 478 |
|
|
popright(); |
| 479 |
gwlarson |
3.1 |
#endif |
| 480 |
gwlarson |
3.7 |
odUpdate(0); /* draw left eye */ |
| 481 |
|
|
glFlush(); /* flush OpenGL */ |
| 482 |
|
|
} |
| 483 |
gwlarson |
3.1 |
rayqleft = RAYQLEN; |
| 484 |
|
|
/* flush X11 and return # pending */ |
| 485 |
|
|
return(odev.inpready = XPending(ourdisplay)); |
| 486 |
|
|
} |
| 487 |
|
|
|
| 488 |
|
|
|
| 489 |
|
|
checkglerr(where) /* check for GL or GLU error */ |
| 490 |
|
|
char *where; |
| 491 |
|
|
{ |
| 492 |
|
|
register GLenum errcode; |
| 493 |
|
|
|
| 494 |
|
|
while ((errcode = glGetError()) != GL_NO_ERROR) { |
| 495 |
|
|
sprintf(errmsg, "OpenGL error %s: %s", |
| 496 |
|
|
where, gluErrorString(errcode)); |
| 497 |
|
|
error(WARNING, errmsg); |
| 498 |
|
|
} |
| 499 |
|
|
} |
| 500 |
|
|
|
| 501 |
|
|
|
| 502 |
|
|
static |
| 503 |
|
|
xferdepth() /* load and clear depth buffer */ |
| 504 |
|
|
{ |
| 505 |
|
|
register GLfloat *dbp; |
| 506 |
gwlarson |
3.9 |
register GLubyte *pbuf; |
| 507 |
gwlarson |
3.1 |
|
| 508 |
gwlarson |
3.3 |
if (depthbuffer == NULL) { /* allocate private depth buffer */ |
| 509 |
gwlarson |
3.1 |
#ifdef STEREO |
| 510 |
|
|
depthright = (GLfloat *)malloc( |
| 511 |
|
|
odev.hres*odev.vres*sizeof(GLfloat)); |
| 512 |
|
|
#endif |
| 513 |
|
|
depthbuffer = (GLfloat *)malloc( |
| 514 |
|
|
odev.hres*odev.vres*sizeof(GLfloat)); |
| 515 |
gwlarson |
3.5 |
CHECK(depthbuffer==NULL, SYSTEM, "out of memory in xferdepth"); |
| 516 |
gwlarson |
3.1 |
} |
| 517 |
gwlarson |
3.3 |
/* allocate alpha buffer for portals */ |
| 518 |
|
|
if (gmPortals) |
| 519 |
gwlarson |
3.10 |
pbuf = (GLubyte *)malloc(odev.hres*odev.vres*sizeof(GLubyte)); |
| 520 |
gwlarson |
3.3 |
else |
| 521 |
gwlarson |
3.9 |
pbuf = NULL; |
| 522 |
gwlarson |
3.1 |
#ifdef STEREO |
| 523 |
gwlarson |
3.12 |
pushright(); |
| 524 |
gwlarson |
3.1 |
glReadPixels(0, 0, odev.hres, odev.vres, |
| 525 |
|
|
GL_DEPTH_COMPONENT, GL_FLOAT, depthright); |
| 526 |
gwlarson |
3.9 |
if (pbuf != NULL) { |
| 527 |
|
|
glClear(GL_COLOR_BUFFER_BIT); |
| 528 |
|
|
gmDrawPortals(0xff, -1, -1, -1); |
| 529 |
gwlarson |
3.3 |
glReadPixels(0, 0, odev.hres, odev.vres, |
| 530 |
gwlarson |
3.10 |
GL_RED, GL_UNSIGNED_BYTE, pbuf); |
| 531 |
gwlarson |
3.9 |
} |
| 532 |
gwlarson |
3.3 |
for (dbp = depthright + odev.hres*odev.vres; dbp-- > depthright; ) |
| 533 |
gwlarson |
3.10 |
if (pbuf != NULL && pbuf[dbp-depthright]&0x40) |
| 534 |
gwlarson |
3.3 |
*dbp = FHUGE; |
| 535 |
|
|
else |
| 536 |
|
|
*dbp = mapdepth(*dbp); |
| 537 |
|
|
glClear(GL_DEPTH_BUFFER_BIT); |
| 538 |
gwlarson |
3.1 |
odDepthMap(1, depthright); |
| 539 |
gwlarson |
3.12 |
popright(); |
| 540 |
gwlarson |
3.1 |
#endif |
| 541 |
gwlarson |
3.2 |
/* read back depth buffer */ |
| 542 |
gwlarson |
3.1 |
glReadPixels(0, 0, odev.hres, odev.vres, |
| 543 |
|
|
GL_DEPTH_COMPONENT, GL_FLOAT, depthbuffer); |
| 544 |
gwlarson |
3.9 |
if (pbuf != NULL) { |
| 545 |
|
|
glClear(GL_COLOR_BUFFER_BIT); /* find portals */ |
| 546 |
|
|
gmDrawPortals(0xff, -1, -1, -1); |
| 547 |
gwlarson |
3.2 |
glReadPixels(0, 0, odev.hres, odev.vres, |
| 548 |
gwlarson |
3.10 |
GL_RED, GL_UNSIGNED_BYTE, pbuf); |
| 549 |
|
|
#ifdef DEBUG |
| 550 |
|
|
glXSwapBuffers(ourdisplay, gwind); |
| 551 |
|
|
#endif |
| 552 |
gwlarson |
3.9 |
} |
| 553 |
gwlarson |
3.1 |
for (dbp = depthbuffer + odev.hres*odev.vres; dbp-- > depthbuffer; ) |
| 554 |
gwlarson |
3.10 |
if (pbuf != NULL && pbuf[dbp-depthbuffer]&0x40) |
| 555 |
gwlarson |
3.2 |
*dbp = FHUGE; |
| 556 |
|
|
else |
| 557 |
|
|
*dbp = mapdepth(*dbp); |
| 558 |
gwlarson |
3.3 |
glClear(GL_DEPTH_BUFFER_BIT); /* clear system depth buffer */ |
| 559 |
gwlarson |
3.1 |
odDepthMap(0, depthbuffer); /* transfer depth data */ |
| 560 |
gwlarson |
3.9 |
if (pbuf != NULL) |
| 561 |
|
|
free((char *)pbuf); /* free our portal buffer */ |
| 562 |
gwlarson |
3.1 |
} |
| 563 |
|
|
|
| 564 |
|
|
|
| 565 |
gwlarson |
3.2 |
static |
| 566 |
|
|
freedepth() /* free recorded depth buffer */ |
| 567 |
|
|
{ |
| 568 |
|
|
if (depthbuffer == NULL) |
| 569 |
|
|
return; |
| 570 |
|
|
#ifdef STEREO |
| 571 |
|
|
odDepthMap(1, NULL); |
| 572 |
|
|
free((char *)depthright); |
| 573 |
|
|
depthright = NULL; |
| 574 |
|
|
#endif |
| 575 |
|
|
odDepthMap(0, NULL); |
| 576 |
|
|
free((char *)depthbuffer); |
| 577 |
|
|
depthbuffer = NULL; |
| 578 |
|
|
} |
| 579 |
|
|
|
| 580 |
|
|
|
| 581 |
gwlarson |
3.1 |
static double |
| 582 |
|
|
getdistance(dx, dy, direc) /* distance from fore plane along view ray */ |
| 583 |
|
|
int dx, dy; |
| 584 |
|
|
FVECT direc; |
| 585 |
|
|
{ |
| 586 |
|
|
GLfloat gldepth; |
| 587 |
|
|
double dist; |
| 588 |
|
|
|
| 589 |
|
|
if (dx<0 | dx>=odev.hres | dy<0 | dy>=odev.vres) |
| 590 |
|
|
return(FHUGE); |
| 591 |
|
|
if (depthbuffer != NULL) |
| 592 |
|
|
dist = depthbuffer[dy*odev.hres + dx]; |
| 593 |
|
|
else { |
| 594 |
|
|
glReadPixels(dx,dy, 1,1, GL_DEPTH_COMPONENT, |
| 595 |
|
|
GL_FLOAT, &gldepth); |
| 596 |
gwlarson |
3.3 |
dist = mapdepth(gldepth); |
| 597 |
gwlarson |
3.1 |
} |
| 598 |
|
|
if (dist >= .99*FHUGE) |
| 599 |
|
|
return(FHUGE); |
| 600 |
|
|
return((dist-odev.v.vfore)/DOT(direc,odev.v.vdir)); |
| 601 |
|
|
} |
| 602 |
|
|
|
| 603 |
|
|
|
| 604 |
|
|
#ifdef STEREO |
| 605 |
|
|
static |
| 606 |
|
|
pushright() /* push on right view & buffer */ |
| 607 |
|
|
{ |
| 608 |
|
|
double d; |
| 609 |
|
|
|
| 610 |
|
|
setstereobuf(STEREO_BUFFER_RIGHT); |
| 611 |
gwlarson |
3.7 |
if (viewflags & VWPERSP) { |
| 612 |
gwlarson |
3.1 |
glMatrixMode(GL_MODELVIEW); |
| 613 |
|
|
glPushMatrix(); |
| 614 |
|
|
d = -eyesepdist / sqrt(odev.v.hn2); |
| 615 |
|
|
glTranslated(d*odev.v.hvec[0], d*odev.v.hvec[1], |
| 616 |
|
|
d*odev.v.hvec[2]); |
| 617 |
|
|
checkglerr("setting right view"); |
| 618 |
|
|
} |
| 619 |
|
|
} |
| 620 |
|
|
|
| 621 |
|
|
|
| 622 |
|
|
static |
| 623 |
|
|
popright() /* pop off right view & buffer */ |
| 624 |
|
|
{ |
| 625 |
gwlarson |
3.7 |
if (viewflags & VWPERSP) { |
| 626 |
gwlarson |
3.1 |
glMatrixMode(GL_MODELVIEW); |
| 627 |
|
|
glPopMatrix(); |
| 628 |
|
|
} |
| 629 |
|
|
setstereobuf(STEREO_BUFFER_LEFT); |
| 630 |
|
|
} |
| 631 |
|
|
#endif |
| 632 |
|
|
|
| 633 |
|
|
|
| 634 |
|
|
static int |
| 635 |
|
|
mytmflags() /* figure out tone mapping flags */ |
| 636 |
|
|
{ |
| 637 |
|
|
extern char *progname; |
| 638 |
|
|
register char *cp, *tail; |
| 639 |
|
|
/* find basic name */ |
| 640 |
|
|
for (cp = tail = progname; *cp; cp++) |
| 641 |
|
|
if (*cp == '/') |
| 642 |
|
|
tail = cp+1; |
| 643 |
|
|
for (cp = tail; *cp && *cp != '.'; cp++) |
| 644 |
|
|
; |
| 645 |
|
|
#ifdef DEBUG |
| 646 |
|
|
if (cp > tail && cp[-1] == 'h') |
| 647 |
|
|
return(TM_F_HUMAN); |
| 648 |
|
|
else |
| 649 |
|
|
return(TM_F_CAMERA); |
| 650 |
|
|
#else |
| 651 |
|
|
if (cp > tail && cp[-1] == 'h') |
| 652 |
|
|
return(TM_F_HUMAN|TM_F_NOSTDERR); |
| 653 |
|
|
else |
| 654 |
|
|
return(TM_F_CAMERA|TM_F_NOSTDERR); |
| 655 |
|
|
#endif |
| 656 |
|
|
} |
| 657 |
|
|
|
| 658 |
|
|
|
| 659 |
|
|
static |
| 660 |
|
|
getevent() /* get next event */ |
| 661 |
|
|
{ |
| 662 |
|
|
XNextEvent(ourdisplay, levptr(XEvent)); |
| 663 |
|
|
switch (levptr(XEvent)->type) { |
| 664 |
|
|
case ConfigureNotify: |
| 665 |
|
|
resizewindow(levptr(XConfigureEvent)); |
| 666 |
|
|
break; |
| 667 |
|
|
case UnmapNotify: |
| 668 |
gwlarson |
3.7 |
viewflags &= ~VWMAPPED; |
| 669 |
gwlarson |
3.1 |
break; |
| 670 |
|
|
case MapNotify: |
| 671 |
gwlarson |
3.2 |
odRemap(0); |
| 672 |
gwlarson |
3.7 |
viewflags |= VWMAPPED; |
| 673 |
gwlarson |
3.1 |
break; |
| 674 |
|
|
case Expose: |
| 675 |
|
|
fixwindow(levptr(XExposeEvent)); |
| 676 |
|
|
break; |
| 677 |
|
|
case KeyPress: |
| 678 |
|
|
getkey(levptr(XKeyPressedEvent)); |
| 679 |
|
|
break; |
| 680 |
|
|
case ButtonPress: |
| 681 |
|
|
if (FRAMESTATE(levptr(XButtonPressedEvent)->state)) |
| 682 |
|
|
getframe(levptr(XButtonPressedEvent)); |
| 683 |
|
|
else |
| 684 |
|
|
getmove(levptr(XButtonPressedEvent)); |
| 685 |
|
|
break; |
| 686 |
|
|
} |
| 687 |
|
|
} |
| 688 |
|
|
|
| 689 |
|
|
|
| 690 |
|
|
static |
| 691 |
|
|
draw3dline(wp) /* draw 3d line in world coordinates */ |
| 692 |
|
|
register FVECT wp[2]; |
| 693 |
|
|
{ |
| 694 |
|
|
glVertex3d(wp[0][0], wp[0][1], wp[0][2]); |
| 695 |
|
|
glVertex3d(wp[1][0], wp[1][1], wp[1][2]); |
| 696 |
|
|
} |
| 697 |
|
|
|
| 698 |
|
|
|
| 699 |
|
|
static |
| 700 |
|
|
draw_grids(fore) /* draw holodeck section grids */ |
| 701 |
|
|
int fore; |
| 702 |
|
|
{ |
| 703 |
|
|
glPushAttrib(GL_LIGHTING_BIT|GL_ENABLE_BIT); |
| 704 |
|
|
glDisable(GL_LIGHTING); |
| 705 |
|
|
if (fore) |
| 706 |
gwlarson |
3.8 |
glColor3ub(4, 250, 250); |
| 707 |
gwlarson |
3.1 |
else |
| 708 |
|
|
glColor3ub(0, 0, 0); |
| 709 |
|
|
glBegin(GL_LINES); /* draw each grid line */ |
| 710 |
|
|
gridlines(draw3dline); |
| 711 |
|
|
glEnd(); |
| 712 |
|
|
checkglerr("drawing grid lines"); |
| 713 |
|
|
glPopAttrib(); |
| 714 |
|
|
} |
| 715 |
|
|
|
| 716 |
|
|
|
| 717 |
|
|
static |
| 718 |
|
|
moveview(dx, dy, mov, orb) /* move our view */ |
| 719 |
|
|
int dx, dy, mov, orb; |
| 720 |
|
|
{ |
| 721 |
|
|
VIEW nv; |
| 722 |
|
|
FVECT odir, v1, wip; |
| 723 |
|
|
double d, d1; |
| 724 |
|
|
register int li; |
| 725 |
|
|
/* start with old view */ |
| 726 |
|
|
copystruct(&nv, &odev.v); |
| 727 |
|
|
/* orient our motion */ |
| 728 |
|
|
if (viewray(v1, odir, &odev.v, |
| 729 |
|
|
(dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) |
| 730 |
|
|
return(0); /* outside view */ |
| 731 |
|
|
if (mov | orb) { /* moving relative to geometry */ |
| 732 |
|
|
d = getdistance(dx, dy, odir); /* distance from front plane */ |
| 733 |
|
|
#ifdef DOBJ |
| 734 |
|
|
d1 = dobj_trace(NULL, v1, odir); |
| 735 |
|
|
if (d1 < d) |
| 736 |
|
|
d = d1; |
| 737 |
|
|
#endif |
| 738 |
|
|
if (d >= .99*FHUGE) |
| 739 |
|
|
d = 0.5*(dev_zmax+dev_zmin); /* just guess */ |
| 740 |
|
|
VSUM(wip, v1, odir, d); |
| 741 |
|
|
VSUB(odir, wip, odev.v.vp); |
| 742 |
|
|
} else /* panning with constant viewpoint */ |
| 743 |
|
|
VCOPY(nv.vdir, odir); |
| 744 |
|
|
if (orb && mov) { /* orbit left/right */ |
| 745 |
|
|
spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); |
| 746 |
|
|
VSUM(nv.vp, wip, odir, -1.); |
| 747 |
|
|
spinvector(nv.vdir, nv.vdir, nv.vup, d); |
| 748 |
|
|
} else if (orb) { /* orbit up/down */ |
| 749 |
|
|
fcross(v1, odir, nv.vup); |
| 750 |
|
|
if (normalize(v1) == 0.) |
| 751 |
|
|
return(0); |
| 752 |
|
|
spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); |
| 753 |
|
|
VSUM(nv.vp, wip, odir, -1.); |
| 754 |
|
|
spinvector(nv.vdir, nv.vdir, v1, d); |
| 755 |
|
|
} else if (mov) { /* move forward/backward */ |
| 756 |
|
|
d = MOVPCT/100. * mov; |
| 757 |
|
|
VSUM(nv.vp, nv.vp, odir, d); |
| 758 |
|
|
} |
| 759 |
gwlarson |
3.7 |
if (!mov ^ !orb && viewflags&VWHEADLOCK) { /* restore height */ |
| 760 |
gwlarson |
3.1 |
VSUM(v1, odev.v.vp, nv.vp, -1.); |
| 761 |
|
|
d = DOT(v1, nv.vup); |
| 762 |
|
|
VSUM(nv.vp, nv.vp, odev.v.vup, d); |
| 763 |
|
|
} |
| 764 |
|
|
if (setview(&nv) != NULL) |
| 765 |
|
|
return(0); /* illegal view */ |
| 766 |
|
|
dev_view(&nv); |
| 767 |
gwlarson |
3.2 |
inpresflags |= DFL(DC_SETVIEW); |
| 768 |
gwlarson |
3.1 |
return(1); |
| 769 |
|
|
} |
| 770 |
|
|
|
| 771 |
|
|
|
| 772 |
|
|
static |
| 773 |
|
|
getframe(ebut) /* get focus frame */ |
| 774 |
|
|
XButtonPressedEvent *ebut; |
| 775 |
|
|
{ |
| 776 |
|
|
int startx = ebut->x, starty = ebut->y; |
| 777 |
|
|
int endx, endy; |
| 778 |
|
|
|
| 779 |
|
|
XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent)); |
| 780 |
|
|
endx = levptr(XButtonReleasedEvent)->x; |
| 781 |
|
|
endy = levptr(XButtonReleasedEvent)->y; |
| 782 |
|
|
if (endx == startx | endy == starty) { |
| 783 |
|
|
XBell(ourdisplay, 0); |
| 784 |
|
|
return; |
| 785 |
|
|
} |
| 786 |
|
|
if (endx < startx) {register int c = endx; endx = startx; startx = c;} |
| 787 |
|
|
if (endy < starty) {register int c = endy; endy = starty; starty = c;} |
| 788 |
|
|
sprintf(odev_args, "%.3f %.3f %.3f %.3f", |
| 789 |
|
|
(startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres, |
| 790 |
|
|
(endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres); |
| 791 |
|
|
inpresflags |= DFL(DC_FOCUS); |
| 792 |
|
|
} |
| 793 |
|
|
|
| 794 |
|
|
|
| 795 |
|
|
static |
| 796 |
|
|
getmove(ebut) /* get view change */ |
| 797 |
|
|
XButtonPressedEvent *ebut; |
| 798 |
|
|
{ |
| 799 |
|
|
int movdir = MOVDIR(ebut->button); |
| 800 |
|
|
int movorb = MOVORB(ebut->state); |
| 801 |
gwlarson |
3.2 |
int ndrawn; |
| 802 |
gwlarson |
3.1 |
Window rootw, childw; |
| 803 |
|
|
int rootx, rooty, wx, wy; |
| 804 |
|
|
unsigned int statemask; |
| 805 |
|
|
|
| 806 |
|
|
XNoOp(ourdisplay); /* makes sure we're not idle */ |
| 807 |
|
|
|
| 808 |
gwlarson |
3.7 |
viewflags &= ~VWSTEADY; /* flag moving view */ |
| 809 |
gwlarson |
3.11 |
setglpersp(); /* start us off in perspective */ |
| 810 |
gwlarson |
3.1 |
while (!XCheckMaskEvent(ourdisplay, |
| 811 |
|
|
ButtonReleaseMask, levptr(XEvent))) { |
| 812 |
|
|
/* get cursor position */ |
| 813 |
|
|
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
| 814 |
|
|
&rootx, &rooty, &wx, &wy, &statemask)) |
| 815 |
|
|
break; /* on another screen */ |
| 816 |
|
|
/* compute view motion */ |
| 817 |
|
|
if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { |
| 818 |
|
|
sleep(1); |
| 819 |
|
|
continue; /* cursor in bad place */ |
| 820 |
|
|
} |
| 821 |
|
|
draw_grids(1); /* redraw grid */ |
| 822 |
|
|
#ifdef STEREO |
| 823 |
|
|
pushright(); |
| 824 |
|
|
draw_grids(1); |
| 825 |
gwlarson |
3.3 |
ndrawn = gmDrawGeom(); |
| 826 |
gwlarson |
3.1 |
#ifdef DOBJ |
| 827 |
gwlarson |
3.3 |
ndrawn += dobj_render(); |
| 828 |
gwlarson |
3.1 |
#endif |
| 829 |
|
|
popright(); |
| 830 |
|
|
#endif |
| 831 |
|
|
/* redraw octrees */ |
| 832 |
gwlarson |
3.3 |
ndrawn = gmDrawGeom(); |
| 833 |
gwlarson |
3.1 |
#ifdef DOBJ |
| 834 |
gwlarson |
3.2 |
ndrawn += dobj_render(); /* redraw objects */ |
| 835 |
gwlarson |
3.1 |
#endif |
| 836 |
gwlarson |
3.6 |
glXSwapBuffers(ourdisplay, gwind); |
| 837 |
|
|
if (!ndrawn) |
| 838 |
gwlarson |
3.2 |
sleep(1); /* for reasonable interaction */ |
| 839 |
gwlarson |
3.1 |
} |
| 840 |
|
|
if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ |
| 841 |
|
|
movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); |
| 842 |
|
|
wx = levptr(XButtonReleasedEvent)->x; |
| 843 |
|
|
wy = levptr(XButtonReleasedEvent)->y; |
| 844 |
|
|
moveview(wx, odev.vres-1-wy, movdir, movorb); |
| 845 |
|
|
} |
| 846 |
gwlarson |
3.7 |
viewflags |= VWSTEADY; /* done goofing around */ |
| 847 |
gwlarson |
3.1 |
} |
| 848 |
|
|
|
| 849 |
|
|
|
| 850 |
|
|
static |
| 851 |
gwlarson |
3.11 |
setglpersp() /* set perspective view in GL */ |
| 852 |
gwlarson |
3.1 |
{ |
| 853 |
|
|
double d, xmin, xmax, ymin, ymax; |
| 854 |
|
|
GLfloat vec[4]; |
| 855 |
|
|
double depthlim[2]; |
| 856 |
|
|
/* set depth limits */ |
| 857 |
gwlarson |
3.2 |
gmDepthLimit(depthlim, odev.v.vp, odev.v.vdir); |
| 858 |
gwlarson |
3.1 |
if (depthlim[0] >= depthlim[1]) { |
| 859 |
|
|
dev_zmin = 1.; |
| 860 |
|
|
dev_zmax = 100.; |
| 861 |
|
|
} else { |
| 862 |
|
|
dev_zmin = 0.5*depthlim[0]; |
| 863 |
gwlarson |
3.13 |
dev_zmax = 1.25*depthlim[1]; |
| 864 |
gwlarson |
3.1 |
if (dev_zmin > dev_zmax/5.) |
| 865 |
|
|
dev_zmin = dev_zmax/5.; |
| 866 |
|
|
} |
| 867 |
|
|
if (odev.v.vfore > FTINY) |
| 868 |
|
|
dev_zmin = odev.v.vfore; |
| 869 |
|
|
if (odev.v.vaft > FTINY) |
| 870 |
|
|
dev_zmax = odev.v.vaft; |
| 871 |
|
|
if (dev_zmin < dev_zmax/100.) |
| 872 |
|
|
dev_zmin = dev_zmax/100.; |
| 873 |
|
|
setzrat(); |
| 874 |
|
|
xmax = dev_zmin * tan(PI/180./2. * odev.v.horiz); |
| 875 |
|
|
xmin = -xmax; |
| 876 |
|
|
d = odev.v.hoff * (xmax - xmin); |
| 877 |
|
|
xmin += d; xmax += d; |
| 878 |
|
|
ymax = dev_zmin * tan(PI/180./2. * odev.v.vert); |
| 879 |
|
|
ymin = -ymax; |
| 880 |
|
|
d = odev.v.voff * (ymax - ymin); |
| 881 |
|
|
ymin += d; ymax += d; |
| 882 |
|
|
/* set view matrix */ |
| 883 |
|
|
glMatrixMode(GL_PROJECTION); |
| 884 |
|
|
glLoadIdentity(); |
| 885 |
|
|
glFrustum(xmin, xmax, ymin, ymax, dev_zmin, dev_zmax); |
| 886 |
|
|
gluLookAt(odev.v.vp[0], odev.v.vp[1], odev.v.vp[2], |
| 887 |
|
|
odev.v.vp[0] + odev.v.vdir[0], |
| 888 |
|
|
odev.v.vp[1] + odev.v.vdir[1], |
| 889 |
|
|
odev.v.vp[2] + odev.v.vdir[2], |
| 890 |
|
|
odev.v.vup[0], odev.v.vup[1], odev.v.vup[2]); |
| 891 |
|
|
checkglerr("setting perspective view"); |
| 892 |
|
|
vec[0] = vec[1] = vec[2] = 0.; vec[3] = 1.; |
| 893 |
|
|
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vec); |
| 894 |
|
|
vec[0] = -odev.v.vdir[0]; |
| 895 |
|
|
vec[1] = -odev.v.vdir[1]; |
| 896 |
|
|
vec[2] = -odev.v.vdir[2]; |
| 897 |
|
|
vec[3] = 0.; |
| 898 |
|
|
glLightfv(GL_LIGHT0, GL_POSITION, vec); |
| 899 |
|
|
vec[0] = vec[1] = vec[2] = .7; vec[3] = 1.; |
| 900 |
|
|
glLightfv(GL_LIGHT0, GL_SPECULAR, vec); |
| 901 |
|
|
glLightfv(GL_LIGHT0, GL_DIFFUSE, vec); |
| 902 |
|
|
vec[0] = vec[1] = vec[2] = .3; vec[3] = 1.; |
| 903 |
|
|
glLightfv(GL_LIGHT0, GL_AMBIENT, vec); |
| 904 |
|
|
glEnable(GL_LIGHT0); |
| 905 |
|
|
glEnable(GL_LIGHTING); /* light our GL objects */ |
| 906 |
|
|
glShadeModel(GL_SMOOTH); |
| 907 |
gwlarson |
3.7 |
viewflags &= ~VWORTHO; |
| 908 |
|
|
viewflags |= VWPERSP; |
| 909 |
gwlarson |
3.1 |
} |
| 910 |
|
|
|
| 911 |
|
|
|
| 912 |
|
|
static |
| 913 |
|
|
setglortho() /* set up orthographic view for cone drawing */ |
| 914 |
|
|
{ |
| 915 |
gwlarson |
3.6 |
glDrawBuffer(GL_FRONT); /* use single-buffer mode */ |
| 916 |
gwlarson |
3.1 |
/* set view matrix */ |
| 917 |
|
|
glMatrixMode(GL_PROJECTION); |
| 918 |
|
|
glLoadIdentity(); |
| 919 |
|
|
glOrtho(0., (double)odev.hres, 0., (double)odev.vres, |
| 920 |
|
|
0.001*OMAXDEPTH, 1.001*(-OMAXDEPTH)); |
| 921 |
|
|
checkglerr("setting orthographic view"); |
| 922 |
|
|
glDisable(GL_LIGHTING); /* cones are constant color */ |
| 923 |
|
|
glShadeModel(GL_FLAT); |
| 924 |
gwlarson |
3.7 |
viewflags &= ~VWPERSP; |
| 925 |
|
|
viewflags |= VWORTHO; |
| 926 |
gwlarson |
3.1 |
} |
| 927 |
|
|
|
| 928 |
|
|
|
| 929 |
|
|
static |
| 930 |
|
|
wipeclean() /* prepare for redraw */ |
| 931 |
|
|
{ |
| 932 |
gwlarson |
3.6 |
glDrawBuffer(GL_BACK); /* use double-buffer mode */ |
| 933 |
gwlarson |
3.9 |
glReadBuffer(GL_BACK); |
| 934 |
gwlarson |
3.6 |
/* clear buffers */ |
| 935 |
gwlarson |
3.1 |
#ifdef STEREO |
| 936 |
|
|
setstereobuf(STEREO_BUFFER_RIGHT); |
| 937 |
gwlarson |
3.6 |
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
| 938 |
gwlarson |
3.1 |
setstereobuf(STEREO_BUFFER_LEFT); |
| 939 |
|
|
#endif |
| 940 |
gwlarson |
3.6 |
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
| 941 |
|
|
freedepth(); |
| 942 |
gwlarson |
3.7 |
if ((viewflags&(VWCHANGE|VWSTEADY)) == |
| 943 |
|
|
(VWCHANGE|VWSTEADY)) { /* clear samples if new */ |
| 944 |
gwlarson |
3.1 |
odClean(); |
| 945 |
gwlarson |
3.7 |
viewflags &= ~VWCHANGE; /* change noted */ |
| 946 |
gwlarson |
3.10 |
} else if (viewflags & VWSTEADY) |
| 947 |
|
|
odRedrawAll(); |
| 948 |
gwlarson |
3.1 |
setglpersp(&odev.v); /* reset view & clipping planes */ |
| 949 |
|
|
} |
| 950 |
|
|
|
| 951 |
|
|
|
| 952 |
|
|
static |
| 953 |
|
|
getkey(ekey) /* get input key */ |
| 954 |
|
|
register XKeyPressedEvent *ekey; |
| 955 |
|
|
{ |
| 956 |
|
|
Window rootw, childw; |
| 957 |
|
|
int rootx, rooty, wx, wy; |
| 958 |
|
|
unsigned int statemask; |
| 959 |
|
|
int n; |
| 960 |
|
|
char buf[8]; |
| 961 |
|
|
|
| 962 |
|
|
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); |
| 963 |
|
|
if (n != 1) |
| 964 |
|
|
return; |
| 965 |
|
|
switch (buf[0]) { |
| 966 |
|
|
case 'h': /* turn on height motion lock */ |
| 967 |
gwlarson |
3.7 |
viewflags |= VWHEADLOCK; |
| 968 |
gwlarson |
3.1 |
return; |
| 969 |
|
|
case 'H': /* turn off height motion lock */ |
| 970 |
gwlarson |
3.7 |
viewflags &= ~VWHEADLOCK; |
| 971 |
gwlarson |
3.1 |
return; |
| 972 |
|
|
case 'l': /* retrieve last view */ |
| 973 |
|
|
inpresflags |= DFL(DC_LASTVIEW); |
| 974 |
|
|
return; |
| 975 |
|
|
case 'p': /* pause computation */ |
| 976 |
|
|
inpresflags |= DFL(DC_PAUSE); |
| 977 |
|
|
return; |
| 978 |
|
|
case 'v': /* spit out view */ |
| 979 |
|
|
inpresflags |= DFL(DC_GETVIEW); |
| 980 |
|
|
return; |
| 981 |
|
|
case 'f': /* frame view position */ |
| 982 |
|
|
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
| 983 |
|
|
&rootx, &rooty, &wx, &wy, &statemask)) |
| 984 |
|
|
return; /* on another screen */ |
| 985 |
|
|
sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres, |
| 986 |
|
|
1.-(wy+.5)/odev.vres); |
| 987 |
|
|
inpresflags |= DFL(DC_FOCUS); |
| 988 |
|
|
return; |
| 989 |
|
|
case 'F': /* unfocus */ |
| 990 |
|
|
odev_args[0] = '\0'; |
| 991 |
|
|
inpresflags |= DFL(DC_FOCUS); |
| 992 |
|
|
return; |
| 993 |
|
|
case '\n': |
| 994 |
|
|
case '\r': /* resume computation */ |
| 995 |
|
|
inpresflags |= DFL(DC_RESUME); |
| 996 |
|
|
return; |
| 997 |
|
|
case CTRL('R'): /* redraw screen */ |
| 998 |
gwlarson |
3.10 |
odRemap(0); /* new tone mapping */ |
| 999 |
gwlarson |
3.6 |
glClear(GL_DEPTH_BUFFER_BIT); |
| 1000 |
gwlarson |
3.1 |
#ifdef STEREO |
| 1001 |
|
|
setstereobuf(STEREO_BUFFER_RIGHT); |
| 1002 |
gwlarson |
3.6 |
glClear(GL_DEPTH_BUFFER_BIT); |
| 1003 |
gwlarson |
3.1 |
setstereobuf(STEREO_BUFFER_LEFT); |
| 1004 |
|
|
#endif |
| 1005 |
|
|
return; |
| 1006 |
|
|
case CTRL('L'): /* refresh from server */ |
| 1007 |
|
|
if (inpresflags & DFL(DC_REDRAW)) |
| 1008 |
gwlarson |
3.12 |
return; /* already called */ |
| 1009 |
gwlarson |
3.1 |
XRaiseWindow(ourdisplay, gwind); |
| 1010 |
gwlarson |
3.11 |
XFlush(ourdisplay); /* raise up window */ |
| 1011 |
gwlarson |
3.10 |
sleep(1); /* wait for restacking */ |
| 1012 |
gwlarson |
3.11 |
dev_clear(); /* clear buffer and samples */ |
| 1013 |
gwlarson |
3.10 |
odRemap(1); /* start fresh histogram */ |
| 1014 |
gwlarson |
3.1 |
inpresflags |= DFL(DC_REDRAW); /* resend values from server */ |
| 1015 |
|
|
return; |
| 1016 |
|
|
case 'K': /* kill rtrace process(es) */ |
| 1017 |
|
|
inpresflags |= DFL(DC_KILL); |
| 1018 |
|
|
break; |
| 1019 |
|
|
case 'R': /* restart rtrace */ |
| 1020 |
|
|
inpresflags |= DFL(DC_RESTART); |
| 1021 |
|
|
break; |
| 1022 |
|
|
case 'C': /* clobber holodeck */ |
| 1023 |
|
|
inpresflags |= DFL(DC_CLOBBER); |
| 1024 |
|
|
break; |
| 1025 |
|
|
case 'q': /* quit the program */ |
| 1026 |
|
|
inpresflags |= DFL(DC_QUIT); |
| 1027 |
|
|
return; |
| 1028 |
|
|
default: |
| 1029 |
|
|
XBell(ourdisplay, 0); |
| 1030 |
|
|
return; |
| 1031 |
|
|
} |
| 1032 |
|
|
} |
| 1033 |
|
|
|
| 1034 |
|
|
|
| 1035 |
|
|
static |
| 1036 |
|
|
fixwindow(eexp) /* repair damage to window */ |
| 1037 |
|
|
register XExposeEvent *eexp; |
| 1038 |
|
|
{ |
| 1039 |
|
|
int xmin, ymin, xmax, ymax; |
| 1040 |
|
|
|
| 1041 |
|
|
if (odev.hres == 0 | odev.vres == 0) { /* first exposure */ |
| 1042 |
|
|
resizewindow((XConfigureEvent *)eexp); |
| 1043 |
|
|
return; |
| 1044 |
|
|
} |
| 1045 |
|
|
xmin = eexp->x; xmax = eexp->x + eexp->width; |
| 1046 |
|
|
ymin = odev.vres - eexp->y - eexp->height; ymax = odev.vres - eexp->y; |
| 1047 |
gwlarson |
3.10 |
|
| 1048 |
|
|
if (xmin <= 0 && xmax >= odev.hres-1 && |
| 1049 |
|
|
ymin <= 0 && ymax >= odev.vres) { |
| 1050 |
|
|
DCHECK(eexp->count, WARNING, "multiple clear in fixwindow"); |
| 1051 |
gwlarson |
3.11 |
wipeclean(); /* make sure we're go */ |
| 1052 |
gwlarson |
3.10 |
return; |
| 1053 |
|
|
} |
| 1054 |
gwlarson |
3.1 |
/* clear portion of depth */ |
| 1055 |
gwlarson |
3.2 |
glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); |
| 1056 |
gwlarson |
3.1 |
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
| 1057 |
|
|
glDepthFunc(GL_ALWAYS); |
| 1058 |
|
|
glBegin(GL_POLYGON); |
| 1059 |
|
|
glVertex3i(xmin, ymin, OMAXDEPTH); |
| 1060 |
|
|
glVertex3i(xmax, ymin, OMAXDEPTH); |
| 1061 |
|
|
glVertex3i(xmax, ymax, OMAXDEPTH); |
| 1062 |
|
|
glVertex3i(xmin, ymax, OMAXDEPTH); |
| 1063 |
|
|
glEnd(); |
| 1064 |
|
|
#ifdef STEREO |
| 1065 |
|
|
setstereobuf(STEREO_BUFFER_RIGHT); |
| 1066 |
|
|
glBegin(GL_POLYGON); |
| 1067 |
|
|
glVertex3i(xmin, ymin, OMAXDEPTH); |
| 1068 |
|
|
glVertex3i(xmax, ymin, OMAXDEPTH); |
| 1069 |
|
|
glVertex3i(xmax, ymax, OMAXDEPTH); |
| 1070 |
|
|
glVertex3i(xmin, ymax, OMAXDEPTH); |
| 1071 |
|
|
glEnd(); |
| 1072 |
|
|
odRedraw(1, xmin, ymin, xmax, ymax); |
| 1073 |
|
|
setstereobuf(STEREO_BUFFER_LEFT); |
| 1074 |
|
|
#endif |
| 1075 |
gwlarson |
3.2 |
glPopAttrib(); |
| 1076 |
gwlarson |
3.1 |
odRedraw(0, xmin, ymin, xmax, ymax); |
| 1077 |
|
|
} |
| 1078 |
|
|
|
| 1079 |
|
|
|
| 1080 |
|
|
static |
| 1081 |
|
|
resizewindow(ersz) /* resize window */ |
| 1082 |
|
|
register XConfigureEvent *ersz; |
| 1083 |
|
|
{ |
| 1084 |
|
|
glViewport(0, 0, ersz->width, ersz->height); |
| 1085 |
|
|
|
| 1086 |
|
|
if (ersz->width == odev.hres && ersz->height == odev.vres) |
| 1087 |
|
|
return; |
| 1088 |
|
|
|
| 1089 |
|
|
odev.hres = ersz->width; |
| 1090 |
|
|
odev.vres = ersz->height; |
| 1091 |
|
|
|
| 1092 |
|
|
odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); |
| 1093 |
|
|
odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); |
| 1094 |
|
|
|
| 1095 |
gwlarson |
3.2 |
inpresflags |= DFL(DC_SETVIEW); |
| 1096 |
gwlarson |
3.7 |
viewflags |= VWCHANGE; |
| 1097 |
gwlarson |
3.1 |
} |