home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume21 / sipp / part03 < prev    next >
Text File  |  1991-07-23  |  42KB  |  1,602 lines

  1. Newsgroups: comp.sources.misc
  2. From: Jonas Yngvesson <jonas-y@isy.liu.se>
  3. Subject:  v21i028:  sipp - A 3D rendering library v2.1, Part03/08
  4. Message-ID: <1991Jul23.181639.27742@sparky.IMD.Sterling.COM>
  5. X-Md4-Signature: 9437cc6be08ae2027e031f34838dc298
  6. Date: Tue, 23 Jul 1991 18:16:39 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: Jonas Yngvesson <jonas-y@isy.liu.se>
  10. Posting-number: Volume 21, Issue 28
  11. Archive-name: sipp/part03
  12. Supersedes: sipp2.0: Volume 16, Issue 5-10
  13. Environment: UNIX
  14.  
  15. #!/bin/sh
  16. # This is part 03 of sipp-2.1
  17. # ============= libsipp/noise.c ==============
  18. if test ! -d 'libsipp'; then
  19.     echo 'x - creating directory libsipp'
  20.     mkdir 'libsipp'
  21. fi
  22. if test -f 'libsipp/noise.c' -a X"$1" != X"-c"; then
  23.     echo 'x - skipping libsipp/noise.c (File already exists)'
  24. else
  25. echo 'x - extracting libsipp/noise.c (Text)'
  26. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/noise.c' &&
  27. #include <math.h>
  28. X
  29. #include <sipp.h>
  30. #include <noise.h>
  31. X
  32. /* ================================================================ */
  33. /*                      Noise and friends                           */
  34. X
  35. X
  36. /*
  37. X *    Noise and Dnoise routines
  38. X *
  39. X *    Many thanks to Jon Buller of Colorado State University for these
  40. X *    routines.
  41. X */
  42. X
  43. X
  44. #define NUMPTS    512
  45. #define P1    173
  46. #define P2    263
  47. #define P3    337
  48. #define phi    0.6180339
  49. X
  50. X
  51. bool noise_ready = FALSE;
  52. X
  53. static double pts[NUMPTS];
  54. X
  55. X
  56. /*
  57. X * Doubles might have values that is too large to be
  58. X * stored in an int. If this is the case we "fold" the
  59. X * value about MAXINT or MININT until it is of an
  60. X * acceptable size.
  61. X *
  62. X * NOTE: 32 bit integers is assumed.
  63. X */
  64. static double
  65. iadjust(f)
  66. X    double f;
  67. {
  68. X    while ((f > 2147483647.0) || (f < -2147483648.0)) {
  69. X        if (f > 2147483647.0) {         /* 2**31 - 1 */
  70. X            f = (4294967294.0 - f);     /* 2**32 - 2 */
  71. X        } else if (f < -2147483648.0){
  72. X            f = (-4294967296.0 - f);
  73. X        }
  74. X    }
  75. X    return f;
  76. }
  77. X
  78. X
  79. X
  80. /*
  81. X * Initialize the array of random numbers.
  82. X * RANDOM() is defined in sipp.h
  83. X */
  84. void noise_init()
  85. {
  86. X    int i;
  87. X   
  88. X    for (i = 0; i < NUMPTS; ++i)
  89. X        pts[i] = RANDOM();
  90. X    noise_ready = TRUE;
  91. }
  92. X
  93. X
  94. X
  95. double noise(v)
  96. Vector *v;
  97. {
  98. X   Vector p;
  99. X   int xi, yi, zi;
  100. X   int xa, xb, xc, ya, yb, yc, za, zb, zc;
  101. X   double xf, yf, zf;
  102. X   double x2, x1, x0, y2, y1, y0, z2, z1, z0;
  103. X   double p000, p100, p200, p010, p110, p210, p020, p120, p220;
  104. X   double p001, p101, p201, p011, p111, p211, p021, p121, p221;
  105. X   double p002, p102, p202, p012, p112, p212, p022, p122, p222;
  106. X   double tmp;
  107. X
  108. X   p = *v;
  109. X
  110. X   p.x = iadjust(p.x);
  111. X   p.y = iadjust(p.y);
  112. X   p.z = iadjust(p.z);
  113. X   xi = floor(p.x);
  114. X   xa = floor(P1 * (xi * phi - floor(xi * phi)));
  115. X   xb = floor(P1 * ((xi + 1) * phi - floor((xi + 1) * phi)));
  116. X   xc = floor(P1 * ((xi + 2) * phi - floor((xi + 2) * phi)));
  117. X
  118. X   yi = floor(p.y);
  119. X   ya = floor(P2 * (yi * phi - floor(yi * phi)));
  120. X   yb = floor(P2 * ((yi + 1) * phi - floor((yi + 1) * phi)));
  121. X   yc = floor(P2 * ((yi + 2) * phi - floor((yi + 2) * phi)));
  122. X
  123. X   zi = floor(p.z);
  124. X   za = floor(P3 * (zi * phi - floor(zi * phi)));
  125. X   zb = floor(P3 * ((zi + 1) * phi - floor((zi + 1) * phi)));
  126. X   zc = floor(P3 * ((zi + 2) * phi - floor((zi + 2) * phi)));
  127. X
  128. X   p000 = pts[(xa + ya + za) % NUMPTS];
  129. X   p100 = pts[(xb + ya + za) % NUMPTS];
  130. X   p200 = pts[(xc + ya + za) % NUMPTS];
  131. X   p010 = pts[(xa + yb + za) % NUMPTS];
  132. X   p110 = pts[(xb + yb + za) % NUMPTS];
  133. X   p210 = pts[(xc + yb + za) % NUMPTS];
  134. X   p020 = pts[(xa + yc + za) % NUMPTS];
  135. X   p120 = pts[(xb + yc + za) % NUMPTS];
  136. X   p220 = pts[(xc + yc + za) % NUMPTS];
  137. X   p001 = pts[(xa + ya + zb) % NUMPTS];
  138. X   p101 = pts[(xb + ya + zb) % NUMPTS];
  139. X   p201 = pts[(xc + ya + zb) % NUMPTS];
  140. X   p011 = pts[(xa + yb + zb) % NUMPTS];
  141. X   p111 = pts[(xb + yb + zb) % NUMPTS];
  142. X   p211 = pts[(xc + yb + zb) % NUMPTS];
  143. X   p021 = pts[(xa + yc + zb) % NUMPTS];
  144. X   p121 = pts[(xb + yc + zb) % NUMPTS];
  145. X   p221 = pts[(xc + yc + zb) % NUMPTS];
  146. X   p002 = pts[(xa + ya + zc) % NUMPTS];
  147. X   p102 = pts[(xb + ya + zc) % NUMPTS];
  148. X   p202 = pts[(xc + ya + zc) % NUMPTS];
  149. X   p012 = pts[(xa + yb + zc) % NUMPTS];
  150. X   p112 = pts[(xb + yb + zc) % NUMPTS];
  151. X   p212 = pts[(xc + yb + zc) % NUMPTS];
  152. X   p022 = pts[(xa + yc + zc) % NUMPTS];
  153. X   p122 = pts[(xb + yc + zc) % NUMPTS];
  154. X   p222 = pts[(xc + yc + zc) % NUMPTS];
  155. X
  156. X   xf = p.x - xi;
  157. X   x1 = xf * xf;
  158. X   x2 = 0.5 * x1;
  159. X   x1 = 0.5 + xf - x1;
  160. X   x0 = 0.5 - xf + x2;
  161. X
  162. X   yf = p.y - yi;
  163. X   y1 = yf * yf;
  164. X   y2 = 0.5 * y1;
  165. X   y1 = 0.5 + yf - y1;
  166. X   y0 = 0.5 - yf + y2;
  167. X
  168. X   zf = p.z - zi;
  169. X   z1 = zf * zf;
  170. X   z2 = 0.5 * z1;
  171. X   z1 = 0.5 + zf - z1;
  172. X   z0 = 0.5 - zf + z2;
  173. X
  174. X   /*
  175. X    * This expression is split up since some compilers
  176. X    * chokes on expressions of this size.
  177. X    */
  178. X   tmp =  z0 * (y0 * (x0 * p000 + x1 * p100 + x2 * p200) +
  179. X                y1 * (x0 * p010 + x1 * p110 + x2 * p210) +
  180. X                y2 * (x0 * p020 + x1 * p120 + x2 * p220));
  181. X   tmp += z1 * (y0 * (x0 * p001 + x1 * p101 + x2 * p201) +
  182. X                y1 * (x0 * p011 + x1 * p111 + x2 * p211) +
  183. X                y2 * (x0 * p021 + x1 * p121 + x2 * p221));
  184. X   tmp += z2 * (y0 * (x0 * p002 + x1 * p102 + x2 * p202) +
  185. X                y1 * (x0 * p012 + x1 * p112 + x2 * p212) +
  186. X                y2 * (x0 * p022 + x1 * p122 + x2 * p222));
  187. X
  188. X   return tmp;
  189. }
  190. X
  191. X
  192. X
  193. double turbulence(p, octaves)
  194. X    Vector *p;
  195. X    int     octaves;
  196. {
  197. X    Vector tmp;
  198. X    double scale = 1.0;
  199. X    double t     = 0.0;
  200. X
  201. X    while (octaves--) {
  202. X        tmp.x = p->x * scale;
  203. X        tmp.y = p->y * scale;
  204. X        tmp.z = p->z * scale;
  205. X        t += (noise(&tmp) / scale);
  206. X        scale *= 2.0;
  207. X    }
  208. X    return t;
  209. }
  210. X
  211. X
  212. X
  213. Vector Dnoise(p)
  214. Vector *p;
  215. {
  216. X   Vector v;
  217. X   int xi, yi, zi;
  218. X   int xa, xb, xc, ya, yb, yc, za, zb, zc;
  219. X   double xf, yf, zf;
  220. X   double x2, x1, x0, y2, y1, y0, z2, z1, z0;
  221. X   double xd2, xd1, xd0, yd2, yd1, yd0, zd2, zd1, zd0;
  222. X   double p000, p100, p200, p010, p110, p210, p020, p120, p220;
  223. X   double p001, p101, p201, p011, p111, p211, p021, p121, p221;
  224. X   double p002, p102, p202, p012, p112, p212, p022, p122, p222;
  225. X
  226. X   xi = floor(p->x);
  227. X   xa = floor(P1 * (xi * phi - floor(xi * phi)));
  228. X   xb = floor(P1 * ((xi + 1) * phi - floor((xi + 1) * phi)));
  229. X   xc = floor(P1 * ((xi + 2) * phi - floor((xi + 2) * phi)));
  230. X
  231. X   yi = floor(p->y);
  232. X   ya = floor(P2 * (yi * phi - floor(yi * phi)));
  233. X   yb = floor(P2 * ((yi + 1) * phi - floor((yi + 1) * phi)));
  234. X   yc = floor(P2 * ((yi + 2) * phi - floor((yi + 2) * phi)));
  235. X
  236. X   zi = floor(p->z);
  237. X   za = floor(P3 * (zi * phi - floor(zi * phi)));
  238. X   zb = floor(P3 * ((zi + 1) * phi - floor((zi + 1) * phi)));
  239. X   zc = floor(P3 * ((zi + 2) * phi - floor((zi + 2) * phi)));
  240. X
  241. X   p000 = pts[(xa + ya + za) % NUMPTS];
  242. X   p100 = pts[(xb + ya + za) % NUMPTS];
  243. X   p200 = pts[(xc + ya + za) % NUMPTS];
  244. X   p010 = pts[(xa + yb + za) % NUMPTS];
  245. X   p110 = pts[(xb + yb + za) % NUMPTS];
  246. X   p210 = pts[(xc + yb + za) % NUMPTS];
  247. X   p020 = pts[(xa + yc + za) % NUMPTS];
  248. X   p120 = pts[(xb + yc + za) % NUMPTS];
  249. X   p220 = pts[(xc + yc + za) % NUMPTS];
  250. X   p001 = pts[(xa + ya + zb) % NUMPTS];
  251. X   p101 = pts[(xb + ya + zb) % NUMPTS];
  252. X   p201 = pts[(xc + ya + zb) % NUMPTS];
  253. X   p011 = pts[(xa + yb + zb) % NUMPTS];
  254. X   p111 = pts[(xb + yb + zb) % NUMPTS];
  255. X   p211 = pts[(xc + yb + zb) % NUMPTS];
  256. X   p021 = pts[(xa + yc + zb) % NUMPTS];
  257. X   p121 = pts[(xb + yc + zb) % NUMPTS];
  258. X   p221 = pts[(xc + yc + zb) % NUMPTS];
  259. X   p002 = pts[(xa + ya + zc) % NUMPTS];
  260. X   p102 = pts[(xb + ya + zc) % NUMPTS];
  261. X   p202 = pts[(xc + ya + zc) % NUMPTS];
  262. X   p012 = pts[(xa + yb + zc) % NUMPTS];
  263. X   p112 = pts[(xb + yb + zc) % NUMPTS];
  264. X   p212 = pts[(xc + yb + zc) % NUMPTS];
  265. X   p022 = pts[(xa + yc + zc) % NUMPTS];
  266. X   p122 = pts[(xb + yc + zc) % NUMPTS];
  267. X   p222 = pts[(xc + yc + zc) % NUMPTS];
  268. X
  269. X   xf = p->x - xi;
  270. X   x1 = xf * xf;
  271. X   x2 = 0.5 * x1;
  272. X   x1 = 0.5 + xf - x1;
  273. X   x0 = 0.5 - xf + x2;
  274. X   xd2 = xf;
  275. X   xd1 = 1.0 - xf - xf;
  276. X   xd0 = xf - 1.0;
  277. X
  278. X   yf = p->y - yi;
  279. X   y1 = yf * yf;
  280. X   y2 = 0.5 * y1;
  281. X   y1 = 0.5 + yf - y1;
  282. X   y0 = 0.5 - yf + y2;
  283. X   yd2 = yf;
  284. X   yd1 = 1.0 - yf - yf;
  285. X   yd0 = yf - 1.0;
  286. X
  287. X   zf = p->z - zi;
  288. X   z1 = zf * zf;
  289. X   z2 = 0.5 * z1;
  290. X   z1 = 0.5 + zf - z1;
  291. X   z0 = 0.5 - zf + z2;
  292. X   zd2 = zf;
  293. X   zd1 = 1.0 - zf - zf;
  294. X   zd0 = zf - 1.0;
  295. X
  296. X   /*
  297. X    * This expressions are split up since some compilers
  298. X    * chokes on expressions of this size.
  299. X    */
  300. X   v.x        = z0 * (y0 * (xd0 * p000 + xd1 * p100 + xd2 * p200) +
  301. X                      y1 * (xd0 * p010 + xd1 * p110 + xd2 * p210) +
  302. X                      y2 * (xd0 * p020 + xd1 * p120 + xd2 * p220));
  303. X   v.x       += z1 * (y0 * (xd0 * p001 + xd1 * p101 + xd2 * p201) +
  304. X                      y1 * (xd0 * p011 + xd1 * p111 + xd2 * p211) +
  305. X                      y2 * (xd0 * p021 + xd1 * p121 + xd2 * p221));
  306. X   v.x       += z2 * (y0 * (xd0 * p002 + xd1 * p102 + xd2 * p202) +
  307. X                      y1 * (xd0 * p012 + xd1 * p112 + xd2 * p212) +
  308. X                      y2 * (xd0 * p022 + xd1 * p122 + xd2 * p222));
  309. X                                  
  310. X   v.y        = z0 * (yd0 * (x0 * p000 + x1 * p100 + x2 * p200) +
  311. X                      yd1 * (x0 * p010 + x1 * p110 + x2 * p210) +
  312. X                      yd2 * (x0 * p020 + x1 * p120 + x2 * p220));
  313. X   v.y       += z1 * (yd0 * (x0 * p001 + x1 * p101 + x2 * p201) +
  314. X                      yd1 * (x0 * p011 + x1 * p111 + x2 * p211) +
  315. X                      yd2 * (x0 * p021 + x1 * p121 + x2 * p221));
  316. X   v.y       += z2 * (yd0 * (x0 * p002 + x1 * p102 + x2 * p202) +
  317. X                      yd1 * (x0 * p012 + x1 * p112 + x2 * p212) +
  318. X                      yd2 * (x0 * p022 + x1 * p122 + x2 * p222));
  319. X                                  
  320. X   v.z        = zd0 * (y0 * (x0 * p000 + x1 * p100 + x2 * p200) +
  321. X                       y1 * (x0 * p010 + x1 * p110 + x2 * p210) +
  322. X                       y2 * (x0 * p020 + x1 * p120 + x2 * p220));
  323. X   v.z       += zd1 * (y0 * (x0 * p001 + x1 * p101 + x2 * p201) +
  324. X                       y1 * (x0 * p011 + x1 * p111 + x2 * p211) +
  325. X                       y2 * (x0 * p021 + x1 * p121 + x2 * p221));
  326. X   v.z       += zd2 * (y0 * (x0 * p002 + x1 * p102 + x2 * p202) +
  327. X                       y1 * (x0 * p012 + x1 * p112 + x2 * p212) +
  328. X                       y2 * (x0 * p022 + x1 * p122 + x2 * p222));
  329. X
  330. X   return v;
  331. }
  332. X
  333. X
  334. X
  335. /*
  336. X * End of noise routines
  337. X */
  338. SHAR_EOF
  339. chmod 0664 libsipp/noise.c ||
  340. echo 'restore of libsipp/noise.c failed'
  341. Wc_c="`wc -c < 'libsipp/noise.c'`"
  342. test 9232 -eq "$Wc_c" ||
  343.     echo 'libsipp/noise.c: original size 9232, current size' "$Wc_c"
  344. fi
  345. # ============= libsipp/noise.h ==============
  346. if test -f 'libsipp/noise.h' -a X"$1" != X"-c"; then
  347.     echo 'x - skipping libsipp/noise.h (File already exists)'
  348. else
  349. echo 'x - extracting libsipp/noise.h (Text)'
  350. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/noise.h' &&
  351. /*
  352. X * Declarations needed to use noise() and friends...
  353. X */
  354. X
  355. #ifndef _NOISE_H 
  356. #define _NOISE_H 
  357. X
  358. #include <geometric.h>
  359. X
  360. extern void     noise_init();
  361. extern double   noise();
  362. extern double   turbulence();
  363. extern Vector   Dnoise();
  364. X
  365. X
  366. #endif /* _NOISE_H */
  367. SHAR_EOF
  368. chmod 0664 libsipp/noise.h ||
  369. echo 'restore of libsipp/noise.h failed'
  370. Wc_c="`wc -c < 'libsipp/noise.h'`"
  371. test 257 -eq "$Wc_c" ||
  372.     echo 'libsipp/noise.h: original size 257, current size' "$Wc_c"
  373. fi
  374. # ============= libsipp/objects.c ==============
  375. if test -f 'libsipp/objects.c' -a X"$1" != X"-c"; then
  376.     echo 'x - skipping libsipp/objects.c (File already exists)'
  377. else
  378. echo 'x - extracting libsipp/objects.c (Text)'
  379. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/objects.c' &&
  380. /**
  381. X ** sipp - SImple Polygon Processor
  382. X **
  383. X **  A general 3d graphic package
  384. X **
  385. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  386. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  387. X **
  388. X ** This program is free software; you can redistribute it and/or modify
  389. X ** it under the terms of the GNU General Public License as published by
  390. X ** the Free Software Foundation; either version 1, or any later version.
  391. X ** This program is distributed in the hope that it will be useful,
  392. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  393. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  394. X ** GNU General Public License for more details.
  395. X ** You can receive a copy of the GNU General Public License from the
  396. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  397. X **/
  398. X
  399. /**
  400. X ** objects.c - Functions that handles object and surface creation
  401. X **             and the object hierarchies and the object database.
  402. X **/
  403. X
  404. #include <stdio.h>
  405. X
  406. #include <objects.h>
  407. #include <sipp.h>
  408. #include <smalloc.h>
  409. X
  410. X
  411. static Vertex       *vertex_tree;     /* Vertex tree for current object. */
  412. static Vertex_ref   *vertex_stack;    /* Vertex stack for current polygon. */
  413. static Vertex_ref   *vstack_bottom;   /* Last entry in vertex stack. */
  414. static Polygon      *poly_stack;      /* Polygon stack for current object. */
  415. static int           first_vertex;    /* Used when determining if we are   */
  416. X                                      /* installing the first vertex in an */
  417. X                                      /* object. *Not* a boolean!          */
  418. static double        dist_limit;      /* Minimal distance between two      */
  419. X                                      /* vertices without them being       */
  420. X                                      /* considered to be the same vertex. */
  421. X
  422. Inst_object         *object_db;       /* Object database. */
  423. X
  424. X
  425. X
  426. /*
  427. X * Search for a vertex in a vertex tree. Vertices are asumed
  428. X * to be equal if they differ less than dist_limit in all directions.
  429. X *
  430. X * If the vertex is not found, install it in the tree.
  431. X */
  432. static Vertex *
  433. vertex_lookup(x, y, z, u, v, w, p)
  434. X    double   x, y, z, u, v, w;
  435. X    Vertex **p;
  436. {
  437. X    double  xdist, ydist, zdist;
  438. X
  439. X    if (*p == NULL) {
  440. X        *p = (Vertex *)smalloc(sizeof(Vertex));
  441. X        (*p)->x = x;
  442. X        (*p)->y = y;
  443. X        (*p)->z = z;
  444. X        (*p)->a = 0;
  445. X        (*p)->b = 0;
  446. X        (*p)->c = 0;
  447. X        (*p)->u = u;
  448. X        (*p)->v = v;
  449. X        (*p)->w = w;
  450. X        (*p)->big = NULL;
  451. X        (*p)->sml = NULL;
  452. X        return *p;
  453. X    } else if ((xdist = x - ((*p)->x)) > dist_limit) {
  454. X        return (vertex_lookup(x, y, z, u, v, w, &((*p)->big)));
  455. X    } else if (xdist < -dist_limit) {
  456. X        return (vertex_lookup(x, y, z, u, v, w, &((*p)->sml)));
  457. X    } else if ((ydist = y - ((*p)->y)) > dist_limit) {
  458. X        return (vertex_lookup(x, y, z, u, v, w, &((*p)->big)));
  459. X    } else if (ydist < -dist_limit) {
  460. X        return (vertex_lookup(x, y, z, u, v, w, &((*p)->sml)));
  461. X    } else if ((zdist = z - ((*p)->z)) > dist_limit) {
  462. X        return (vertex_lookup(x, y, z, u, v, w, &((*p)->big)));
  463. X    } else if (zdist < -dist_limit) {
  464. X        return (vertex_lookup(x, y, z, u, v, w, &((*p)->sml)));
  465. X    } else {
  466. X        return *p;
  467. X    }
  468. }
  469. X
  470. X
  471. X
  472. /*
  473. X * Push a vertex on the vertex stack (without texture coordinates).
  474. X */
  475. void
  476. vertex_push(x, y, z)
  477. X    double  x, y, z;
  478. {
  479. X    vertex_tx_push(x, y, z, (double)0.0, (double)0.0, (double)0.0);
  480. }
  481. X
  482. X
  483. X
  484. /*
  485. X * Push a vertex on the vertex stack (with texture coordinates).
  486. X */
  487. void
  488. vertex_tx_push(x, y, z, u, v, w)
  489. X    double  x, y, z, u, v, w;
  490. {
  491. X    Vertex_ref *vref;
  492. X
  493. X   /* 
  494. X    * To get a reasonable dist_limit we use the following "heuristic" 
  495. X    * value:
  496. X    * The distance between the first two vertices installed in
  497. X    * the surface, multiplied by the magic number 1e-10, unless
  498. X    * they are the same vertex. In that case 1e-10 is used until
  499. X    * we get a vertex that differs from the first.
  500. X    */
  501. X    if (!first_vertex)
  502. X        first_vertex++;
  503. X    else if (first_vertex == 1) {
  504. X        dist_limit = sqrt((x - vertex_tree->x) * (x - vertex_tree->x)
  505. X                        + (y - vertex_tree->y) * (y - vertex_tree->y)
  506. X                        + (z - vertex_tree->z) * (z - vertex_tree->z))
  507. X                   * 1e-10;                      /* Magic!!! */
  508. X        if (dist_limit != 0.0)
  509. X            first_vertex++;
  510. X        else
  511. X            dist_limit = 1e-10;                  /* More Magic */
  512. X    }
  513. X    vref = (Vertex_ref *)smalloc(sizeof(Vertex_ref));
  514. X    if (vertex_stack == NULL) {
  515. X        vertex_stack = vref;
  516. X    } else {
  517. X        vstack_bottom->next = vref;
  518. X    }
  519. X    vstack_bottom = vref;
  520. X    vref->vertex = vertex_lookup(x, y, z, u, v, w, &vertex_tree);
  521. X    vref->next = NULL;
  522. }
  523. X
  524. X
  525. X
  526. /*
  527. X * Push a polygon on the polygon stack. Empty the vertex stack afterwards.
  528. X */
  529. void
  530. polygon_push()
  531. {
  532. X    Polygon *polyref;
  533. X
  534. X    if (vertex_stack != NULL) {
  535. X        polyref = (Polygon *)smalloc(sizeof(Polygon));
  536. X        polyref->vertices = vertex_stack;
  537. X        polyref->backface = 0;
  538. X        polyref->next = poly_stack;
  539. X        poly_stack = polyref;
  540. X        vertex_stack = NULL;
  541. X    }
  542. }
  543. X
  544. X
  545. X
  546. /*
  547. X * Create a surface of all polygons in the polygon stack.
  548. X * Empty the polygon stack afterwards.
  549. X */
  550. Surface *
  551. surface_create(surf_desc, shader)
  552. X    void   *surf_desc;
  553. X    Shader *shader;
  554. {
  555. X    Surface *surfref;
  556. X    
  557. X    if (poly_stack != NULL) {
  558. X        surfref = (Surface *)smalloc(sizeof(Surface));
  559. X        surfref->vertices = vertex_tree;
  560. X        surfref->polygons = poly_stack;
  561. X        surfref->surface = surf_desc;
  562. X        surfref->shader = shader;
  563. X        surfref->ref_count = 0;
  564. X        surfref->next = NULL;
  565. X        vertex_tree = NULL;
  566. X        poly_stack = NULL;
  567. X        first_vertex = 0;
  568. X        return surfref;
  569. X    } else
  570. X        return NULL;
  571. }
  572. X
  573. X
  574. X
  575. /*
  576. X * Create a surface to be shaded with the simple shader.
  577. X */
  578. Surface *
  579. surface_basic_create(ambient, red, grn, blu, specular, c3)
  580. X    double ambient, red, grn, blu, specular, c3;
  581. {
  582. X    Surf_desc *surf_desc;
  583. X
  584. X    surf_desc = (Surf_desc *)smalloc(sizeof(Surf_desc));
  585. X    surf_desc->ambient = ambient;
  586. X    surf_desc->color.red = red;
  587. X    surf_desc->color.grn = grn;
  588. X    surf_desc->color.blu = blu;
  589. X    surf_desc->specular = specular;
  590. X    surf_desc->c3 = c3;
  591. X    return surface_create(surf_desc, basic_shader);
  592. }
  593. X
  594. X
  595. X
  596. /*
  597. X * Set SURFACE to be shaded with the shading function SHADER
  598. X * using the surface description SURF_DESC.
  599. X */
  600. void
  601. surface_set_shader(surface, surf_desc, shader)
  602. X    Surface *surface;
  603. X    void    *surf_desc;
  604. X    Shader  *shader;
  605. {
  606. X    
  607. X    if (surface != NULL) {
  608. X        surface->surface = surf_desc;
  609. X        surface->shader = shader;
  610. X    }
  611. }
  612. X
  613. X
  614. X
  615. /*
  616. X * Set SURFACE to be shaded with the simple shader.
  617. X */
  618. void
  619. surface_basic_shader(surface, ambient, red, grn, blu, specular, c3)
  620. X    Surface *surface;
  621. X    double  ambient, red, grn, blu, specular, c3;
  622. {
  623. X    Surf_desc *surf_desc;
  624. X
  625. X    surf_desc = (Surf_desc *)smalloc(sizeof(Surf_desc));
  626. X    surf_desc->ambient = ambient;
  627. X    surf_desc->color.red = red;
  628. X    surf_desc->color.grn = grn;
  629. X    surf_desc->color.blu = blu;
  630. X    surf_desc->specular = specular;
  631. X    surf_desc->c3 = c3;
  632. X    surface_set_shader(surface, surf_desc, basic_shader);
  633. }
  634. X
  635. X
  636. X
  637. /*
  638. X * Copy a vertex tree.
  639. X */
  640. static Vertex *
  641. copy_vertices(vp)
  642. X    Vertex *vp;
  643. {
  644. X    Vertex *tmp;
  645. X
  646. X    if (vp == NULL)
  647. X        return NULL;
  648. X    tmp = (Vertex *)smalloc(sizeof(Vertex));
  649. X    *tmp = *vp;
  650. X    tmp->big = copy_vertices(vp->big);
  651. X    tmp->sml = copy_vertices(vp->sml);
  652. X    return tmp;
  653. }
  654. X
  655. X
  656. X
  657. /*
  658. X * We have a list of vertes references, each pointing into a certain
  659. X * vertex tree. Create a new list with pointers into a copy of the
  660. X * first vertex tree.
  661. X */
  662. static Vertex_ref *
  663. copy_vlist(vp, surface)
  664. X    Vertex_ref *vp;
  665. X    Surface    *surface;
  666. {
  667. X    Vertex_ref *tmp;
  668. X    
  669. X    if (vp == NULL)
  670. X        return NULL;
  671. X    tmp = (Vertex_ref *)smalloc(sizeof(Vertex_ref));
  672. X    tmp->vertex = vertex_lookup(vp->vertex->x, vp->vertex->y, vp->vertex->z,
  673. X                                vp->vertex->u, vp->vertex->v, vp->vertex->w,
  674. X                                &(surface->vertices));
  675. X    tmp->next = copy_vlist(vp->next, surface);
  676. X    return tmp;
  677. }
  678. X
  679. X
  680. X
  681. /*
  682. X * Copy a list of polygons.
  683. X */
  684. static Polygon *
  685. copy_polygons(pp, surface)
  686. X    Polygon *pp;
  687. X    Surface *surface;
  688. {
  689. X    Polygon *tmp;
  690. X
  691. X    if (pp == NULL)
  692. X        return NULL;
  693. X    tmp = (Polygon *)smalloc(sizeof(Polygon));
  694. X    tmp->vertices = copy_vlist(pp->vertices, surface);
  695. X    tmp->next = copy_polygons(pp->next, surface);
  696. X    return tmp;
  697. }
  698. X
  699. X
  700. X
  701. /*
  702. X * Copy a list of surfaces. All polygons and vertices are copied but
  703. X * the shader and surface descriptions are the same as in the
  704. X * original surfaces.
  705. X */
  706. static Surface *
  707. surface_copy(surface)
  708. X    Surface  *surface;
  709. {
  710. X    Surface  *newsurf;
  711. X
  712. X    if (surface != NULL) {
  713. X        newsurf = (Surface *)smalloc(sizeof(Surface));
  714. X        if (newsurf == NULL) {
  715. X            return NULL;
  716. X        }
  717. X        memcpy(newsurf, surface, sizeof(Surface));
  718. X        newsurf->vertices = copy_vertices(surface->vertices);
  719. X        newsurf->polygons = copy_polygons(surface->polygons, newsurf);
  720. X        newsurf->ref_count = 1;
  721. X        newsurf->next = surface_copy(surface->next);
  722. X        return newsurf;
  723. X    } else {
  724. X        return NULL;
  725. X    }
  726. }
  727. X
  728. X
  729. X
  730. /*
  731. X * Delete a vertex tree.
  732. X */
  733. static void
  734. delete_vertices(vtree)
  735. X    Vertex **vtree;
  736. {
  737. X    if (*vtree != NULL) {
  738. X        delete_vertices(&((*vtree)->big));
  739. X        delete_vertices(&((*vtree)->sml));
  740. X        free(*vtree);
  741. X        *vtree = NULL;
  742. X    }
  743. }
  744. X
  745. X
  746. X
  747. /*
  748. X * Delete a surface list.
  749. X */
  750. static void
  751. surface_delete(surface)
  752. X    Surface *surface;
  753. {
  754. X    Vertex_ref *vref1, *vref2;
  755. X    Polygon    *polyref1, *polyref2;
  756. X
  757. X    if (surface != NULL) {
  758. X        if (--surface->ref_count == 0) {
  759. X            if (surface->next != NULL) {
  760. X                surface_delete(surface->next);
  761. X            }
  762. X            polyref2 = surface->polygons;
  763. X            while (polyref2 != NULL) {
  764. X                vref2 = polyref2->vertices;
  765. X                while (vref2 != NULL) {
  766. X                    vref1 = vref2;
  767. X                    vref2 = vref2->next;
  768. X                    free(vref1);
  769. X                }
  770. X                polyref1 = polyref2;
  771. X                polyref2 = polyref2->next;
  772. X                free(polyref1);
  773. X            }
  774. X            delete_vertices(&(surface->vertices));
  775. X            free(surface);
  776. X        }
  777. X    }
  778. }
  779. X
  780. X
  781. X
  782. /*
  783. X * Install an object in the rendering database.
  784. X */
  785. static void
  786. r_object_install(obj, obj_tree)
  787. X    Object       *obj;
  788. X    Inst_object **obj_tree;
  789. {
  790. X    if (obj != NULL) {
  791. X        if (*obj_tree == NULL) {
  792. X            obj->ref_count++;
  793. X            *obj_tree = (Inst_object *)smalloc(sizeof(Inst_object));
  794. X            (*obj_tree)->object = obj;
  795. X            (*obj_tree)->big = NULL;
  796. X            (*obj_tree)->sml = NULL;
  797. X        } else if (obj > (*obj_tree)->object) {
  798. X            r_object_install(obj, &(*obj_tree)->big);
  799. X        } else if (obj < (*obj_tree)->object) {
  800. X            r_object_install(obj, &(*obj_tree)->sml);
  801. X        }
  802. X    }
  803. }
  804. X
  805. X
  806. X
  807. /*
  808. X * Interface to r_object_install(). (Why are there no
  809. X * subfunctions in C?...)
  810. X */
  811. void
  812. object_install(obj)
  813. X    Object *obj;
  814. {
  815. X    r_object_install(obj, &object_db);
  816. }
  817. X
  818. X
  819. X
  820. /*
  821. X * Subfunction to r_object_uninstall.
  822. X */
  823. static void
  824. r_del(r, q)
  825. X    Inst_object **r;
  826. X    Inst_object  *q;
  827. {
  828. X    if ((*r)->big != NULL) {
  829. X        r_del(&((*r)->big), q);
  830. X    } else {
  831. X        q->object = (*r)->object;
  832. X        q = *r;
  833. X        *r = (*r)->sml;
  834. X        free(q);
  835. X    }
  836. }
  837. X
  838. X
  839. X
  840. /*
  841. X * Delete an object from the rendering database.
  842. X * The object itself is not deleted of course.
  843. X */
  844. static void
  845. r_object_uninstall(obj, root)
  846. X    Object       *obj;
  847. X    Inst_object **root;
  848. {
  849. X    Inst_object *ptr;
  850. X
  851. X    if (*root == NULL) {
  852. X        return;            /* Object is not in the tree */
  853. X    } else if (obj < (*root)->object) {
  854. X        r_object_uninstall(obj, &(*root)->sml);
  855. X    } else if (obj > (*root)->object) {
  856. X        r_object_uninstall(obj, &(*root)->big);
  857. X    } else {
  858. X        obj->ref_count--;
  859. X        ptr = *root;
  860. X        if (ptr->big == NULL) {
  861. X            *root = ptr->sml;
  862. X        } else if (ptr->sml == NULL) {
  863. X            *root = ptr->big;
  864. X        } else {
  865. X            r_del(&ptr->sml, ptr);
  866. X        }
  867. X    }
  868. }
  869. X
  870. X    
  871. X    
  872. /*
  873. X * Interface to r_object_uninstall.
  874. X */
  875. void
  876. object_uninstall(obj)
  877. X    Object *obj;
  878. {
  879. X    r_object_uninstall(obj, &object_db);
  880. }
  881. X
  882. X
  883. X
  884. /*
  885. X * Create an empty object. Before it is rendered it
  886. X * must get a surface or a subobject. 
  887. X */
  888. Object *
  889. object_create()
  890. {
  891. X    Object *obj;
  892. X
  893. X    obj = (Object *)smalloc(sizeof(Object));
  894. X    obj->surfaces = NULL;
  895. X    obj->sub_obj = NULL;
  896. X    MatCopy(&obj->transf, &ident_matrix);
  897. X    obj->ref_count = 0;
  898. X    obj->next = NULL;
  899. X
  900. X    return obj;
  901. }
  902. X
  903. X
  904. X
  905. /*
  906. X * Copy the top object in an object hierarchy.
  907. X * The new object will reference the same
  908. X * subobjects and surfaces as the original object.
  909. X * if REF_COUNT_UPDATE is true, the reference counts
  910. X * in the surfaces and subobjects will be incremented.
  911. X */
  912. static Object *
  913. object_copy(object, ref_count_update)
  914. X    Object *object;
  915. X    bool    ref_count_update;
  916. {
  917. X    Object *newobj;
  918. X
  919. X    if (object == NULL) {
  920. X        return NULL;
  921. X    }
  922. X
  923. X    if ((newobj = (Object *)smalloc(sizeof(Object))) != NULL) {
  924. X        memcpy(newobj, object, sizeof(Object));
  925. X        if (ref_count_update) {
  926. X            if (newobj->sub_obj != NULL) {
  927. X                newobj->sub_obj->ref_count++;
  928. X            }
  929. X            if (newobj->surfaces != NULL) {
  930. X                newobj->surfaces->ref_count++;
  931. X            }
  932. X        }
  933. X        MatCopy(&newobj->transf, &ident_matrix);
  934. X        newobj->ref_count = 0;
  935. X        newobj->next = NULL;
  936. X    }
  937. X
  938. X    return newobj;
  939. }
  940. X
  941. X
  942. X
  943. /*
  944. X * Copy a list of objects. If SURF_COPY is true
  945. X * the surfaces in the objects will be copied too.
  946. X */
  947. static Object *
  948. object_list_copy(object, surf_copy)
  949. X    Object *object;
  950. X    bool    surf_copy;
  951. {
  952. X    Object *newobj;
  953. X
  954. X    if (object == NULL) {
  955. X        return NULL;
  956. X    }
  957. X
  958. X    if ((newobj = (Object *)smalloc(sizeof(Object))) != NULL) {
  959. X        memcpy(newobj, object, sizeof(Object));
  960. X        newobj->ref_count = 0;
  961. X    } else {
  962. X        return NULL;
  963. X    }
  964. X
  965. X    if (surf_copy) {
  966. X        newobj->surfaces = surface_copy(object->surfaces);
  967. X    } else if (newobj->surfaces != NULL){
  968. X        newobj->surfaces->ref_count++;
  969. X    }
  970. X
  971. X    newobj->sub_obj = object_list_copy(object->sub_obj, surf_copy);
  972. X    if (newobj->sub_obj != NULL) {
  973. X        newobj->sub_obj->ref_count++;
  974. X    }
  975. X    newobj->next = object_list_copy(object->next, surf_copy);
  976. X    if (newobj->next != NULL) {
  977. X        newobj->next->ref_count++;
  978. X    }
  979. X
  980. X    return newobj;
  981. }
  982. X    
  983. X
  984. X
  985. /*
  986. X * Copy the top node of an object hierarchy. The
  987. X * subobjects and surface references will be the
  988. X * same as in the original.
  989. X */
  990. Object *
  991. object_instance(obj)
  992. X    Object *obj;
  993. {
  994. X    return object_copy(obj, TRUE);
  995. }
  996. X
  997. X
  998. X
  999. /*
  1000. X * Copy an object hierarchy. The objects in
  1001. X * the new hierarchy will reference the same
  1002. X * surfaces as the object in
  1003. X * the old hierarchy, but all object nodes
  1004. X * will be duplicated.
  1005. X */
  1006. Object *
  1007. object_dup(object)
  1008. X    Object *object;
  1009. {
  1010. X    Object *newobj;
  1011. X
  1012. X    if ((newobj = object_copy(object, FALSE)) == NULL) {
  1013. X        return NULL;
  1014. X    }
  1015. X
  1016. X    newobj->sub_obj = object_list_copy(object->sub_obj, FALSE);
  1017. X    newobj->next = NULL;
  1018. X
  1019. X    return newobj;
  1020. }
  1021. X
  1022. X
  1023. /*
  1024. X * Copy an object hierarchy. All object nodes
  1025. X * and surfaces in the old hierarchy
  1026. X * will be duplicated.
  1027. X */
  1028. Object *
  1029. object_deep_dup(object)
  1030. X    Object *object;
  1031. {
  1032. X    Object *newobj;
  1033. X
  1034. X    if ((newobj = object_copy(object, FALSE)) == NULL) {
  1035. X        return NULL;
  1036. X    }
  1037. X
  1038. X    newobj->surfaces = surface_copy(object->surfaces);
  1039. X    newobj->sub_obj = object_list_copy(object->sub_obj, TRUE);
  1040. X    newobj->next = NULL;
  1041. X
  1042. X    return newobj;
  1043. }
  1044. X
  1045. X
  1046. X
  1047. /*
  1048. X * Recursively delete an object hierarchy. Reference
  1049. X * counts are decremented and if the result is zero
  1050. X * the recursion continues and the memory used is freed.
  1051. X */
  1052. static void
  1053. r_object_delete(object)
  1054. X    Object * object;
  1055. {
  1056. X    if (object != NULL) {
  1057. X        if (--object->ref_count == 0) {
  1058. X            surface_delete(object->surfaces);
  1059. X            r_object_delete(object->sub_obj);
  1060. X            r_object_delete(object->next);
  1061. X            free(object);
  1062. X        }
  1063. X    }
  1064. }
  1065. X
  1066. X
  1067. X
  1068. /*
  1069. X * Delete an object hierarchy. This is only possible
  1070. X * to do on a top level object.
  1071. X */
  1072. void
  1073. object_delete(object)
  1074. X    Object * object;
  1075. {
  1076. X    if (object != NULL) {
  1077. X        if (object->ref_count == 0) {         /* Is it a top level object? */
  1078. X            surface_delete(object->surfaces);
  1079. X            r_object_delete(object->sub_obj);
  1080. X            r_object_delete(object->next);
  1081. X            free(object);
  1082. X        }
  1083. X    }
  1084. }
  1085. X
  1086. X
  1087. X
  1088. /*
  1089. X * Remove SUBOBJ as a subobject in OBJECT. SUBOBJ is only
  1090. X * removed from the list of subojects in OBJECT. If the
  1091. X * memory is uses should be freed, object_delete() must
  1092. X * be used.
  1093. X */
  1094. void
  1095. object_sub_subobj(object, subobj)
  1096. X    Object *object, *subobj;
  1097. {
  1098. X    Object *oref1;
  1099. X    Object *oref2;
  1100. X
  1101. X    if (object == NULL || subobj == NULL || object->sub_obj == NULL) {
  1102. X        return;
  1103. X    }
  1104. X
  1105. X    if (object->sub_obj == subobj) {
  1106. X        object->sub_obj = subobj->next;
  1107. X    } else {
  1108. X        oref1 = object->sub_obj;
  1109. X        oref2 = oref1->next;
  1110. X        while (oref2 != NULL && oref2 != subobj) {
  1111. X            oref1 = oref2;
  1112. X            oref2 = oref2->next;
  1113. X        }
  1114. X        if (oref2 == subobj) {
  1115. X            oref1->next = oref2->next;
  1116. X        }
  1117. X    }
  1118. X
  1119. X    subobj->ref_count--;
  1120. }
  1121. X
  1122. X
  1123. X
  1124. /*
  1125. X * Add SUBOBJ as a subobject in OBJECT. SUBOBJ is appended
  1126. X * on the *end* of OBJECT's subobject list, 
  1127. X * so that if SUBOBJ, for some obscure reason, 
  1128. X * were the head of an object list, we don't loose
  1129. X * the rest of that list. 
  1130. X * Remove SUBOBJ from the rendering database since it is no
  1131. X * longer a root object in an hierarchy.
  1132. X */
  1133. void
  1134. object_add_subobj(object, subobj)
  1135. X    Object *object, *subobj;
  1136. {
  1137. X    Object *oref;
  1138. X
  1139. X    if (object == NULL || subobj == NULL) {
  1140. X        return;
  1141. X    }
  1142. X
  1143. X    if (object->sub_obj == NULL) {
  1144. X        object->sub_obj = subobj;
  1145. X    } else {
  1146. X        oref = object->sub_obj;
  1147. X        while (oref->next != NULL) {
  1148. X            oref = oref->next;
  1149. X        }
  1150. X        oref->next = subobj;
  1151. X    }
  1152. X
  1153. X    subobj->ref_count++;
  1154. X    object_uninstall(subobj);
  1155. }
  1156. X
  1157. X
  1158. X
  1159. /*
  1160. X * Remove SURFACE as a surface in OBJECT.
  1161. X */
  1162. void
  1163. object_sub_surface(object, surface)
  1164. X    Object   *object;
  1165. X    Surface  *surface;
  1166. {
  1167. X    Surface *sref1;
  1168. X    Surface *sref2;
  1169. X
  1170. X    if (object == NULL || surface == NULL || object->surfaces == NULL) {
  1171. X        return;
  1172. X    }
  1173. X
  1174. X    if (object->surfaces == surface) {
  1175. X        object->surfaces = surface->next;
  1176. X    } else {
  1177. X        sref1 = object->surfaces;
  1178. X        sref2 = sref1->next;
  1179. X        while (sref2 != NULL && sref2 != surface) {
  1180. X            sref1 = sref2;
  1181. X            sref2 = sref2->next;
  1182. X        }
  1183. X        if (sref2 == surface) {
  1184. X            sref1->next = sref2->next;
  1185. X        }
  1186. X    }
  1187. X
  1188. X    surface->ref_count--;
  1189. }
  1190. X
  1191. X
  1192. X
  1193. /*
  1194. X * Add SURFACE to the list of surfaces belonging
  1195. X * to OBJECT. SURFACE is appended on the *end* of the
  1196. X * list for the same reasons as in object_add_subobj.
  1197. X */
  1198. void
  1199. object_add_surface(object, surface)
  1200. X    Object  *object;
  1201. X    Surface *surface;
  1202. {
  1203. X    Surface *sref;
  1204. X
  1205. X    if (object == NULL || surface == NULL) {
  1206. X        return;
  1207. X    }
  1208. X
  1209. X    if (object->surfaces == NULL) {
  1210. X        object->surfaces = surface;
  1211. X    } else {
  1212. X        sref = object->surfaces;
  1213. X        while (sref->next != NULL) {
  1214. X            sref = sref->next;
  1215. X        }
  1216. X        sref->next = surface;
  1217. X    }
  1218. X
  1219. X    surface->ref_count++;
  1220. }
  1221. X    
  1222. X    
  1223. X    
  1224. /*
  1225. X * Initialize the data structures.
  1226. X */
  1227. void
  1228. objects_init()
  1229. {
  1230. X    vertex_tree    = NULL;
  1231. X    vertex_stack   = NULL;
  1232. X    first_vertex   = 0;
  1233. X    poly_stack     = NULL;
  1234. X    object_db      = NULL;
  1235. }
  1236. SHAR_EOF
  1237. chmod 0644 libsipp/objects.c ||
  1238. echo 'restore of libsipp/objects.c failed'
  1239. Wc_c="`wc -c < 'libsipp/objects.c'`"
  1240. test 19672 -eq "$Wc_c" ||
  1241.     echo 'libsipp/objects.c: original size 19672, current size' "$Wc_c"
  1242. fi
  1243. # ============= libsipp/objects.h ==============
  1244. if test -f 'libsipp/objects.h' -a X"$1" != X"-c"; then
  1245.     echo 'x - skipping libsipp/objects.h (File already exists)'
  1246. else
  1247. echo 'x - extracting libsipp/objects.h (Text)'
  1248. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/objects.h' &&
  1249. /**
  1250. X ** sipp - SImple Polygon Processor
  1251. X **
  1252. X **  A general 3d graphic package
  1253. X **
  1254. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1255. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1256. X **
  1257. X ** This program is free software; you can redistribute it and/or modify
  1258. X ** it under the terms of the GNU General Public License as published by
  1259. X ** the Free Software Foundation; either version 1, or any later version.
  1260. X ** This program is distributed in the hope that it will be useful,
  1261. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1262. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1263. X ** GNU General Public License for more details.
  1264. X ** You can receive a copy of the GNU General Public License from the
  1265. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1266. X **/
  1267. X
  1268. /**
  1269. X ** objects.h - Types and interface to objects.c
  1270. X **/
  1271. X
  1272. #ifndef OBJECT_H
  1273. #define OBJECT_H
  1274. X
  1275. #include <sipp.h>
  1276. X
  1277. X
  1278. /*
  1279. X * Objects installed in the database for rendering are kept
  1280. X * in a binary tree, internal to sipp. This database will
  1281. X * automatically contain all top level objects, but the user
  1282. X * can also force objects in or out of it.
  1283. X */
  1284. typedef struct inst_obj_t {
  1285. X    Object *object;
  1286. X    struct inst_obj_t *big;
  1287. X    struct inst_obj_t *sml;
  1288. } Inst_object;
  1289. X
  1290. X
  1291. extern Inst_object         *object_db;       /* Object database. */
  1292. X
  1293. X
  1294. extern void   objects_init();
  1295. X
  1296. X
  1297. #endif /* OBJECT_H */
  1298. SHAR_EOF
  1299. chmod 0644 libsipp/objects.h ||
  1300. echo 'restore of libsipp/objects.h failed'
  1301. Wc_c="`wc -c < 'libsipp/objects.h'`"
  1302. test 1430 -eq "$Wc_c" ||
  1303.     echo 'libsipp/objects.h: original size 1430, current size' "$Wc_c"
  1304. fi
  1305. # ============= libsipp/patchlevel.h ==============
  1306. if test -f 'libsipp/patchlevel.h' -a X"$1" != X"-c"; then
  1307.     echo 'x - skipping libsipp/patchlevel.h (File already exists)'
  1308. else
  1309. echo 'x - extracting libsipp/patchlevel.h (Text)'
  1310. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/patchlevel.h' &&
  1311. #define PATCHLEVEL ""
  1312. SHAR_EOF
  1313. chmod 0664 libsipp/patchlevel.h ||
  1314. echo 'restore of libsipp/patchlevel.h failed'
  1315. Wc_c="`wc -c < 'libsipp/patchlevel.h'`"
  1316. test 22 -eq "$Wc_c" ||
  1317.     echo 'libsipp/patchlevel.h: original size 22, current size' "$Wc_c"
  1318. fi
  1319. # ============= libsipp/planet.c ==============
  1320. if test -f 'libsipp/planet.c' -a X"$1" != X"-c"; then
  1321.     echo 'x - skipping libsipp/planet.c (File already exists)'
  1322. else
  1323. echo 'x - extracting libsipp/planet.c (Text)'
  1324. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/planet.c' &&
  1325. #include <stdio.h>
  1326. #include <math.h>
  1327. X
  1328. #include <sipp.h>
  1329. #include <geometric.h>
  1330. #include <noise.h>
  1331. X
  1332. X
  1333. /* A reasonably nice brown color */
  1334. static Color  land = {0.28125, 0.1875, 0.09375};
  1335. X
  1336. /* Oceans are usually blue */
  1337. static Color  sea = {0.0, 0.0, 1.0};
  1338. X
  1339. /* And Clouds are white */
  1340. static Color  cloud = {1.0, 1.0, 1.0};
  1341. X
  1342. X
  1343. /* 
  1344. X * This was designed to work with a unit sphere.  
  1345. X * 
  1346. X * Thanks to Jon Buller       jonb@vector.dallas.tx.us
  1347. X */
  1348. double
  1349. turb(size, scale_factor, loc)
  1350. X    int size;
  1351. X    double scale_factor;
  1352. X    Vector loc;
  1353. {
  1354. X    double cur_scale, result;
  1355. X    int cur;
  1356. X
  1357. X    result = noise(&loc);
  1358. X    cur_scale = 1.0;
  1359. X
  1360. X    cur = 1;
  1361. X    while (cur < size) {
  1362. X        cur <<= 1;
  1363. X        cur_scale = cur_scale * scale_factor;
  1364. X        loc.x *= 2.0;
  1365. X        loc.y *= 2.0;
  1366. X        loc.z *= 2.0;
  1367. X        result += noise(&loc) * cur_scale;
  1368. X    }
  1369. X    return result;
  1370. }
  1371. X
  1372. X
  1373. X
  1374. extern bool noise_ready;
  1375. X
  1376. void
  1377. planet_shader(nx, ny, nz, u, v, w, view_vec, lights, sd, color)
  1378. X    double        nx, ny, nz, u, v, w;
  1379. X    Vector        view_vec;
  1380. X    Lightsource  *lights;
  1381. X    Surf_desc    *sd;
  1382. X    Color        *color;
  1383. {
  1384. X    Vector  tmp;
  1385. X    double  amt;
  1386. X
  1387. X    if (!noise_ready) {
  1388. X        noise_init();
  1389. X    }
  1390. X
  1391. X    tmp.x = u;
  1392. X    tmp.y = v;
  1393. X    tmp.z = w;
  1394. X
  1395. X    if (turb(430, 0.7, tmp) > 0.15)
  1396. X        sd->color = land;
  1397. X    else 
  1398. X        sd->color = sea;
  1399. X
  1400. X    VecScalMul(tmp, 12.0, tmp)
  1401. X
  1402. X    amt = turb(18, 0.6, tmp);
  1403. X    if (amt > -0.25) {
  1404. X        amt += 0.25;
  1405. X        sd->color.red += amt * (cloud.red - sd->color.red);
  1406. X        sd->color.grn += amt * (cloud.grn - sd->color.grn);
  1407. X        sd->color.blu += amt * (cloud.blu - sd->color.blu);
  1408. X    }
  1409. X
  1410. X    basic_shader(nx, ny, nz, u, v, w, view_vec, lights, sd, color);
  1411. }
  1412. SHAR_EOF
  1413. chmod 0664 libsipp/planet.c ||
  1414. echo 'restore of libsipp/planet.c failed'
  1415. Wc_c="`wc -c < 'libsipp/planet.c'`"
  1416. test 1700 -eq "$Wc_c" ||
  1417.     echo 'libsipp/planet.c: original size 1700, current size' "$Wc_c"
  1418. fi
  1419. # ============= libsipp/primitives.h ==============
  1420. if test -f 'libsipp/primitives.h' -a X"$1" != X"-c"; then
  1421.     echo 'x - skipping libsipp/primitives.h (File already exists)'
  1422. else
  1423. echo 'x - extracting libsipp/primitives.h (Text)'
  1424. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/primitives.h' &&
  1425. /**
  1426. X ** sipp - SImple Polygon Processor
  1427. X **
  1428. X **  A general 3d graphic package
  1429. X **
  1430. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1431. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1432. X **
  1433. X ** This program is free software; you can redistribute it and/or modify
  1434. X ** it under the terms of the GNU General Public License as published by
  1435. X ** the Free Software Foundation; either version 1, or any later version.
  1436. X ** This program is distributed in the hope that it will be useful,
  1437. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1438. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1439. X ** GNU General Public License for more details.
  1440. X ** You can receive a copy of the GNU General Public License from the
  1441. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1442. X **/
  1443. X
  1444. /**
  1445. X ** primitives.h - Interface to the various primitive object functions.
  1446. X **/
  1447. X
  1448. #ifndef PRIMITIVES_H
  1449. #define PRIMITIVES_H
  1450. X
  1451. X
  1452. extern Object *sipp_torus();
  1453. extern Object *sipp_cone();
  1454. extern Object *sipp_cylinder();
  1455. extern Object *sipp_ellipsoid();
  1456. extern Object *sipp_sphere();
  1457. extern Object *sipp_prism();
  1458. extern Object *sipp_block();
  1459. extern Object *sipp_cube();
  1460. extern Object *sipp_bezier();
  1461. X
  1462. X
  1463. #endif /* PRIMITIVES_H */
  1464. SHAR_EOF
  1465. chmod 0664 libsipp/primitives.h ||
  1466. echo 'restore of libsipp/primitives.h failed'
  1467. Wc_c="`wc -c < 'libsipp/primitives.h'`"
  1468. test 1264 -eq "$Wc_c" ||
  1469.     echo 'libsipp/primitives.h: original size 1264, current size' "$Wc_c"
  1470. fi
  1471. # ============= libsipp/prism.c ==============
  1472. if test -f 'libsipp/prism.c' -a X"$1" != X"-c"; then
  1473.     echo 'x - skipping libsipp/prism.c (File already exists)'
  1474. else
  1475. echo 'x - extracting libsipp/prism.c (Text)'
  1476. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/prism.c' &&
  1477. /*
  1478. X * File: sipp_prism.c 
  1479. X *
  1480. X * Create a prism from a polygonal cross-section in XY.
  1481. X * The points must be given in counter-clockwise order as viewed from
  1482. X * the front (+ve Z).
  1483. X *
  1484. X * Author:  David Jones
  1485. X *          djones@awesome.berkeley.edu
  1486. X *          11may91 djones
  1487. X *
  1488. X * Adapted for inclusion into the SIPP package:  Inge Wallin
  1489. X */
  1490. X
  1491. #include <stdio.h>
  1492. #include <math.h>
  1493. X
  1494. #include <sipp.h>
  1495. X
  1496. X
  1497. Object *
  1498. sipp_prism(num_points, points, length, surface, shader)
  1499. X    int          num_points;
  1500. X    Vector  * points;
  1501. X    double    length;
  1502. X    void    * surface;
  1503. X    Shader  * shader;
  1504. {
  1505. X    Object  * prism;
  1506. X    int         i;
  1507. X    int         j;
  1508. X
  1509. X    prism = object_create();
  1510. X
  1511. X    /* The top. */
  1512. X    for (i = 0; i < num_points ; ++i) {
  1513. X        vertex_tx_push(points[i].x, points[i].y, length / 2.0, 
  1514. X                       points[i].x, points[i].y, length / 2.0);
  1515. X    }
  1516. X    polygon_push();
  1517. X    object_add_surface(prism, surface_create(surface, shader) );
  1518. X
  1519. X    /* The bottom */
  1520. X    for (i = num_points - 1; i >= 0 ; --i) {
  1521. X        vertex_tx_push(points[i].x, points[i].y, -length / 2.0, 
  1522. X                       points[i].x, points[i].y, -length / 2.0);
  1523. X    }
  1524. X    polygon_push();
  1525. X    object_add_surface(prism, surface_create(surface, shader) );
  1526. X
  1527. X    /* The sides */
  1528. X    for (i = 0; i < num_points ; ++i) {
  1529. X        j = i + 1;
  1530. X        if (j == num_points)
  1531. X            j=0;
  1532. X        vertex_tx_push(points[i].x, points[i].y,  length / 2.0, 
  1533. X                       points[i].x, points[i].y,  length / 2.0);
  1534. X        vertex_tx_push(points[i].x, points[i].y, -length / 2.0, 
  1535. X                       points[i].x, points[i].y, -length / 2.0);
  1536. X        vertex_tx_push(points[j].x, points[j].y, -length / 2.0, 
  1537. X                       points[j].x, points[j].y, -length / 2.0);
  1538. X        vertex_tx_push(points[j].x, points[j].y,  length / 2.0, 
  1539. X                       points[j].x, points[j].y,  length / 2.0);
  1540. X        polygon_push();
  1541. X        object_add_surface(prism, surface_create(surface, shader) );
  1542. X    }
  1543. X    
  1544. X    return prism;
  1545. }
  1546. X
  1547. X
  1548. Object *
  1549. sipp_block(xsize, ysize, zsize, surface, shader)
  1550. X    double    xsize;
  1551. X    double    ysize;
  1552. X    double    zsize;
  1553. X    void    * surface;
  1554. X    Shader  * shader;
  1555. {
  1556. X    Vector   points[4];
  1557. X    int      i;
  1558. X
  1559. X    points[0].x =   xsize / 2.0;
  1560. X    points[0].y = - ysize / 2.0;
  1561. X    points[1].x =   xsize / 2.0;
  1562. X    points[1].y =   ysize / 2.0;
  1563. X    points[2].x = - xsize / 2.0;
  1564. X    points[2].y =   ysize / 2.0;
  1565. X    points[3].x = - xsize / 2.0;
  1566. X    points[3].y = - ysize / 2.0;
  1567. X    for (i = 0; i < 4; ++i) {
  1568. X        points[i].z = 0.0;
  1569. X    }
  1570. X
  1571. X    return sipp_prism(4, &points[0], zsize, surface, shader);
  1572. }
  1573. X
  1574. X
  1575. Object *
  1576. sipp_cube(size, surface, shader)
  1577. X    double   size;
  1578. X    void     *surface;
  1579. X    Shader   *shader;
  1580. {
  1581. X    return sipp_block(size, size, size, surface, shader);
  1582. }
  1583. SHAR_EOF
  1584. chmod 0664 libsipp/prism.c ||
  1585. echo 'restore of libsipp/prism.c failed'
  1586. Wc_c="`wc -c < 'libsipp/prism.c'`"
  1587. test 2773 -eq "$Wc_c" ||
  1588.     echo 'libsipp/prism.c: original size 2773, current size' "$Wc_c"
  1589. fi
  1590. true || echo 'restore of libsipp/rendering.c failed'
  1591. echo End of part 3, continue with part 4
  1592. exit 0
  1593.  
  1594. -- 
  1595. ------------------------------------------------------------------------------
  1596.  J o n a s   Y n g v e s s o n
  1597. Dept. of Electrical Engineering                             jonas-y@isy.liu.se
  1598. University of Linkoping, Sweden                   ...!uunet!isy.liu.se!jonas-y
  1599.  
  1600. exit 0 # Just in case...
  1601.