| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: xdebugamb.c,v 3.1 2003/02/22 02:07:28 greg Exp $";
|
| 3 |
#endif
|
| 4 |
---
|
| 5 |
> static char SCCSid[] = "@(#)ambient.c 2.29 5/2/95 LBL";
|
| 6 |
68c68
|
| 7 |
< #define tracktime (shm_boundary == NULL || ambfp == NULL)
|
| 8 |
---
|
| 9 |
> #define tracktime 0
|
| 10 |
249a250
|
| 11 |
> printf("check %e %e %e\n", av->pos[0], av->pos[1], av->pos[2]);
|
| 12 |
289a291
|
| 13 |
> printf("use %e %e %e\n", av->pos[0], av->pos[1], av->pos[2]);
|
| 14 |
508a511
|
| 15 |
> printf("insert %e %e %e\n", av->pos[0], av->pos[1], av->pos[2]);
|
| 16 |
*/
|
| 17 |
/*
|
| 18 |
* Display an image and watch the rays get traced.
|
| 19 |
*
|
| 20 |
* 9/21/90 Greg Ward
|
| 21 |
*/
|
| 22 |
|
| 23 |
#include "standard.h"
|
| 24 |
#include "view.h"
|
| 25 |
#include <time.h>
|
| 26 |
#include "resolu.h"
|
| 27 |
#include <ctype.h>
|
| 28 |
#include <X11/Xlib.h>
|
| 29 |
|
| 30 |
#define MAXDEPTH 32 /* ridiculous ray tree depth */
|
| 31 |
|
| 32 |
#ifdef SMLFLT
|
| 33 |
#define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3)
|
| 34 |
#else
|
| 35 |
#define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3)
|
| 36 |
#endif
|
| 37 |
|
| 38 |
char rtcom[] = "rtrace -h- -otp -fa -x 1";
|
| 39 |
char xicom[] = "ximage -c 128";
|
| 40 |
|
| 41 |
VIEW ourview = STDVIEW; /* view for picture */
|
| 42 |
RESOLU ourres; /* picture resolution */
|
| 43 |
|
| 44 |
char *progname; /* program name */
|
| 45 |
|
| 46 |
char *picture; /* picture name */
|
| 47 |
float *zbuffer;
|
| 48 |
|
| 49 |
#define zval(x,y) (zbuffer[(y)*scanlen(&ourres)+(x)])
|
| 50 |
|
| 51 |
FILE *pin; /* input stream */
|
| 52 |
|
| 53 |
Display *theDisplay = NULL; /* connection to server */
|
| 54 |
|
| 55 |
struct node { /* ray tree node */
|
| 56 |
int ipt[2];
|
| 57 |
struct node *sister;
|
| 58 |
struct node *daughter;
|
| 59 |
};
|
| 60 |
|
| 61 |
#define newnode() (struct node *)calloc(1, sizeof(struct node))
|
| 62 |
|
| 63 |
int slow = 0; /* slow trace? */
|
| 64 |
|
| 65 |
|
| 66 |
main(argc, argv) /* takes both the octree and the image */
|
| 67 |
int argc;
|
| 68 |
char *argv[];
|
| 69 |
{
|
| 70 |
int i;
|
| 71 |
char combuf[256];
|
| 72 |
|
| 73 |
progname = argv[0];
|
| 74 |
for (i = 1; i < argc-3; i++)
|
| 75 |
if (!strcmp(argv[i], "-s"))
|
| 76 |
slow++;
|
| 77 |
else
|
| 78 |
break;
|
| 79 |
if (i > argc-3) {
|
| 80 |
fprintf(stderr, "Usage: %s [-s] [rtrace args] octree picture zbuffer\n",
|
| 81 |
progname);
|
| 82 |
exit(1);
|
| 83 |
}
|
| 84 |
picture = argv[argc-2];
|
| 85 |
/* get the viewing parameters */
|
| 86 |
if (viewfile(picture, &ourview, &ourres) <= 0 ||
|
| 87 |
setview(&ourview) != NULL) {
|
| 88 |
fprintf(stderr, "%s: cannot get view from \"%s\"\n",
|
| 89 |
progname, picture);
|
| 90 |
exit(1);
|
| 91 |
}
|
| 92 |
zbload(argv[argc-1]);
|
| 93 |
/* open the display */
|
| 94 |
if ((theDisplay = XOpenDisplay(NULL)) == NULL) {
|
| 95 |
fprintf(stderr,
|
| 96 |
"%s: cannot open display; DISPLAY variable set?\n",
|
| 97 |
progname);
|
| 98 |
exit(1);
|
| 99 |
}
|
| 100 |
/* build input command */
|
| 101 |
sprintf(combuf, "%s %s | %s", xicom, picture, rtcom);
|
| 102 |
for ( ; i < argc-2; i++) {
|
| 103 |
strcat(combuf, " ");
|
| 104 |
strcat(combuf, argv[i]);
|
| 105 |
}
|
| 106 |
/* start the damn thing */
|
| 107 |
if ((pin = popen(combuf, "r")) == NULL)
|
| 108 |
exit(1);
|
| 109 |
sleep(20);
|
| 110 |
/* loop on input */
|
| 111 |
mainloop();
|
| 112 |
/* close pipe and exit */
|
| 113 |
pclose(pin);
|
| 114 |
exit(0);
|
| 115 |
}
|
| 116 |
|
| 117 |
|
| 118 |
zbload(fname)
|
| 119 |
char *fname;
|
| 120 |
{
|
| 121 |
int fd;
|
| 122 |
zbuffer = (float *)malloc(ourres.xr*ourres.yr*sizeof(float));
|
| 123 |
fd = open(fname,0);
|
| 124 |
if (fd<0) exit(1);
|
| 125 |
read(fd,zbuffer,ourres.xr*ourres.yr*sizeof(float));
|
| 126 |
close(fd);
|
| 127 |
}
|
| 128 |
|
| 129 |
|
| 130 |
mainloop() /* get and process input */
|
| 131 |
{
|
| 132 |
static struct node *sis[MAXDEPTH];
|
| 133 |
register struct node *newp;
|
| 134 |
char buf[128];
|
| 135 |
int level;
|
| 136 |
register int i;
|
| 137 |
|
| 138 |
level = 0;
|
| 139 |
while (fgets(buf, sizeof(buf), pin) != NULL) {
|
| 140 |
if (isalpha(buf[0])) {
|
| 141 |
i = 0;
|
| 142 |
while (isalpha(buf[++i]))
|
| 143 |
;
|
| 144 |
buf[i++] = '\0';
|
| 145 |
ambval(buf, buf+i);
|
| 146 |
continue;
|
| 147 |
}
|
| 148 |
if ((newp = newnode()) == NULL) {
|
| 149 |
fprintf(stderr, "%s: memory error\n", progname);
|
| 150 |
return;
|
| 151 |
}
|
| 152 |
for (i = 0; buf[i] == '\t'; i++)
|
| 153 |
;
|
| 154 |
if (strtoipt(newp->ipt, buf+i) < 0) {
|
| 155 |
fprintf(stderr, "%s: bad read\n", progname);
|
| 156 |
return;
|
| 157 |
}
|
| 158 |
newp->sister = sis[i];
|
| 159 |
sis[i] = newp;
|
| 160 |
if (i < level) {
|
| 161 |
newp->daughter = sis[level];
|
| 162 |
sis[level] = NULL;
|
| 163 |
}
|
| 164 |
level = i;
|
| 165 |
if (i == 0) {
|
| 166 |
setvec(sis[0]->ipt);
|
| 167 |
tracerays(sis[0]);
|
| 168 |
freetree(sis[0]);
|
| 169 |
sis[0] = NULL;
|
| 170 |
if (!slow)
|
| 171 |
XFlush(theDisplay);
|
| 172 |
}
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
|
| 177 |
freetree(tp) /* free a trace tree */
|
| 178 |
struct node *tp;
|
| 179 |
{
|
| 180 |
register struct node *kid;
|
| 181 |
|
| 182 |
for (kid = tp->daughter; kid != NULL; kid = kid->sister)
|
| 183 |
freetree(kid);
|
| 184 |
free((void *)tp);
|
| 185 |
}
|
| 186 |
|
| 187 |
|
| 188 |
tracerays(tp) /* trace a ray tree */
|
| 189 |
struct node *tp;
|
| 190 |
{
|
| 191 |
register struct node *kid;
|
| 192 |
|
| 193 |
for (kid = tp->daughter; kid != NULL; kid = kid->sister) {
|
| 194 |
vector(tp->ipt, kid->ipt);
|
| 195 |
tracerays(kid);
|
| 196 |
}
|
| 197 |
}
|
| 198 |
|
| 199 |
|
| 200 |
strtoipt(ipt, str) /* convert string x y z to image point */
|
| 201 |
int ipt[2];
|
| 202 |
char *str;
|
| 203 |
{
|
| 204 |
FVECT im_pt, pt;
|
| 205 |
|
| 206 |
if (!sscanvec(str, pt))
|
| 207 |
return(-1);
|
| 208 |
if (DOT(pt,pt) <= FTINY) /* origin is really infinity */
|
| 209 |
ipt[0] = ipt[1] = -1; /* null vector */
|
| 210 |
else {
|
| 211 |
viewloc(im_pt, &ourview, pt);
|
| 212 |
loc2pix(ipt, &ourres, im_pt[0], im_pt[1]);
|
| 213 |
}
|
| 214 |
return(0);
|
| 215 |
}
|
| 216 |
|
| 217 |
|
| 218 |
#define rwind RootWindow(theDisplay,ourScreen)
|
| 219 |
#define ourScreen DefaultScreen(theDisplay)
|
| 220 |
|
| 221 |
GC insGC = 0;
|
| 222 |
GC accGC = 0;
|
| 223 |
GC useGC = 0;
|
| 224 |
GC vecGC = 0;
|
| 225 |
Window gwind = 0;
|
| 226 |
int xoff=0, yoff=0;
|
| 227 |
|
| 228 |
|
| 229 |
setvec(ipt) /* set up vector drawing for pick */
|
| 230 |
int *ipt;
|
| 231 |
{
|
| 232 |
extern Window xfindwind();
|
| 233 |
XWindowAttributes wa;
|
| 234 |
XColor xc;
|
| 235 |
XGCValues gcv;
|
| 236 |
int rx, ry, wx, wy;
|
| 237 |
Window rw, cw;
|
| 238 |
unsigned int pm;
|
| 239 |
/* compute pointer location */
|
| 240 |
if (gwind == 0) {
|
| 241 |
register char *wn;
|
| 242 |
for (wn = picture; *wn; wn++);
|
| 243 |
while (wn > picture && wn[-1] != '/') wn--;
|
| 244 |
if ((gwind = xfindwind(theDisplay, rwind, wn, 4)) == 0) {
|
| 245 |
fprintf(stderr, "%s: cannot find display window!\n",
|
| 246 |
progname);
|
| 247 |
exit(1);
|
| 248 |
}
|
| 249 |
}
|
| 250 |
if (ipt != NULL) {
|
| 251 |
XQueryPointer(theDisplay, gwind, &rw, &cw, &rx, &ry, &wx, &wy, &pm);
|
| 252 |
xoff = wx - ipt[0];
|
| 253 |
yoff = wy - ipt[1];
|
| 254 |
}
|
| 255 |
/* set graphics contexts */
|
| 256 |
if (vecGC == 0) {
|
| 257 |
XGetWindowAttributes(theDisplay, gwind, &wa);
|
| 258 |
xc.red = 65535; xc.green = 0; xc.blue = 0;
|
| 259 |
xc.flags = DoRed|DoGreen|DoBlue;
|
| 260 |
if (XAllocColor(theDisplay, wa.colormap, &xc)) {
|
| 261 |
gcv.foreground = xc.pixel;
|
| 262 |
vecGC = XCreateGC(theDisplay,gwind,GCForeground,&gcv);
|
| 263 |
} else {
|
| 264 |
gcv.function = GXinvert;
|
| 265 |
vecGC = XCreateGC(theDisplay,gwind,GCFunction,&gcv);
|
| 266 |
}
|
| 267 |
xc.red = 0; xc.green = 0; xc.blue = 65535;
|
| 268 |
xc.flags = DoRed|DoGreen|DoBlue;
|
| 269 |
XAllocColor(theDisplay, wa.colormap, &xc);
|
| 270 |
gcv.foreground = xc.pixel;
|
| 271 |
insGC = XCreateGC(theDisplay,gwind,GCForeground,&gcv);
|
| 272 |
xc.red = 32000; xc.green = 0; xc.blue = 10000;
|
| 273 |
xc.flags = DoRed|DoGreen|DoBlue;
|
| 274 |
XAllocColor(theDisplay, wa.colormap, &xc);
|
| 275 |
gcv.foreground = xc.pixel;
|
| 276 |
accGC = XCreateGC(theDisplay,gwind,GCForeground,&gcv);
|
| 277 |
xc.red = 0; xc.green = 65000; xc.blue = 0;
|
| 278 |
xc.flags = DoRed|DoGreen|DoBlue;
|
| 279 |
XAllocColor(theDisplay, wa.colormap, &xc);
|
| 280 |
gcv.foreground = xc.pixel;
|
| 281 |
useGC = XCreateGC(theDisplay,gwind,GCForeground,&gcv);
|
| 282 |
}
|
| 283 |
}
|
| 284 |
|
| 285 |
|
| 286 |
vector(ip1, ip2) /* draw a vector */
|
| 287 |
int ip1[2], ip2[2];
|
| 288 |
{
|
| 289 |
if (ip2[0] == -1 && ip2[1] == -1)
|
| 290 |
return; /* null vector */
|
| 291 |
XDrawLine(theDisplay, gwind, vecGC,
|
| 292 |
ip1[0]+xoff, ip1[1]+yoff,
|
| 293 |
ip2[0]+xoff, ip2[1]+yoff);
|
| 294 |
if (slow) {
|
| 295 |
XFlush(theDisplay);
|
| 296 |
sleep(1);
|
| 297 |
}
|
| 298 |
}
|
| 299 |
|
| 300 |
|
| 301 |
ambval(typ, str)
|
| 302 |
char *typ, *str;
|
| 303 |
{
|
| 304 |
int ipt[2];
|
| 305 |
FVECT im_pt, pt;
|
| 306 |
|
| 307 |
if (!sscanvec(str, pt))
|
| 308 |
return(-1);
|
| 309 |
viewloc(im_pt, &ourview, pt);
|
| 310 |
loc2pix(ipt, &ourres, im_pt[0], im_pt[1]);
|
| 311 |
if (ipt[0] < 0 || ipt[0] > scanlen(&ourres) ||
|
| 312 |
ipt[1] < 0 || ipt[1] > numscans(&ourres))
|
| 313 |
return;
|
| 314 |
if (im_pt[2] > zval(ipt[0],ipt[1])+.05)
|
| 315 |
return; /* obstructed */
|
| 316 |
setvec(NULL);
|
| 317 |
if (!strcmp(typ, "insert"))
|
| 318 |
XDrawRectangle(theDisplay, gwind, insGC,
|
| 319 |
ipt[0]+xoff, ipt[1]+yoff, 2, 2);
|
| 320 |
else if (!strcmp(typ, "check"))
|
| 321 |
XDrawRectangle(theDisplay, gwind, accGC,
|
| 322 |
ipt[0]+xoff, ipt[1]+yoff, 2, 2);
|
| 323 |
else if (!strcmp(typ, "use"))
|
| 324 |
XDrawRectangle(theDisplay, gwind, useGC,
|
| 325 |
ipt[0]+xoff, ipt[1]+yoff, 2, 2);
|
| 326 |
XFlush(theDisplay);
|
| 327 |
}
|