From pa.dec.com!decwrl!uunet!sparky!kent Thu Jul 25 08:57:38 PDT 1991
Article: 2530 of comp.sources.misc
Newsgroups: comp.sources.misc
Path: pa.dec.com!decwrl!uunet!sparky!kent
From: Jonas Yngvesson <jonas-y@isy.liu.se>
Subject:  v21i030:  sipp - A 3D rendering library v2.1, Part05/08
Message-ID: <1991Jul23.181721.27867@sparky.IMD.Sterling.COM>
X-Md4-Signature: c4e97e17ae1eaba0071086ec8bae4365
Keywords: rendering graphics z-buffer
Sender: kent@sparky.IMD.Sterling.COM (Kent Landfield)
Organization: Dept of EE, University of Linkoping
References: <csm-v21i026=sipp.130352@sparky.imd.sterling.com>
Date: Tue, 23 Jul 1991 18:17:21 GMT
Approved: kent@sparky.imd.sterling.com
Lines: 1758

Submitted-by: Jonas Yngvesson <jonas-y@isy.liu.se>
Posting-number: Volume 21, Issue 30
Archive-name: sipp/part05
Supersedes: sipp2.0: Volume 16, Issue 5-10
Environment: UNIX

#!/bin/sh
# This is part 05 of sipp-2.1
# ============= libsipp/sipp.h ==============
if test ! -d 'libsipp'; then
    echo 'x - creating directory libsipp'
    mkdir 'libsipp'
fi
if test -f 'libsipp/sipp.h' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/sipp.h (File already exists)'
else
echo 'x - extracting libsipp/sipp.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp.h' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** sipp.h - Public inteface to the sipp rendering library.
X **/
X
X
#ifndef _SIPP_H
#define _SIPP_H
X
#include <geometric.h>
X
X
#ifndef M_PI
#define M_PI 3.1415926535897932384626
#endif
X
#ifndef FALSE
typedef int bool;
#define FALSE  0
#define TRUE   1
#endif 
X
/*
X * Customize for those that don't have memcpy() and friends, but
X * have bcopy() instead.
X */
X
#ifdef NOMEMCPY
#define memcpy(to, from, n) bcopy((from), (to), (n))
#endif
X
X
/*
X * The macro RANDOM() should return a random number
X * in the range [-1, 1].
X */
extern double drand48();
#define RANDOM()  (2.0 * drand48() - 1.0)
X
X
/*
X * Modes for rendering
X */
#define PHONG      0
#define GOURAUD    1
#define FLAT       2
#define LINE       3
X
X
/*
X * Interface to shader functions.
X */
typedef void Shader();
X
X
/*
X * Colors are handled as an rgb-triple
X * with values between 0 and 1.
X */
typedef struct {
X    double   red;
X    double   grn;
X    double   blu;
} Color;
X
X
/*
X * Structure storing the vertices in surfaces. The vertices for a
X * surface are stored in a binary tree sorted first on x, then y and last z.
X */
typedef struct vertex_t {
X    double            x, y, z;    /* vertex position */
X    double            a, b, c;    /* average normal at vertex */
X    double            u, v, w;    /* texture parameters (if any) */
X    struct vertex_t  *big, *sml;  /* pointers to children in the tree */
} Vertex;
X
X
/*
X * Structure to keep a list of vertex references.
X */
typedef struct vertex_ref_t {
X    Vertex       *vertex;
X    struct vertex_ref_t *next;
} Vertex_ref;
X
X
/*
X * Polygon definition. A polygon is defined by a list of
X * references to its vertices (counterclockwize order).
X */
typedef struct polygon_t {
X    Vertex_ref *vertices;     /* vertex list */
X    bool        backface;   /* polygon is backfacing (used at rendering) */
X    struct polygon_t  *next;
} Polygon;
X
X
/*
X * Surface definition. Each surface consists of a vertex tree, 
X * a polygon list, a pointer to a surface description and a pointer
X * to a shader function.
X */
typedef struct surface_t {
X    Vertex           *vertices;          /* vertex tree */
X    Polygon          *polygons;          /* polygon list */
X    void             *surface;           /* surface description */
X    Shader           *shader;            /* shader function */
X    int               ref_count;         /* no of references to this surface */
X    struct surface_t *next;              /* next surface in the list */
} Surface;
X
X
/*
X * Object definition. Object consists of one or more
X * surfaces and/or one or more subojects. Each object
X * has its own transformation matrix that affects itself
X * and all its subobjects.
X */
typedef struct object_t {
X    Surface         *surfaces;       /* List of surfaces */
X    struct object_t *sub_obj;        /* List of subobjects */
X    Transf_mat       transf;         /* Transformation matrix */
X    int              ref_count;      /* No of references to this object */
X    struct object_t *next;           /* Next object in this list */
} Object;
X
X
X
/*
X * Lightsource definition. 
X */
typedef struct lightsource {
X    double              intensity;  /* intensity, same for r, g, b */
X    Vector              dir;        /* direction from origo */  
X    struct lightsource *next;       /* next lightsource in the list */
} Lightsource;
X
X
/*
X * Surface description used by the basic shader. This shader
X * does simple shading of surfaces of a single color.
X */
typedef struct {
X    double  ambient;       /* Fraction of color visible in ambient light */
X    double  specular;      /* Fraction of colour specularly reflected */
X    double  c3;            /* "Shinyness" 0 = shiny,  1 = dull */
X    Color   color;         /* Colour of the surface */
} Surf_desc;
X
X
extern char  * SIPP_VERSION;
X
X
/*
X * This defines all public functions implemented in sipp.
X */
extern void          sipp_init();
extern void          sipp_show_backfaces();
extern void          vertex_push();
extern void          vertex_tx_push();
extern void          polygon_push();
extern Surface      *surface_create();
extern Surface      *surface_basic_create();
extern void          surface_set_shader();
extern void          surface_basic_shader();
extern Object       *object_create();
extern Object       *object_instance();
extern Object       *object_dup();
extern Object       *object_deep_dup();
extern void          object_delete();
extern void          object_add_surface();
extern void          object_sub_surface();
extern void          object_add_subobj();
extern void          object_sub_subobj();
extern void          object_install();
extern void          object_uninstall();
extern void          object_set_transf();
extern Transf_mat   *object_get_transf();
extern void          object_clear_transf();
extern void          object_transform();
extern void          object_rot_x();
extern void          object_rot_y();
extern void          object_rot_z();
extern void          object_rot();
extern void          object_scale();
extern void          object_move();
extern void          lightsource_push();
extern void          view_from();
extern void          view_at();
extern void          view_up();
extern void          view_focal();
extern void          viewpoint();
extern void          render_image_file();
extern void          render_image_pixmap();
extern void          basic_shader();
X
/*
X * Macros to ensure compatibility with older versions.
X */
#define render_image(xres, yres, image_file)\
X                render_image_file((xres), (yres), (image_file), PHONG, 2)
X
X
#endif /* _SIPP_H */
SHAR_EOF
chmod 0664 libsipp/sipp.h ||
echo 'restore of libsipp/sipp.h failed'
Wc_c="`wc -c < 'libsipp/sipp.h'`"
test 6406 -eq "$Wc_c" ||
	echo 'libsipp/sipp.h: original size 6406, current size' "$Wc_c"
