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

  1. Newsgroups: comp.sources.misc
  2. From: Jonas Yngvesson <jonas-y@isy.liu.se>
  3. Subject:  v21i030:  sipp - A 3D rendering library v2.1, Part05/08
  4. Message-ID: <1991Jul23.181721.27867@sparky.IMD.Sterling.COM>
  5. X-Md4-Signature: c4e97e17ae1eaba0071086ec8bae4365
  6. Date: Tue, 23 Jul 1991 18:17:21 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 30
  11. Archive-name: sipp/part05
  12. Supersedes: sipp2.0: Volume 16, Issue 5-10
  13. Environment: UNIX
  14.  
  15. #!/bin/sh
  16. # This is part 05 of sipp-2.1
  17. # ============= libsipp/sipp.h ==============
  18. if test ! -d 'libsipp'; then
  19.     echo 'x - creating directory libsipp'
  20.     mkdir 'libsipp'
  21. fi
  22. if test -f 'libsipp/sipp.h' -a X"$1" != X"-c"; then
  23.     echo 'x - skipping libsipp/sipp.h (File already exists)'
  24. else
  25. echo 'x - extracting libsipp/sipp.h (Text)'
  26. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp.h' &&
  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 ** sipp.h - Public inteface to the sipp rendering library.
  48. X **/
  49. X
  50. X
  51. #ifndef _SIPP_H
  52. #define _SIPP_H
  53. X
  54. #include <geometric.h>
  55. X
  56. X
  57. #ifndef M_PI
  58. #define M_PI 3.1415926535897932384626
  59. #endif
  60. X
  61. #ifndef FALSE
  62. typedef int bool;
  63. #define FALSE  0
  64. #define TRUE   1
  65. #endif 
  66. X
  67. /*
  68. X * Customize for those that don't have memcpy() and friends, but
  69. X * have bcopy() instead.
  70. X */
  71. X
  72. #ifdef NOMEMCPY
  73. #define memcpy(to, from, n) bcopy((from), (to), (n))
  74. #endif
  75. X
  76. X
  77. /*
  78. X * The macro RANDOM() should return a random number
  79. X * in the range [-1, 1].
  80. X */
  81. extern double drand48();
  82. #define RANDOM()  (2.0 * drand48() - 1.0)
  83. X
  84. X
  85. /*
  86. X * Modes for rendering
  87. X */
  88. #define PHONG      0
  89. #define GOURAUD    1
  90. #define FLAT       2
  91. #define LINE       3
  92. X
  93. X
  94. /*
  95. X * Interface to shader functions.
  96. X */
  97. typedef void Shader();
  98. X
  99. X
  100. /*
  101. X * Colors are handled as an rgb-triple
  102. X * with values between 0 and 1.
  103. X */
  104. typedef struct {
  105. X    double   red;
  106. X    double   grn;
  107. X    double   blu;
  108. } Color;
  109. X
  110. X
  111. /*
  112. X * Structure storing the vertices in surfaces. The vertices for a
  113. X * surface are stored in a binary tree sorted first on x, then y and last z.
  114. X */
  115. typedef struct vertex_t {
  116. X    double            x, y, z;    /* vertex position */
  117. X    double            a, b, c;    /* average normal at vertex */
  118. X    double            u, v, w;    /* texture parameters (if any) */
  119. X    struct vertex_t  *big, *sml;  /* pointers to children in the tree */
  120. } Vertex;
  121. X
  122. X
  123. /*
  124. X * Structure to keep a list of vertex references.
  125. X */
  126. typedef struct vertex_ref_t {
  127. X    Vertex       *vertex;
  128. X    struct vertex_ref_t *next;
  129. } Vertex_ref;
  130. X
  131. X
  132. /*
  133. X * Polygon definition. A polygon is defined by a list of
  134. X * references to its vertices (counterclockwize order).
  135. X */
  136. typedef struct polygon_t {
  137. X    Vertex_ref *vertices;     /* vertex list */
  138. X    bool        backface;   /* polygon is backfacing (used at rendering) */
  139. X    struct polygon_t  *next;
  140. } Polygon;
  141. X
  142. X
  143. /*
  144. X * Surface definition. Each surface consists of a vertex tree, 
  145. X * a polygon list, a pointer to a surface description and a pointer
  146. X * to a shader function.
  147. X */
  148. typedef struct surface_t {
  149. X    Vertex           *vertices;          /* vertex tree */
  150. X    Polygon          *polygons;          /* polygon list */
  151. X    void             *surface;           /* surface description */
  152. X    Shader           *shader;            /* shader function */
  153. X    int               ref_count;         /* no of references to this surface */
  154. X    struct surface_t *next;              /* next surface in the list */
  155. } Surface;
  156. X
  157. X
  158. /*
  159. X * Object definition. Object consists of one or more
  160. X * surfaces and/or one or more subojects. Each object
  161. X * has its own transformation matrix that affects itself
  162. X * and all its subobjects.
  163. X */
  164. typedef struct object_t {
  165. X    Surface         *surfaces;       /* List of surfaces */
  166. X    struct object_t *sub_obj;        /* List of subobjects */
  167. X    Transf_mat       transf;         /* Transformation matrix */
  168. X    int              ref_count;      /* No of references to this object */
  169. X    struct object_t *next;           /* Next object in this list */
  170. } Object;
  171. X
  172. X
  173. X
  174. /*
  175. X * Lightsource definition. 
  176. X */
  177. typedef struct lightsource {
  178. X    double              intensity;  /* intensity, same for r, g, b */
  179. X    Vector              dir;        /* direction from origo */  
  180. X    struct lightsource *next;       /* next lightsource in the list */
  181. } Lightsource;
  182. X
  183. X
  184. /*
  185. X * Surface description used by the basic shader. This shader
  186. X * does simple shading of surfaces of a single color.
  187. X */
  188. typedef struct {
  189. X    double  ambient;       /* Fraction of color visible in ambient light */
  190. X    double  specular;      /* Fraction of colour specularly reflected */
  191. X    double  c3;            /* "Shinyness" 0 = shiny,  1 = dull */
  192. X    Color   color;         /* Colour of the surface */
  193. } Surf_desc;
  194. X
  195. X
  196. extern char  * SIPP_VERSION;
  197. X
  198. X
  199. /*
  200. X * This defines all public functions implemented in sipp.
  201. X */
  202. extern void          sipp_init();
  203. extern void          sipp_show_backfaces();
  204. extern void          vertex_push();
  205. extern void          vertex_tx_push();
  206. extern void          polygon_push();
  207. extern Surface      *surface_create();
  208. extern Surface      *surface_basic_create();
  209. extern void          surface_set_shader();
  210. extern void          surface_basic_shader();
  211. extern Object       *object_create();
  212. extern Object       *object_instance();
  213. extern Object       *object_dup();
  214. extern Object       *object_deep_dup();
  215. extern void          object_delete();
  216. extern void          object_add_surface();
  217. extern void          object_sub_surface();
  218. extern void          object_add_subobj();
  219. extern void          object_sub_subobj();
  220. extern void          object_install();
  221. extern void          object_uninstall();
  222. extern void          object_set_transf();
  223. extern Transf_mat   *object_get_transf();
  224. extern void          object_clear_transf();
  225. extern void          object_transform();
  226. extern void          object_rot_x();
  227. extern void          object_rot_y();
  228. extern void          object_rot_z();
  229. extern void          object_rot();
  230. extern void          object_scale();
  231. extern void          object_move();
  232. extern void          lightsource_push();
  233. extern void          view_from();
  234. extern void          view_at();
  235. extern void          view_up();
  236. extern void          view_focal();
  237. extern void          viewpoint();
  238. extern void          render_image_file();
  239. extern void          render_image_pixmap();
  240. extern void          basic_shader();
  241. X
  242. /*
  243. X * Macros to ensure compatibility with older versions.
  244. X */
  245. #define render_image(xres, yres, image_file)\
  246. X                render_image_file((xres), (yres), (image_file), PHONG, 2)
  247. X
  248. X
  249. #endif /* _SIPP_H */
  250. SHAR_EOF
  251. chmod 0664 libsipp/sipp.h ||
  252. echo 'restore of libsipp/sipp.h failed'
  253. Wc_c="`wc -c < 'libsipp/sipp.h'`"
  254. test 6406 -eq "$Wc_c" ||
  255.     echo 'libsipp/sipp.h: original size 6406, current size' "$Wc_c"
  256. fi
  257. # ============= libsipp/sipp_bitmap.c ==============
  258. if test -f 'libsipp/sipp_bitmap.c' -a X"$1" != X"-c"; then
  259.     echo 'x - skipping libsipp/sipp_bitmap.c (File already exists)'
  260. else
  261. echo 'x - extracting libsipp/sipp_bitmap.c (Text)'
  262. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp_bitmap.c' &&
  263. /**
  264. X ** sipp - SImple Polygon Processor
  265. X **
  266. X **  A general 3d graphic package
  267. X **
  268. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  269. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  270. X **
  271. X ** This program is free software; you can redistribute it and/or modify
  272. X ** it under the terms of the GNU General Public License as published by
  273. X ** the Free Software Foundation; either version 1, or any later version.
  274. X ** This program is distributed in the hope that it will be useful,
  275. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  276. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  277. X ** GNU General Public License for more details.
  278. X ** You can receive a copy of the GNU General Public License from the
  279. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  280. X **/
  281. X
  282. /**
  283. X ** sipp_bitmap.c - A sample bitmap for use in SIPP and routines to use it.
  284. X **/
  285. X
  286. #include <sys/types.h>
  287. #include <stdio.h>
  288. X
  289. #include <sipp.h>
  290. #include <sipp_bitmap.h>
  291. #include <patchlevel.h>
  292. X
  293. /* ================================================================ */
  294. X
  295. X
  296. /*
  297. X * Return a pointer to a new Sipp_bitmap, with the size WIDTH x HEIGHT.
  298. X * Enough space is allocated so each line ends on a byte boundary.
  299. X */
  300. Sipp_bitmap *
  301. sipp_bitmap_create(width, height)
  302. X    int   width;
  303. X    int   height;
  304. {
  305. X    Sipp_bitmap  * bm;
  306. X
  307. X    bm = (Sipp_bitmap *) malloc(sizeof(Sipp_bitmap));
  308. X    if (bm != NULL) {
  309. X        bm->width = width;
  310. X        bm->height = height;
  311. X        bm->width_bytes = (width >> 3);
  312. X        if ((width & 7) != 0) {
  313. X            bm->width_bytes++;
  314. X        } 
  315. X        bm->buffer = (u_char *) calloc(bm->width_bytes * height, 
  316. X                                       sizeof(u_char));
  317. X    }
  318. X
  319. X    if (bm == NULL || bm->buffer == NULL) {
  320. X        fprintf(stderr, "sipp_bitmap_create(): Out of virtual memory.\n");
  321. X        exit(1);
  322. X    }
  323. X
  324. X    return bm;
  325. }
  326. X
  327. X
  328. X
  329. /*
  330. X * Destruct a bitmap, and free allocated memory.
  331. X */
  332. void
  333. sipp_bitmap_destruct(bm)
  334. X    Sipp_bitmap  * bm;
  335. {
  336. X    if (bm != NULL) {
  337. X        if (bm->buffer != NULL) {
  338. X            free(bm->buffer);
  339. X        }
  340. X        free(bm);
  341. X    }
  342. }
  343. X
  344. X
  345. X
  346. X
  347. /*
  348. X * Draw a line in the Sipp_bitmap BM from (X1, Y1)
  349. X * to (X2, Y2)
  350. X */
  351. X
  352. #define PLOT(bm, width_bytes, yres, x, y) \
  353. X        (bm)[(((yres) - 1) - (y)) * (width_bytes) + ((x) >> 3)]\
  354. X         |= (1 << (7 - (x) & 7))
  355. X
  356. void
  357. sipp_bitmap_line(bm, x1, y1, x2, y2)
  358. X    Sipp_bitmap  * bm;
  359. X    int            x1, y1;
  360. X    int            x2, y2;
  361. {
  362. X    int   d;
  363. X    int   x,  y;
  364. X    int   ax, ay;
  365. X    int   sx, sy;
  366. X    int   dx, dy;
  367. X
  368. X    dx = x2 - x1;
  369. X    ax = abs(dx) << 1;
  370. X    sx = ((dx < 0) ? -1 : 1);
  371. X    dy = y2 - y1;
  372. X    ay = abs(dy) << 1;
  373. X    sy = ((dy < 0) ? -1 : 1);
  374. X
  375. X    x = x1;
  376. X    y = y1;
  377. X    if (ax > ay) {
  378. X        d = ay - (ax >> 1);
  379. X        for (;;) {
  380. X            PLOT(bm->buffer, bm->width_bytes, bm->height, x, y);
  381. X            if (x == x2) {
  382. X                return;
  383. X            }
  384. X            if (d >= 0) {
  385. X                y += sy;
  386. X                d -= ax;
  387. X            }
  388. X            x += sx;
  389. X            d += ay;
  390. X        }
  391. X    } else {
  392. X        d = ax - (ay >> 1);
  393. X        for (;;) {
  394. X            PLOT(bm->buffer, bm->width_bytes, bm->height, x, y);
  395. X            if (y == y2) {
  396. X                return;
  397. X            }
  398. X            if (d >= 0) {
  399. X                x += sx;
  400. X                d -=  ay;
  401. X            }
  402. X            y += sy;
  403. X            d += ax;
  404. X        }
  405. X    }
  406. }
  407. X
  408. X
  409. X
  410. /*
  411. X * Write the Sipp_bitmap BM to the open file FILE.
  412. X */
  413. X
  414. void
  415. sipp_bitmap_write(file, bm)
  416. X    FILE         * file;
  417. X    Sipp_bitmap  * bm;
  418. {
  419. X    int    written;
  420. X    int    wrote;
  421. X    int    left;
  422. X
  423. X    fprintf(file,  "P4\n");
  424. X    fprintf(file,  "#Image rendered with SIPP %s%s\n", 
  425. X            SIPP_VERSION, PATCHLEVEL);
  426. X    fprintf(file,  "%d\n%d\n", bm->width, bm->height);
  427. X
  428. X    wrote   = 0;
  429. X    written = 0;
  430. X    left    = bm->width_bytes * bm->height;
  431. X    while ((wrote = fwrite(bm->buffer + written, 1, left, file)) != left) {
  432. X        written += wrote;
  433. X        left -= wrote;
  434. X    }
  435. }
  436. SHAR_EOF
  437. chmod 0664 libsipp/sipp_bitmap.c ||
  438. echo 'restore of libsipp/sipp_bitmap.c failed'
  439. Wc_c="`wc -c < 'libsipp/sipp_bitmap.c'`"
  440. test 4002 -eq "$Wc_c" ||
  441.     echo 'libsipp/sipp_bitmap.c: original size 4002, current size' "$Wc_c"
  442. fi
  443. # ============= libsipp/sipp_bitmap.h ==============
  444. if test -f 'libsipp/sipp_bitmap.h' -a X"$1" != X"-c"; then
  445.     echo 'x - skipping libsipp/sipp_bitmap.h (File already exists)'
  446. else
  447. echo 'x - extracting libsipp/sipp_bitmap.h (Text)'
  448. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp_bitmap.h' &&
  449. /**
  450. X ** sipp - SImple Polygon Processor
  451. X **
  452. X **  A general 3d graphic package
  453. X **
  454. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  455. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  456. X **
  457. X ** This program is free software; you can redistribute it and/or modify
  458. X ** it under the terms of the GNU General Public License as published by
  459. X ** the Free Software Foundation; either version 1, or any later version.
  460. X ** This program is distributed in the hope that it will be useful,
  461. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  462. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  463. X ** GNU General Public License for more details.
  464. X ** You can receive a copy of the GNU General Public License from the
  465. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  466. X **/
  467. X
  468. /**
  469. X ** sipp_bitmap.h - Interface to sipp_bitmap.c
  470. X **/
  471. X
  472. #ifndef BITMAP_H
  473. #define BITMAP_H
  474. X
  475. #include <sys/types.h>
  476. X
  477. /* The generic line drawer usable for any bitmap type. */
  478. typedef void   (*Bitmap_line_func)();
  479. X
  480. X
  481. /* The SIPP bitmap and its associated functions. */
  482. X
  483. typedef struct{
  484. X    int       width;
  485. X    int       height;
  486. X    int       width_bytes;
  487. X    u_char   *buffer;
  488. } Sipp_bitmap;
  489. X
  490. X
  491. extern Sipp_bitmap   *sipp_bitmap_create();
  492. extern void           sipp_bitmap_destruct();
  493. extern void           sipp_bitmap_line();
  494. extern void           sipp_bitmap_write();
  495. X
  496. #endif /* BITMAP_H */
  497. SHAR_EOF
  498. chmod 0664 libsipp/sipp_bitmap.h ||
  499. echo 'restore of libsipp/sipp_bitmap.h failed'
  500. Wc_c="`wc -c < 'libsipp/sipp_bitmap.h'`"
  501. test 1432 -eq "$Wc_c" ||
  502.     echo 'libsipp/sipp_bitmap.h: original size 1432, current size' "$Wc_c"
  503. fi
  504. # ============= libsipp/sipp_pixmap.c ==============
  505. if test -f 'libsipp/sipp_pixmap.c' -a X"$1" != X"-c"; then
  506.     echo 'x - skipping libsipp/sipp_pixmap.c (File already exists)'
  507. else
  508. echo 'x - extracting libsipp/sipp_pixmap.c (Text)'
  509. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp_pixmap.c' &&
  510. /**
  511. X ** sipp - SImple Polygon Processor
  512. X **
  513. X **  A general 3d graphic package
  514. X **
  515. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  516. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  517. X **
  518. X ** This program is free software; you can redistribute it and/or modify
  519. X ** it under the terms of the GNU General Public License as published by
  520. X ** the Free Software Foundation; either version 1, or any later version.
  521. X ** This program is distributed in the hope that it will be useful,
  522. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  523. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  524. X ** GNU General Public License for more details.
  525. X ** You can receive a copy of the GNU General Public License from the
  526. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  527. X **/
  528. X
  529. /**
  530. X ** sipp_pixmap.c - A sample pixmap for use in SIPP and routines to use it.
  531. X **/
  532. X
  533. #include <sys/types.h>
  534. #include <stdio.h>
  535. X
  536. #include <sipp.h>
  537. #include <sipp_pixmap.h>
  538. #include <patchlevel.h>
  539. X
  540. /* ================================================================ */
  541. X
  542. X
  543. /*
  544. X * Return a pointer to a new Sipp_pixmap, with the size WIDTH x HEIGHT.
  545. X */
  546. X
  547. Sipp_pixmap *
  548. sipp_pixmap_create(width, height)
  549. X    int   width;
  550. X    int   height;
  551. {
  552. X    Sipp_pixmap  * pm;
  553. X
  554. X    pm = (Sipp_pixmap *) malloc(sizeof(Sipp_pixmap));
  555. X    if (pm != NULL) {
  556. X        pm->width = width;
  557. X        pm->height = height;
  558. X        pm->buffer = (u_char *) calloc(3 * width * height, sizeof(u_char));
  559. X    }
  560. X
  561. X    if (pm == NULL || pm->buffer == NULL) {
  562. X        fprintf(stderr, "sipp_pixmap_create(): Out of virtual memory.\n");
  563. X        exit(1);
  564. X    }
  565. X
  566. X    return pm;
  567. }
  568. X
  569. X
  570. X
  571. /*
  572. X * Destruct a pixmap, and free allocated memory.
  573. X */
  574. X
  575. void
  576. sipp_pixmap_destruct(pm)
  577. X    Sipp_pixmap  * pm;
  578. {
  579. X    if (pm != NULL) {
  580. X        if (pm->buffer != NULL) {
  581. X            free(pm->buffer);
  582. X        }
  583. X        free(pm);
  584. X    }
  585. }
  586. X
  587. X
  588. X
  589. X
  590. /*
  591. X * Set the pixel at (X, Y) in Sipp_pixmap PM to the color
  592. X * (RED, GRN, BLU).  
  593. X */
  594. X
  595. void
  596. sipp_pixmap_set_pixel(pm, x, y, red, grn, blu)
  597. X    Sipp_pixmap  * pm;
  598. X    int            x;
  599. X    int            y;
  600. X    u_char         red;
  601. X    u_char         grn;
  602. X    u_char         blu;
  603. {
  604. X    u_char  * cp;
  605. X
  606. X    if (x < 0 || y < 0 || x >= pm->width || y >= pm->height) {
  607. X        fprintf(stderr, 
  608. "Tried to write to pixel (%d, %d) in a Sipp_pixmap which was only %d x %d.\n",
  609. X                x, y, pm->width, pm->height);
  610. X    } else {
  611. X        cp = pm->buffer + 3 * (y * pm->width + x);
  612. X        *cp++ = red;
  613. X        *cp++ = grn;
  614. X        *cp = blu;
  615. X    }
  616. }
  617. X
  618. X
  619. X
  620. /*
  621. X * Write the Sipp_pixmap PM to the open file FILE.
  622. X */
  623. X
  624. void
  625. sipp_pixmap_write(file, pm)
  626. X    FILE         * file;
  627. X    Sipp_pixmap  * pm;
  628. {
  629. X    u_char  * byte;
  630. X    int       nbytes;
  631. X    int       i;
  632. X
  633. X    fprintf(file,  "P6\n");
  634. X    fprintf(file,  "#Image rendered with SIPP %s%s\n", 
  635. X            SIPP_VERSION, PATCHLEVEL);
  636. X    fprintf(file,  "%d\n%d\n255\n", pm->width, pm->height);
  637. X
  638. X    byte = pm->buffer;
  639. X    nbytes = pm->width * pm->height;
  640. X    for (i = 0; i < nbytes; ++i) {
  641. X        putc(*byte++, file);
  642. X        putc(*byte++, file);
  643. X        putc(*byte++, file);
  644. X    }
  645. }
  646. SHAR_EOF
  647. chmod 0664 libsipp/sipp_pixmap.c ||
  648. echo 'restore of libsipp/sipp_pixmap.c failed'
  649. Wc_c="`wc -c < 'libsipp/sipp_pixmap.c'`"
  650. test 3142 -eq "$Wc_c" ||
  651.     echo 'libsipp/sipp_pixmap.c: original size 3142, current size' "$Wc_c"
  652. fi
  653. # ============= libsipp/sipp_pixmap.h ==============
  654. if test -f 'libsipp/sipp_pixmap.h' -a X"$1" != X"-c"; then
  655.     echo 'x - skipping libsipp/sipp_pixmap.h (File already exists)'
  656. else
  657. echo 'x - extracting libsipp/sipp_pixmap.h (Text)'
  658. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp_pixmap.h' &&
  659. /**
  660. X ** sipp - SImple Polygon Processor
  661. X **
  662. X **  A general 3d graphic package
  663. X **
  664. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  665. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  666. X **
  667. X ** This program is free software; you can redistribute it and/or modify
  668. X ** it under the terms of the GNU General Public License as published by
  669. X ** the Free Software Foundation; either version 1, or any later version.
  670. X ** This program is distributed in the hope that it will be useful,
  671. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  672. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  673. X ** GNU General Public License for more details.
  674. X ** You can receive a copy of the GNU General Public License from the
  675. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  676. X **/
  677. X
  678. /**
  679. X ** sipp_pixmap.h - Interface to sipp_pixmap.c.
  680. X **/
  681. X
  682. #ifndef SIPP_PIXMAP_H
  683. #define SIPP_PIXMAP_H
  684. X
  685. X
  686. #include <sys/types.h>
  687. X
  688. /* The generic pixel setter usable for any pixmap type. */
  689. typedef void   (*Pixmap_set_pixel_func)();
  690. X
  691. X
  692. /* The SIPP pixmap and its associated functions. */
  693. X
  694. typedef struct {
  695. X    int       width;
  696. X    int       height;
  697. X    u_char  * buffer;
  698. } Sipp_pixmap;
  699. X
  700. X
  701. extern Sipp_pixmap  * sipp_pixmap_create();
  702. extern void           sipp_pixmap_destruct();
  703. extern void           sipp_pixmap_set_pixel();
  704. extern void           sipp_pixmap_write();
  705. X
  706. X
  707. #endif /* SIPP_PIXMAP_H */
  708. SHAR_EOF
  709. chmod 0664 libsipp/sipp_pixmap.h ||
  710. echo 'restore of libsipp/sipp_pixmap.h failed'
  711. Wc_c="`wc -c < 'libsipp/sipp_pixmap.h'`"
  712. test 1435 -eq "$Wc_c" ||
  713.     echo 'libsipp/sipp_pixmap.h: original size 1435, current size' "$Wc_c"
  714. fi
  715. # ============= libsipp/smalloc.c ==============
  716. if test -f 'libsipp/smalloc.c' -a X"$1" != X"-c"; then
  717.     echo 'x - skipping libsipp/smalloc.c (File already exists)'
  718. else
  719. echo 'x - extracting libsipp/smalloc.c (Text)'
  720. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/smalloc.c' &&
  721. /**
  722. X ** sipp - SImple Polygon Processor
  723. X **
  724. X **  A general 3d graphic package
  725. X **
  726. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  727. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  728. X **
  729. X ** This program is free software; you can redistribute it and/or modify
  730. X ** it under the terms of the GNU General Public License as published by
  731. X ** the Free Software Foundation; either version 1, or any later version.
  732. X ** This program is distributed in the hope that it will be useful,
  733. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  734. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  735. X ** GNU General Public License for more details.
  736. X ** You can receive a copy of the GNU General Public License from the
  737. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  738. X **/
  739. X
  740. /**
  741. X ** smalloc.c - "Safe" malloc and calloc.
  742. X **/
  743. X
  744. #include <malloc.h>
  745. #include <stdio.h>
  746. X
  747. X
  748. char *
  749. smalloc(size)
  750. X    int size;
  751. {
  752. X    char *p;
  753. X
  754. X    p = (char *)malloc(size);
  755. X    if (p == NULL) {
  756. X        fprintf(stderr, "smalloc(): Out of memory.\n");
  757. X        exit(1);
  758. X    }
  759. X
  760. X    return p;
  761. }
  762. X
  763. X
  764. char *
  765. scalloc(size, itemsize)
  766. X    int size;
  767. X    int itemsize;
  768. {
  769. X    char *p;
  770. X
  771. X    p = (char *)calloc(size, itemsize);
  772. X    if (p == NULL) {
  773. X        fprintf(stderr, "scalloc(): Out of memory.\n");
  774. X        exit(1);
  775. X    }
  776. X
  777. X    return p;
  778. }
  779. X
  780. X
  781. X
  782. SHAR_EOF
  783. chmod 0644 libsipp/smalloc.c ||
  784. echo 'restore of libsipp/smalloc.c failed'
  785. Wc_c="`wc -c < 'libsipp/smalloc.c'`"
  786. test 1374 -eq "$Wc_c" ||
  787.     echo 'libsipp/smalloc.c: original size 1374, current size' "$Wc_c"
  788. fi
  789. # ============= libsipp/smalloc.h ==============
  790. if test -f 'libsipp/smalloc.h' -a X"$1" != X"-c"; then
  791.     echo 'x - skipping libsipp/smalloc.h (File already exists)'
  792. else
  793. echo 'x - extracting libsipp/smalloc.h (Text)'
  794. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/smalloc.h' &&
  795. /**
  796. X ** sipp - SImple Polygon Processor
  797. X **
  798. X **  A general 3d graphic package
  799. X **
  800. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  801. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  802. X **
  803. X ** This program is free software; you can redistribute it and/or modify
  804. X ** it under the terms of the GNU General Public License as published by
  805. X ** the Free Software Foundation; either version 1, or any later version.
  806. X ** This program is distributed in the hope that it will be useful,
  807. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  808. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  809. X ** GNU General Public License for more details.
  810. X ** You can receive a copy of the GNU General Public License from the
  811. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  812. X **/
  813. X
  814. /**
  815. X ** smalloc.h - Interface to smalloc.c
  816. X **/
  817. X
  818. #ifndef SMALLOC_H
  819. #define SMALLOC_H
  820. X
  821. X
  822. extern char   *smalloc();
  823. extern char   *scalloc();
  824. X
  825. X
  826. #endif /* SMALLOC_H */
  827. SHAR_EOF
  828. chmod 0644 libsipp/smalloc.h ||
  829. echo 'restore of libsipp/smalloc.h failed'
  830. Wc_c="`wc -c < 'libsipp/smalloc.h'`"
  831. test 1006 -eq "$Wc_c" ||
  832.     echo 'libsipp/smalloc.h: original size 1006, current size' "$Wc_c"
  833. fi
  834. # ============= libsipp/strauss.c ==============
  835. if test -f 'libsipp/strauss.c' -a X"$1" != X"-c"; then
  836.     echo 'x - skipping libsipp/strauss.c (File already exists)'
  837. else
  838. echo 'x - extracting libsipp/strauss.c (Text)'
  839. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/strauss.c' &&
  840. /**
  841. X ** sipp - SImple Polygon Processor
  842. X **
  843. X **  A general 3d graphic package
  844. X **
  845. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  846. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  847. X **
  848. X ** This program is free software; you can redistribute it and/or modify
  849. X ** it under the terms of the GNU General Public License as published by
  850. X ** the Free Software Foundation; either version 1, or any later version.
  851. X ** This program is distributed in the hope that it will be useful,
  852. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  853. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  854. X ** GNU General Public License for more details.
  855. X ** You can receive a copy of the GNU General Public License from the
  856. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  857. X **/
  858. X
  859. /**
  860. X ** strauss.c - Shading model designed by Paul S. Strauss at 
  861. X **             Silicon Graphics Inc.
  862. X **             Described in IEEE CG&A November 1990.
  863. X **/
  864. X
  865. #include <math.h>
  866. X
  867. #include <sipp.h>
  868. #include <shaders.h>
  869. #include <geometric.h>
  870. X
  871. X
  872. /*
  873. X * Strauss describes, in his article, two functions that simulates
  874. X * fresnel reflection and geometric attenuation. These functions
  875. X * are dependent of an angle between 0 and pi/2 normalized to lie
  876. X * between 0 and 1. He also mentions that he uses versions of the
  877. X * functions that depend on the cosine of an angle instead (since 
  878. X * that can be calculated with a dot product).
  879. X * These versions are not described in the article so I have empirically 
  880. X * tried to recreate them. I don't know if this bears any resemblence
  881. X * to what he used, so any errors are my fault.
  882. X *
  883. X * What i did was only to change x in the functions to (1 - x) and modify
  884. X * the Kf and Kg constants slightly. The original values in the article
  885. X * was: Kf = 1.12, Kg = 1.02
  886. X */
  887. X
  888. #define Kf   1.2   /* Factor used in approximation of fresnel reflection */
  889. #define Kg   1.031 /* Factor used in approximation of geometric attenuation */
  890. X
  891. X
  892. /*
  893. X * Function to simulate fresnel reflection.
  894. X */
  895. #define FR(x) ((1.0/((1.0-(x)-Kf)*(1.0-(x)-Kf))-1.0/(Kf*Kf))\
  896. X               /(1.0/((1.0-Kf)*(1.0-Kf))-1.0/(Kf*Kf)))
  897. X
  898. /*
  899. X * Function to simulate geometric attenuation.
  900. X */
  901. #define GA(x) ((1.0/((1.0-Kg)*(1.0-Kg))-1.0/((1.0-(x)-Kg)*(1.0-(x)-Kg)))\
  902. X               /(1.0/((1.0-Kg)*(1.0-Kg))-1.0/(Kg*Kg)))
  903. X
  904. X
  905. X
  906. void
  907. strauss_shader(a, b, c, u, v, w, view_vec, lights, sd, color)
  908. X    double        a, b, c, u, v, w;
  909. X    Vector        view_vec;
  910. X    Lightsource  *lights;
  911. X    Strauss_desc *sd;
  912. X    Color        *color;
  913. {
  914. X    Vector       normal;       /* Normalized surface normal */
  915. X    Vector       highlight;    /* Highlight vector */
  916. X    double       c_alpha;      /* cos(angle between normal and lightvector) */
  917. X    double       c_beta;       /* cos(angle between highlight & view_vec) */
  918. X    double       c_gamma;      /* cos(angle between normal & view_vec) */
  919. X    double       rd;           /* Diffuse reflectivity */
  920. X    double       rs;           /* Specular reflectivity */
  921. X    double       rj;           /* Adjusted specular reflectivity */
  922. X    double       rn;           /* Specular reflectivity at normal incidence */
  923. X    double       h;            /* Shininess exponent */
  924. X    Vector       qd;           /* Diffuse reflection factor */
  925. X    Vector       qs;           /* Specular reflection factor */
  926. X    Color        col;          /* Resulting color */
  927. X    Lightsource *lp;
  928. X
  929. X    MakeVector(normal, a, b, c);
  930. X    vecnorm(&normal);
  931. X    c_gamma = VecDot(normal, view_vec);
  932. X    col.red = col.grn = col.blu = 0.0;
  933. X    rd = 1.0 - sd->smoothness * sd->smoothness * sd->smoothness;
  934. X
  935. X    for (lp = lights; lp != (Lightsource *)0; lp = lp->next) {
  936. X
  937. X        c_alpha = VecDot(normal, lp->dir);
  938. X
  939. X        if (c_alpha >= 0) {
  940. X
  941. X            VecScalMul(highlight, 2 * c_alpha, normal);
  942. X            VecSub(highlight, highlight, lp->dir);
  943. X            c_beta = VecDot(highlight, view_vec);
  944. X            
  945. X            MakeVector(qd, sd->color.red, sd->color.grn, sd->color.blu);
  946. X            VecScalMul(qd, (c_alpha * rd 
  947. X                            * (1.0 - sd->metalness * sd->smoothness)), qd);
  948. X            
  949. X            if (c_beta >= 0) {
  950. X                
  951. X                h = 3 / (1.0 - sd->smoothness);
  952. X                rn = 1.0 - rd;
  953. X                rj = rn + (rn + 0.1) * FR(c_alpha) * GA(c_alpha) * GA(c_gamma);
  954. X                if (rj > 1.0) {
  955. X                    rj = 1.0;
  956. X                }
  957. X                rs = exp(h * log(c_beta)) * rj;
  958. X                
  959. X                MakeVector(qs, 
  960. X                           sd->color.red - 1.0, 
  961. X                           sd->color.grn - 1.0, 
  962. X                           sd->color.blu - 1.0);
  963. X                VecScalMul(qs, sd->metalness * (1.0 - FR(c_alpha)), qs);
  964. X                qs.x += 1.0;
  965. X                qs.y += 1.0;
  966. X                qs.z += 1.0;
  967. X                
  968. X                VecScalMul(qs, rs, qs);
  969. X                VecAdd(qd, qd, qs);
  970. X
  971. X            }
  972. X            
  973. X            VecScalMul(qd, lp->intensity, qd);
  974. X            
  975. X            col.red += qd.x;
  976. X            col.grn += qd.y;
  977. X            col.blu += qd.z;
  978. X
  979. X        }
  980. X    }
  981. X
  982. X    col.red += sd->ambient * rd * sd->color.red;
  983. X    col.grn += sd->ambient * rd * sd->color.grn;
  984. X    col.blu += sd->ambient * rd * sd->color.blu;
  985. X
  986. X    color->red = ((col.red > 1.0) ? 1.0 : col.red);
  987. X    color->grn = ((col.grn > 1.0) ? 1.0 : col.grn);
  988. X    color->blu = ((col.blu > 1.0) ? 1.0 : col.blu);
  989. }
  990. SHAR_EOF
  991. chmod 0664 libsipp/strauss.c ||
  992. echo 'restore of libsipp/strauss.c failed'
  993. Wc_c="`wc -c < 'libsipp/strauss.c'`"
  994. test 5426 -eq "$Wc_c" ||
  995.     echo 'libsipp/strauss.c: original size 5426, current size' "$Wc_c"
  996. fi
  997. # ============= libsipp/torus.c ==============
  998. if test -f 'libsipp/torus.c' -a X"$1" != X"-c"; then
  999.     echo 'x - skipping libsipp/torus.c (File already exists)'
  1000. else
  1001. echo 'x - extracting libsipp/torus.c (Text)'
  1002. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/torus.c' &&
  1003. /**
  1004. X ** sipp - SImple Polygon Processor
  1005. X **
  1006. X **  A general 3d graphic package
  1007. X **
  1008. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1009. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1010. X **
  1011. X ** This program is free software; you can redistribute it and/or modify
  1012. X ** it under the terms of the GNU General Public License as published by
  1013. X ** the Free Software Foundation; either version 1, or any later version.
  1014. X ** This program is distributed in the hope that it will be useful,
  1015. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1016. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1017. X ** GNU General Public License for more details.
  1018. X ** You can receive a copy of the GNU General Public License from the
  1019. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1020. X **/
  1021. X
  1022. /**
  1023. X ** torus.c - Creating a torus as a sipp object.
  1024. X **/
  1025. X
  1026. #include <xalloca.h>
  1027. #include <math.h>
  1028. X
  1029. #include <sipp.h>
  1030. X
  1031. X
  1032. static void
  1033. arr_rot(arr1, arr2, len, angle)
  1034. X    Vector arr1[];
  1035. X    Vector arr2[];
  1036. X    int    len;
  1037. X    double angle;
  1038. {
  1039. X    int    i;
  1040. X    double sa, ca;
  1041. X
  1042. X    sa = sin(angle);
  1043. X    ca = cos(angle);
  1044. X    for (i = 0; i < len; i++) {
  1045. X        arr2[i].x = arr1[i].x * ca - arr1[i].y * sa;
  1046. X        arr2[i].y = arr1[i].x * sa + arr1[i].y * ca;
  1047. X        arr2[i].z = arr1[i].z;
  1048. X    }
  1049. }
  1050. X
  1051. X
  1052. static void
  1053. push_band(arr1, arr2, len)
  1054. X    Vector arr1[];
  1055. X    Vector arr2[];
  1056. X    int    len;
  1057. {
  1058. X    int i, j;
  1059. X
  1060. X    for (i = 0; i < len; i++) {
  1061. X        j = (i + 1) % len;
  1062. X        vertex_tx_push(arr1[i].x, arr1[i].y, arr1[i].z, 
  1063. X                       arr1[i].x, arr1[i].y, arr1[i].z);
  1064. X        vertex_tx_push(arr2[i].x, arr2[i].y, arr2[i].z, 
  1065. X                       arr2[i].x, arr2[i].y, arr2[i].z);
  1066. X        vertex_tx_push(arr2[j].x, arr2[j].y, arr2[j].z, 
  1067. X                       arr2[j].x, arr2[j].y, arr2[j].z);
  1068. X        vertex_tx_push(arr1[j].x, arr1[j].y, arr1[j].z, 
  1069. X                       arr1[j].x, arr1[j].y, arr1[j].z);
  1070. X        polygon_push();
  1071. X    }
  1072. }
  1073. X
  1074. X
  1075. X
  1076. Object *
  1077. sipp_torus(bigradius, smallradius, res1, res2, surface, shader)
  1078. X    double  bigradius;      /* Radius of the ring */
  1079. X    double  smallradius;    /* Radius of the "tube" */
  1080. X    int     res1;           /* Number of polygons around the ring */
  1081. X    int     res2;           /* Number of polygons around the tube */
  1082. X    void   *surface;
  1083. X    Shader *shader;
  1084. {
  1085. X    Object *torus;
  1086. X    Vector *arr1;
  1087. X    Vector *arr2;
  1088. X    Vector *tmp;
  1089. X    int     i;
  1090. X
  1091. X    /* Create two arrays to hold vertices around the tube */
  1092. X    arr1 = (Vector *)alloca(res2 * sizeof(Vector));
  1093. X    arr2 = (Vector *)alloca(res2 * sizeof(Vector));
  1094. X
  1095. X    for (i = 0; i < res2; i++) {
  1096. X        arr1[i].x = bigradius + smallradius * cos(i * 2.0 * M_PI / res2);
  1097. X        arr1[i].y = 0.0;
  1098. X        arr1[i].z = smallradius * sin(i * 2.0 * M_PI / res2);
  1099. X    }
  1100. X
  1101. X    /* Sweep out the torus by rotating the two perimeters */
  1102. X    /* defined in arr1 and arr2. */
  1103. X    for (i = 0; i < res1; i++) {
  1104. X        arr_rot(arr1, arr2, res2, 2.0 * M_PI / (double)res1);
  1105. X        push_band(arr1, arr2, res2);
  1106. X        tmp = arr1;
  1107. X        arr1 = arr2;
  1108. X        arr2 = tmp;
  1109. X    }
  1110. X
  1111. X    torus = object_create();
  1112. X    object_add_surface(torus, surface_create(surface, shader));
  1113. X
  1114. X    return torus;
  1115. }
  1116. X
  1117. SHAR_EOF
  1118. chmod 0664 libsipp/torus.c ||
  1119. echo 'restore of libsipp/torus.c failed'
  1120. Wc_c="`wc -c < 'libsipp/torus.c'`"
  1121. test 3223 -eq "$Wc_c" ||
  1122.     echo 'libsipp/torus.c: original size 3223, current size' "$Wc_c"
  1123. fi
  1124. # ============= libsipp/transforms.c ==============
  1125. if test -f 'libsipp/transforms.c' -a X"$1" != X"-c"; then
  1126.     echo 'x - skipping libsipp/transforms.c (File already exists)'
  1127. else
  1128. echo 'x - extracting libsipp/transforms.c (Text)'
  1129. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/transforms.c' &&
  1130. /**
  1131. X ** sipp - SImple Polygon Processor
  1132. X **
  1133. X **  A general 3d graphic package
  1134. X **
  1135. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1136. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1137. X **
  1138. X ** This program is free software; you can redistribute it and/or modify
  1139. X ** it under the terms of the GNU General Public License as published by
  1140. X ** the Free Software Foundation; either version 1, or any later version.
  1141. X ** This program is distributed in the hope that it will be useful,
  1142. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1143. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1144. X ** GNU General Public License for more details.
  1145. X ** You can receive a copy of the GNU General Public License from the
  1146. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1147. X **/
  1148. X
  1149. /** 
  1150. X ** transforms.c - Functions that handles object transformations.
  1151. X **/
  1152. X
  1153. #include <stdio.h>
  1154. X
  1155. #include <geometric.h>
  1156. #include <sipp.h>
  1157. X
  1158. X
  1159. /*
  1160. X * Set the transformation matrix of OBJ to MATRIX.
  1161. X */
  1162. void
  1163. object_set_transf(obj, matrix)
  1164. X    Object     *obj;
  1165. X    Transf_mat *matrix;
  1166. {
  1167. X    MatCopy(&obj->transf, matrix);
  1168. }
  1169. X
  1170. X
  1171. /*
  1172. X * Retrieve the transformation matrix of OBJ
  1173. X */
  1174. Transf_mat *
  1175. object_get_transf(obj, matrix)
  1176. X    Object     *obj;
  1177. X    Transf_mat *matrix;
  1178. {
  1179. X    Transf_mat *tmp;
  1180. X
  1181. X    if (matrix != NULL) {
  1182. X        MatCopy(matrix, &obj->transf);
  1183. X        return matrix;
  1184. X    } else {
  1185. X        tmp = transf_mat_create(NULL);
  1186. X        MatCopy(tmp, &obj->transf);
  1187. X        return tmp;
  1188. X    }
  1189. }
  1190. X
  1191. X
  1192. /*
  1193. X * Set the transformation matrix of OBJ to the identity matrix.
  1194. X */
  1195. void
  1196. object_clear_transf(obj)
  1197. X    Object *obj;
  1198. {
  1199. X    MatCopy(&obj->transf, &ident_matrix);
  1200. }
  1201. X
  1202. X
  1203. /*
  1204. X * Post multiply MATRIX into the transformation matrix of OBJ.
  1205. X */
  1206. void
  1207. object_transform(obj, matrix)
  1208. X    Object     *obj;
  1209. X    Transf_mat *matrix;
  1210. {
  1211. X    mat_mul(&obj->transf, &obj->transf, matrix);
  1212. }
  1213. X
  1214. X
  1215. /*
  1216. X * Rotate the object OBJ ANG radians about the x-axis.
  1217. X */
  1218. void
  1219. object_rot_x(obj, ang)
  1220. X    Object *obj;
  1221. X    double  ang;
  1222. {
  1223. X    mat_rotate_x(&obj->transf, ang);
  1224. }
  1225. X
  1226. X
  1227. /*
  1228. X * Rotate the object OBJ ANG radians about the y-axis.
  1229. X */
  1230. void
  1231. object_rot_y(obj, ang)
  1232. X    Object *obj;
  1233. X    double  ang;
  1234. {
  1235. X    mat_rotate_y(&obj->transf, ang);
  1236. }
  1237. X
  1238. X
  1239. /*
  1240. X * Rotate the object OBJ ANG radians about the z-axis.
  1241. X */
  1242. void
  1243. object_rot_z(obj, ang)
  1244. X    Object *obj;
  1245. X    double  ang;
  1246. {
  1247. X    mat_rotate_z(&obj->transf, ang);
  1248. }
  1249. X
  1250. X
  1251. /*
  1252. X * Rotate the object OBJ ANG radians about the line defined
  1253. X * by POINT and VEC.
  1254. X */
  1255. void
  1256. object_rot(obj, point, vec, ang)
  1257. X    Object *obj;
  1258. X    Vector *point;
  1259. X    Vector *vec;
  1260. X    double  ang;
  1261. {
  1262. X    mat_rotate(&obj->transf, point, vec, ang);
  1263. }
  1264. X
  1265. X
  1266. /*
  1267. X * Scale the object OBJ with respect to the origin.
  1268. X */
  1269. void
  1270. object_scale(obj, xscale, yscale, zscale)
  1271. X    Object *obj;
  1272. X    double  xscale, yscale, zscale;
  1273. {
  1274. X    mat_scale(&obj->transf, xscale, yscale, zscale);
  1275. }
  1276. X
  1277. X
  1278. /*
  1279. X * Translate the object OBJ.
  1280. X */
  1281. void
  1282. object_move(obj, dx, dy, dz)
  1283. X    Object *obj;
  1284. X    double  dx, dy, dz;
  1285. {
  1286. X    mat_translate(&obj->transf, dx, dy, dz);
  1287. }
  1288. X
  1289. X
  1290. X
  1291. X
  1292. SHAR_EOF
  1293. chmod 0644 libsipp/transforms.c ||
  1294. echo 'restore of libsipp/transforms.c failed'
  1295. Wc_c="`wc -c < 'libsipp/transforms.c'`"
  1296. test 3036 -eq "$Wc_c" ||
  1297.     echo 'libsipp/transforms.c: original size 3036, current size' "$Wc_c"
  1298. fi
  1299. # ============= libsipp/viewpoint.c ==============
  1300. if test -f 'libsipp/viewpoint.c' -a X"$1" != X"-c"; then
  1301.     echo 'x - skipping libsipp/viewpoint.c (File already exists)'
  1302. else
  1303. echo 'x - extracting libsipp/viewpoint.c (Text)'
  1304. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/viewpoint.c' &&
  1305. /**
  1306. X ** sipp - SImple Polygon Processor
  1307. X **
  1308. X **  A general 3d graphic package
  1309. X **
  1310. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1311. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1312. X **
  1313. X ** This program is free software; you can redistribute it and/or modify
  1314. X ** it under the terms of the GNU General Public License as published by
  1315. X ** the Free Software Foundation; either version 1, or any later version.
  1316. X ** This program is distributed in the hope that it will be useful,
  1317. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1318. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1319. X ** GNU General Public License for more details.
  1320. X ** You can receive a copy of the GNU General Public License from the
  1321. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1322. X **/
  1323. X
  1324. /**
  1325. X ** viewpoint.c - Functions that handles the viewpoint definition and
  1326. X **               calculation of viewing transformation.
  1327. X **/
  1328. X
  1329. #include <geometric.h>
  1330. #include <viewpoint.h>
  1331. X
  1332. X
  1333. Viewpoint camera;          /* Viewpoint of the scene  */
  1334. X
  1335. /*
  1336. X * Constants used in viewing transformation.
  1337. X */
  1338. double        hither;          /* Hither z-clipping plane */
  1339. double        yon;             /* Yonder z-clipping plane */
  1340. X
  1341. X
  1342. /*
  1343. X * Calculate the vector from the point of interest
  1344. X * to the viewpoint. The shaders needs this vector normalized
  1345. X * while the view coordinate transformation needs it
  1346. X * non normalized, therefore we need this routine to
  1347. X * recalculate the non normalized value.
  1348. X */
  1349. void
  1350. view_vec_eval()
  1351. {
  1352. X    MakeVector(camera.vec, 
  1353. X               camera.x0 - camera.x, 
  1354. X               camera.y0 - camera.y, 
  1355. X               camera.z0 - camera.z);
  1356. }
  1357. X
  1358. X
  1359. X    
  1360. /*
  1361. X * Define the viewpoint.
  1362. X */
  1363. void
  1364. view_from(x0, y0, z0)
  1365. X    double x0, y0, z0;
  1366. {
  1367. X    camera.x0 = x0;
  1368. X    camera.y0 = y0;
  1369. X    camera.z0 = z0;
  1370. X    view_vec_eval();
  1371. }
  1372. X
  1373. X
  1374. /*
  1375. X * Define the point that we are looking at.
  1376. X */
  1377. void
  1378. view_at(x, y, z)
  1379. X    double x, y, z;
  1380. {
  1381. X    camera.x = x;
  1382. X    camera.y = y;
  1383. X    camera.z = z;
  1384. X    view_vec_eval();
  1385. }
  1386. X
  1387. X
  1388. /*
  1389. X * Define the "up" direction of the view (well, rather the y-z plane).
  1390. X */
  1391. void
  1392. view_up(x, y, z)
  1393. X    double x, y, z;
  1394. {
  1395. X    MakeVector(camera.up, x, y, z);
  1396. }
  1397. X
  1398. X
  1399. /*
  1400. X * Set the focal ratio for the "camera".
  1401. X */
  1402. void
  1403. view_focal(ratio)
  1404. X    double ratio;
  1405. {
  1406. X    camera.focal_ratio = ratio;
  1407. }
  1408. X
  1409. X
  1410. /*
  1411. X * Set all viewpoint parameters in one call.
  1412. X */
  1413. void
  1414. viewpoint(x0, y0, z0, x, y, z, ux, uy, uz, ratio)
  1415. X    double x0, y0, z0, x, y, z, ux, uy, uz, ratio;
  1416. {
  1417. X    camera.x0 = x0;
  1418. X    camera.y0 = y0;
  1419. X    camera.z0 = z0;
  1420. X    camera.x = x;
  1421. X    camera.y = y;
  1422. X    camera.z = z;
  1423. X    MakeVector(camera.up, ux, uy, uz);
  1424. X    camera.focal_ratio = ratio;
  1425. X    view_vec_eval();
  1426. }
  1427. X
  1428. X
  1429. X
  1430. /*
  1431. X * Build a transformation matrix for transformation
  1432. X * into view coordinates.
  1433. X */
  1434. void
  1435. get_view_transf(view_mat)
  1436. X    Transf_mat *view_mat;
  1437. {
  1438. X    Vector tmp;
  1439. X    double transl[3];
  1440. X    double vy, vz;
  1441. X    int i, j;
  1442. X    
  1443. X    /*
  1444. X     * First we need a translation so the origo
  1445. X     * of the view coordinate system is placed
  1446. X     * in the viewpoint.
  1447. X     */
  1448. X    transl[0] = -camera.x0;
  1449. X    transl[1] = -camera.y0;
  1450. X    transl[2] = -camera.z0;
  1451. X
  1452. X    /*
  1453. X     * Then we need a rotation that makes the
  1454. X     * up-vector point up, and alignes the sightline
  1455. X     * with the z-axis.
  1456. X     * This code might seem magic but the algebra behind
  1457. X     * it can be found in Jim Blinn's Corner in IEEE CG&A July 1988
  1458. X     */
  1459. X    VecCopy(tmp, camera.vec);
  1460. X    VecNegate(tmp);
  1461. X    vecnorm(&tmp);
  1462. X    vecnorm(&camera.up);
  1463. X    vz = VecDot(tmp, camera.up);
  1464. X    if ((vz * vz) > 1.0) {        /* this should not happen, but... */
  1465. X        vz = 1.0;
  1466. X    } else {
  1467. X        vy = sqrt(1.0 - vz * vz);
  1468. X        if (vy == 0.0) {          /* oops, the world collapses... */
  1469. X            vy = 1.0e10;
  1470. X            vz = 1.0;
  1471. X        } else {
  1472. X            vy = 1.0 / vy;
  1473. X        }
  1474. X    }
  1475. X
  1476. X    view_mat->mat[0][2] = tmp.x;
  1477. X    view_mat->mat[1][2] = tmp.y;
  1478. X    view_mat->mat[2][2] = tmp.z;
  1479. X
  1480. X    VecScalMul(tmp, vz, tmp);
  1481. X    VecSub(tmp, camera.up, tmp);
  1482. X    view_mat->mat[0][1] = tmp.x * vy;
  1483. X    view_mat->mat[1][1] = tmp.y * vy;
  1484. X    view_mat->mat[2][1] = tmp.z * vy;
  1485. X
  1486. X    view_mat->mat[0][0] = (view_mat->mat[1][1] * view_mat->mat[2][2] 
  1487. X                           - view_mat->mat[1][2] * view_mat->mat[2][1]);
  1488. X    view_mat->mat[1][0] = (view_mat->mat[2][1] * view_mat->mat[0][2] 
  1489. X                           - view_mat->mat[2][2] * view_mat->mat[0][1]);
  1490. X    view_mat->mat[2][0] = (view_mat->mat[0][1] * view_mat->mat[1][2] 
  1491. X                           - view_mat->mat[0][2] * view_mat->mat[1][1]);
  1492. X
  1493. X    /*
  1494. X     * Install the translation into the matrix.
  1495. X     * Note that it is PRE-multiplied into the matrix.
  1496. X     */
  1497. X    for (i = 0; i < 3; i++) {
  1498. X        view_mat->mat[3][i] = 0.0;
  1499. X        for (j = 0; j < 3; j++) {
  1500. X            view_mat->mat[3][i] += transl[j] * view_mat->mat[j][i];
  1501. X        }
  1502. X    }
  1503. X
  1504. X    /*
  1505. X     * Since the screen coordinates are defined in a left handed
  1506. X     * coordinate system, we must switch the sign of the first
  1507. X     * column in the matrix to get appropriate signs of x-values.
  1508. X     */
  1509. X    view_mat->mat[0][0] = -view_mat->mat[0][0];
  1510. X    view_mat->mat[1][0] = -view_mat->mat[1][0];
  1511. X    view_mat->mat[2][0] = -view_mat->mat[2][0];
  1512. X    view_mat->mat[3][0] = -view_mat->mat[3][0];
  1513. X
  1514. X     
  1515. X    /*
  1516. X     * Define the perspective transformation
  1517. X     * using heuristic values for hither and yon.
  1518. X     */
  1519. X    hither = VecLen(camera.vec) / ZCLIPF;
  1520. X    yon    = VecLen(camera.vec) * ZCLIPF;
  1521. }
  1522. SHAR_EOF
  1523. chmod 0644 libsipp/viewpoint.c ||
  1524. echo 'restore of libsipp/viewpoint.c failed'
  1525. Wc_c="`wc -c < 'libsipp/viewpoint.c'`"
  1526. test 5462 -eq "$Wc_c" ||
  1527.     echo 'libsipp/viewpoint.c: original size 5462, current size' "$Wc_c"
  1528. fi
  1529. # ============= libsipp/viewpoint.h ==============
  1530. if test -f 'libsipp/viewpoint.h' -a X"$1" != X"-c"; then
  1531.     echo 'x - skipping libsipp/viewpoint.h (File already exists)'
  1532. else
  1533. echo 'x - extracting libsipp/viewpoint.h (Text)'
  1534. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/viewpoint.h' &&
  1535. /**
  1536. X ** sipp - SImple Polygon Processor
  1537. X **
  1538. X **  A general 3d graphic package
  1539. X **
  1540. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1541. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1542. X **
  1543. X ** This program is free software; you can redistribute it and/or modify
  1544. X ** it under the terms of the GNU General Public License as published by
  1545. X ** the Free Software Foundation; either version 1, or any later version.
  1546. X ** This program is distributed in the hope that it will be useful,
  1547. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1548. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1549. X ** GNU General Public License for more details.
  1550. X ** You can receive a copy of the GNU General Public License from the
  1551. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1552. X **/
  1553. X
  1554. /**
  1555. X ** viewpoint.h - Types and interface to viewpoint.c
  1556. X **/
  1557. X
  1558. #ifndef VIEWPOINT_H
  1559. #define VIEWPOINT_H
  1560. X
  1561. #include <geometric.h>
  1562. X
  1563. #define ZCLIPF 100.0        /* Magic number used when defining hither & yon */
  1564. X
  1565. X
  1566. /*
  1567. X * Viewpoint definition
  1568. X */
  1569. typedef struct {
  1570. X    double x0, y0, z0;    /* viewpoint position */
  1571. X    double x, y, z;       /* point to look at */
  1572. X    Vector vec;           /* vector from point to eye, used in shading calc. */
  1573. X    Vector up;            /* Up direction in the view */ 
  1574. X    double focal_ratio;
  1575. } Viewpoint;
  1576. X
  1577. X
  1578. extern Viewpoint camera;          /* Viewpoint of the scene  */
  1579. extern double    hither;          /* Hither z-clipping plane */
  1580. extern double    yon;             /* Yonder z-clipping plane */
  1581. X
  1582. extern void      view_vec_eval();
  1583. extern void      get_view_transf();
  1584. X
  1585. X
  1586. #endif /* VIEWPOINT_H */
  1587. SHAR_EOF
  1588. chmod 0644 libsipp/viewpoint.h ||
  1589. echo 'restore of libsipp/viewpoint.h failed'
  1590. Wc_c="`wc -c < 'libsipp/viewpoint.h'`"
  1591. test 1666 -eq "$Wc_c" ||
  1592.     echo 'libsipp/viewpoint.h: original size 1666, current size' "$Wc_c"
  1593. fi
  1594. # ============= libsipp/wood.c ==============
  1595. if test -f 'libsipp/wood.c' -a X"$1" != X"-c"; then
  1596.     echo 'x - skipping libsipp/wood.c (File already exists)'
  1597. else
  1598. echo 'x - extracting libsipp/wood.c (Text)'
  1599. sed 's/^X//' << 'SHAR_EOF' > 'libsipp/wood.c' &&
  1600. /**
  1601. X ** sipp - SImple Polygon Processor
  1602. X **
  1603. X **  A general 3d graphic package
  1604. X **
  1605. X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
  1606. X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
  1607. X **
  1608. X ** This program is free software; you can redistribute it and/or modify
  1609. X ** it under the terms of the GNU General Public License as published by
  1610. X ** the Free Software Foundation; either version 1, or any later version.
  1611. X ** This program is distributed in the hope that it will be useful,
  1612. X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  1613. X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1614. X ** GNU General Public License for more details.
  1615. X ** You can receive a copy of the GNU General Public License from the
  1616. X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1617. X **/
  1618. X
  1619. /**
  1620. X ** wood.c - Wood shader: Simulates wood using multiple skewed "cylinders"
  1621. X **          distorted with noise and turbulence.
  1622. X **/
  1623. X
  1624. #include <math.h>
  1625. #include <stdio.h>
  1626. X
  1627. #include <sipp.h>
  1628. #include <noise.h>
  1629. #include <shaders.h>
  1630. X
  1631. X
  1632. #define BOARDSIZE  45.0    /* "Log" or "board" size in texture coordinates */
  1633. X
  1634. extern bool noise_ready;
  1635. X
  1636. void
  1637. wood_shader(nx, ny, nz, u, v, w, view_vec, lights, wd, color)
  1638. X    double  nx, ny, nz, u, v, w;
  1639. X    Vector  view_vec;
  1640. X    Lightsource *lights;
  1641. X    Wood_desc *wd;
  1642. X    Color *color;
  1643. {
  1644. X    Vector    pos;
  1645. X    Vector    tmp;
  1646. X    Surf_desc surface;
  1647. X    double chaos;
  1648. X    double val;
  1649. X    double val2;
  1650. X    double skewoff;
  1651. X    double t;
  1652. X    double rad;
  1653. X
  1654. X    if (!noise_ready) {
  1655. X        noise_init();
  1656. X    }
  1657. X
  1658. X    /*
  1659. X     * Scale the texture coordinates.
  1660. X     */
  1661. X    pos.x = u * wd->scale;
  1662. X    pos.y = v * wd->scale;
  1663. X    pos.z = w * wd->scale;
  1664. X
  1665. X    /*
  1666. X     * Get some noise values. Drag out the texture in
  1667. X     * the direction of the "stem" of the fictive tree, the
  1668. X     * pattern should vary less along that direction.
  1669. X     */
  1670. X    pos.x *= 0.08;
  1671. X    chaos = turbulence(&pos, 5) * 0.5;
  1672. X    val2 = noise(&pos);
  1673. X
  1674. X    /*
  1675. X     * Make the pattern "semi"-periodic so it looks as if
  1676. X     * a new board is used at regular intervals
  1677. X     */
  1678. X    if (pos.z > 0.0) {
  1679. X        tmp.z = floor((pos.z + BOARDSIZE * 0.5) / BOARDSIZE);
  1680. X        pos.z -= tmp.z * BOARDSIZE;
  1681. X    } else {
  1682. X        tmp.z = floor((BOARDSIZE * 0.5 - pos.z) / BOARDSIZE);
  1683. X        pos.z += tmp.z * BOARDSIZE;
  1684. X    }
  1685. X    if (pos.y > 0.0) {
  1686. X        tmp.y = floor((pos.y + BOARDSIZE * 0.5) / BOARDSIZE);
  1687. X        pos.y -= tmp.y * BOARDSIZE;
  1688. X    } else {
  1689. X        tmp.y = floor((BOARDSIZE * 0.5 - pos.y) / BOARDSIZE);
  1690. X        pos.y += tmp.y * BOARDSIZE;
  1691. X    }
  1692. X
  1693. X    /* 
  1694. X     * Skew the "stem" a bit so the "cylinders" isn't perfectly
  1695. X     * symmertic about the x-axis. Skew the different "logs"
  1696. X     * slightly differently.
  1697. X     */
  1698. X    tmp.x = 0.0;
  1699. X    skewoff = noise(&tmp);
  1700. X    pos.z -= (0.05 + 0.03 * skewoff) * (u * wd->scale - 2.0);
  1701. X    pos.y -= (0.05 + 0.03 * skewoff) * (u * wd->scale - 2.0);
  1702. X
  1703. X    /*
  1704. X     * Calculate the distance from the middle of the "stem" and
  1705. X     * distort this distance with the turbulence value.
  1706. X     */
  1707. X    rad = sqrt(pos.y * pos.y + pos.z * pos.z);
  1708. X    rad += chaos;
  1709. X    val = rad - floor(rad);
  1710. X
  1711. X    /*
  1712. X     * Choose a color dependent on the distorted distance.
  1713. X     */
  1714. X    if (val < 0.1) {
  1715. X        surface.color.red = wd->base.red;
  1716. X        surface.color.grn = wd->base.grn;
  1717. X        surface.color.blu = wd->base.blu;
  1718. X    } else if (val < 0.9) {
  1719. X        t = 1.0 - pow(val / 0.8 - 0.1, 6.0);
  1720. X        surface.color.red = wd->ring.red + t * (wd->base.red 
  1721. X                                                  - wd->ring.red);
  1722. X        surface.color.grn = wd->ring.grn + t * (wd->base.grn 
  1723. X                                                  - wd->ring.grn);
  1724. X        surface.color.blu = wd->ring.blu + t * (wd->base.blu 
  1725. X                                                  - wd->ring.blu);
  1726. X    } else {
  1727. X        surface.color.red = wd->ring.red;
  1728. X        surface.color.grn = wd->ring.grn;
  1729. X        surface.color.blu = wd->ring.blu;
  1730. X    }
  1731. X
  1732. X    /*
  1733. X     * Add a little extra "noise" so the pattern doesn't get
  1734. X     * too regular, this could be small cracks, or other anomalies
  1735. X     * in the wood.
  1736. X     */
  1737. X    if (val2 < 0.01 && val2 > 0.0) {
  1738. X        surface.color.red = wd->ring.red;
  1739. X        surface.color.grn = wd->ring.grn;
  1740. X        surface.color.blu = wd->ring.blu;
  1741. X    }
  1742. X
  1743. X
  1744. X    surface.ambient  = wd->ambient;
  1745. X    surface.specular = wd->specular;
  1746. X    surface.c3       = wd->c3;
  1747. X    basic_shader(nx, ny, nz, u, v, w, view_vec, lights, &surface, color);
  1748. }
  1749. SHAR_EOF
  1750. chmod 0644 libsipp/wood.c ||
  1751. echo 'restore of libsipp/wood.c failed'
  1752. Wc_c="`wc -c < 'libsipp/wood.c'`"
  1753. test 4490 -eq "$Wc_c" ||
  1754.     echo 'libsipp/wood.c: original size 4490, current size' "$Wc_c"
  1755. fi
  1756. true || echo 'restore of libsipp/xalloca.c failed'
  1757. echo End of part 5, continue with part 6
  1758. exit 0
  1759.  
  1760. -- 
  1761. ------------------------------------------------------------------------------
  1762.  J o n a s   Y n g v e s s o n
  1763. Dept. of Electrical Engineering                             jonas-y@isy.liu.se
  1764. University of Linkoping, Sweden                   ...!uunet!isy.liu.se!jonas-y
  1765.  
  1766. exit 0 # Just in case...
  1767.