home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * Kurt Akeley
- * 1 March 1988
- *
- * Magnify the region under the cursor. Use two mouse buttons to
- * control display mode and source (do not interpret ID plane info).
- */
-
- #include <stdio.h>
- #include <gl.h>
- #include <device.h>
- #include <string.h>
-
- /* mode values */
- #define RGBMODE 0
- #define COLORINDEX 1
-
- /* offset used in snoopmode display */
- #define OFFSET 5
-
- /* steps used by arrow keys */
- #define LARGESTEP 16
- #define SMALLSTEP 1
-
- /* takes argv[0] stripped of path */
- char *progname;
-
- /* takes return of gversion */
- char gv[20];
-
- /* initial zoom and window size */
- #define INITZOOM 6
- #define INITSIZE (INITZOOM * 51)
-
- /* initialized modes */
- long readsrc = SRC_FRONT;
- long toggle = SRC_FRONT;
- long dspmode = RGBMODE;
- long shademode = FLAT;
- long snoopmode = TRUE;
- long arrowmode = FALSE;
- long Backbuffer = FALSE;
- long Cmode = FALSE;
- long widauxmode = FALSE;
- long zoom = INITZOOM;
- long leftshift = FALSE;
- long rightshift = FALSE;
- long ctrlkey = FALSE;
- long blocked = FALSE;
-
- /* updated by getorigin() and getsize() whenever window is changed */
- long xorigin,yorigin;
- long xsize,ysize;
- long xll,yll,xur,yur;
-
- int rgbmode_avail = TRUE;
-
- unsigned long *cbuf;
- unsigned short *cbufs;
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int i;
- int x, y;
- int xmouse,ymouse;
- short dev,val;
- int menu,mval;
- int zoommenu;
- char *t;
-
- if ((t=strrchr(argv[0],'/')) == NULL)
- progname = argv[0];
- else
- progname = t + 1;
- if (argc > 1)
- zoom = atoi(argv[1]);
- gversion(gv);
- prefsize(INITSIZE,INITSIZE);
- winopen(progname);
- stepunit(zoom,zoom);
- winconstraints();
- if (getgdesc(GD_BITS_NORM_DBL_RED) == 0)
- rgbmode_avail = FALSE;
- if (rgbmode_avail) {
- RGBmode();
- doublebuffer();
- } else {
- Cmode = TRUE;
- dspmode = COLORINDEX;
- cmode();
- }
- changetitle();
- gconfig();
- shademodel(GOURAUD);
- qdevice(RIGHTMOUSE);
- qdevice(ESCKEY);
- qdevice(REDRAW);
- qdevice(ONEKEY);
- qdevice(TWOKEY);
- qdevice(THREEKEY);
- qdevice(FOURKEY);
- qdevice(FIVEKEY);
- qdevice(SIXKEY);
- qdevice(SEVENKEY);
- qdevice(EIGHTKEY);
- qdevice(NINEKEY);
- qdevice(ZEROKEY);
- qdevice(LEFTARROWKEY);
- qdevice(RIGHTARROWKEY);
- qdevice(UPARROWKEY);
- qdevice(DOWNARROWKEY);
- qdevice(LEFTSHIFTKEY);
- qdevice(RIGHTSHIFTKEY);
- qdevice(CTRLKEY);
- qdevice(AKEY);
- qdevice(BKEY);
- qdevice(CKEY);
- qdevice(GKEY);
- qdevice(QKEY);
- qdevice(SKEY);
- qdevice(WKEY);
- newwindow();
- makebuf();
- zoommenu = defpup("1 %x1|2 %x2|3 %x3|4 %x4|6 %x6|8 %x8|10 %x10|16 %x16|32 %x32");
- menu = defpup("Options %t");
- addtopup(menu, "Arrow %x100"); setpup(menu, 1, PUP_BOX);
- addtopup(menu, "Backbuffer %x105");
- if (rgbmode_avail) setpup(menu, 2, PUP_BOX);
- else setpup(menu, 2, PUP_GREY);
- addtopup(menu, "RGBmode %x106");
- if (rgbmode_avail) setpup(menu, 3, PUP_CHECK);
- else setpup(menu, 3, PUP_GREY);
- addtopup(menu, "Gouraud %x102"); setpup(menu, 4, PUP_BOX);
- addtopup(menu, "Snoop %x103"); setpup(menu, 5, PUP_CHECK);
- addtopup(menu, "Zoom %m", zoommenu);
- addtopup(menu, "Stop %x101"); setpup(menu, 7, PUP_BOX);
- addtopup(menu, "Exit %x104");
-
- while (1) {
- while (blocked || qtest()) {
- dev = qread(&val);
- switch (dev) {
- case RIGHTMOUSE:
- if (val) {
- switch(dopup(menu)) {
- case 1:
- zoom = 1;
- break;
- case 2:
- zoom = 2;
- break;
- case 3:
- zoom = 3;
- break;
- case 4:
- zoom = 4;
- break;
- case 6:
- zoom = 6;
- break;
- case 8:
- zoom = 8;
- break;
- case 10:
- zoom = 10;
- break;
- case 16:
- zoom = 16;
- break;
- case 32:
- zoom = 32;
- break;
- case 100:
- arrowmode = !arrowmode;
- if (arrowmode)
- setpup(menu, 1, PUP_CHECK);
- else setpup(menu, 1, PUP_BOX);
- break;
- case 101:
- blocked = !blocked;
- if (blocked)
- setpup(menu, 7, PUP_CHECK);
- else setpup(menu, 7, PUP_BOX);
- drawblock();
- break;
- case 102:
- shademode = (shademode==GOURAUD)?FLAT:GOURAUD;
- if (shademode == GOURAUD)
- setpup(menu, 4, PUP_CHECK);
- else setpup(menu, 4, PUP_BOX);
- break;
- case 103:
- snoopmode = !snoopmode;
- if (snoopmode)
- setpup(menu, 5, PUP_CHECK);
- else setpup(menu, 5, PUP_BOX);
- break;
- case 104:
- doexit(0);
- break;
- case 105:
- Backbuffer = !Backbuffer;
- if (Backbuffer)
- setpup(menu, 2, PUP_CHECK);
- else setpup(menu, 2, PUP_BOX);
- break;
- case 106:
- Cmode = !Cmode;
- if (!Cmode)
- setpup(menu, 3, PUP_CHECK);
- else setpup(menu, 3, PUP_BOX);
- break;
- }
- }
- break;
- case ESCKEY:
- if (!val)
- doexit(0);
- break;
- case REDRAW:
- newwindow();
- if (blocked)
- drawblock();
- break;
- case AKEY:
- if (val)
- arrowmode = !arrowmode;
- if (arrowmode)
- setpup(menu, 1, PUP_CHECK);
- else setpup(menu, 1, PUP_BOX);
- break;
- case BKEY:
- if (val)
- Backbuffer = !Backbuffer;
- if (Backbuffer)
- setpup(menu, 2, PUP_CHECK);
- else setpup(menu, 2, PUP_BOX);
- break;
- case CKEY:
- if (val)
- Cmode = !Cmode;
- if (!Cmode)
- setpup(menu, 3, PUP_CHECK);
- else setpup(menu, 3, PUP_BOX);
- break;
- case GKEY:
- if (val)
- shademode = (shademode==GOURAUD) ? FLAT : GOURAUD;
- if (shademode == GOURAUD)
- setpup(menu, 4, PUP_CHECK);
- else setpup(menu, 4, PUP_BOX);
- break;
- case QKEY:
- if (val && ctrlkey)
- blocked = FALSE;
- if (blocked)
- setpup(menu, 7, PUP_CHECK);
- else setpup(menu, 7, PUP_BOX);
- break;
- case SKEY:
- if (val) {
- if (ctrlkey) {
- blocked = TRUE;
- setpup(menu, 7, PUP_CHECK);
- drawblock();
- } else
- snoopmode = !snoopmode;
- if (snoopmode)
- setpup(menu, 5, PUP_CHECK);
- else setpup(menu, 5, PUP_BOX);
- }
- break;
- case WKEY:
- if (val)
- widauxmode = !widauxmode;
- break;
- case LEFTSHIFTKEY:
- leftshift = val;
- break;
- case RIGHTSHIFTKEY:
- rightshift = val;
- break;
- case CTRLKEY:
- ctrlkey = val;
- break;
- case LEFTARROWKEY:
- if (val) {
- if (leftshift || rightshift)
- xmouse -= LARGESTEP;
- else
- xmouse -= SMALLSTEP;
- if (xmouse < 0)
- xmouse = 0;
- }
- break;
- case RIGHTARROWKEY:
- if (val) {
- if (leftshift || rightshift)
- xmouse += LARGESTEP;
- else
- xmouse += SMALLSTEP;
- if (xmouse > getgdesc(GD_XPMAX))
- xmouse = getgdesc(GD_XPMAX);
- }
- break;
- case DOWNARROWKEY:
- if (val) {
- if (leftshift || rightshift)
- ymouse -= LARGESTEP;
- else
- ymouse -= SMALLSTEP;
- if (ymouse < 0)
- ymouse = 0;
- }
- break;
- case UPARROWKEY:
- if (val) {
- if (leftshift || rightshift)
- ymouse += LARGESTEP;
- else
- ymouse += SMALLSTEP;
- if (ymouse > getgdesc(GD_YPMAX))
- ymouse = getgdesc(GD_YPMAX);
- }
- break;
- case ONEKEY:
- zoom = 1;
- break;
- case TWOKEY:
- zoom = 2;
- break;
- case THREEKEY:
- zoom = 3;
- break;
- case FOURKEY:
- zoom = 4;
- break;
- case FIVEKEY:
- zoom = 5;
- break;
- case SIXKEY:
- zoom = 6;
- break;
- case SEVENKEY:
- zoom = 7;
- break;
- case EIGHTKEY:
- zoom = 8;
- break;
- case NINEKEY:
- zoom = 9;
- break;
- case ZEROKEY:
- zoom = 10;
- break;
- }
- changetitle();
- makebuf();
- }
-
- /* control source buffer with left mouse button */
- if (Backbuffer)
- i = getbutton(LEFTMOUSE) ? SRC_FRONT : SRC_BACK;
- else
- i = getbutton(LEFTMOUSE) ? SRC_BACK : SRC_FRONT;
- if (i != readsrc && rgbmode_avail) {
- readsrc = i;
- changetitle();
- }
- setreadsource(readsrc);
-
- /* control display mode with middle mouse button */
- if (Cmode)
- i = getbutton(MIDDLEMOUSE) ? RGBMODE : COLORINDEX;
- else
- i = getbutton(MIDDLEMOUSE) ? COLORINDEX : RGBMODE;
- if (rgbmode_avail)
- if (i != dspmode) {
- dspmode = i;
- if (dspmode == COLORINDEX)
- cmode();
- else
- RGBmode();
- gconfig();
- changetitle();
- }
-
- /* track mouse and do the zoom */
- if (!arrowmode) {
- xmouse = getvaluator(MOUSEX);
- ymouse = getvaluator(MOUSEY);
- }
- x = xmouse - xorigin;
- y = ymouse - yorigin;
- finish(); /* avoid latency problems */
- if (shademode == FLAT) {
- rectzoom((float)zoom,(float)zoom);
- if (dspmode == RGBMODE && readsrc == SRC_FRONT)
- {
- readdisplay(x+xorigin+xll, y+yorigin+yll,
- x+xorigin+xur, y+yorigin+yur, cbuf,
- RD_IGNORE_PUP|RD_IGNORE_OVERLAY|RD_IGNORE_UNDERLAY);
- lrectwrite(0, 0, xur-xll, yur-yll, cbuf);
- }
- else
- {
- rectcopy(x+xll,y+yll,x+xur,y+yur,0,0);
- }
- }
- else
- smoothcopy(x+xll,y+yll,x+xur,y+yur);
-
- /* display pixel information if in snoopmode */
- if (snoopmode)
- dosnoop(x,y,xmouse,ymouse);
- swap();
- }
- } /* end of main */
-
- newwindow() {
- getorigin(&xorigin,&yorigin);
- getsize(&xsize,&ysize);
- viewport(0,xsize-1,0,ysize-1);
- ortho2(-0.5,(float)xsize-0.5,-0.5,(float)ysize-0.5);
- }
-
- swap() {
- /* swap buffers and toggle the notion of front and back */
- if (rgbmode_avail) {
- swapbuffers();
- toggle = (toggle==SRC_FRONT) ? SRC_BACK : SRC_FRONT;
- }
- }
-
- setreadsource(src) {
- if (!rgbmode_avail)
- return;
-
- /* set readsource using toggle to avoid swapbuffer problem */
- switch (src) {
- case SRC_FRONT:
- case SRC_BACK:
- if (src == toggle)
- readsource(SRC_FRONT);
- else
- readsource(SRC_BACK);
- break;
- case SRC_ZBUFFER:
- readsource(SRC_ZBUFFER);
- break;
- case SRC_OVER:
- readsource(SRC_OVER);
- break;
- }
- }
-
- changetitle()
- {
- /* change the window title based on mode switches */
- static char oldtitle[100] = "";
- char title[100];
- if (blocked) {
- sprintf(title,"%s - stopped",progname);
- } else {
- sprintf(title,"%s %d%s%s%s%s",progname,zoom,
- (arrowmode==FALSE)?"":" arrow",
- (readsrc==SRC_FRONT)?"":" backbuffer",
- (dspmode==RGBMODE)?"":" cmode",
- (shademode==FLAT)?"":" gouraud");
- }
- if (strcmp(title,oldtitle) != 0)
- wintitle(title);
- strcpy(oldtitle,title);
- }
-
- makebuf() {
- /* compute source rect size based on window size and zoom factor */
- /* malloc a buffer based on the computed size */
- static size = 0;
- int msize;
- long xsrc,ysrc;
-
- xsrc = xsize / zoom;
- ysrc = ysize / zoom;
- xll = -(xsrc/2);
- xur = xll + xsrc + 1;
- yll = -(ysrc/2);
- yur = yll + ysrc + 1;
- msize = ((xur-xll) + 1) * ((yur-yll) + 1);
- if (msize != size) {
- stepunit(zoom,zoom);
- winconstraints();
- if (size > 0)
- free(cbuf);
- if ((cbuf=(unsigned long*)malloc(msize*4)) == 0) {
- fprintf(stderr," ERROR: can't malloc %d longs. abort.\n",msize);
- doexit(1);
- }
- size = msize;
- }
- }
-
- smoothcopy(xll,yll,xur,yur) {
- /* read specified pixels, then draw gouraud shaded polygons */
- /* assume that matrix and viewport are set up 1-to-1 object-to-pixel */
- register x,y;
- register unsigned long *cp0,*cp1;
- register unsigned short *cp0s,*cp1s;
- long width;
- long height;
- long xfinal,yfinal;
- long vbuf[8];
- float fzoom;
-
- width = xur - xll + 1;
- height = yur - yll + 1;
- xfinal = width - 1;
- yfinal = height - 1;
- if (dspmode == RGBMODE)
- if (readsrc == SRC_FRONT)
- readdisplay(xll+xorigin, yll+yorigin,
- xur+xorigin, yur+yorigin, cbuf,
- RD_IGNORE_PUP|RD_IGNORE_OVERLAY|RD_IGNORE_UNDERLAY);
- else lrectread(xll, yll, xur, yur, cbuf);
- else {
- cbufs = (unsigned short*)cbuf;
- rectread(xll,yll,xur,yur,cbufs);
- maskindex(cbuf,(xur-xll+1)*(yur-yll+1),0xfff);
- }
- pushmatrix();
- fzoom = (float)zoom;
- scale(fzoom,fzoom,fzoom);
- for (y=0; y<yfinal; y++) {
- vbuf[1] = vbuf[3] = y;
- vbuf[5] = vbuf[7] = y+1;
- if (dspmode == RGBMODE) {
- cp0 = &cbuf[y * width];
- cp1 = &cbuf[(y+1) * width + 1];
- for (x=0; x<xfinal; x++) {
- vbuf[0] = vbuf[6] = x;
- vbuf[2] = vbuf[4] = x+1;
- bgnpolygon();
- cpack(*cp0++);
- v2i(vbuf);
- cpack(*cp0);
- v2i(vbuf+2);
- cpack(*cp1--);
- v2i(vbuf+4);
- cpack(*cp1++);
- v2i(vbuf+6);
- endpolygon();
- cp1 += 1;
- }
- }
- else {
- cp0s = &cbufs[y * width];
- cp1s = &cbufs[(y+1) * width + 1];
- for (x=0; x<xfinal; x++) {
- vbuf[0] = vbuf[6] = x;
- vbuf[2] = vbuf[4] = x+1;
- bgnpolygon();
- color(*cp0s++);
- v2i(vbuf);
- color(*cp0s);
- v2i(vbuf+2);
- color(*cp1s--);
- v2i(vbuf+4);
- color(*cp1s++);
- v2i(vbuf+6);
- endpolygon();
- cp1s += 1;
- }
- }
- }
- popmatrix();
- }
-
- maskindex(buf,size,mask)
- unsigned long *buf; /* long-aligned buffer of shorts */
- long size; /* number of shorts in buffer */
- unsigned long mask;
- {
- /* mask upper 4 bits of each short - do two at a time */
- register i;
- register unsigned long *p;
- register unsigned long m;
-
- p = buf;
- m = mask | (mask<<16);
- for (i=size/2; i--;)
- *p++ &= m;
- }
-
- drawblock() {
- /* draw routine called when blocked */
- if (Cmode)
- color(0x19);
- else
- cpack(0xbfbfbf);
- clear();
- swap();
- }
-
- doexit(i) {
- gexit();
- exit(i);
- }
-
- dosnoop(x,y,xmouse,ymouse) {
- unsigned long front,back,z,wid,aux,pup;
- char s[100];
- setreadsource(SRC_FRONT);
- lrectread(x,y,x,y,&front);
- if (rgbmode_avail) {
- setreadsource(SRC_BACK);
- lrectread(x,y,x,y,&back);
- setreadsource(SRC_ZBUFFER);
- lrectread(x,y,x,y,&z);
- /* GAB -- mask Z to 24 bits to fix problems on VGX.
- * This code should be taught about VGX's stencil bitplanes
- * eventually.
- */
- z = z & 0xFFFFFF;
- }
-
- if (widauxmode) {
- readsource(SRC_AUTO); /* this call required for PI to work */
- drawmode(PUPDRAW);
- lrectread(x,y,x,y,&pup);
- pup &= 3;
- drawmode(OVERDRAW);
- lrectread(x,y,x,y,&aux);
- aux &= 0xf;
- drawmode(1000);
- lrectread(x,y,x,y,&wid);
- wid &= 0xf;
- drawmode(NORMALDRAW);
- }
- if (dspmode == RGBMODE) {
- cpack(0x000000);
- if (widauxmode)
- if (strncmp(gv, "GL4DGT", strlen("GL4DGT")) == 0)
- sprintf(s,"%03x %03x %08x %08x %x %x %x",
- xmouse,ymouse,front,back,(aux>>2)&3,aux&3,wid);
- else
- sprintf(s,"%03x %03x %08x %08x %x %x %x",
- xmouse,ymouse,front,back,pup,aux,wid);
- else
- sprintf(s,"%03x %03x %08x %08x %06x",
- xmouse,ymouse,front,back,z);
- rectfi(OFFSET,OFFSET,
- OFFSET+strwidth(s)+6,OFFSET+2*getheight()+4);
- cpack(0xFFFFFF);
- recti(OFFSET,OFFSET,
- OFFSET+strwidth(s)+6,OFFSET+2*getheight()+4);
- cmov2i(OFFSET+5,OFFSET+5);
- charstr(s);
- if (widauxmode)
- sprintf(s," X Y FRONT BACK P O W");
- else
- sprintf(s," X Y FRONT BACK Z");
- cmov2i(OFFSET+5,OFFSET+5+getheight());
- charstr(s);
- /* invert color of outline of selected pixel */
- if (shademode == FLAT) {
- long tmp;
- tmp = (readsrc==SRC_FRONT) ? front : back;
- RGBcolor(tmp&0x80 ? 0 : 0xff,
- tmp&0x8000 ? 0 : 0xff,
- tmp&0x800000 ? 0 : 0xff);
- recti(-xll*zoom,-yll*zoom,
- -xll*zoom+zoom-1,-yll*zoom+zoom-1);
- }
- } else {
- color(BLACK);
- if (!rgbmode_avail) {
- sprintf(s,"%03x %03x %03x",
- xmouse,ymouse,front&0xFFF);
- }
- else if (widauxmode) {
- if (strncmp(gv, "GL4DGT", strlen("GL4DGT")) == 0)
- sprintf(s,"%03x %03x %03x %03x %06x %x %x %x",
- xmouse,ymouse,front&0xFFF,back&0xFFF,z,(aux>>2)&3,aux&3,wid);
- else
- sprintf(s,"%03x %03x %03x %03x %06x %x %x %x",
- xmouse,ymouse,front&0xFFF,back&0xFFF,z,pup,aux,wid);
- }
- else {
- sprintf(s,"%03x %03x %03x %03x %06x",
- xmouse,ymouse,front&0xFFF,back&0xFFF,z);
- }
- rectfi(OFFSET,OFFSET,
- OFFSET+strwidth(s)+6,OFFSET+2*getheight()+4);
- color(WHITE);
- recti(OFFSET,OFFSET,
- OFFSET+strwidth(s)+6,OFFSET+2*getheight()+4);
- cmov2i(OFFSET+5,OFFSET+5);
- charstr(s);
- if (!rgbmode_avail)
- sprintf(s," X Y IDX");
- else if (widauxmode)
- sprintf(s," X Y FNT BCK Z P O W");
- else
- sprintf(s," X Y FNT BCK Z");
- cmov2i(OFFSET+5,OFFSET+5+getheight());
- charstr(s);
- if (shademode == FLAT) {
- color((readsrc==SRC_FRONT)
- ? (((front&0xFFF)==7)?0:7) :(((back&0xFFF)==7)?0:7));
- recti(-xll*zoom,-yll*zoom,
- -xll*zoom+zoom-1,-yll*zoom+zoom-1);
- }
- }
- }
-