home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / envtools / tofish.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.8 KB  |  130 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    tofish -
  19.  *        Create a fish-eye view of an environment map.
  20.  *
  21.  *                Paul Haeberli - 1987
  22.  */
  23. #include "texture.h"
  24.  
  25. #define PI    (3.1415926535)
  26. #define EPSILON    (0.00001)
  27.  
  28. TEXTURE *tm;
  29. int doreflect;
  30. int size, samples;
  31.  
  32. main(argc,argv)
  33. int argc;
  34. char **argv;
  35. {
  36.     float vr, vg, vb;
  37.     float tr, tg, tb;
  38.     int x, y, i;
  39.     IMAGE *image;
  40.     vect pos, c;
  41.     float angle;
  42.     short *rbuf, *gbuf, *bbuf;
  43.  
  44.     if(argc<6) {
  45.     fprintf(stderr,"usage: tofish size samples file.env file.rgb angle\n"); 
  46.     exit(1);
  47.     }
  48.     size = atoi(argv[1]);
  49.     samples = atoi(argv[2]);
  50.     tm = tmopen(argv[3]);
  51.     angle = atof(argv[5]);
  52.     if(!tm) {
  53.     printf("can't open environment map.\n");
  54.     exit(1);
  55.     }
  56.     rbuf = (short *)malloc(size*sizeof(short));
  57.     gbuf = (short *)malloc(size*sizeof(short));
  58.     bbuf = (short *)malloc(size*sizeof(short));
  59.     image = iopen(argv[4],"w",RLE(1),3,size,size,3);
  60.     for(y=0; y<size; y++) {
  61.     for(x=0; x<size; x++) {
  62.         tr = tg = tb = 0.0;
  63.         for(i=0; i<samples; i++) {
  64.         if(samples>1) {
  65.             pos.x = (x+frand())/size-0.5;
  66.             pos.y = (y+frand())/size-0.5;
  67.             pos.z = 0;
  68.         } else {
  69.             pos.x = (float)x/size-0.5;
  70.             pos.y = (float)y/size-0.5;
  71.             pos.z = 0.0;
  72.         }
  73.         tofish(&pos,angle,&c);
  74.         tr += c.x;
  75.         tg += c.y;
  76.         tb += c.z;
  77.         }
  78.         rbuf[x] = 255*tr/samples;
  79.         gbuf[x] = 255*tg/samples;
  80.         bbuf[x] = 255*tb/samples;
  81.     }
  82.     putrow(image,rbuf,y,0);
  83.     putrow(image,gbuf,y,1);
  84.     putrow(image,bbuf,y,2);
  85.     tpercentdone(100.0*y/(size-1));
  86.     }
  87.     iclose(image);
  88. }
  89.  
  90. tofish(p,angle,c)
  91. vect *p, *c; 
  92. float angle;
  93. {
  94.     vect e, v, r;
  95.     float away, mag;
  96.     float radmult, z;
  97.  
  98.     mag = sqrt(p->x*p->x+p->y*p->y);
  99.     if(mag>0.5) {
  100.     c->x = 1.0;
  101.     c->y = 1.0;
  102.     c->z = 1.0;
  103.     return;
  104.     } else if(mag>EPSILON) {
  105.     away = (PI*mag*angle)/180.0;
  106.     radmult = sin(away);
  107.     v.x = (radmult*p->x)/mag;
  108.     v.y = (radmult*p->y)/mag;
  109.     v.z = cos(away);
  110.     } else {
  111.     v.x = 0.0;
  112.     v.y = 0.0;
  113.     v.z = 1.0;
  114.     }
  115.     v.z = -v.z;
  116.     lookx(&v);
  117.     envsample(tm,&v,c);
  118. }
  119.  
  120. lookx(v)
  121. vect *v;
  122. {
  123.     float temp;
  124.  
  125.     temp = v->z;    /* to make us look down the x axis */
  126.     v->z = v->y;
  127.     v->y = -v->x;
  128.     v->x = temp;
  129. }
  130.