fi
# ============= libsipp/sipp_bitmap.c ==============
if test -f 'libsipp/sipp_bitmap.c' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/sipp_bitmap.c (File already exists)'
else
echo 'x - extracting libsipp/sipp_bitmap.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp_bitmap.c' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** sipp_bitmap.c - A sample bitmap for use in SIPP and routines to use it.
X **/
X
#include <sys/types.h>
#include <stdio.h>
X
#include <sipp.h>
#include <sipp_bitmap.h>
#include <patchlevel.h>
X
/* ================================================================ */
X
X
/*
X * Return a pointer to a new Sipp_bitmap, with the size WIDTH x HEIGHT.
X * Enough space is allocated so each line ends on a byte boundary.
X */
Sipp_bitmap *
sipp_bitmap_create(width, height)
X    int   width;
X    int   height;
{
X    Sipp_bitmap  * bm;
X
X    bm = (Sipp_bitmap *) malloc(sizeof(Sipp_bitmap));
X    if (bm != NULL) {
X        bm->width = width;
X        bm->height = height;
X        bm->width_bytes = (width >> 3);
X        if ((width & 7) != 0) {
X            bm->width_bytes++;
X        } 
X        bm->buffer = (u_char *) calloc(bm->width_bytes * height, 
X                                       sizeof(u_char));
X    }
X
X    if (bm == NULL || bm->buffer == NULL) {
X        fprintf(stderr, "sipp_bitmap_create(): Out of virtual memory.\n");
X        exit(1);
X    }
X
X    return bm;
}
X
X
X
/*
X * Destruct a bitmap, and free allocated memory.
X */
void
sipp_bitmap_destruct(bm)
X    Sipp_bitmap  * bm;
{
X    if (bm != NULL) {
X        if (bm->buffer != NULL) {
X            free(bm->buffer);
X        }
X        free(bm);
X    }
}
X
X
X
X
/*
X * Draw a line in the Sipp_bitmap BM from (X1, Y1)
X * to (X2, Y2)
X */
X
#define PLOT(bm, width_bytes, yres, x, y) \
X        (bm)[(((yres) - 1) - (y)) * (width_bytes) + ((x) >> 3)]\
X         |= (1 << (7 - (x) & 7))
X
void
sipp_bitmap_line(bm, x1, y1, x2, y2)
X    Sipp_bitmap  * bm;
X    int            x1, y1;
X    int            x2, y2;
{
X    int   d;
X    int   x,  y;
X    int   ax, ay;
X    int   sx, sy;
X    int   dx, dy;
X
X    dx = x2 - x1;
X    ax = abs(dx) << 1;
X    sx = ((dx < 0) ? -1 : 1);
X    dy = y2 - y1;
X    ay = abs(dy) << 1;
X    sy = ((dy < 0) ? -1 : 1);
X
X    x = x1;
X    y = y1;
X    if (ax > ay) {
X        d = ay - (ax >> 1);
X        for (;;) {
X            PLOT(bm->buffer, bm->width_bytes, bm->height, x, y);
X            if (x == x2) {
X                return;
X            }
X            if (d >= 0) {
X                y += sy;
X                d -= ax;
X            }
X            x += sx;
X            d += ay;
X        }
X    } else {
X        d = ax - (ay >> 1);
X        for (;;) {
X            PLOT(bm->buffer, bm->width_bytes, bm->height, x, y);
X            if (y == y2) {
X                return;
X            }
X            if (d >= 0) {
X                x += sx;
X                d -=  ay;
X            }
X            y += sy;
X            d += ax;
X        }
X    }
}
X
X
X
/*
X * Write the Sipp_bitmap BM to the open file FILE.
X */
X
void
sipp_bitmap_write(file, bm)
X    FILE         * file;
X    Sipp_bitmap  * bm;
{
X    int    written;
X    int    wrote;
X    int    left;
X
X    fprintf(file,  "P4\n");
X    fprintf(file,  "#Image rendered with SIPP %s%s\n", 
X            SIPP_VERSION, PATCHLEVEL);
X    fprintf(file,  "%d\n%d\n", bm->width, bm->height);
X
X    wrote   = 0;
X    written = 0;
X    left    = bm->width_bytes * bm->height;
X    while ((wrote = fwrite(bm->buffer + written, 1, left, file)) != left) {
X        written += wrote;
X        left -= wrote;
X    }
}
SHAR_EOF
chmod 0664 libsipp/sipp_bitmap.c ||
echo 'restore of libsipp/sipp_bitmap.c failed'
Wc_c="`wc -c < 'libsipp/sipp_bitmap.c'`"
test 4002 -eq "$Wc_c" ||
	echo 'libsipp/sipp_bitmap.c: original size 4002, current size' "$Wc_c"
