home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / 3Dmodeling / surface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.4 KB  |  105 lines

  1. /*
  2.  * Copyright 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. /* Tom Davis -- 1992 */
  19.  
  20. #include "3d.h"
  21. #include <stdio.h>
  22. #include <math.h>
  23. #define PI    3.1415926535
  24.  
  25. /* The analyticsurface routine takes a surface description:
  26.  * 
  27.  * (x(s, t), y(s, t), z(s, t)), 
  28.  * 
  29.  * together with the analytic partial derivitives in the s and t
  30.  * directions:
  31.  * 
  32.  * (xs(s, t), ys(s, t), zs(s, t)), 
  33.  * (xt(s, t), yt(s, t), zt(s, t)), 
  34.  * 
  35.  * The domains of s values and t values, and counts for the number of
  36.  * subdivisions to take in each direction.  If the flag flipnormals is 1,
  37.  * the directions of the calculated normal vectors will be reversed.
  38.  * 
  39.  */
  40.  
  41. void analyticsurface(float (*x)(float, float), float (*y)(float, float), 
  42.              float (*z)(float, float), 
  43.              float (*xs)(float, float), float (*ys)(float, float), 
  44.              float (*zs)(float, float), 
  45.              float (*xt)(float, float), float (*yt)(float, float), 
  46.              float (*zt)(float, float), 
  47.              long scount, float smin, float smax,
  48.              long tcount, float tmin, float tmax,
  49.              long flipnormals, 
  50.              void (*savefunc)())
  51. {
  52.     float s, t, s1, t1;
  53.     long is, it;
  54.     float n0[3], n1[3], n2[3], n3[3];
  55.     float p0[3], p1[3], p2[3], p3[3];
  56.     float v[3], w[3];
  57.     
  58.     for (is = 0; is < scount; is++) {
  59.         s = smin + is*(smax - smin)/scount;
  60.     s1 = s + (smax - smin)/scount;
  61.     for (it = 0; it < tcount; it++) {
  62.         t = smin + it*(tmax - tmin)/scount;
  63.         t1 = t + (tmax - tmin)/scount;
  64.         p0[0] = x(s, t); p0[1] = y(s, t); p0[2] = z(s, t);
  65.         p1[0] = x(s1, t); p1[1] = y(s1, t); p1[2] = z(s1, t);
  66.         p2[0] = x(s1, t1); p2[1] = y(s1, t1); p2[2] = z(s1, t1);
  67.         p3[0] = x(s, t1); p3[1] = y(s, t1); p3[2] = z(s, t1);
  68.  
  69.         /* now calculate the normal vectors: */
  70.  
  71.         v[0] = xs(s, t); v[1] = ys(s, t); v[2] = zs(s, t);
  72.         w[0] = xt(s, t); w[1] = yt(s, t); w[2] = zt(s, t);
  73.         crossprod(v, w, n0);
  74.         normalize(n0);
  75.         if (flipnormals) scalarmult(-1.0, n0, n0);
  76.  
  77.         v[0] = xs(s1, t); v[1] = ys(s1, t); v[2] = zs(s1, t);
  78.         w[0] = xt(s1, t); w[1] = yt(s1, t); w[2] = zt(s1, t);
  79.         crossprod(v, w, n1);
  80.         normalize(n1);
  81.         if (flipnormals) scalarmult(-1.0, n1, n1);
  82.  
  83.         v[0] = xs(s1, t1); v[1] = ys(s1, t1); v[2] = zs(s1, t1);
  84.         w[0] = xt(s1, t1); w[1] = yt(s1, t1); w[2] = zt(s1, t1);
  85.         crossprod(v, w, n2);
  86.         normalize(n2);
  87.         if (flipnormals) scalarmult(-1.0, n2, n2);
  88.  
  89.         v[0] = xs(s, t1); v[1] = ys(s, t1); v[2] = zs(s, t1);
  90.         w[0] = xt(s, t1); w[1] = yt(s, t1); w[2] = zt(s, t1);
  91.         crossprod(v, w, n3);
  92.         normalize(n3);
  93.         if (flipnormals) scalarmult(-1.0, n3, n3);
  94.  
  95.         m_xformpt(p0, p0, n0, n0);
  96.         m_xformpt(p1, p1, n1, n1);
  97.         m_xformpt(p2, p2, n2, n2);
  98.         m_xformpt(p3, p3, n3, n3);
  99.  
  100.         (*savefunc)(ADD_QUAD, n0, p0, n1, p1, n2, p2, n3, p3);    
  101.     }
  102.     }
  103. }
  104.  
  105.