home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / VGX / envmap / fastobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.8 KB  |  127 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/gl.h>
  20. #include <gl/device.h>
  21.  
  22. #include "fastobj.h"
  23.  
  24. fastobj
  25. *readfastobj(char *name)
  26. {
  27.     FILE    *inf;
  28.     fastobj    *obj;
  29.     int        i, j;
  30.     int        nlongs;
  31.     int        magic;
  32.     int        *ip;
  33.  
  34.     inf = fopen(name,"r");
  35.     if(!inf) {
  36.         fprintf(stderr,"readfast: can't open input file %s\n",name);
  37.         exit(1);
  38.     }
  39.     fread(&magic,sizeof(int),1,inf);
  40.     if(magic != FASTMAGIC) {
  41.     fprintf(stderr,"readfast: bad magic in object file\n");
  42.     fclose(inf);
  43.         exit(1);
  44.     }
  45.     obj = (fastobj *)malloc(sizeof(fastobj));
  46.     fread(&obj->npoints,sizeof(int),1,inf);
  47. /*** IGNORE COLORS FIELD ***/
  48.     fread(&magic,sizeof(int),1,inf);
  49.  
  50.     /*
  51.      * Insure that the data is quad-word aligned and begins on a page
  52.      * boundary.  This shields us from the performance loss which occurs 
  53.      * whenever we try to fetch data which straddles a page boundary  (the OS
  54.      * has to map in the next virtual page and re-start the DMA transfer).
  55.      */
  56.     nlongs = 8 * obj->npoints;
  57.     obj->data = (float *) malloc(nlongs*sizeof(int) + 4096);
  58.     obj->data = (float *) (((int)(obj->data)) + 0xfff);
  59.     obj->data = (float *) (((int)(obj->data)) & 0xfffff000);
  60.  
  61.     for (i = 0, ip = (int *)obj->data;  i < nlongs/4;  i++, ip += 4)
  62.         fread(ip, 3 * sizeof(int), 1, inf);
  63.     fclose(inf);
  64.  
  65.     return obj;
  66. }
  67.  
  68. void
  69. drawfastobj(fastobj *obj)
  70. {
  71.     register float *p,*end;
  72.  
  73.     p = obj->data;
  74.     end = p + 8 * obj->npoints;
  75.  
  76.     while ( p < end) {
  77.         bgnpolygon();
  78.         n3f(p);
  79.         v3f(p+4);
  80.         n3f(p+8);
  81.         v3f(p+12);
  82.         n3f(p+16);
  83.         v3f(p+20);
  84.         n3f(p+24);
  85.         v3f(p+28);
  86.         endpolygon();
  87.         p += 32;
  88.     }
  89. }
  90.  
  91.  
  92. /*
  93.  * objmaxpoint
  94.  *
  95.  * find the vertex farthest from the origin,
  96.  * so we can set the near and far clipping planes tightly.
  97.  */
  98.  
  99. #define MAXVERT(v) if ( (len = sqrt(    (*(v))  *  (*(v))  +      \
  100.                     (*(v+1)) * (*(v+1)) +           \
  101.                     (*(v+2)) * (*(v+2)) )) > max)  \
  102.             max = len;
  103.  
  104. float
  105. objmaxpoint(obj)
  106. fastobj *obj;
  107. {
  108.     register float *p, *end;
  109.     register int npolys;
  110.     register float len;
  111.     register float max = 0.0;
  112.  
  113.     p = obj->data;
  114.  
  115.     end = p + 8 * obj->npoints;
  116.     while ( p < end) {
  117.         MAXVERT(p+4);
  118.         MAXVERT(p+12);
  119.         MAXVERT(p+20);
  120.         MAXVERT(p+28);
  121.         p += 32;
  122.     }
  123.  
  124.     return max;
  125. }
  126.  
  127.