/***************************************************************************** Utilities for polygon objects for CS480 Programming Assignment 3 ****************************************************************************** Author: Stan Sclaroff Boston University Computer Science Dept. March 20, 1995 ****************************************************************************** Description: This file contains utilities for building, freeing, polygon objects. It includes a routine for reading in a simple OSU ascii file containing one polygonal object. The result is loaded into our polygon object data structure. ******************************************************************************/ #include #include #include #include "draw.h" /* this is the maximum number of vertices we allow one polygon to have */ #define MAX_NUM_POLY_VERTS 8 /* read in an Ohio State U. polygon object from a file */ /* the object consists of a list of vertices, and then a connectivity table */ Polygon *read_osu_polygons(fname,color) char *fname; /* ascii file name */ short *color; /* RGB color for polygon object */ { int i,j,k,n,n_poly_points,n_polys; Polygon *poly; float *poly_points,*point; int *poly_table,*table; FILE *f; /* open the OSU text file */ f = fopen(fname,"r"); if(f==0) { fprintf(stderr,"Error reading OSU ascii file: cannot find file\n"); return(0); } /* first record is the number of vertices and number of polygons */ fscanf(f,"%d %d\n",&n_poly_points, &n_polys); /* allocate the points array */ poly_points = (float *)malloc(sizeof(float)*n_poly_points*3); for(point=poly_points,i=0;i maxv[j]) maxv[j] = *p; else if(*p < minv[j]) minv[j] = *p; } } /* create a polygon object, given 3-D vertices, polygon connectivity table, and desired color */ Polygon *create_polygon_object(points,num_points,poly_table,num_polys,rgb_color) float *points; /* the polygon object's 3-D vertices */ int num_points; /* number of 3-D points */ int *poly_table,num_polys; /* polygon connectivity table and number of polygons */ short *rgb_color; /* desired color */ { Polygon *poly; int i,j,k; float minv[3],maxv[3]; /* allocate the memory */ poly = (Polygon *)malloc(sizeof(Polygon)); /* fill in the data structure */ /* use the points and table sent in by the caller */ poly->n_points = num_points; poly->points = points; poly->polygon_table = poly_table; poly->n_polygons = num_polys; /* computer axis-aligned min/max */ points_minmax(points,num_points,minv,maxv,3); /* fill in the rest */ for(i=0;i<3;++i) { poly->color[i] = rgb_color[i]; poly->center[i] = (maxv[i] + minv[i])*0.5; poly->max[i] = maxv[i]; poly->min[i] = minv[i]; } /* return result */ return(poly); } /* free a polygon structure */ void free_polygon_object(poly) Polygon *poly; { if(poly!=0) { if(poly->points!=0) free(poly->points); if(poly->polygon_table!=0) free(poly->polygon_table); free(poly); } } /* this routine perspective projects the polygons and passes them through the */ /* 3-D view-volume clipper */ perspective_project_polygons(winid,M,poly,xmin,xmax,ymin,ymax,zmin,zmax,rgb_color) float xmin,xmax,ymin,ymax; /* the viewport corners */ float zmin,zmax; /* the z clip planes */ float M[4][4]; /* the view-normalized perspective projection matrix */ Polygon *poly; /* a polygon object */ long winid; /* id of window to draw to */ short *rgb_color; /* the desired color */ { int i,j,*table,n; float *transformed_points,*p,*tp,h[4],res[4],*p1,*p2; /* allocate a temporary point array for perspective transformed vertices */ transformed_points = (float *)malloc(sizeof(float)*poly->n_points*3); /* transform all the polygon vertices */ h[3] = 1.0; p = poly->points; tp = transformed_points; for(i=0;in_points;++i) { /* perspective transform homogeneous in coordinates */ for(j=0;j<3;++j) h[j] = p[j]; MATRIX_MULT_VECTOR_4x4(res,M,h); /* homogonize x,y */ tp[0] = res[0]/res[3]; tp[1] = res[1]/res[3]; tp[2] = res[2]; p+=3; tp+=3; } /* now loop through polygons, clipping line segements as we go */ table = poly->polygon_table; for(i=0;in_polygons;++i) { n = *table++; for(j=0;j