#include "structs.h"
#include <osbind.h>
#include <vdibind.h>


int centerx,centery;
double scale;


/* just does object. only generates view coords viz or not */
void makeview(object,ox,oy,oz,theta,phi,rho)
int object;
long ox,oy,oz;
int theta,phi,rho;
{
Point3 *rel,*vlist,*view;
long cx,cy,cz;
int i;
int np;
TOBJECT *op;
Point3 rp,wp;
int t,p,r;

op=objectlist[object];

np=op->nvert;
cx=op->center.x;
cy=op->center.y;
cz=op->center.z;
view=op->viewlist;

vlist=op->vertlist;
t=op->theta;
p=op->phi;
r=op->rho;


for (i=0;i<np;i++)
     {
     rel=vlist+i;
     rp.x=rel->x;
     rp.y=rel->y;
        rp.z=rel->z;
	wp=rp;
        rp=rotxz(wp,360-t);
        wp=rotxy(rp,360-r);
        rp=rotyz(wp,p);

     wp.x=cx+rp.x;
     wp.y=cy+rp.y;
     wp.z=cz+rp.z;
     rp=docalc(wp,ox,oy,oz,theta,phi,rho);
     rel=view+i;
     rel->x=rp.x;
     rel->y=rp.y;

     if (rp.z>-10) rp.z=-10;

     rel->z=rp.z;
     }

}




     
void makepic(object)
int object;
{
Point3 *vlist,*curv;
Point2 *plist,*curp;
Point2 picp;
Point3 vp;
int np,i;

np=objectlist[object]->nvert;
plist=objectlist[object]->piclist;
vlist=objectlist[object]->viewlist;


for (i=0;i<np;i++)
        {
        curv=vlist+i;
        curp=plist+i;
        vp.x=curv->x;
        vp.y=curv->y;
        vp.z=curv->z;
        picp=topic(vp);
        curp->x=picp.x;
        curp->y=picp.y;
        }

}


/* world point to view point */
Point3 docalc(p,ox,oy,oz,theta,phi,rho)    /* takes a point and converts it based on view angles */
Point3 p;
long ox,oy,oz;
int theta,phi,rho;
{
Point3 res;
Point3 t;
Point3 des;

/* set up system to rotate around observer */
t.x=(p.x)-ox;
t.y=(p.y)-oy;
t.z=(p.z)-oz;

if (theta) des=rotxz(t,theta);
        else des=t;
if (rho) res=rotxy(des,rho);
        else res=des;
if (phi) des=rotyz(res,phi);
        else des=res;

des.z=-des.z; 

return (des);
}



void vizpolys()
{
POLY *pp;
int i;

for (i=0;i<50;i++)
        {
        if (objectlist[ primlist[i].owner ]->viz==0)
                primlist[i].viz=0;        
         else
        if (visible(&primlist[i])) primlist[i].viz=1;
           else primlist[i].viz=0;
        }

}



visible(q)
PRIMIT *q;
{
Point3 P,Q,S,R;
long c;
int ob;
TOBJECT *op;
Point3 *vl;
Point3 *vp,*vp2;
int i;
int ch=0;
int np;
Point3 p3;

op=objectlist[q->owner];
vl=op->viewlist;
np=q->np;




/* find surface normal */
/* requires  polygon points to be ordered clockwise */
/* while looking straight on to the visible side of the polygon */
vp=vl+q->pts[0];
vp2=vl+q->pts[1];

P.x=vp2->x - vp->x;
P.y=vp2->y - vp->y;
P.z=vp2->z - vp->z;

vp2=vl+q->pts[2];

Q.x=vp2->x - vp->x;
Q.y=vp2->y - vp->y;
Q.z=vp2->z - vp->z;

R.x=P.y*Q.z-P.z*Q.y;
R.y=P.z*Q.x-P.x*Q.z;
R.z=P.x*Q.y-P.y*Q.x;
/* find vector from eye to polygon */
/* we are using view coords so eye is always at center 0,0,0 */

S.x=-vp->x;
S.y=-vp->y;
S.z=-vp->z;
/* calculate scalar product */

c= (long)S.x*(long)R.x+(long)S.y*(long)R.y+(long)S.z*(long)R.z;
/* if the result is greater than one, the two vectors face in the 
general same direction */
/* for left hand view....*/


if (c>0) return 1;
        else return 0;
}



Point2 topic(p)
Point3 p;
{
Point2 res;
long sx,sy;
double inc;
int dout;
int rx,ry;

/* convert to screen(picture)  coordinates */

        sx=((p.x)*128) /p.z;
        sy=((p.y)*128) /p.z;

inc=(double)sx;
inc*=scale;
rx=(int)inc;
inc=(double)sy;
inc*=scale;
ry=(int)inc;


/* convert to computer coordinates */
        res.x=centerx+rx;
        res.y=centery+ry;

return (res);

}




void background()
{

v_clrwk(vhandle);

}
