/* vogel.c:  graphics functions to draw scenes.  Written by Peng Zhang      
             Oct. 19, 1995                                               */

/*----------------------------- Local Includes -----------------------------*/
 
#include "polytope.h"
 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <collision.h>
#include <vogl.h>
#include <vodevice.h>

 
 
 
/*----------------------------- Local Constants -----------------------------*/
 

/*------------------------------ Local Globals ------------------------------*/
 
extern __col_Polyhedron      __col_polyhedronLibrary[1000];
extern __col_State  state;

 
/*---------------------------------Functions-------------------------------- */

void vogl_draw_scene(Status *status, float size, int wire);
void draw_poly(__col_Polyhedron *p, int wire);
void openwindow();



/*****************************************************************************\
draw_scene()
-----------------------------------------------------------------------------
description : Draw out the scene for the simulation
input       : status, polytope list
output      :
\*****************************************************************************/

void vogl_draw_scene(Status *status, float size,int wire)
{
    int i;
   printf("inside vogl_draw_scene, num_poly is: %d\n", status->num_polytopes);
          printf("inside draw_scene1,wire : %d\n", wire);
          printf("inside draw_scene1,size : %f\n", size);

/*  for(i=0; i<status->num_polytopes;i++)
     dumpPolyhedron(state.polytopes[i].polytope);    */     

       pushmatrix();
   scale(size,size,size);
   for(i=0; i < status->num_polytopes; i++)  {
          printf("inside draw_scene,wire : %d\n", wire);
       draw_poly(state.polytopes[i].polytope, wire); 
        swapbuffers();
   }
        popmatrix();
}
 
/*****************************************************************************\
@ draw_poly()
-----------------------------------------------------------------------------
description:  Draw the picture of a polyhedron by passing its data structure.
input      :  a polyhedron
output     :
\*****************************************************************************/
void draw_poly(__col_Polyhedron *p, int wire)
{
   col_Fnode *fn, *fn2;
   int clor = 1;
 
 
          printf("inside draw_poly, wire : %d\n", wire);
   for(fn = p->faces; fn; fn = fn->next) {
     if(wire) bgnclosedline();  else bgnpolygon();
       clor = clor % 5 ;
       color(clor+1);
       clor++;
         for(fn2 = fn->f.f->verts; fn2; fn2 = fn2->next) {
             __col_xformPoint(p->pose, fn2->f.v->coords, fn2->f.v->xcoords);
             v3d(fn2->f.v->xcoords);
            }
     if(wire) endclosedline(); else endpolygon();
   }
 
}


/*****************************************************************************\
@ openwindow()
-----------------------------------------------------------------------------
description : Do the necessary initiation for vogl.
input       :
output      :
\*****************************************************************************/
void openwindow()
/* Opens my own window */
{
  prefsize(500L, 500L);
  winopen("Cube-Master");
  doublebuffer();
  gconfig();
  perspective(500,1.0,1.0,40.0);
  lookat(0.0,0.0,35.0,0.0,0.0,0.0,3600);
  backface(TRUE);
  qdevice(KEYBD);
  unqdevice(INPUTCHANGE);
}
 