fi
# ============= libsipp/sipp_bitmap.h ==============
if test -f 'libsipp/sipp_bitmap.h' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/sipp_bitmap.h (File already exists)'
else
echo 'x - extracting libsipp/sipp_bitmap.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp_bitmap.h' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** sipp_bitmap.h - Interface to sipp_bitmap.c
X **/
X
#ifndef BITMAP_H
#define BITMAP_H
X
#include <sys/types.h>
X
/* The generic line drawer usable for any bitmap type. */
typedef void   (*Bitmap_line_func)();
X
X
/* The SIPP bitmap and its associated functions. */
X
typedef struct{
X    int       width;
X    int       height;
X    int       width_bytes;
X    u_char   *buffer;
} Sipp_bitmap;
X
X
extern Sipp_bitmap   *sipp_bitmap_create();
extern void           sipp_bitmap_destruct();
extern void           sipp_bitmap_line();
extern void           sipp_bitmap_write();
X
#endif /* BITMAP_H */
SHAR_EOF
chmod 0664 libsipp/sipp_bitmap.h ||
echo 'restore of libsipp/sipp_bitmap.h failed'
Wc_c="`wc -c < 'libsipp/sipp_bitmap.h'`"
test 1432 -eq "$Wc_c" ||
	echo 'libsipp/sipp_bitmap.h: original size 1432, current size' "$Wc_c"
fi
# ============= libsipp/sipp_pixmap.c ==============
if test -f 'libsipp/sipp_pixmap.c' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/sipp_pixmap.c (File already exists)'
else
echo 'x - extracting libsipp/sipp_pixmap.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp_pixmap.c' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** sipp_pixmap.c - A sample pixmap for use in SIPP and routines to use it.
X **/
X
#include <sys/types.h>
#include <stdio.h>
X
#include <sipp.h>
#include <sipp_pixmap.h>
#include <patchlevel.h>
X
/* ================================================================ */
X
X
/*
X * Return a pointer to a new Sipp_pixmap, with the size WIDTH x HEIGHT.
X */
X
Sipp_pixmap *
sipp_pixmap_create(width, height)
X    int   width;
X    int   height;
{
X    Sipp_pixmap  * pm;
X
X    pm = (Sipp_pixmap *) malloc(sizeof(Sipp_pixmap));
X    if (pm != NULL) {
X        pm->width = width;
X        pm->height = height;
X        pm->buffer = (u_char *) calloc(3 * width * height, sizeof(u_char));
X    }
X
X    if (pm == NULL || pm->buffer == NULL) {
X        fprintf(stderr, "sipp_pixmap_create(): Out of virtual memory.\n");
X        exit(1);
X    }
X
X    return pm;
}
X
X
X
/*
X * Destruct a pixmap, and free allocated memory.
X */
X
void
sipp_pixmap_destruct(pm)
X    Sipp_pixmap  * pm;
{
X    if (pm != NULL) {
X        if (pm->buffer != NULL) {
X            free(pm->buffer);
X        }
X        free(pm);
X    }
}
X
X
X
X
/*
X * Set the pixel at (X, Y) in Sipp_pixmap PM to the color
X * (RED, GRN, BLU).  
X */
X
void
sipp_pixmap_set_pixel(pm, x, y, red, grn, blu)
X    Sipp_pixmap  * pm;
X    int            x;
X    int            y;
X    u_char         red;
X    u_char         grn;
X    u_char         blu;
{
X    u_char  * cp;
X
X    if (x < 0 || y < 0 || x >= pm->width || y >= pm->height) {
X        fprintf(stderr, 
"Tried to write to pixel (%d, %d) in a Sipp_pixmap which was only %d x %d.\n",
X                x, y, pm->width, pm->height);
X    } else {
X        cp = pm->buffer + 3 * (y * pm->width + x);
X        *cp++ = red;
X        *cp++ = grn;
X        *cp = blu;
X    }
}
X
X
X
/*
X * Write the Sipp_pixmap PM to the open file FILE.
X */
X
void
sipp_pixmap_write(file, pm)
X    FILE         * file;
X    Sipp_pixmap  * pm;
{
X    u_char  * byte;
X    int       nbytes;
X    int       i;
X
X    fprintf(file,  "P6\n");
X    fprintf(file,  "#Image rendered with SIPP %s%s\n", 
X            SIPP_VERSION, PATCHLEVEL);
X    fprintf(file,  "%d\n%d\n255\n", pm->width, pm->height);
X
X    byte = pm->buffer;
X    nbytes = pm->width * pm->height;
X    for (i = 0; i < nbytes; ++i) {
X        putc(*byte++, file);
X        putc(*byte++, file);
X        putc(*byte++, file);
X    }
}
SHAR_EOF
chmod 0664 libsipp/sipp_pixmap.c ||
echo 'restore of libsipp/sipp_pixmap.c failed'
Wc_c="`wc -c < 'libsipp/sipp_pixmap.c'`"
test 3142 -eq "$Wc_c" ||
	echo 'libsipp/sipp_pixmap.c: original size 3142, current size' "$Wc_c"
fi
# ============= libsipp/sipp_pixmap.h ==============
if test -f 'libsipp/sipp_pixmap.h' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/sipp_pixmap.h (File already exists)'
else
echo 'x - extracting libsipp/sipp_pixmap.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/sipp_pixmap.h' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** sipp_pixmap.h - Interface to sipp_pixmap.c.
X **/
X
#ifndef SIPP_PIXMAP_H
#define SIPP_PIXMAP_H
X
X
#include <sys/types.h>
X
/* The generic pixel setter usable for any pixmap type. */
typedef void   (*Pixmap_set_pixel_func)();
X
X
/* The SIPP pixmap and its associated functions. */
X
typedef struct {
X    int       width;
X    int       height;
X    u_char  * buffer;
} Sipp_pixmap;
X
X
extern Sipp_pixmap  * sipp_pixmap_create();
extern void           sipp_pixmap_destruct();
extern void           sipp_pixmap_set_pixel();
extern void           sipp_pixmap_write();
X
X
#endif /* SIPP_PIXMAP_H */
SHAR_EOF
chmod 0664 libsipp/sipp_pixmap.h ||
echo 'restore of libsipp/sipp_pixmap.h failed'
Wc_c="`wc -c < 'libsipp/sipp_pixmap.h'`"
test 1435 -eq "$Wc_c" ||
	echo 'libsipp/sipp_pixmap.h: original size 1435, current size' "$Wc_c"
