home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / VGX / shadows / fastobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.3 KB  |  200 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. #include <math.h>
  18. #include "stdio.h"
  19. #include "gl.h"
  20. #include "device.h"
  21.  
  22. #include "fastobj.h"
  23.  
  24.  
  25. fastobj *readfastobj(name)
  26. char *name;
  27. {
  28.     FILE    *inf;
  29.     fastobj    *obj;
  30.     int        i, j;
  31.     int        nlongs;
  32.     int        magic;
  33.     int        *ip;
  34.     char    filename[512];
  35.  
  36.     inf = fopen(name,"r");
  37.     if(!inf) {
  38.       sprintf(filename,"%s",name);
  39.     inf = fopen(filename,"r");
  40.     if(!inf) {
  41.         fprintf(stderr,"readfast: can't open input file %s\n",name);
  42.         exit(1);
  43.         }
  44.     }
  45.     fread(&magic,sizeof(int),1,inf);
  46.     if(magic != FASTMAGIC) {
  47.     fprintf(stderr,"readfast: bad magic in object file\n");
  48.     fclose(inf);
  49.         exit(1);
  50.     }
  51.     obj = (fastobj *)malloc(sizeof(fastobj));
  52.     fread(&obj->npoints,sizeof(int),1,inf);
  53.     fread(&obj->colors,sizeof(int),1,inf);
  54.  
  55.     /*
  56.      * Insure that the data is quad-word aligned and begins on a page
  57.      * boundary.  This shields us from the performance loss which occurs 
  58.      * whenever we try to fetch data which straddles a page boundary  (the OS
  59.      * has to map in the next virtual page and re-start the DMA transfer).
  60.      */
  61.     nlongs = 8 * obj->npoints;
  62. /* Not important here...
  63.     obj->data = (int *) malloc(nlongs*sizeof(int) + 4096);
  64.     obj->data = (int *) (((int)(obj->data)) + 0xfff);
  65.     obj->data = (int *) (((int)(obj->data)) & 0xfffff000);
  66. */
  67.     obj->data = (int *) malloc(nlongs*sizeof(int));
  68.  
  69.     for (i = 0, ip = obj->data;  i < nlongs/4;  i++, ip += 4)
  70.     fread(ip, 3 * sizeof(int), 1, inf);
  71.     fclose(inf);
  72.     return obj;
  73. }
  74.  
  75.  
  76. #if FALSE
  77. drawfastobj(obj)
  78. fastobj *obj;
  79. {
  80.     register int *p,*end;
  81.     register int npolys;
  82.  
  83.     p = obj->data;
  84.     end = p + 8 * obj->npoints;
  85.  
  86.     if(obj->colors) {
  87.     npolys = obj->npoints/4;
  88.     p = obj->data;
  89.     while(npolys--) {
  90.         PolyOrLine();
  91.         c3i(p);
  92.         v3f(p+4);
  93.         c3i(p+8);
  94.         v3f(p+12);
  95.         c3i(p+16);
  96.         v3f(p+20);
  97.         c3i(p+24);
  98.         v3f(p+28);
  99.         EndPolyOrLine();
  100.         p += 32;
  101.     }
  102.     } else {
  103.     while ( p < end) {
  104.         PolyOrLine();
  105.         n3f(p);
  106.         v3f(p+4);
  107.         n3f(p+8);
  108.         v3f(p+12);
  109.         n3f(p+16);
  110.         v3f(p+20);
  111.         n3f(p+24);
  112.         v3f(p+28);
  113.         EndPolyOrLine();
  114.         p += 32;
  115.     }
  116.     }
  117. }
  118.  
  119.  
  120. /*
  121.  * objmaxpoint
  122.  *
  123.  * find the vertex farthest from the origin,
  124.  * so we can set the near and far clipping planes tightly.
  125.  */
  126.  
  127. #define MAXVERT(v) if ( (len = sqrt(    (*(v))  *  (*(v))  +           \
  128.                     (*(v+1)) * (*(v+1)) +           \
  129.                     (*(v+2)) * (*(v+2)) )) > max)  \
  130.             max = len;
  131.  
  132. float
  133. objmaxpoint(obj)
  134. fastobj *obj;
  135. {
  136.     register float *p, *end;
  137.     register int npolys;
  138.     register float len;
  139.     register float max = 0.0;
  140.  
  141.     p = (float *) (obj->data);
  142.  
  143.     if (obj->colors) {
  144.     npolys = obj->npoints/4;
  145.     while(npolys--) {
  146.         MAXVERT(p+4);
  147.         MAXVERT(p+12);
  148.         MAXVERT(p+20);
  149.         MAXVERT(p+28);
  150.         p += 32;
  151.     }
  152.     } else {
  153.     end = p + 8 * obj->npoints;
  154.     while ( p < end) {
  155.         MAXVERT(p+4);
  156.         MAXVERT(p+12);
  157.         MAXVERT(p+20);
  158.         MAXVERT(p+28);
  159.         p += 32;
  160.     }
  161.     }
  162.  
  163.     return max;
  164. }
  165.  
  166. splitobj(obj, objs, start, end)
  167. fastobj    *obj;
  168. fastobj    objs[];
  169. int    start;
  170. int    end;
  171. {
  172.     int    *p, quad_per_piece, quad, factor, curp;
  173.  
  174.     factor = end - start;
  175.     if (obj->npoints /4 < factor) {
  176.     perror("not enough 4vtx polygons to split into %d pieces\n", obj->npoints);
  177.     exit(-1);
  178.     }
  179.  
  180.     p = obj->data;
  181.     quad = obj->npoints/4;
  182.     quad_per_piece = quad / factor;
  183.  
  184.     if (obj->colors) {
  185.     perror("can't do yet\n");
  186.     }
  187.     else {
  188.     objs[start].npoints = ((quad % factor) + quad_per_piece) * 4;
  189.     while (--factor) {
  190.         curp = (start + factor);
  191.         objs[curp].npoints = quad_per_piece * 4;
  192.         objs[curp].data = p;
  193.         p += quad_per_piece * 32;
  194.     }
  195.     objs[start].data = p;
  196.     obj->swirl = &(objs[start]);
  197.     }
  198. }
  199. #endif
  200.