#define NORMALIZE4(V) { \
V[0]/=V[3]; \
V[1]/=V[3]; \
V[2]/=V[3]; \
}
#define popmatrix4( U, M, V ) { \
U[0] = V[0]*M[0]+V[1]*M[4]+V[2]*M[8]+V[3]*M[12]; \
U[1] = V[0]*M[1]+V[1]*M[5]+V[2]*M[9]+V[3]*M[13]; \
U[2] = V[0]*M[2]+V[1]*M[6]+V[2]*M[10]+V[3]*M[14]; \
U[3] = V[0]*M[3]+V[1]*M[7]+V[2]*M[11]+V[3]*M[15]; \
if (U[3]) NORMALIZE4(U); \
} /// != 0.0)&& (U[3] != 1) 1 changes nothing, NOT 0
double pmat[16] = { 0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0 } ;
void setpmat() {
double fl; // = tan(dtor(90-fovx/aspect/2)); /// UNIT focal len
fl = 1/tan(dtor(fov/Aspect/2)); /// same number /// fl * cx = actual focal length
pmat[0] = fl/Aspect;
pmat[5] = fl;
pmat[10] = (farclip + nearclip) / (nearclip - farclip);
pmat[14] = 2*farclip*nearclip / (nearclip - farclip);
}
void fovmat(double v[],double p[]) {
int cx = (int)(_Width/2),cy = (int)(_Height/2);
double pnt2[4], pnt[4] = { 0,0,0,1 } ;
COPYVECTOR(pnt,p);NORMALIZE(pnt); /// matrix in unit form, normalize
popmatrix4(pnt2,pmat,pnt);
COPYVECTOR(v,pnt2);
v[0] *= -cx; v[1] *= -cy;
v[0] += cx; v[1] += cy;
}