//********************************************************************
//                        PYRAMID.CPP
// Build and display a polygon mesh model of a 4-sided pyramid.
// Program skeleton.
//******************************************************************** 

//***********************DATA STRUCTURES******************************
struct point3d {float x; float y; float z;};  /* a 3d point */
struct polygon {int n; int *inds;}; /* n = number of polygon vertices;
                                       inds is an array of indices
                                       into the points arrays to
                                       specify which vertices form
                                       the polygon */
struct point3d w_pts[5];    /* 5 pyramid vertices in world coords */
struct point3d v_pts[5];    /* 5 pyramid vertices in viewing coords */
POINT s_pts[5];             /* 5 pyramid vertices in screen coords */
struct polygon polys[5];    /* 5 polygons defining the pyramid */

/* global variables: */

float v11,v12,v21,v22,v23,v31,v32,v33,v43; /* viewing xform matrix elts.*/
int screen_dist;         /* Viewing parameters */
float rho, theta, phi;
int xmax,ymax;           /* Screen dimensions */
int num_vertices=5, num_polygons=5;

//********************************************************************
// Function prototypes
//********************************************************************

void coeff(float r, float t, float p);  /*calculates xform matrix elts.*/
void convert(float x, float y, float z,
             float *xv, float *yv, float *zv,
             int *xs, int *ys);           /* converts a 3D world coord pt
                                                     to 3D viewing coordinates
                                                     and 2D screen coordinates */
void build_pyramid(void);  /* sets up pyramid points and polygons arrays */
void draw_polygon(int f);  /* draws a polygon with index f ... */
                                 /* if hide_flag is 0 or polygon is visible */
float visible(int p);      /* returns a negative # if polygon p is visible */

//********************************************************************
//      Main Function--Called when pyramid is to be displayed
//      hide_flag determines if back-face culling is to be done
//********************************************************************
void doit(int hide_flag)
{
/* Set the values of rho, theta, phi, and screen_dist here*/
build_pyramid();       /* build the polygon model of the pyramid */ 
coeff(rho,theta,phi);  /* calculate transformation matrix elements */
for (int i=0; i<num_vertices; i++)
   {
   /* Loop to convert polygon vertices from world coordinates
      to viewing and screen coordinates; must call convert()   */ 
   }
for (int f=0; f<num_polygons; f++)  /* draw each polygon, if visible */
   { 
    /* Loop to draw each polygon if hide_flag is off or if the polygon
     is visible; must call visible() and draw_polygon() */
   }
}

//********************************************************************
// Function Definitions
//********************************************************************

void coeff(float r, float t, float p)
{
// Code to calculate the non-zero viewing transformation matrix elements:
   v11,v12,v21,v22,v23,v31,v32,v33,v34,v44
}

void convert(float x, float y, float z,
             float *xv, float *yv, float *zv,
             int *xs, int *ys)
{
// Code to calculate the viewing coordinates and screen coordinates of
   a point from its 3-D world coordinates. Must implement the viewing,
   perspective, and window transformations described in class. 

void build_pyramid(void)
{
// Code to define the pyramid by setting up the w_pts and polys arrays.
}

float visible(int f)  /* returns a negative # if polygon f is visible*/
{
// Code to calculate and return the value of the visibility determinant
   of polygon f. Negative means invisible, positive visible.
}

void draw_polygon(int f) /* draws polygon # f */
{
// Code to draw polygon # f by obtaining its vertex numbers from the polys
array, getting the screen coordinates of the vertices from the s_pts
array, and making appropriate calls to the system polygon-drawing
primitive.
}