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

  1. Newsgroups: comp.sources.misc
  2. From: Jonas Yngvesson <jonas-y@isy.liu.se>
  3. Subject:  v21i027:  sipp - A 3D rendering library v2.1, Part02/08
  4. Message-ID: <1991Jul23.181546.27671@sparky.IMD.Sterling.COM>
  5. X-Md4-Signature: a7e457e725d8e5318d5d31fe2378656d
  6. Date: Tue, 23 Jul 1991 18:15:46 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 27
  11. Archive-name: sipp/part02
  12. Supersedes: sipp2.0: Volume 16, Issue 5-10
  13. Environment: UNIX
  14.  
  15. #!/bin/sh
  16. # This is part 02 of sipp-2.1
  17. # ============= libsipp/bumpy.c ==============
  18. if test ! -d 'libsipp'; then
  19.     echo 'x - creating directory libsipp'
  20.     mkdir 'libsipp'
  21. fi
  22. if test -f 'libsipp/bumpy.c' -a X"$1" != X"-c"; then
  23.     echo 'x - skipping libsipp/bumpy.c (File already exists)'
  24. else
  25. echo 'x - extracting libsipp/bumpy.c (Text)'
  26. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/bumpy.c' &&
  27. /**
  28. X ** sipp - SImple Polygon Processor
  29. X **
  30. X **  A general 3d graphic package
  31. X **
  32. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  33. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  34. X **
  35. X ** This program is free software; you can redistribute it and/or modify
  36. X ** it under the terms of the GNU General Public License as published by
  37. X ** the Free Software Foundation; either version 1, or any later version.
  38. X ** This program is distributed in the hope that it will be useful,
  39. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  41. X ** GNU General Public License for more details.
  42. X ** You can receive a copy of the GNU General Public License from the
  43. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  44. X **/
  45. X
  46. /**
  47. X ** bumpy.c - Bumpy shader: simulates an bumpy surfaces using noise and Dnoise
  48. X **/
  49. X
  50. #include <math.h>
  51. #include <stdio.h>
  52. X
  53. #include <sipp.h>
  54. #include <geometric.h>
  55. #include <noise.h>
  56. #include <shaders.h>
  57. X
  58. X
  59. extern bool noise_ready;
  60. X
  61. X
  62. void
  63. bumpy_shader(nx, ny, nz, u, v, w, view_vec, lights, bd, color)
  64. X    double        nx, ny, nz;
  65. X    double        u, v, w;
  66. X    Vector        view_vec;
  67. X    Lightsource  *lights;
  68. X    Bumpy_desc   *bd;
  69. X    Color        *color;
  70. {
  71. X    Vector     tmp;
  72. X    double     len;
  73. X    double     no;
  74. X
  75. X    if (!noise_ready) {
  76. X        noise_init();
  77. X    }
  78. X
  79. X    tmp.x = u * bd->scale;
  80. X    tmp.y = v * bd->scale;
  81. X    tmp.z = w * bd->scale;
  82. X
  83. X    if ((bd->bumpflag && bd->holeflag)
  84. X          || ((no = noise(&tmp)) < 0.0 && bd->bumpflag)
  85. X          || (no > 0.0 && bd->holeflag)) {
  86. X        tmp = Dnoise(&tmp);
  87. X        len = sqrt(nx * nx + ny * ny + nz * nz);
  88. X        nx = nx / len + tmp.x;
  89. X        ny = ny / len + tmp.y;
  90. X        nz = nz / len + tmp.z;
  91. X    }
  92. X
  93. X    bd->shader(nx, ny, nz, u, v, w, view_vec, lights, bd->surface, color);
  94. }
  95. SHAR_EOF
  96. chmod 0664 libsipp/bumpy.c ||
  97. echo 'restore of libsipp/bumpy.c failed'
  98. Wc_c="`wc -c < 'libsipp/bumpy.c'`"
  99. test 1894 -eq "$Wc_c" ||
  100.     echo 'libsipp/bumpy.c: original size 1894, current size' "$Wc_c"
  101. fi
  102. # ============= libsipp/cone.c ==============
  103. if test -f 'libsipp/cone.c' -a X"$1" != X"-c"; then
  104.     echo 'x - skipping libsipp/cone.c (File already exists)'
  105. else
  106. echo 'x - extracting libsipp/cone.c (Text)'
  107. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/cone.c' &&
  108. /*
  109. X * File:  sipp_cone.c
  110. X * 
  111. X * Create a, possibly truncated, cone. Cylinder is a special case
  112. X * of a truncated cone with the same top and bottom radius.
  113. X *
  114. X * Author:  David Jones
  115. X *          djones@awesome.berkeley.edu
  116. X *
  117. X * Adapted for inclusion into the SIPP package by Jonas Yngvesson
  118. X */
  119. X
  120. #include <stdio.h>
  121. #include <math.h>
  122. X
  123. #include <sipp.h>
  124. #include <xalloca.h>
  125. X
  126. X
  127. Object *
  128. sipp_cone(radius_bot, radius_top, length, res, surface, shader)
  129. X    double    radius_bot;
  130. X    double    radius_top;
  131. X    double    length;
  132. X    int          res;
  133. X    void    * surface;
  134. X    Shader  * shader;
  135. {
  136. X    Object  * cone;
  137. X    double  * xb;
  138. X    double  * yb;
  139. X    double  * xt;
  140. X    double  * yt;
  141. X    double    frac;
  142. X    double    half_length;
  143. X    int       bot_exists;
  144. X    int       top_exists;
  145. X    int       i;
  146. X    
  147. X    /*
  148. X     * If both top and bottom radii is zero it's a line
  149. X     * and can't be rendered.
  150. X     */
  151. X    if (radius_bot == 0.0 && radius_top == 0.0) {
  152. X        return NULL;
  153. X    }
  154. X
  155. X    if (radius_bot > 0.0) {
  156. X        xb = (double *) alloca((res + 1) * sizeof(double));
  157. X        yb = (double *) alloca((res + 1) * sizeof(double));
  158. X        bot_exists = 1;
  159. X    } else if (radius_bot == 0.0 ){
  160. X        bot_exists = 0;
  161. X    } else {
  162. X        return NULL;
  163. X    }
  164. X
  165. X    if (radius_top > 0.0) {
  166. X        xt = (double *) alloca((res + 1) * sizeof(double));
  167. X        yt = (double *) alloca((res + 1) * sizeof(double));
  168. X        top_exists = 1;
  169. X    } else if (radius_top == 0.0) {
  170. X        top_exists = 0;
  171. X    } else {
  172. X        return NULL;
  173. X    }
  174. X    
  175. X    /* list of coordinates */
  176. X    for (i = 0; i <= res ; ++i) {
  177. X        frac = ((double) i) / ((double) res);
  178. X        if (bot_exists) {
  179. X            xb[i] = radius_bot * cos(frac * 2.0 * M_PI);
  180. X            yb[i] = radius_bot * sin(frac * 2.0 * M_PI);
  181. X        }
  182. X        if (top_exists) {
  183. X            xt[i] = radius_top * cos(frac * 2.0 * M_PI);
  184. X            yt[i] = radius_top * sin(frac * 2.0 * M_PI);
  185. X        }
  186. X    }
  187. X    
  188. X    half_length = length * 0.5;
  189. X
  190. X    /* empty object */
  191. X    cone = object_create();
  192. X    
  193. X    /* The bottom surface */
  194. X    if (bot_exists) {
  195. X        for (i = res - 1; i >= 0 ; --i) {
  196. X            vertex_tx_push(xb[i], yb[i], -half_length,
  197. X                           xb[i], yb[i], -half_length);
  198. X        }
  199. X        polygon_push();
  200. X        object_add_surface(cone, surface_create(surface, shader));
  201. X    }
  202. X    
  203. X    /* The top surface */
  204. X    if (top_exists) {
  205. X        for (i = 0; i < res ; ++i) {
  206. X            vertex_tx_push(xt[i], yt[i], half_length,  
  207. X                           xt[i], yt[i], half_length);
  208. X        }
  209. X        polygon_push();
  210. X        object_add_surface(cone, surface_create(surface, shader));
  211. X    }
  212. X    
  213. X    /* The side surface */
  214. X    for (i = 0; i < res ; ++i) {
  215. X        if (top_exists && bot_exists) {
  216. X            vertex_tx_push(xb[i], yb[i], -half_length,
  217. X                           xb[i], yb[i], -half_length);
  218. X            vertex_tx_push(xb[i+1], yb[i+1], -half_length,
  219. X                           xb[i+1], yb[i+1], -half_length);
  220. X            vertex_tx_push(xt[i+1], yt[i+1], half_length,
  221. X                           xt[i+1], yt[i+1], half_length);
  222. X            vertex_tx_push(xt[i], yt[i], half_length,
  223. X                           xt[i], yt[i], half_length);
  224. X        } else if (top_exists) {
  225. X            vertex_tx_push(0.0, 0.0, -half_length,
  226. X                           0.0, 0.0, -half_length);
  227. X            vertex_tx_push(xt[i+1], yt[i+1], half_length,
  228. X                           xt[i+1], yt[i+1], half_length);
  229. X            vertex_tx_push(xt[i], yt[i], half_length,
  230. X                           xt[i], yt[i], half_length);
  231. X        } else {
  232. X            vertex_tx_push(xb[i], yb[i], -half_length,
  233. X                           xb[i], yb[i], -half_length);
  234. X            vertex_tx_push(xb[i+1], yb[i+1], -half_length,
  235. X                           xb[i+1], yb[i+1], -half_length);
  236. X            vertex_tx_push(0.0, 0.0, half_length,
  237. X                           0.0, 0.0, half_length);
  238. X        }
  239. X        polygon_push();
  240. X    }
  241. X    object_add_surface(cone, surface_create(surface, shader));
  242. X    
  243. X    return cone;
  244. }
  245. X
  246. X
  247. X
  248. Object *
  249. sipp_cylinder(radius, length, res, surface, shader)
  250. X    double    radius;
  251. X    double    length;
  252. X    int       res;
  253. X    void     *surface;
  254. X    Shader   *shader;
  255. {
  256. X    Object   *cylinder;
  257. X
  258. X    cylinder = sipp_cone(radius, radius, length, res, surface, shader);
  259. X
  260. X    return cylinder;
  261. }
  262. SHAR_EOF
  263. chmod 0664 libsipp/cone.c ||
  264. echo 'restore of libsipp/cone.c failed'
  265. Wc_c="`wc -c < 'libsipp/cone.c'`"
  266. test 4352 -eq "$Wc_c" ||
  267.     echo 'libsipp/cone.c: original size 4352, current size' "$Wc_c"
  268. fi
  269. # ============= libsipp/ellipsoid.c ==============
  270. if test -f 'libsipp/ellipsoid.c' -a X"$1" != X"-c"; then
  271.     echo 'x - skipping libsipp/ellipsoid.c (File already exists)'
  272. else
  273. echo 'x - extracting libsipp/ellipsoid.c (Text)'
  274. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/ellipsoid.c' &&
  275. /**
  276. X ** sipp - SImple Polygon Processor
  277. X **
  278. X **  A general 3d graphic package
  279. X **
  280. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  281. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  282. X **
  283. X ** This program is free software; you can redistribute it and/or modify
  284. X ** it under the terms of the GNU General Public License as published by
  285. X ** the Free Software Foundation; either version 1, or any later version.
  286. X ** This program is distributed in the hope that it will be useful,
  287. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  288. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  289. X ** GNU General Public License for more details.
  290. X ** You can receive a copy of the GNU General Public License from the
  291. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  292. X **/
  293. X
  294. /**
  295. X ** ellipsoid.c - Creating ellipsiods and spheres as sipp objects.
  296. X **/
  297. X
  298. #include <xalloca.h>
  299. #include <math.h>
  300. X
  301. #include <sipp.h>
  302. X
  303. X
  304. Object *
  305. sipp_ellipsoid(x_rad, y_rad, z_rad, res, surface, shader)
  306. X    double  x_rad;
  307. X    double  y_rad;
  308. X    double  z_rad;
  309. X    int     res;
  310. X    void   *surface;
  311. X    Shader *shader;
  312. {
  313. X    int      i, j;
  314. X    double   factor;
  315. X    double   factor1;
  316. X    double   factor2;
  317. X    double   zradprim;
  318. X    double   zradprim1;
  319. X    double   zradprim2;
  320. X    double  *x_arr;
  321. X    double  *y_arr;
  322. X    Object  *ellipsoid;
  323. X    
  324. X    /* Odd resolutions make ugly spheres since the poles will be */
  325. X    /* different in size. */
  326. X    if (res & 1) {
  327. X        res++;
  328. X    }
  329. X
  330. X    /* Create two arrays with the coordinates of the points */
  331. X    /* around the perimeter at Z = 0 */
  332. X    x_arr = (double *) alloca(res * sizeof(double));
  333. X    y_arr = (double *) alloca(res * sizeof(double));
  334. X    for (i = 0; i < res; i++) {
  335. X        x_arr[i] = x_rad * cos(i * 2.0 * M_PI / res);
  336. X        y_arr[i] = y_rad * sin(i * 2.0 * M_PI / res);
  337. X    }
  338. X
  339. X    /* Create the top pole */
  340. X    factor = sin(2.0 * M_PI / res);
  341. X    zradprim = z_rad * cos(2.0 * M_PI / res);
  342. X    for (i = 0; i < res; i++) {
  343. X        vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
  344. X                       x_arr[i] * factor, y_arr[i] * factor, zradprim);
  345. X        vertex_tx_push(factor * x_arr[(i + 1) % res],
  346. X                       factor * y_arr[(i + 1) % res],
  347. X                       zradprim, 
  348. X                       factor * x_arr[(i + 1) % res],
  349. X                       factor * y_arr[(i + 1) % res],
  350. X                       zradprim);
  351. X        vertex_tx_push(0.0, 0.0, z_rad, 0.0, 0.0, z_rad);
  352. X        polygon_push();
  353. X    }
  354. X
  355. X    /* Create the surface between the poles. */
  356. X    factor2 = factor;
  357. X    zradprim2 = zradprim;
  358. X    for (j = 1; j < res / 2 - 1; j++) {
  359. X        factor1 = factor2;
  360. X        factor2 = sin((j + 1) * M_PI / (res / 2));
  361. X        zradprim1 = zradprim2;
  362. X        zradprim2 = z_rad * cos((j + 1) * M_PI / (res / 2));
  363. X
  364. X        for (i = 0; i < res; i++) {
  365. X            vertex_tx_push(factor1 * x_arr[i], factor1 * y_arr[i], zradprim1, 
  366. X                           factor1 * x_arr[i], factor1 * y_arr[i], zradprim1);
  367. X            vertex_tx_push(factor2 * x_arr[i], factor2 * y_arr[i], zradprim2, 
  368. X                           factor2 * x_arr[i], factor2 * y_arr[i], zradprim2);
  369. X            vertex_tx_push(factor2 * x_arr[(i + 1) % res], 
  370. X                           factor2 * y_arr[(i + 1) % res], zradprim2, 
  371. X                           factor2 * x_arr[(i + 1) % res],
  372. X                           factor2 * y_arr[(i + 1) % res], zradprim2);
  373. X            vertex_tx_push(factor1 * x_arr[(i + 1) % res], 
  374. X                           factor1 * y_arr[(i + 1) % res], zradprim1, 
  375. X                           factor1 * x_arr[(i + 1) % res],
  376. X                           factor1 * y_arr[(i + 1) % res], zradprim1);
  377. X            polygon_push();
  378. X        }
  379. X    }
  380. X
  381. X    /* Create the bottom pole */
  382. X    factor = sin(2.0 * M_PI / res);
  383. X    zradprim = -z_rad * cos(2.0 * M_PI / res);
  384. X    for (i = 0; i < res; i++) {
  385. X        vertex_tx_push(x_arr[(i + 1) % res] * factor,
  386. X                       y_arr[(i + 1) % res] * factor,
  387. X                       zradprim, 
  388. X                       x_arr[(i + 1) % res] * factor,
  389. X                       y_arr[(i + 1) % res] * factor,
  390. X                       zradprim);
  391. X        vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
  392. X                       x_arr[i] * factor, y_arr[i] * factor, zradprim);
  393. X        vertex_tx_push(0.0, 0.0, -z_rad, 0.0, 0.0, -z_rad);
  394. X        polygon_push();
  395. X    }
  396. X
  397. X    ellipsoid = object_create();
  398. X    object_add_surface(ellipsoid, surface_create(surface, shader));
  399. X
  400. X    return ellipsoid;
  401. }
  402. X
  403. X
  404. Object *
  405. sipp_sphere(radius, res, surface, shader)
  406. X    double  radius;
  407. X    int     res;
  408. X    void   *surface;
  409. X    Shader *shader;
  410. {
  411. X    return sipp_ellipsoid(radius, radius, radius, res, surface, shader);
  412. }
  413. SHAR_EOF
  414. chmod 0664 libsipp/ellipsoid.c ||
  415. echo 'restore of libsipp/ellipsoid.c failed'
  416. Wc_c="`wc -c < 'libsipp/ellipsoid.c'`"
  417. test 4769 -eq "$Wc_c" ||
  418.     echo 'libsipp/ellipsoid.c: original size 4769, current size' "$Wc_c"
  419. fi
  420. # ============= libsipp/geometric.c ==============
  421. if test -f 'libsipp/geometric.c' -a X"$1" != X"-c"; then
  422.     echo 'x - skipping libsipp/geometric.c (File already exists)'
  423. else
  424. echo 'x - extracting libsipp/geometric.c (Text)'
  425. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/geometric.c' &&
  426. /**
  427. X ** sipp - SImple Polygon Processor
  428. X **
  429. X **  A general 3d graphic package
  430. X **
  431. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  432. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  433. X **
  434. X ** This program is free software; you can redistribute it and/or modify
  435. X ** it under the terms of the GNU General Public License as published by
  436. X ** the Free Software Foundation; either version 1, or any later version.
  437. X ** This program is distributed in the hope that it will be useful,
  438. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  439. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  440. X ** GNU General Public License for more details.
  441. X ** You can receive a copy of the GNU General Public License from the
  442. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  443. X **/
  444. X
  445. /**
  446. X ** geometric.c - Matrixes, transformations and coordinates.
  447. X **/
  448. X
  449. #include <stdio.h>
  450. #include <math.h>
  451. X
  452. #include <sipp.h>
  453. #include <geometric.h>
  454. X
  455. X
  456. /* =================================================================== */
  457. X /* */
  458. X
  459. X
  460. Transf_mat   ident_matrix = {{        /* Unit tranfs. matrix */
  461. X    { 1.0, 0.0, 0.0 },
  462. X    { 0.0, 1.0, 0.0 },
  463. X    { 0.0, 0.0, 1.0 },
  464. X    { 0.0, 0.0, 0.0 }
  465. }};
  466. X
  467. X
  468. /* =================================================================== */
  469. X
  470. /*
  471. X * Allocate a new matrix, and if INITMAT != NULL copy the contents
  472. X * of INITMAT to the new matrix,  otherwise copy the identity matrix
  473. X * to the new matrix.
  474. X */
  475. X
  476. Transf_mat *
  477. transf_mat_create(initmat)
  478. X    Transf_mat  * initmat;
  479. {
  480. X    Transf_mat  * mat;
  481. X
  482. X    mat = (Transf_mat *) malloc(sizeof(Transf_mat));
  483. X    if (initmat != NULL)
  484. X    MatCopy(mat, initmat);
  485. X    else
  486. X    MatCopy(mat, &ident_matrix);
  487. X
  488. X    return mat;
  489. }
  490. X
  491. X
  492. void
  493. transf_mat_destruct(mat)
  494. X    Transf_mat  * mat;
  495. {
  496. X    free(mat);
  497. }
  498. X
  499. X
  500. /* =================================================================== */
  501. /*          Transformation routines (see also geometric.h)             */
  502. X
  503. X
  504. /*
  505. X * Normalize a vector.
  506. X */
  507. X
  508. void
  509. vecnorm(vec)
  510. X    Vector  *vec;
  511. {
  512. X    double   len;
  513. X
  514. X    len =  VecLen(*vec);
  515. X    if (len == 0.0)
  516. X    fprintf(stderr, "vecnorm(): Vector has length 0!\n");
  517. X    else
  518. X    VecScalMul(*vec, 1.0 / len, *vec);
  519. }
  520. X
  521. X
  522. X
  523. /*
  524. X * Set MAT to the transformation matrix that represents the 
  525. X * concatenation between the previous transformation in MAT
  526. X * and a translation along the vector described by DX, DY and DZ.
  527. X *
  528. X * [a  b  c  0]   [ 1  0  0  0]     [ a     b     c    0]
  529. X * [d  e  f  0]   [ 0  1  0  0]     [ d     e     f    0]
  530. X * [g  h  i  0] * [ 0  0  1  0]  =  [ g     h     i    0]
  531. X * [j  k  l  1]   [Tx Ty Tz  1]     [j+Tx  k+Ty  l+Tz  1]
  532. X */
  533. X
  534. void
  535. mat_translate(mat,  dx,  dy,  dz)
  536. X    Transf_mat  * mat;
  537. X    double        dx;
  538. X    double        dy; 
  539. X    double        dz;
  540. {
  541. X    mat->mat[3][0] += dx;
  542. X    mat->mat[3][1] += dy;
  543. X    mat->mat[3][2] += dz;
  544. }
  545. X
  546. X
  547. X
  548. /*
  549. X * Set MAT to the transformation matrix that represents the 
  550. X * concatenation between the previous transformation in MAT
  551. X * and a rotation with the angle ANG around the X axis.
  552. X *
  553. X * [a  b  c  0]   [1   0   0  0]     [a  b*Ca-c*Sa  b*Sa+c*Ca  0]
  554. X * [d  e  f  0]   [0  Ca  Sa  0]     [d  e*Ca-f*Sa  e*Sa+f*Ca  0]
  555. X * [g  h  i  0] * [0 -Sa  Ca  0]  =  [g  h*Ca-i*Sa  h*Sa+i*Ca  0]
  556. X * [j  k  l  1]   [0   0   0  1]     [j  k*Ca-l*Sa  k*Se+l*Ca  1]
  557. X */
  558. X
  559. void
  560. mat_rotate_x(mat, ang)
  561. X    Transf_mat  * mat;
  562. X    double        ang;
  563. {
  564. X    double   cosang;
  565. X    double   sinang;
  566. X    double   tmp;
  567. X    int      i;
  568. X    
  569. X    cosang = cos(ang);
  570. X    sinang = sin(ang);
  571. X    if (fabs(cosang) < 1.0e-15) {
  572. X        cosang = 0.0;
  573. X    }
  574. X    if (fabs(sinang) < 1.0e-15) {
  575. X        sinang = 0.0;
  576. X    }
  577. X    for (i = 0; i < 4; ++i) {
  578. X    tmp = mat->mat[i][1];
  579. X    mat->mat[i][1] = mat->mat[i][1] * cosang
  580. X                   - mat->mat[i][2] * sinang;
  581. X    mat->mat[i][2] = tmp * sinang + mat->mat[i][2] * cosang;
  582. X    }
  583. }
  584. X
  585. X
  586. X
  587. /*
  588. X * Set MAT to the transformation matrix that represents the 
  589. X * concatenation between the previous transformation in MAT
  590. X * and a rotation with the angle ANG around the Y axis.
  591. X *
  592. X * [a  b  c  0]   [Ca   0 -Sa  0]     [a*Ca+c*Sa  b  -a*Sa+c*Ca  0]
  593. X * [d  e  f  0] * [ 0   1   0  0]  =  [d*Ca+f*Sa  e  -d*Sa+f*Ca  0]
  594. X * [g  h  i  0]   [Sa   0  Ca  0]     [g*Ca+i*Sa  h  -g*Sa+i*Ca  0]
  595. X * [j  k  l  1]   [ 0   0   0  1]     [j*Ca+l*Sa  k  -j*Sa+l*Ca  1]
  596. X */
  597. X
  598. void
  599. mat_rotate_y(mat, ang)
  600. X    Transf_mat  * mat;
  601. X    double        ang;
  602. {
  603. X    double   cosang;
  604. X    double   sinang;
  605. X    double   tmp;
  606. X    int      i;
  607. X    
  608. X    cosang = cos(ang);
  609. X    sinang = sin(ang);
  610. X    if (fabs(cosang) < 1.0e-15) {
  611. X        cosang = 0.0;
  612. X    }
  613. X    if (fabs(sinang) < 1.0e-15) {
  614. X        sinang = 0.0;
  615. X    }
  616. X    for (i = 0; i < 4; ++i) {
  617. X    tmp = mat->mat[i][0];
  618. X    mat->mat[i][0] = mat->mat[i][0] * cosang
  619. X                   + mat->mat[i][2] * sinang;
  620. X    mat->mat[i][2] = -tmp * sinang + mat->mat[i][2] * cosang;
  621. X    }
  622. }
  623. X
  624. X
  625. X
  626. /*
  627. X * Set MAT to the transformation matrix that represents the 
  628. X * concatenation between the previous transformation in MAT
  629. X * and a rotation with the angle ANG around the Z axis.
  630. X *
  631. X * [a  b  c  0]   [ Ca  Sa   0  0]     [a*Ca-b*Sa  a*Sa+b*Ca  c  0]
  632. X * [d  e  f  0]   [-Sa  Ca   0  0]     [d*Ca-e*Sa  d*Sa+e*Ca  f  0]
  633. X * [g  h  i  0] * [  0   0   1  0]  =  [g*Ca-h*Sa  g*Sa+h*Ca  i  0]
  634. X * [j  k  l  1]   [  0   0   0  1]     [j*Ca-k*Sa  j*Sa+k*Ca  l  0]
  635. X */
  636. X
  637. void
  638. mat_rotate_z(mat, ang)
  639. X    Transf_mat  * mat;
  640. X    double        ang;
  641. {
  642. X    double   cosang;
  643. X    double   sinang;
  644. X    double   tmp;
  645. X    int      i;
  646. X    
  647. X    cosang = cos(ang);
  648. X    sinang = sin(ang);
  649. X    if (fabs(cosang) < 1.0e-15) {
  650. X        cosang = 0.0;
  651. X    }
  652. X    if (fabs(sinang) < 1.0e-15) {
  653. X        sinang = 0.0;
  654. X    }
  655. X    for (i = 0; i < 4; ++i) {
  656. X    tmp = mat->mat[i][0];
  657. X    mat->mat[i][0] = mat->mat[i][0] * cosang
  658. X                   - mat->mat[i][1] * sinang;
  659. X    mat->mat[i][1] = tmp * sinang + mat->mat[i][1] * cosang;
  660. X    }
  661. }
  662. X
  663. X
  664. X
  665. /*
  666. X * Set MAT to the transformation matrix that represents the
  667. X * concatenation between the previous transformation in MAT
  668. X * and a rotation with the angle ANG around the line represented
  669. X * by the point POINT and the vector VECTOR.
  670. X */
  671. X
  672. void
  673. mat_rotate(mat, point, vector, ang)
  674. X    Transf_mat  * mat;
  675. X    Vector      * point;
  676. X    Vector      * vector;
  677. X    double        ang;
  678. {
  679. X    double   ang2;
  680. X    double   ang3;
  681. X
  682. X    ang2 = atan2(vector->y, vector->x);
  683. X    ang3 = atan2(hypot(vector->x, vector->y), vector->z);
  684. X    mat_translate(mat, -point->x, -point->y, -point->z);
  685. X    mat_rotate_z(mat, -ang2);
  686. X    mat_rotate_y(mat, -ang3);
  687. X    mat_rotate_z(mat, ang);
  688. X    mat_rotate_y(mat, ang3);
  689. X    mat_rotate_z(mat, ang2);
  690. X    mat_translate(mat, point->x, point->y, point->z);
  691. }
  692. X
  693. X
  694. X
  695. /*
  696. X * Set MAT to the transformation matrix that represents the 
  697. X * concatenation between the previous transformation in MAT
  698. X * and a scaling with the scaling factors XSCALE, YSCALE and ZSCALE
  699. X *
  700. X * [a  b  c  0]   [Sx  0  0  0]     [a*Sx  b*Sy  c*Sz  0]
  701. X * [d  e  f  0]   [ 0 Sy  0  0]     [d*Sx  e*Sy  f*Sz  0]
  702. X * [g  h  i  0] * [ 0  0 Sz  0]  =  [g*Sx  h*Sy  i*Sz  0]
  703. X * [j  k  l  1]   [ 0  0  0  1]     [j*Sx  k*Sy  l*Sz  1]
  704. X */
  705. X
  706. void
  707. mat_scale(mat, xscale, yscale, zscale)
  708. X    Transf_mat  * mat;
  709. X    double        xscale;
  710. X    double        yscale;
  711. X    double        zscale;
  712. {
  713. X    int   i;
  714. X
  715. X    for (i = 0; i < 4; ++i) {
  716. X    mat->mat[i][0] *= xscale;
  717. X    mat->mat[i][1] *= yscale;
  718. X    mat->mat[i][2] *= zscale;
  719. X    }
  720. }
  721. X
  722. X
  723. X
  724. /*
  725. X * Set MAT to the transformation matrix that represents the
  726. X * concatenation between the previous transformation in MAT
  727. X * and a mirroring in the plane defined by the point POINT
  728. X * and the normal vector NORM.
  729. X */
  730. X
  731. void
  732. mat_mirror_plane(mat, point, norm)
  733. X    Transf_mat  * mat;
  734. X    Vector      * point;
  735. X    Vector      * norm;
  736. {
  737. X    Transf_mat   tmp;
  738. X    double   factor;
  739. X
  740. X    /* The first thing we do is to make a transformation matrix */
  741. X    /* for mirroring through a plane with the same normal vector */
  742. X    /* as our, but through the origin instead. */
  743. X    factor = 2.0 / (norm->x * norm->x + norm->y * norm->y 
  744. X            + norm->z * norm->z);
  745. X    
  746. X    /* The diagonal elements. */
  747. X    tmp.mat[0][0] = 1 - factor * norm->x * norm->x;
  748. X    tmp.mat[1][1] = 1 - factor * norm->y * norm->y;
  749. X    tmp.mat[2][2] = 1 - factor * norm->z * norm->z;
  750. X    
  751. X    /* The rest of the matrix */
  752. X    tmp.mat[1][0] = tmp.mat[0][1] = -factor * norm->x * norm->y;
  753. X    tmp.mat[2][0] = tmp.mat[0][2] = -factor * norm->x * norm->z;
  754. X    tmp.mat[2][1] = tmp.mat[1][2] = -factor * norm->y * norm->z;
  755. X    tmp.mat[3][0] = tmp.mat[3][1] = tmp.mat[3][2] = 0.0;
  756. X
  757. X    /* Do the actual transformation. This is done in 3 steps: */
  758. X    /* 1) Translate the plane so that it goes through the origin. */
  759. X    /* 2) Do the actual mirroring. */
  760. X    /* 3) Translate it all back to the starting position. */
  761. X    mat_translate(mat, -point->x, -point->y, -point->z);
  762. X    mat_mul(mat, mat, &tmp);
  763. X    mat_translate(mat, point->x, point->y, point->z);
  764. }
  765. X
  766. X
  767. X
  768. /*
  769. X * Multiply the Matrix A with the Matrix B, and store the result
  770. X * into the Matrix RES. It is possible for RES to point to the 
  771. X * same Matrix as either A or B since the result is stored into
  772. X * a temporary during computation.
  773. X *
  774. X * [a b c 0]  [A B C 0]     [aA+bD+cG    aB+bE+cH    aC+bF+cI    0]
  775. X * [d e f 0]  [D E F 0]     [dA+eD+fG    dB+eE+fH    dC+eF+fI    0]
  776. X * [g h i 0]  [G H I 0]  =  [gA+hD+iG    gB+hE+iH    gC+hF+iI    0]
  777. X * [j k l 1]  [J K L 1]     [jA+kD+lG+J  jB+kE+lH+K  jC+kF+lI+L  1]
  778. X */
  779. X
  780. void
  781. mat_mul(res, a, b)
  782. X    Transf_mat  * res;
  783. X    Transf_mat  * a;
  784. X    Transf_mat  * b;
  785. {
  786. X    Transf_mat   tmp;
  787. X    int      i;
  788. X
  789. X    for (i = 0; i < 4; ++i) {
  790. X    tmp.mat[i][0] = a->mat[i][0] * b->mat[0][0]
  791. X                  + a->mat[i][1] * b->mat[1][0] 
  792. X                  + a->mat[i][2] * b->mat[2][0];
  793. X    tmp.mat[i][1] = a->mat[i][0] * b->mat[0][1]
  794. X                    + a->mat[i][1] * b->mat[1][1] 
  795. X                  + a->mat[i][2] * b->mat[2][1];
  796. X    tmp.mat[i][2] = a->mat[i][0] * b->mat[0][2]
  797. X                   + a->mat[i][1] * b->mat[1][2]
  798. X                  + a->mat[i][2] * b->mat[2][2];
  799. X    }
  800. X
  801. X    tmp.mat[3][0] += b->mat[3][0];
  802. X    tmp.mat[3][1] += b->mat[3][1];
  803. X    tmp.mat[3][2] += b->mat[3][2];
  804. X
  805. X    MatCopy(res, &tmp);
  806. }
  807. X
  808. X
  809. X
  810. /*
  811. X * Transform the Point3d VEC with the transformation matrix MAT, and
  812. X * put the result into the vector *RES.
  813. X *
  814. X *               [a  b  c  0]
  815. X *               [d  e  f  0]
  816. X * [x  y  z  1]  [g  h  i  0]  =  [ax+dy+gz+j  bx+ey+hz+k  cx+fy+iz+l  1]
  817. X *               [j  k  l  1]
  818. X */
  819. X
  820. void
  821. point_transform(res, vec, mat)
  822. X    Vector      * res;
  823. X    Vector      * vec;
  824. X    Transf_mat  * mat;
  825. {
  826. X    res->x = mat->mat[0][0] * vec->x + mat->mat[1][0] * vec->y 
  827. X        + mat->mat[2][0] * vec->z + mat->mat[3][0];
  828. X    res->y = mat->mat[0][1] * vec->x + mat->mat[1][1] * vec->y 
  829. X        + mat->mat[2][1] * vec->z + mat->mat[3][1];
  830. X    res->z = mat->mat[0][2] * vec->x + mat->mat[1][2] * vec->y 
  831. X        + mat->mat[2][2] * vec->z + mat->mat[3][2];
  832. }
  833. SHAR_EOF
  834. chmod 0664 libsipp/geometric.c ||
  835. echo 'restore of libsipp/geometric.c failed'
  836. Wc_c="`wc -c < 'libsipp/geometric.c'`"
  837. test 10785 -eq "$Wc_c" ||
  838.     echo 'libsipp/geometric.c: original size 10785, current size' "$Wc_c"
  839. fi
  840. # ============= libsipp/geometric.h ==============
  841. if test -f 'libsipp/geometric.h' -a X"$1" != X"-c"; then
  842.     echo 'x - skipping libsipp/geometric.h (File already exists)'
  843. else
  844. echo 'x - extracting libsipp/geometric.h (Text)'
  845. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/geometric.h' &&
  846. /**
  847. X ** sipp - SImple Polygon Processor
  848. X **
  849. X **  A general 3d graphic package
  850. X **
  851. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  852. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  853. X **
  854. X ** This program is free software; you can redistribute it and/or modify
  855. X ** it under the terms of the GNU General Public License as published by
  856. X ** the Free Software Foundation; either version 1, or any later version.
  857. X ** This program is distributed in the hope that it will be useful,
  858. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  859. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  860. X ** GNU General Public License for more details.
  861. X ** You can receive a copy of the GNU General Public License from the
  862. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  863. X **/
  864. X
  865. /**
  866. X ** geometric.h - All kinds of stuff with matrixes, transformations, 
  867. X **               coordinates
  868. X **/
  869. X
  870. /* Make sure no multiple including */
  871. #ifndef _GEOMETRIC_H_
  872. #define _GEOMETRIC_H_
  873. X
  874. #include <math.h>
  875. #ifndef NOMEMCPY
  876. #include <memory.h>
  877. #endif
  878. X
  879. X
  880. /* #define PI    3.1415926535897932384626 */
  881. X
  882. X
  883. typedef struct {
  884. X    double x, y, z;
  885. } Vector;
  886. X
  887. X
  888. /*
  889. X * NOTE:
  890. X * Capitalized types denote Vectors and other aggregates.
  891. X * Lower case denote scalars.
  892. X */
  893. X
  894. /* V = Vec(x, y, z) */
  895. #define MakeVector(V, xx, yy, zz)  { (V).x=(xx); \
  896. X                                     (V).y=(yy); \
  897. X                                     (V).z=(zz); }
  898. X
  899. /* A = -A */
  900. #define VecNegate(A)             { (A).x=0-(A).x; \
  901. X                       (A).y=0-(A).y; \
  902. X                       (A).z=0-(A).z; }
  903. X
  904. /* return A . B */
  905. #define VecDot(A, B)    ((A).x*(B).x+(A).y*(B).y+(A).z*(B).z)
  906. X
  907. /* return length(A) */
  908. #define VecLen(A)    (sqrt((double)VecDot(A, A)))
  909. X
  910. /* B = A */
  911. #define VecCopy(B, A)    ((B) = (A))
  912. X
  913. /* C = A + B */
  914. #define VecAdd(C, A, B)     { (C).x=(A).x+(B).x; \
  915. X               (C).y=(A).y+(B).y; \
  916. X               (C).z=(A).z+(B).z; }
  917. X
  918. /* C = A - B */
  919. #define VecSub(C, A, B)     { (C).x=(A).x-(B).x; \
  920. X               (C).y=(A).y-(B).y; \
  921. X               (C).z=(A).z-(B).z; }
  922. X
  923. /* C = a*A */
  924. #define VecScalMul(C, a, A)     { (C).x=(a)*(A).x; \
  925. X                   (C).y=(a)*(A).y; \
  926. X                   (C).z=(a)*(A).z; }
  927. X
  928. /* C = a*A + B */
  929. #define VecAddS(C, a, A, B)     { (C).x=(a)*(A).x+(B).x; \
  930. X                   (C).y=(a)*(A).y+(B).y; \
  931. X                   (C).z=(a)*(A).z+(B).z; }
  932. X
  933. /* C = a*A + b*B */
  934. #define VecComb(C, a, A, b, B)     { (C).x=(a)*(A).x+(b)*(B).x; \
  935. X                   (C).y=(a)*(A).y+(b)*(B).y; \
  936. X                    (C).z=(a)*(A).z+(b)*(B).z; }
  937. X
  938. /* C = A X B */
  939. #define VecCross(C, A, B)        { (C).x=(A).y*(B).z-(A).z*(B).y; \
  940. X                                   (C).y=(A).z*(B).x-(A).x*(B).z; \
  941. X                       (C).z=(A).x*(B).y-(A).y*(B).x; }
  942. X
  943. X
  944. /* ================================================================ */
  945. /*                         Matrix operations                        */
  946. X
  947. X
  948. /*
  949. X * Define a homogenous transformation matrix. The first row (vector) 
  950. X * is the new X axis, i.e. the X axis in the transformed coordinate 
  951. X * system. The second row is the new Y axis, and so on. The last row
  952. X * is the translation, for a transformed point.
  953. X *
  954. X * The reason we make surround the rows with a struct is that we
  955. X * don't want to say (Transf_mat *) &foo[0] instead of &foo when
  956. X * sending an address to a matrix as a parameter to a function.
  957. X * Alas, arrays are not first class objects in C.
  958. X */
  959. X
  960. typedef struct {
  961. X    double   mat[4][3];
  962. } Transf_mat;
  963. X
  964. X
  965. extern Transf_mat   ident_matrix;
  966. X
  967. X
  968. /* *A = *B    N.b. A and B are pointers! */
  969. #define MatCopy(A, B)         (*A) = (*B)
  970. X
  971. X
  972. /*--------------------- Transf_mat-Vector operations -------------------*/
  973. X
  974. X
  975. /* Store a Vector into a row in a Matrix */
  976. /* NOTE: This implementation depends on that a Vector is the same */
  977. /*       type as a row in a Matrix!! */
  978. #define Vec2Row(mat, row, vec)    ((mat).rows[row] = vec)
  979. X
  980. X
  981. /*----------------------------------------------------------------------*/
  982. X
  983. X
  984. /* Function declarations for the functions in geometric.c */
  985. X
  986. extern void          vecnorm(/* Vector */); /* Normalize a vector */
  987. extern Transf_mat  * transf_mat_create(/* Matrix * */);
  988. extern void          mat_translate(/* Matrix *, double, double, double */);
  989. extern void          mat_rotate_x(/* Matrix *, double */);
  990. extern void          mat_rotate_y(/* Matrix *, double */);
  991. extern void          mat_rotate_z(/* Matrix *, double */);
  992. extern void          mat_rotate(/* Matrix *, Vector *, Vector *, double */);
  993. extern void          mat_scale(/* Matrix *, double, double, double */);
  994. extern void          mat_mirror_plane(/* Matrix *, Vector *, Vector * */);
  995. extern void          mat_mul(/* Matrix *, Matrix *, Matrix * */);
  996. extern void          point_transform(/* Vector *, Vector *, Matrix * */);
  997. X
  998. X
  999. #endif  /* _GEOMETRIC_H_ */
  1000. SHAR_EOF
  1001. chmod 0664 libsipp/geometric.h ||
  1002. echo 'restore of libsipp/geometric.h failed'
  1003. Wc_c="`wc -c < 'libsipp/geometric.h'`"
  1004. test 4676 -eq "$Wc_c" ||
  1005.     echo 'libsipp/geometric.h: original size 4676, current size' "$Wc_c"
  1006. fi
  1007. # ============= libsipp/granite.c ==============
  1008. if test -f 'libsipp/granite.c' -a X"$1" != X"-c"; then
  1009.     echo 'x - skipping libsipp/granite.c (File already exists)'
  1010. else
  1011. echo 'x - extracting libsipp/granite.c (Text)'
  1012. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/granite.c' &&
  1013. /**
  1014. X ** sipp - SImple Polygon Processor
  1015. X **
  1016. X **  A general 3d graphic package
  1017. X **
  1018. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1019. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1020. X **
  1021. X ** This program is free software; you can redistribute it and/or modify
  1022. X ** it under the terms of the GNU General Public License as published by
  1023. X ** the Free Software Foundation; either version 1, or any later version.
  1024. X ** This program is distributed in the hope that it will be useful,
  1025. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1026. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1027. X ** GNU General Public License for more details.
  1028. X ** You can receive a copy of the GNU General Public License from the
  1029. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1030. X **/
  1031. X
  1032. /**
  1033. X ** granite.c - Granite shader: using a 1/f fractal noise function for 
  1034. X **             mixing color values.  
  1035. X **/
  1036. X
  1037. #include <math.h>
  1038. #include <stdio.h>
  1039. X
  1040. #include <sipp.h>
  1041. #include <noise.h>
  1042. #include <shaders.h>
  1043. X
  1044. X
  1045. extern bool noise_ready;
  1046. X
  1047. X
  1048. static void 
  1049. granite (p, color, gd)
  1050. X    Vector *p;
  1051. X    Color *color;
  1052. X    Granite_desc *gd;
  1053. {
  1054. X    int i;
  1055. X    Vector v;
  1056. X    double temp, n = 0.5, freq = 1.0;
  1057. X
  1058. X    v = *p;
  1059. X    
  1060. X    for (i = 0; i < 6 ; freq *= 2.0, i++) {
  1061. X        v.x *= 4.0 * freq;
  1062. X        v.y *= 4.0 * freq;
  1063. X        v.z *= 4.0 * freq;
  1064. X        temp = 0.5 * noise(&v);
  1065. /*        temp = fabs(temp);*/
  1066. X        n += temp / freq;
  1067. X    }
  1068. X
  1069. X   color->red = gd->col1.red * n + gd->col2.red * (1.0 - n);
  1070. X   color->grn = gd->col1.grn * n + gd->col2.grn * (1.0 - n);
  1071. X   color->blu = gd->col1.blu * n + gd->col2.blu * (1.0 - n);
  1072. }
  1073. X
  1074. X
  1075. void
  1076. granite_shader(nx, ny, nz, u, v, w, view_vec, lights, gd, color)
  1077. X    double  nx, ny, nz, u, v, w;
  1078. X    Vector  view_vec;
  1079. X    Lightsource *lights;
  1080. X    Granite_desc *gd;
  1081. X    Color *color;
  1082. {
  1083. X    Vector     tmp;
  1084. X    Surf_desc  surface;
  1085. X
  1086. X    if (!noise_ready) {
  1087. X        noise_init();
  1088. X    }
  1089. X
  1090. X    tmp.x = u * gd->scale;
  1091. X    tmp.y = v * gd->scale;
  1092. X    tmp.z = w * gd->scale;
  1093. X    granite(&tmp, &surface.color, gd);
  1094. X    surface.ambient  = gd->ambient;
  1095. X    surface.specular = gd->specular;
  1096. X    surface.c3       = gd->c3;
  1097. X    basic_shader(nx, ny, nz, u, v, w, view_vec, lights, &surface, color);
  1098. }
  1099. SHAR_EOF
  1100. chmod 0644 libsipp/granite.c ||
  1101. echo 'restore of libsipp/granite.c failed'
  1102. Wc_c="`wc -c < 'libsipp/granite.c'`"
  1103. test 2260 -eq "$Wc_c" ||
  1104.     echo 'libsipp/granite.c: original size 2260, current size' "$Wc_c"
  1105. fi
  1106. # ============= libsipp/lightsource.c ==============
  1107. if test -f 'libsipp/lightsource.c' -a X"$1" != X"-c"; then
  1108.     echo 'x - skipping libsipp/lightsource.c (File already exists)'
  1109. else
  1110. echo 'x - extracting libsipp/lightsource.c (Text)'
  1111. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/lightsource.c' &&
  1112. /**
  1113. X ** sipp - SImple Polygon Processor
  1114. X **
  1115. X **  A general 3d graphic package
  1116. X **
  1117. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1118. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1119. X **
  1120. X ** This program is free software; you can redistribute it and/or modify
  1121. X ** it under the terms of the GNU General Public License as published by
  1122. X ** the Free Software Foundation; either version 1, or any later version.
  1123. X ** This program is distributed in the hope that it will be useful,
  1124. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1125. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1126. X ** GNU General Public License for more details.
  1127. X ** You can receive a copy of the GNU General Public License from the
  1128. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1129. X **/
  1130. X
  1131. /**
  1132. X ** lightsource.c - Functions that handles lightsources.
  1133. X **/
  1134. X
  1135. #include <geometric.h>
  1136. #include <lightsource.h>
  1137. #include <sipp.h>
  1138. #include <smalloc.h>
  1139. X
  1140. X
  1141. Lightsource  *lightsrc_stack;  /* Stack of installed lightsources. */
  1142. X
  1143. X
  1144. /*
  1145. X * Define a new lightsource in the scene.
  1146. X */
  1147. void
  1148. lightsource_push(x, y, z, intensity)
  1149. X    double  x, y, z, intensity;
  1150. {
  1151. X    double       norm;
  1152. X    Lightsource *lp;
  1153. X
  1154. X    norm = sqrt(x * x + y * y + z * z);
  1155. X    lp = (Lightsource *)smalloc(sizeof(Lightsource));
  1156. X    lp->dir.x = x / norm;
  1157. X    lp->dir.y = y / norm;
  1158. X    lp->dir.z = z / norm;
  1159. X    lp->intensity = intensity;
  1160. X    lp->next = lightsrc_stack;
  1161. X    lightsrc_stack = lp;
  1162. }
  1163. X
  1164. X
  1165. SHAR_EOF
  1166. chmod 0644 libsipp/lightsource.c ||
  1167. echo 'restore of libsipp/lightsource.c failed'
  1168. Wc_c="`wc -c < 'libsipp/lightsource.c'`"
  1169. test 1503 -eq "$Wc_c" ||
  1170.     echo 'libsipp/lightsource.c: original size 1503, current size' "$Wc_c"
  1171. fi
  1172. # ============= libsipp/lightsource.h ==============
  1173. if test -f 'libsipp/lightsource.h' -a X"$1" != X"-c"; then
  1174.     echo 'x - skipping libsipp/lightsource.h (File already exists)'
  1175. else
  1176. echo 'x - extracting libsipp/lightsource.h (Text)'
  1177. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/lightsource.h' &&
  1178. /**
  1179. X ** sipp - SImple Polygon Processor
  1180. X **
  1181. X **  A general 3d graphic package
  1182. X **
  1183. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1184. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1185. X **
  1186. X ** This program is free software; you can redistribute it and/or modify
  1187. X ** it under the terms of the GNU General Public License as published by
  1188. X ** the Free Software Foundation; either version 1, or any later version.
  1189. X ** This program is distributed in the hope that it will be useful,
  1190. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1191. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1192. X ** GNU General Public License for more details.
  1193. X ** You can receive a copy of the GNU General Public License from the
  1194. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1195. X **/
  1196. X
  1197. /**
  1198. X ** lightsource.h - Interface to lightsource.c
  1199. X **/
  1200. X
  1201. #ifndef LIGHTSOURCE_H
  1202. #define LIGHTSOURCE_H
  1203. X
  1204. #include <sipp.h>
  1205. X
  1206. X
  1207. extern Lightsource  *lightsrc_stack;  /* Lightsource list. */
  1208. X
  1209. X
  1210. #define lightsource_init()    lightsrc_stack = NULL
  1211. X
  1212. X
  1213. #endif /* LIGHTSOURCE_H */
  1214. SHAR_EOF
  1215. chmod 0644 libsipp/lightsource.h ||
  1216. echo 'restore of libsipp/lightsource.h failed'
  1217. Wc_c="`wc -c < 'libsipp/lightsource.h'`"
  1218. test 1109 -eq "$Wc_c" ||
  1219.     echo 'libsipp/lightsource.h: original size 1109, current size' "$Wc_c"
  1220. fi
  1221. # ============= libsipp/marble.c ==============
  1222. if test -f 'libsipp/marble.c' -a X"$1" != X"-c"; then
  1223.     echo 'x - skipping libsipp/marble.c (File already exists)'
  1224. else
  1225. echo 'x - extracting libsipp/marble.c (Text)'
  1226. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/marble.c' &&
  1227. /**
  1228. X ** sipp - SImple Polygon Processor
  1229. X **
  1230. X **  A general 3d graphic package
  1231. X **
  1232. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1233. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1234. X **
  1235. X ** This program is free software; you can redistribute it and/or modify
  1236. X ** it under the terms of the GNU General Public License as published by
  1237. X ** the Free Software Foundation; either version 1, or any later version.
  1238. X ** This program is distributed in the hope that it will be useful,
  1239. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1240. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1241. X ** GNU General Public License for more details.
  1242. X ** You can receive a copy of the GNU General Public License from the
  1243. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1244. X **/
  1245. X
  1246. /**
  1247. X ** marble.c - Marble shader: simulates marble using noise & turbulence
  1248. X **/
  1249. X
  1250. #include <math.h>
  1251. #include <stdio.h>
  1252. X
  1253. #include <sipp.h>
  1254. #include <noise.h>
  1255. #include <shaders.h>
  1256. X
  1257. X
  1258. extern bool noise_ready;
  1259. X
  1260. X
  1261. static void
  1262. marble(p, color, md)
  1263. X    Vector *p;
  1264. X    Color *color;
  1265. X    Marble_desc *md;
  1266. {
  1267. X    double x, t;
  1268. X
  1269. X    x = p->x + turbulence(p, 7) * 5;
  1270. X    x = sin(x);
  1271. X    if (x > -0.1 && x < 0.1) {
  1272. X        color->red = md->strip.red;
  1273. X        color->grn = md->strip.grn;
  1274. X        color->blu = md->strip.blu;
  1275. X    } else if (x > -0.9 && x < 0.9){
  1276. X        /* You are not supposed to understand this... */
  1277. X        t = 7.0 / 6.0 - 1.0 / (6.25 * fabs(x) + 0.375);
  1278. X        color->red = md->strip.red + t * (md->base.red - md->strip.red);
  1279. X        color->grn = md->strip.grn + t * (md->base.grn - md->strip.grn);
  1280. X        color->blu = md->strip.blu + t * (md->base.blu - md->strip.blu);
  1281. X    } else {
  1282. X        color->red = md->base.red;         
  1283. X        color->grn = md->base.grn;
  1284. X        color->blu = md->base.blu;
  1285. X    }
  1286. }
  1287. X
  1288. X
  1289. X
  1290. void
  1291. marble_shader(nx, ny, nz, u, v, w, view_vec, lights, md, color)
  1292. X    double  nx, ny, nz, u, v, w;
  1293. X    Vector  view_vec;
  1294. X    Lightsource *lights;
  1295. X    Marble_desc *md;
  1296. X    Color *color;
  1297. {
  1298. X    Vector     tmp;
  1299. X    Surf_desc  surface;
  1300. X
  1301. X    if (!noise_ready) {
  1302. X        noise_init();
  1303. X    }
  1304. X
  1305. X    tmp.x = u * md->scale;
  1306. X    tmp.y = v * md->scale;
  1307. X    tmp.z = w * md->scale;
  1308. X    marble(&tmp, &surface.color, md);
  1309. X    surface.ambient  = md->ambient;
  1310. X    surface.specular = md->specular;
  1311. X    surface.c3       = md->c3;
  1312. X    basic_shader(nx, ny, nz, u, v, w, view_vec, lights, &surface, color);
  1313. }
  1314. SHAR_EOF
  1315. chmod 0664 libsipp/marble.c ||
  1316. echo 'restore of libsipp/marble.c failed'
  1317. Wc_c="`wc -c < 'libsipp/marble.c'`"
  1318. test 2442 -eq "$Wc_c" ||
  1319.     echo 'libsipp/marble.c: original size 2442, current size' "$Wc_c"
  1320. fi
  1321. # ============= libsipp/mask.c ==============
  1322. if test -f 'libsipp/mask.c' -a X"$1" != X"-c"; then
  1323.     echo 'x - skipping libsipp/mask.c (File already exists)'
  1324. else
  1325. echo 'x - extracting libsipp/mask.c (Text)'
  1326. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/mask.c' &&
  1327. /**
  1328. X ** sipp - SImple Polygon Processor
  1329. X **
  1330. X **  A general 3d graphic package
  1331. X **
  1332. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1333. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1334. X **
  1335. X ** This program is free software; you can redistribute it and/or modify
  1336. X ** it under the terms of the GNU General Public License as published by
  1337. X ** the Free Software Foundation; either version 1, or any later version.
  1338. X ** This program is distributed in the hope that it will be useful,
  1339. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1340. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1341. X ** GNU General Public License for more details.
  1342. X ** You can receive a copy of the GNU General Public License from the
  1343. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1344. X **/
  1345. X
  1346. /**
  1347. X ** mask.c - Mask shader: use a masking function to select between 
  1348. X **          two shaders.
  1349. X **/
  1350. X
  1351. #include <sipp.h>
  1352. #include <shaders.h>
  1353. X
  1354. void
  1355. mask_shader(a, b, c, u, v, w, view_vec, lights, sd, color)
  1356. X    double  a, b, c, u, v, w;
  1357. X    Vector  view_vec;
  1358. X    Lightsource *lights;
  1359. X    Mask_desc *sd;
  1360. X    Color *color;
  1361. {
  1362. X    int  ras_x, ras_y;
  1363. X    bool mapped;
  1364. X
  1365. X    ras_x = u * sd->xscale + sd->x0;
  1366. X    ras_y = v * sd->yscale + sd->y0;
  1367. X    mapped = ((ras_x >=0 && ras_x < sd->xsize) 
  1368. X              && (ras_y >= 0 && ras_y < sd->ysize)
  1369. X              && sd->pixel_test(sd->mask, ras_x, ras_y));
  1370. X    if (mapped) {
  1371. X        sd->fg_shader(a, b, c, u, v, w, view_vec, lights, sd->fg_surface,
  1372. X                      color); 
  1373. X    } else {
  1374. X        sd->bg_shader(a, b, c, u, v, w, view_vec, lights, sd->bg_surface,
  1375. X                      color); 
  1376. X    }
  1377. }
  1378. SHAR_EOF
  1379. chmod 0664 libsipp/mask.c ||
  1380. echo 'restore of libsipp/mask.c failed'
  1381. Wc_c="`wc -c < 'libsipp/mask.c'`"
  1382. test 1687 -eq "$Wc_c" ||
  1383.     echo 'libsipp/mask.c: original size 1687, current size' "$Wc_c"
  1384. fi
  1385. true || echo 'restore of libsipp/noise.c failed'
  1386. echo End of part 2, continue with part 3
  1387. exit 0
  1388.  
  1389. -- 
  1390. ------------------------------------------------------------------------------
  1391.  J o n a s   Y n g v e s s o n
  1392. Dept. of Electrical Engineering                             jonas-y@isy.liu.se
  1393. University of Linkoping, Sweden                   ...!uunet!isy.liu.se!jonas-y
  1394.  
  1395. exit 0 # Just in case...
  1396.