fi
# ============= libsipp/smalloc.c ==============
if test -f 'libsipp/smalloc.c' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/smalloc.c (File already exists)'
else
echo 'x - extracting libsipp/smalloc.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/smalloc.c' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** smalloc.c - "Safe" malloc and calloc.
X **/
X
#include <malloc.h>
#include <stdio.h>
X
X
char *
smalloc(size)
X    int size;
{
X    char *p;
X
X    p = (char *)malloc(size);
X    if (p == NULL) {
X        fprintf(stderr, "smalloc(): Out of memory.\n");
X        exit(1);
X    }
X
X    return p;
}
X
X
char *
scalloc(size, itemsize)
X    int size;
X    int itemsize;
{
X    char *p;
X
X    p = (char *)calloc(size, itemsize);
X    if (p == NULL) {
X        fprintf(stderr, "scalloc(): Out of memory.\n");
X        exit(1);
X    }
X
X    return p;
}
X
X
X
SHAR_EOF
chmod 0644 libsipp/smalloc.c ||
echo 'restore of libsipp/smalloc.c failed'
Wc_c="`wc -c < 'libsipp/smalloc.c'`"
test 1374 -eq "$Wc_c" ||
	echo 'libsipp/smalloc.c: original size 1374, current size' "$Wc_c"
fi
# ============= libsipp/smalloc.h ==============
if test -f 'libsipp/smalloc.h' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/smalloc.h (File already exists)'
else
echo 'x - extracting libsipp/smalloc.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/smalloc.h' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** smalloc.h - Interface to smalloc.c
X **/
X
#ifndef SMALLOC_H
#define SMALLOC_H
X
X
extern char   *smalloc();
extern char   *scalloc();
X
X
#endif /* SMALLOC_H */
SHAR_EOF
chmod 0644 libsipp/smalloc.h ||
echo 'restore of libsipp/smalloc.h failed'
Wc_c="`wc -c < 'libsipp/smalloc.h'`"
test 1006 -eq "$Wc_c" ||
	echo 'libsipp/smalloc.h: original size 1006, current size' "$Wc_c"
fi
# ============= libsipp/strauss.c ==============
if test -f 'libsipp/strauss.c' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/strauss.c (File already exists)'
else
echo 'x - extracting libsipp/strauss.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/strauss.c' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** strauss.c - Shading model designed by Paul S. Strauss at 
X **             Silicon Graphics Inc.
X **             Described in IEEE CG&A November 1990.
X **/
X
#include <math.h>
X
#include <sipp.h>
#include <shaders.h>
#include <geometric.h>
X
X
/*
X * Strauss describes, in his article, two functions that simulates
X * fresnel reflection and geometric attenuation. These functions
X * are dependent of an angle between 0 and pi/2 normalized to lie
X * between 0 and 1. He also mentions that he uses versions of the
X * functions that depend on the cosine of an angle instead (since 
X * that can be calculated with a dot product).
X * These versions are not described in the article so I have empirically 
X * tried to recreate them. I don't know if this bears any resemblence
X * to what he used, so any errors are my fault.
X *
X * What i did was only to change x in the functions to (1 - x) and modify
X * the Kf and Kg constants slightly. The original values in the article
X * was: Kf = 1.12, Kg = 1.02
X */
X
#define Kf   1.2   /* Factor used in approximation of fresnel reflection */
#define Kg   1.031 /* Factor used in approximation of geometric attenuation */
X
X
/*
X * Function to simulate fresnel reflection.
X */
#define FR(x) ((1.0/((1.0-(x)-Kf)*(1.0-(x)-Kf))-1.0/(Kf*Kf))\
X               /(1.0/((1.0-Kf)*(1.0-Kf))-1.0/(Kf*Kf)))
X
/*
X * Function to simulate geometric attenuation.
X */
#define GA(x) ((1.0/((1.0-Kg)*(1.0-Kg))-1.0/((1.0-(x)-Kg)*(1.0-(x)-Kg)))\
X               /(1.0/((1.0-Kg)*(1.0-Kg))-1.0/(Kg*Kg)))
X
X
X
void
strauss_shader(a, b, c, u, v, w, view_vec, lights, sd, color)
X    double        a, b, c, u, v, w;
X    Vector        view_vec;
X    Lightsource  *lights;
X    Strauss_desc *sd;
X    Color        *color;
{
X    Vector       normal;       /* Normalized surface normal */
X    Vector       highlight;    /* Highlight vector */
X    double       c_alpha;      /* cos(angle between normal and lightvector) */
X    double       c_beta;       /* cos(angle between highlight & view_vec) */
X    double       c_gamma;      /* cos(angle between normal & view_vec) */
X    double       rd;           /* Diffuse reflectivity */
X    double       rs;           /* Specular reflectivity */
X    double       rj;           /* Adjusted specular reflectivity */
X    double       rn;           /* Specular reflectivity at normal incidence */
X    double       h;            /* Shininess exponent */
X    Vector       qd;           /* Diffuse reflection factor */
X    Vector       qs;           /* Specular reflection factor */
X    Color        col;          /* Resulting color */
X    Lightsource *lp;
X
X    MakeVector(normal, a, b, c);
X    vecnorm(&normal);
X    c_gamma = VecDot(normal, view_vec);
X    col.red = col.grn = col.blu = 0.0;
X    rd = 1.0 - sd->smoothness * sd->smoothness * sd->smoothness;
X
X    for (lp = lights; lp != (Lightsource *)0; lp = lp->next) {
X
X        c_alpha = VecDot(normal, lp->dir);
X
X        if (c_alpha >= 0) {
X
X            VecScalMul(highlight, 2 * c_alpha, normal);
X            VecSub(highlight, highlight, lp->dir);
X            c_beta = VecDot(highlight, view_vec);
X            
X            MakeVector(qd, sd->color.red, sd->color.grn, sd->color.blu);
X            VecScalMul(qd, (c_alpha * rd 
X                            * (1.0 - sd->metalness * sd->smoothness)), qd);
X            
X            if (c_beta >= 0) {
X                
X                h = 3 / (1.0 - sd->smoothness);
X                rn = 1.0 - rd;
X                rj = rn + (rn + 0.1) * FR(c_alpha) * GA(c_alpha) * GA(c_gamma);
X                if (rj > 1.0) {
X                    rj = 1.0;
X                }
X                rs = exp(h * log(c_beta)) * rj;
X                
X                MakeVector(qs, 
X                           sd->color.red - 1.0, 
X                           sd->color.grn - 1.0, 
X                           sd->color.blu - 1.0);
X                VecScalMul(qs, sd->metalness * (1.0 - FR(c_alpha)), qs);
X                qs.x += 1.0;
X                qs.y += 1.0;
X                qs.z += 1.0;
X                
X                VecScalMul(qs, rs, qs);
X                VecAdd(qd, qd, qs);
X
X            }
X            
X            VecScalMul(qd, lp->intensity, qd);
X            
X            col.red += qd.x;
X            col.grn += qd.y;
X            col.blu += qd.z;
X
X        }
X    }
X
X    col.red += sd->ambient * rd * sd->color.red;
X    col.grn += sd->ambient * rd * sd->color.grn;
X    col.blu += sd->ambient * rd * sd->color.blu;
X
X    color->red = ((col.red > 1.0) ? 1.0 : col.red);
X    color->grn = ((col.grn > 1.0) ? 1.0 : col.grn);
X    color->blu = ((col.blu > 1.0) ? 1.0 : col.blu);
}
SHAR_EOF
chmod 0664 libsipp/strauss.c ||
echo 'restore of libsipp/strauss.c failed'
Wc_c="`wc -c < 'libsipp/strauss.c'`"
test 5426 -eq "$Wc_c" ||
	echo 'libsipp/strauss.c: original size 5426, current size' "$Wc_c"
