home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ARM Club 3
/
TheARMClub_PDCD3.iso
/
hensa
/
graphics
/
a150_1
/
!Demo4
/
c
/
horizon
next >
Wrap
Text File
|
1992-02-08
|
6KB
|
241 lines
/******************************************************************************
* *
* horizon.c *
* *
******************************************************************************/
#include <stdlib.h>
#include <math.h>
#include "GrpLib.h"
#include "extern.h"
/*****************************************************************************/
int sgn(int i)
{
return(i>=0 ? 1 : -1);
}
/*****************************************************************************/
void Horizon(vec4 normal,vec4 point)
{
int a,b,c;
int d,x,y,p;
int col1 = BLUE,col2 = GREEN;
int pts[10][2];
int l1[6],l2[6],l3[6],i3[6];
int j,i=0,n1=0,n2=0,n3=0;
int lg=50;
int xmin=-160,xmax=160,ymin=-128,ymax=128;
a = normal[0];
b = normal[1];
c = normal[2];
c = c*dist;
/* liste de points */
x = xmin; y = ymax;
pts[i][0] = x; pts[i][1] = y;
p = a*x+b*y+c;
if (p >= 0) l1[n1++]=i;
else l2[n2++]=i;
i++;
if (a!=0) {
x = (- c - b*y)/a;
if (x>xmin && x<xmax) {
pts[i][0] = x; pts[i][1] = y;
l1[n1++]=i;
l2[n2++]=i;
i++;
}
}
x = xmax; y = ymax;
pts[i][0] = x; pts[i][1] = y;
p = a*x+b*y+c;
if (p >= 0) l1[n1++]=i;
else l2[n2++]=i;
i++;
if (b!=0) {
y = (- c - a*x)/b;
if (y>ymin && y<ymax) {
pts[i][0] = x; pts[i][1] = y;
l1[n1++]=i;
l2[n2++]=i;
i++;
}
}
x = xmax; y = ymin;
pts[i][0] = x; pts[i][1] = y;
p = a*x+b*y+c;
if (p >= 0) l1[n1++]=i;
else l2[n2++]=i;
i++;
if (a!=0) {
x = (- c - b*y)/a;
if (x>xmin && x<xmax) {
pts[i][0] = x; pts[i][1] = y;
l1[n1++]=i;
l2[n2++]=i;
i++;
}
}
x = xmin; y = ymin;
pts[i][0] = x; pts[i][1] = y;
p = a*x+b*y+c;
if (p >= 0) l1[n1++]=i;
else l2[n2++]=i;
i++;
if (b!=0) {
y = (- c - a*x)/b;
if (y>ymin && y<ymax) {
pts[i][0] = x; pts[i][1] = y;
l1[n1++]=i;
l2[n2++]=i;
i++;
}
}
/* calcul horizon */
if (abs(a)<abs(b)) {
int dy,d,y1,y2;
dy = (int)(lg*sqrt(a*a+b*b)/b);
d = abs(dy);
y1 = (- c - a*xmin)/b;
y2 = (- c - a*xmax)/b;
if (((y1>ymin-d) && (y1<ymax+d)) || ((y2>ymin-d) && (y2<ymax+d))) {
pts[i][0] = xmin; pts[i][1] = y1-4*sgn(dy);
l3[n3] = i;
i3[n3] = 255;
n3++; i++;
pts[i][0] = xmax; pts[i][1] = y2-4*sgn(dy);
l3[n3] = i;
i3[n3] = 255;
n3++; i++;
pts[i][0] = xmax; pts[i][1] = y2+dy;
l3[n3] = i;
i3[n3] = 100;
n3++; i++;
pts[i][0] = xmin; pts[i][1] = y1+dy;
l3[n3] = i;
i3[n3] = 100;
n3++; i++;
}
}
else {
int dx,d,x1,x2;
dx = (int)(lg*sqrt(a*a+b*b)/a);
d = abs(dx);
x1 = (- c - b*ymin)/a;
x2 = (- c - b*ymax)/a;
if (((x1>xmin-d) && (x1<xmax+d)) || ((x2>xmin-d) && (x2<xmax+d))) {
pts[i][0] = x1-4*sgn(dx); pts[i][1] = ymin;
l3[n3] = i;
i3[n3] = 255;
n3++; i++;
pts[i][0] = x2-4*sgn(dx); pts[i][1] = ymax;
l3[n3] = i;
i3[n3] = 255;
n3++; i++;
pts[i][0] = x2+dx; pts[i][1] = ymax;
l3[n3] = i;
i3[n3] = 100;
n3++; i++;
pts[i][0] = x1+dx; pts[i][1] = ymin;
l3[n3] = i;
i3[n3] = 100;
n3++; i++;
}
}
/* trace */
if (n1>=3)
for (j=1 ; j<=n1-2 ; j++) {
GrpReg[0] = pts[l1[0 ]][0]+xorg;
GrpReg[1] = -pts[l1[0 ]][1]+yorg;
GrpReg[2] = pts[l1[j ]][0]+xorg;
GrpReg[3] = -pts[l1[j ]][1]+yorg;
GrpReg[4] = pts[l1[j+1]][0]+xorg;
GrpReg[5] = -pts[l1[j+1]][1]+yorg;
GrpReg[8] = col1;
CallGrp(GrpReg,GrpStack,Uni2DPoly3);
}
if (n3>=3)
for (j=1 ; j<=n3-2 ; j++) {
GrpReg[0] = pts[l3[0 ]][0]+xorg;
GrpReg[1] = -pts[l3[0 ]][1]+yorg;
GrpReg[2] = pts[l3[j ]][0]+xorg;
GrpReg[3] = -pts[l3[j ]][1]+yorg;
GrpReg[4] = pts[l3[j+1]][0]+xorg;
GrpReg[5] = -pts[l3[j+1]][1]+yorg;
GrpReg[6] = i3[0 ];
GrpReg[7] = i3[j ];
GrpReg[8] = i3[j+1];
GrpReg[9] = (int)TblCol + 32*7 +8;
CallGrp(GrpReg,GrpStack,Grd2DPoly3);
}
if (n2>=3)
for (j=1 ; j<=n2-2 ; j++) {
GrpReg[0] = pts[l2[0 ]][0]+xorg;
GrpReg[1] = -pts[l2[0 ]][1]+yorg;
GrpReg[2] = pts[l2[j ]][0]+xorg;
GrpReg[3] = -pts[l2[j ]][1]+yorg;
GrpReg[4] = pts[l2[j+1]][0]+xorg;
GrpReg[5] = -pts[l2[j+1]][1]+yorg;
GrpReg[8] = col2;
CallGrp(GrpReg,GrpStack,Uni2DPoly3);
}
}
/*****************************************************************************/
void PlotHorizon(Envi *envi,Object *obs,Object *obj)
{
vec4 Obj4Rep[4];
mat33 invobs;
vec3 transl;
int env[3];
InvMat(invobs,obs->Rep);
MkVec3(transl,obj->Org[0] - obs->Org[0],
obj->Org[1] - obs->Org[1],
obj->Org[2] - obs->Org[2]);
GrpReg[0] = (int)invobs;
GrpReg[1] = (int)Obj4Rep;
GrpReg[2] = (int)obj->Rep;
GrpReg[3] = (int)transl;
CallGrp(GrpReg,GrpStack,RepToRepIn4Rep); /*Obj4Rep=invobs*(transl o ObjRep)*/
env[0] = (int)(envi->AmbLight); /* ambient light (0-255) */
env[1] = (int)(envi->NbDirecSrc << 24) + ((int)(envi->DirecSrc) & 0x00FFFFFF);
env[2] = (int)(envi->NbPonctSrc << 24) + ((int)(envi->PonctSrc) & 0x00FFFFFF);
Horizon(Obj4Rep[1],Obj4Rep[3]);
}
/*****************************************************************************/
int CollisionHorizon(Object *obs,Object *hor)
{
mat33 inv;
vec3 transl,p;
InvMat(inv,hor->Rep);
MkVec3(transl,- hor->Org[0],
- hor->Org[1],
- hor->Org[2]);
TransformPt(p,inv,transl,obs->Org);
if (p[1]<0) return(1); else return(0);
}