home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
p
/
ply15dat.zip
/
TETRA.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-19
|
4KB
|
137 lines
/*
* coil.c - Create a bunch of springs
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifdef MAC
#include <console.h>
#endif
#include "def.h"
#include "lib.h"
#define SIZE_FACTOR 5
static MATRIX trans;
/* Create tetrahedrons recursively */
static void
create_tetra(int depth, COORD4 *center)
{
long num_face, num_vert ;
COORD4 face_pt[3], obj_pt[4], sub_center, overt ;
long swap, vert_ord[3] ;
long x_dir, y_dir, z_dir ;
if ( depth <= 1 ) {
/* Output tetrahedron */
/* find opposite corners of a cube which form a tetrahedron */
for ( num_vert = 0, x_dir = -1 ; x_dir <= 1 ; x_dir += 2 ) {
for ( y_dir = -1 ; y_dir <= 1 ; y_dir += 2 ) {
for ( z_dir = -1 ; z_dir <= 1 ; z_dir += 2 ) {
if ( x_dir*y_dir*z_dir == 1 ) {
obj_pt[num_vert].x =
center->x + (double)x_dir * center->w ;
obj_pt[num_vert].y =
center->y + (double)y_dir * center->w ;
obj_pt[num_vert].z =
center->z + (double)z_dir * center->w ;
++num_vert ;
}
}
}
}
/* find faces and output */
for ( num_face = 0 ; num_face < 4 ; ++num_face ) {
/* output order:
* face 0: points 0 1 2
* face 1: points 3 2 1
* face 2: points 2 3 0
* face 3: points 1 0 3
*/
for ( num_vert = 0 ; num_vert < 3 ; ++num_vert ) {
vert_ord[num_vert] = (num_face + num_vert) % 4 ;
}
if ( num_face%2 == 1 ) {
swap = vert_ord[0] ;
vert_ord[0] = vert_ord[2] ;
vert_ord[2] = swap ;
}
for ( num_vert = 0 ; num_vert < 3 ; ++num_vert ) {
lib_transform_coord(&overt, &obj_pt[vert_ord[num_vert]], trans);
COPY_COORD( face_pt[num_vert], overt) ;
}
lib_output_polygon(3, face_pt);
}
}
else {
/* Create sub-tetrahedra */
/* find opposite corners of a cube to form sub-tetrahedra */
for ( x_dir = -1 ; x_dir <= 1 ; x_dir += 2 ) {
for ( y_dir = -1 ; y_dir <= 1 ; y_dir += 2 ) {
for ( z_dir = -1 ; z_dir <= 1 ; z_dir += 2 ) {
if ( x_dir*y_dir*z_dir == 1 ) {
sub_center.x =
center->x + (double)x_dir * center->w / 2.0 ;
sub_center.y =
center->y + (double)y_dir * center->w / 2.0 ;
sub_center.z =
center->z + (double)z_dir * center->w / 2.0 ;
sub_center.w = center->w / 2.0 ;
create_tetra( depth-1, &sub_center) ;
}
}
}
}
}
}
void
main(int argc, char *argv[])
{
COORD4 back_color, tetra_color ;
COORD4 center_pt, light ;
COORD4 from, at, up, dir;
MATRIX m1, m2;
/* We are using Polyray */
lib_set_raytracer(OUTPUT_POLYRAY);
lib_create_rotate_matrix(m1, Y_AXIS, PI/4.0);
lib_create_rotate_matrix(m2, Z_AXIS, 0.9553166181); /* acos(1/sqrt(3)) */
lib_matrix_multiply(trans, m1, m2);
/* output viewpoint */
SET_COORD(from, 3.0, 0.0,-8.0) ;
SET_COORD(at, 0.0, 0.0, 0.0) ;
SET_COORD(up, 0.0, 1.0, 0.0) ;
lib_output_viewpoint( &from, &at, &up, 30.0, 1.0, 1.0, 256, 256);
/* output background color - dark blue */
SET_COORD( back_color, 0.039, 0.18, 0.376 ) ;
lib_output_background_color( &back_color ) ;
/* output light source */
SET_COORD4( light,-10.0, 20.0,-20.0, 0.7) ;
lib_output_light( &light ) ;
SET_COORD4( light, 10.0, 20.0, 20.0, 0.7) ;
lib_output_light( &light ) ;
/* output tetrahedron color - red */
SET_COORD( tetra_color, 1.0, 0.2, 0.2 ) ;
lib_output_color(&tetra_color, 0.2, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0) ;
/* compute and output tetrahedral object */
SET_COORD4( center_pt, 0.0, 0.0, 0.0, 1.0 ) ;
create_tetra( SIZE_FACTOR, ¢er_pt) ;
}