fi
# ============= libsipp/torus.c ==============
if test -f 'libsipp/torus.c' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/torus.c (File already exists)'
else
echo 'x - extracting libsipp/torus.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/torus.c' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** torus.c - Creating a torus as a sipp object.
X **/
X
#include <xalloca.h>
#include <math.h>
X
#include <sipp.h>
X
X
static void
arr_rot(arr1, arr2, len, angle)
X    Vector arr1[];
X    Vector arr2[];
X    int    len;
X    double angle;
{
X    int    i;
X    double sa, ca;
X
X    sa = sin(angle);
X    ca = cos(angle);
X    for (i = 0; i < len; i++) {
X        arr2[i].x = arr1[i].x * ca - arr1[i].y * sa;
X        arr2[i].y = arr1[i].x * sa + arr1[i].y * ca;
X        arr2[i].z = arr1[i].z;
X    }
}
X
X
static void
push_band(arr1, arr2, len)
X    Vector arr1[];
X    Vector arr2[];
X    int    len;
{
X    int i, j;
X
X    for (i = 0; i < len; i++) {
X        j = (i + 1) % len;
X        vertex_tx_push(arr1[i].x, arr1[i].y, arr1[i].z, 
X                       arr1[i].x, arr1[i].y, arr1[i].z);
X        vertex_tx_push(arr2[i].x, arr2[i].y, arr2[i].z, 
X                       arr2[i].x, arr2[i].y, arr2[i].z);
X        vertex_tx_push(arr2[j].x, arr2[j].y, arr2[j].z, 
X                       arr2[j].x, arr2[j].y, arr2[j].z);
X        vertex_tx_push(arr1[j].x, arr1[j].y, arr1[j].z, 
X                       arr1[j].x, arr1[j].y, arr1[j].z);
X        polygon_push();
X    }
}
X
X
X
Object *
sipp_torus(bigradius, smallradius, res1, res2, surface, shader)
X    double  bigradius;      /* Radius of the ring */
X    double  smallradius;    /* Radius of the "tube" */
X    int     res1;           /* Number of polygons around the ring */
X    int     res2;           /* Number of polygons around the tube */
X    void   *surface;
X    Shader *shader;
{
X    Object *torus;
X    Vector *arr1;
X    Vector *arr2;
X    Vector *tmp;
X    int     i;
X
X    /* Create two arrays to hold vertices around the tube */
X    arr1 = (Vector *)alloca(res2 * sizeof(Vector));
X    arr2 = (Vector *)alloca(res2 * sizeof(Vector));
X
X    for (i = 0; i < res2; i++) {
X        arr1[i].x = bigradius + smallradius * cos(i * 2.0 * M_PI / res2);
X        arr1[i].y = 0.0;
X        arr1[i].z = smallradius * sin(i * 2.0 * M_PI / res2);
X    }
X
X    /* Sweep out the torus by rotating the two perimeters */
X    /* defined in arr1 and arr2. */
X    for (i = 0; i < res1; i++) {
X        arr_rot(arr1, arr2, res2, 2.0 * M_PI / (double)res1);
X        push_band(arr1, arr2, res2);
X        tmp = arr1;
X        arr1 = arr2;
X        arr2 = tmp;
X    }
X
X    torus = object_create();
X    object_add_surface(torus, surface_create(surface, shader));
X
X    return torus;
}
X
SHAR_EOF
chmod 0664 libsipp/torus.c ||
echo 'restore of libsipp/torus.c failed'
Wc_c="`wc -c < 'libsipp/torus.c'`"
test 3223 -eq "$Wc_c" ||
	echo 'libsipp/torus.c: original size 3223, current size' "$Wc_c"
fi
# ============= libsipp/transforms.c ==============
if test -f 'libsipp/transforms.c' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/transforms.c (File already exists)'
else
echo 'x - extracting libsipp/transforms.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/transforms.c' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/** 
X ** transforms.c - Functions that handles object transformations.
X **/
X
#include <stdio.h>
X
#include <geometric.h>
#include <sipp.h>
X
X
/*
X * Set the transformation matrix of OBJ to MATRIX.
X */
void
object_set_transf(obj, matrix)
X    Object     *obj;
X    Transf_mat *matrix;
{
X    MatCopy(&obj->transf, matrix);
}
X
X
/*
X * Retrieve the transformation matrix of OBJ
X */
Transf_mat *
object_get_transf(obj, matrix)
X    Object     *obj;
X    Transf_mat *matrix;
{
X    Transf_mat *tmp;
X
X    if (matrix != NULL) {
X        MatCopy(matrix, &obj->transf);
X        return matrix;
X    } else {
X        tmp = transf_mat_create(NULL);
X        MatCopy(tmp, &obj->transf);
X        return tmp;
X    }
}
X
X
/*
X * Set the transformation matrix of OBJ to the identity matrix.
X */
void
object_clear_transf(obj)
X    Object *obj;
{
X    MatCopy(&obj->transf, &ident_matrix);
}
X
X
/*
X * Post multiply MATRIX into the transformation matrix of OBJ.
X */
void
object_transform(obj, matrix)
X    Object     *obj;
X    Transf_mat *matrix;
{
X    mat_mul(&obj->transf, &obj->transf, matrix);
}
X
X
/*
X * Rotate the object OBJ ANG radians about the x-axis.
X */
void
object_rot_x(obj, ang)
X    Object *obj;
X    double  ang;
{
X    mat_rotate_x(&obj->transf, ang);
}
X
X
/*
X * Rotate the object OBJ ANG radians about the y-axis.
X */
void
object_rot_y(obj, ang)
X    Object *obj;
X    double  ang;
{
X    mat_rotate_y(&obj->transf, ang);
}
X
X
/*
X * Rotate the object OBJ ANG radians about the z-axis.
X */
void
object_rot_z(obj, ang)
X    Object *obj;
X    double  ang;
{
X    mat_rotate_z(&obj->transf, ang);
}
X
X
/*
X * Rotate the object OBJ ANG radians about the line defined
X * by POINT and VEC.
X */
void
object_rot(obj, point, vec, ang)
X    Object *obj;
X    Vector *point;
X    Vector *vec;
X    double  ang;
{
X    mat_rotate(&obj->transf, point, vec, ang);
}
X
X
/*
X * Scale the object OBJ with respect to the origin.
X */
void
object_scale(obj, xscale, yscale, zscale)
X    Object *obj;
X    double  xscale, yscale, zscale;
{
X    mat_scale(&obj->transf, xscale, yscale, zscale);
}
X
X
/*
X * Translate the object OBJ.
X */
void
object_move(obj, dx, dy, dz)
X    Object *obj;
X    double  dx, dy, dz;
{
X    mat_translate(&obj->transf, dx, dy, dz);
}
X
X
X
X
SHAR_EOF
chmod 0644 libsipp/transforms.c ||
echo 'restore of libsipp/transforms.c failed'
Wc_c="`wc -c < 'libsipp/transforms.c'`"
test 3036 -eq "$Wc_c" ||
	echo 'libsipp/transforms.c: original size 3036, current size' "$Wc_c"
fi
# ============= libsipp/viewpoint.c ==============
if test -f 'libsipp/viewpoint.c' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/viewpoint.c (File already exists)'
else
echo 'x - extracting libsipp/viewpoint.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/viewpoint.c' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** viewpoint.c - Functions that handles the viewpoint definition and
X **               calculation of viewing transformation.
X **/
X
#include <geometric.h>
#include <viewpoint.h>
X
X
Viewpoint camera;          /* Viewpoint of the scene  */
X
/*
X * Constants used in viewing transformation.
X */
double        hither;          /* Hither z-clipping plane */
double        yon;             /* Yonder z-clipping plane */
X
X
/*
X * Calculate the vector from the point of interest
X * to the viewpoint. The shaders needs this vector normalized
X * while the view coordinate transformation needs it
X * non normalized, therefore we need this routine to
X * recalculate the non normalized value.
X */
void
view_vec_eval()
{
X    MakeVector(camera.vec, 
X               camera.x0 - camera.x, 
X               camera.y0 - camera.y, 
X               camera.z0 - camera.z);
}
X
X
X    
/*
X * Define the viewpoint.
X */
void
view_from(x0, y0, z0)
X    double x0, y0, z0;
{
X    camera.x0 = x0;
X    camera.y0 = y0;
X    camera.z0 = z0;
X    view_vec_eval();
}
X
X
/*
X * Define the point that we are looking at.
X */
void
view_at(x, y, z)
X    double x, y, z;
{
X    camera.x = x;
X    camera.y = y;
X    camera.z = z;
X    view_vec_eval();
}
X
X
/*
X * Define the "up" direction of the view (well, rather the y-z plane).
X */
void
view_up(x, y, z)
X    double x, y, z;
{
X    MakeVector(camera.up, x, y, z);
}
X
X
/*
X * Set the focal ratio for the "camera".
X */
void
view_focal(ratio)
X    double ratio;
{
X    camera.focal_ratio = ratio;
}
X
X
/*
X * Set all viewpoint parameters in one call.
X */
void
viewpoint(x0, y0, z0, x, y, z, ux, uy, uz, ratio)
X    double x0, y0, z0, x, y, z, ux, uy, uz, ratio;
{
X    camera.x0 = x0;
X    camera.y0 = y0;
X    camera.z0 = z0;
X    camera.x = x;
X    camera.y = y;
X    camera.z = z;
X    MakeVector(camera.up, ux, uy, uz);
X    camera.focal_ratio = ratio;
X    view_vec_eval();
}
X
X
X
/*
X * Build a transformation matrix for transformation
X * into view coordinates.
X */
void
get_view_transf(view_mat)
X    Transf_mat *view_mat;
{
X    Vector tmp;
X    double transl[3];
X    double vy, vz;
X    int i, j;
X    
X    /*
X     * First we need a translation so the origo
X     * of the view coordinate system is placed
X     * in the viewpoint.
X     */
X    transl[0] = -camera.x0;
X    transl[1] = -camera.y0;
X    transl[2] = -camera.z0;
X
X    /*
X     * Then we need a rotation that makes the
X     * up-vector point up, and alignes the sightline
X     * with the z-axis.
X     * This code might seem magic but the algebra behind
X     * it can be found in Jim Blinn's Corner in IEEE CG&A July 1988
X     */
X    VecCopy(tmp, camera.vec);
X    VecNegate(tmp);
X    vecnorm(&tmp);
X    vecnorm(&camera.up);
X    vz = VecDot(tmp, camera.up);
X    if ((vz * vz) > 1.0) {        /* this should not happen, but... */
X        vz = 1.0;
X    } else {
X        vy = sqrt(1.0 - vz * vz);
X        if (vy == 0.0) {          /* oops, the world collapses... */
X            vy = 1.0e10;
X            vz = 1.0;
X        } else {
X            vy = 1.0 / vy;
X        }
X    }
X
X    view_mat->mat[0][2] = tmp.x;
X    view_mat->mat[1][2] = tmp.y;
X    view_mat->mat[2][2] = tmp.z;
X
X    VecScalMul(tmp, vz, tmp);
X    VecSub(tmp, camera.up, tmp);
X    view_mat->mat[0][1] = tmp.x * vy;
X    view_mat->mat[1][1] = tmp.y * vy;
X    view_mat->mat[2][1] = tmp.z * vy;
X
X    view_mat->mat[0][0] = (view_mat->mat[1][1] * view_mat->mat[2][2] 
X                           - view_mat->mat[1][2] * view_mat->mat[2][1]);
X    view_mat->mat[1][0] = (view_mat->mat[2][1] * view_mat->mat[0][2] 
X                           - view_mat->mat[2][2] * view_mat->mat[0][1]);
X    view_mat->mat[2][0] = (view_mat->mat[0][1] * view_mat->mat[1][2] 
X                           - view_mat->mat[0][2] * view_mat->mat[1][1]);
X
X    /*
X     * Install the translation into the matrix.
X     * Note that it is PRE-multiplied into the matrix.
X     */
X    for (i = 0; i < 3; i++) {
X        view_mat->mat[3][i] = 0.0;
X        for (j = 0; j < 3; j++) {
X            view_mat->mat[3][i] += transl[j] * view_mat->mat[j][i];
X        }
X    }
X
X    /*
X     * Since the screen coordinates are defined in a left handed
X     * coordinate system, we must switch the sign of the first
X     * column in the matrix to get appropriate signs of x-values.
X     */
X    view_mat->mat[0][0] = -view_mat->mat[0][0];
X    view_mat->mat[1][0] = -view_mat->mat[1][0];
X    view_mat->mat[2][0] = -view_mat->mat[2][0];
X    view_mat->mat[3][0] = -view_mat->mat[3][0];
X
X     
X    /*
X     * Define the perspective transformation
X     * using heuristic values for hither and yon.
X     */
X    hither = VecLen(camera.vec) / ZCLIPF;
X    yon    = VecLen(camera.vec) * ZCLIPF;
}
SHAR_EOF
chmod 0644 libsipp/viewpoint.c ||
echo 'restore of libsipp/viewpoint.c failed'
Wc_c="`wc -c < 'libsipp/viewpoint.c'`"
test 5462 -eq "$Wc_c" ||
	echo 'libsipp/viewpoint.c: original size 5462, current size' "$Wc_c"
fi
# ============= libsipp/viewpoint.h ==============
if test -f 'libsipp/viewpoint.h' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/viewpoint.h (File already exists)'
else
echo 'x - extracting libsipp/viewpoint.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/viewpoint.h' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** viewpoint.h - Types and interface to viewpoint.c
X **/
X
#ifndef VIEWPOINT_H
#define VIEWPOINT_H
X
#include <geometric.h>
X
#define ZCLIPF 100.0        /* Magic number used when defining hither & yon */
X
X
/*
X * Viewpoint definition
X */
typedef struct {
X    double x0, y0, z0;    /* viewpoint position */
X    double x, y, z;       /* point to look at */
X    Vector vec;           /* vector from point to eye, used in shading calc. */
X    Vector up;            /* Up direction in the view */ 
X    double focal_ratio;
} Viewpoint;
X
X
extern Viewpoint camera;          /* Viewpoint of the scene  */
extern double    hither;          /* Hither z-clipping plane */
extern double    yon;             /* Yonder z-clipping plane */
X
extern void      view_vec_eval();
extern void      get_view_transf();
X
X
#endif /* VIEWPOINT_H */
SHAR_EOF
chmod 0644 libsipp/viewpoint.h ||
echo 'restore of libsipp/viewpoint.h failed'
Wc_c="`wc -c < 'libsipp/viewpoint.h'`"
test 1666 -eq "$Wc_c" ||
	echo 'libsipp/viewpoint.h: original size 1666, current size' "$Wc_c"
fi
# ============= libsipp/wood.c ==============
if test -f 'libsipp/wood.c' -a X"$1" != X"-c"; then
	echo 'x - skipping libsipp/wood.c (File already exists)'
else
echo 'x - extracting libsipp/wood.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'libsipp/wood.c' &&
/**
X ** sipp - SImple Polygon Processor
X **
X **  A general 3d graphic package
X **
X **  Copyright Jonas Yngvesson  (jonas-y@isy.liu.se) 1988/89/90/91
X **            Inge Wallin      (ingwa@isy.liu.se)         1990/91
X **
X ** This program is free software; you can redistribute it and/or modify
X ** it under the terms of the GNU General Public License as published by
X ** the Free Software Foundation; either version 1, or any later version.
X ** This program is distributed in the hope that it will be useful,
X ** but WITHOUT ANY WARRANTY; without even the implied warranty of
X ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X ** GNU General Public License for more details.
X ** You can receive a copy of the GNU General Public License from the
X ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X **/
X
/**
X ** wood.c - Wood shader: Simulates wood using multiple skewed "cylinders"
X **          distorted with noise and turbulence.
X **/
X
#include <math.h>
#include <stdio.h>
X
#include <sipp.h>
#include <noise.h>
#include <shaders.h>
X
X
#define BOARDSIZE  45.0    /* "Log" or "board" size in texture coordinates */
X
extern bool noise_ready;
X
void
wood_shader(nx, ny, nz, u, v, w, view_vec, lights, wd, color)
X    double  nx, ny, nz, u, v, w;
X    Vector  view_vec;
X    Lightsource *lights;
X    Wood_desc *wd;
X    Color *color;
{
X    Vector    pos;
X    Vector    tmp;
X    Surf_desc surface;
X    double chaos;
X    double val;
X    double val2;
X    double skewoff;
X    double t;
X    double rad;
X
X    if (!noise_ready) {
X        noise_init();
X    }
X
X    /*
X     * Scale the texture coordinates.
X     */
X    pos.x = u * wd->scale;
X    pos.y = v * wd->scale;
X    pos.z = w * wd->scale;
X
X    /*
X     * Get some noise values. Drag out the texture in
X     * the direction of the "stem" of the fictive tree, the
X     * pattern should vary less along that direction.
X     */
X    pos.x *= 0.08;
X    chaos = turbulence(&pos, 5) * 0.5;
X    val2 = noise(&pos);
X
X    /*
X     * Make the pattern "semi"-periodic so it looks as if
X     * a new board is used at regular intervals
X     */
X    if (pos.z > 0.0) {
X        tmp.z = floor((pos.z + BOARDSIZE * 0.5) / BOARDSIZE);
X        pos.z -= tmp.z * BOARDSIZE;
X    } else {
X        tmp.z = floor((BOARDSIZE * 0.5 - pos.z) / BOARDSIZE);
X        pos.z += tmp.z * BOARDSIZE;
X    }
X    if (pos.y > 0.0) {
X        tmp.y = floor((pos.y + BOARDSIZE * 0.5) / BOARDSIZE);
X        pos.y -= tmp.y * BOARDSIZE;
X    } else {
X        tmp.y = floor((BOARDSIZE * 0.5 - pos.y) / BOARDSIZE);
X        pos.y += tmp.y * BOARDSIZE;
X    }
X
X    /* 
X     * Skew the "stem" a bit so the "cylinders" isn't perfectly
X     * symmertic about the x-axis. Skew the different "logs"
X     * slightly differently.
X     */
X    tmp.x = 0.0;
X    skewoff = noise(&tmp);
X    pos.z -= (0.05 + 0.03 * skewoff) * (u * wd->scale - 2.0);
X    pos.y -= (0.05 + 0.03 * skewoff) * (u * wd->scale - 2.0);
X
X    /*
X     * Calculate the distance from the middle of the "stem" and
X     * distort this distance with the turbulence value.
X     */
X    rad = sqrt(pos.y * pos.y + pos.z * pos.z);
X    rad += chaos;
X    val = rad - floor(rad);
X
X    /*
X     * Choose a color dependent on the distorted distance.
X     */
X    if (val < 0.1) {
X        surface.color.red = wd->base.red;
X        surface.color.grn = wd->base.grn;
X        surface.color.blu = wd->base.blu;
X    } else if (val < 0.9) {
X        t = 1.0 - pow(val / 0.8 - 0.1, 6.0);
X        surface.color.red = wd->ring.red + t * (wd->base.red 
X                                                  - wd->ring.red);
X        surface.color.grn = wd->ring.grn + t * (wd->base.grn 
X                                                  - wd->ring.grn);
X        surface.color.blu = wd->ring.blu + t * (wd->base.blu 
X                                                  - wd->ring.blu);
X    } else {
X        surface.color.red = wd->ring.red;
X        surface.color.grn = wd->ring.grn;
X        surface.color.blu = wd->ring.blu;
X    }
X
X    /*
X     * Add a little extra "noise" so the pattern doesn't get
X     * too regular, this could be small cracks, or other anomalies
X     * in the wood.
X     */
X    if (val2 < 0.01 && val2 > 0.0) {
X        surface.color.red = wd->ring.red;
X        surface.color.grn = wd->ring.grn;
X        surface.color.blu = wd->ring.blu;
X    }
X
X
X    surface.ambient  = wd->ambient;
X    surface.specular = wd->specular;
X    surface.c3       = wd->c3;
X    basic_shader(nx, ny, nz, u, v, w, view_vec, lights, &surface, color);
}
SHAR_EOF
chmod 0644 libsipp/wood.c ||
echo 'restore of libsipp/wood.c failed'
Wc_c="`wc -c < 'libsipp/wood.c'`"
test 4490 -eq "$Wc_c" ||
	echo 'libsipp/wood.c: original size 4490, current size' "$Wc_c"
fi
true || echo 'restore of libsipp/xalloca.c failed'
echo End of part 5, continue with part 6
exit 0

-- 
------------------------------------------------------------------------------
 J o n a s   Y n g v e s s o n
Dept. of Electrical Engineering	                         jonas-y@isy.liu.se
University of Linkoping, Sweden                   ...!uunet!isy.liu.se!jonas-y

exit 0 # Just in case...


