APPENDIX G: 3D GRAPHICS SOFTWARE


#include <math.h>
#include <gl/gl.h>
#include <gl/device.h>
#include <sys/times.h>
#include "vector.h"
#include "manip_world.h"
#include "dem.h"
#include "other_objects.h"
#include "world.h"
#include "texture.h"
#include "wm.h"
#define SIZE	(5280.*9.)
#define EARTH_RADIUS 6378000.
#define	MIN_HEIGHT 5.3
#define	PI  3.1415926535
void	SetPos(void);
void	SetAttitude(void);
void	CheckWater(void);
void	CheckButtons(void);
float	xpo, ypo, zpo, xMouse, yMouse;
float	xDown, yDown;
long	xorigin, yorigin, xsize, ysize;
struct	tms for_time;
float	time3=0.0;
int	LEFT_DOWN;
int	MIDDLE_DOWN;
int	RIGHT_DOWN;
float	xDownPo, yDownPo, xDownMouse,  yDownMouse;
int	ViewMode;
float	Range;
float	waterDelZ;
float	waterPosY;
float	rollval, yawval;
void	CamFunc(int);
extern	MAP map;
extern	DEM dem;
extern	float	StartX, StartY;
float	PRIN;
float	HEAD_Height;
float	HEAD_Velocity;
int	timer, frames;
short	val;
int	t1cnt, t2cnt;
Vector	estState, desState;
void	Init3D(void) 
{
    estState = VectorInit(13, 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.);
    desState = VectorInit(13, 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.);
    xsize = 640; ysize = 480;
    
    /*OpenWindowSizeST(xsize, ysize, .25);xorigin = 640-xsize/2;yorigin = 480-ysize;*/
    /*OpenWindowSizeNB(xsize, ysize);xorigin = 640-xsize/2;yorigin = 480-ysize/2;*/
    OpenWindowSize(xsize, ysize);getorigin(&xorigin, &yorigin);
    DefineSomeObjects();
    InitWorld();
    printf("alpha bitplanes - %d\n", (int)getgdesc(GD_BITS_NORM_SNG_ALPHA));
    
    perspective(450, xsize/(float)ysize, 1, 1000000);
    lsetdepth(getgdesc(GD_ZMIN),  getgdesc(GD_ZMAX));
    CamTranslateZ = 1.5;
    CamTilt = 3.14159/2.0;
    CamPan = 0.;
    ShoulderPitch = 0.;
    rollval = 0.;
    yawval = 0.;
    
    HEAD_Height = MIN_HEIGHT;
    HEAD_Velocity = 0.;
    xpo = 0.;
    ypo = 0.;
    zpo = 0.0;
    waterPosY = 0.;
    Range = 500.;
    ViewMode = -1;
    t1cnt=0;t2cnt=0;	
    LEFT_DOWN = FALSE;
    MIDDLE_DOWN = FALSE;
    RIGHT_DOWN = FALSE;
    frames = 0;
}
void	Update3D(void) 
{
	time3=time3+0.04;
	/*ShoulderPitch+=.06;*/
	CheckButtons();
	SetPos();
/*	SetAttitude();
	CheckWater();
	    waterPosY -= 1.5;
	    if(waterPosY < 0.) waterPosY += 400;
	    waterDelZ = 1.5*sin((double)times(&for_time)/160.);
	    ReplaceObjectPosition(waterObj, 0., waterPosY, waterDelZ);
*/
	if(frames++ == 99) {
	    printf("time %f\n", 1./(((float)((unsigned short int)times(&for_time)-timer))/100./60.)  );
	    timer = (unsigned short int)times(&for_time);
	    frames = 0;
	}
	/*ReplaceObjectPosition(MovingBox, 1500.+00.*sin(time3*2.), 4120.+00.*sin(time3*1.3*2.), 862.+2.*sin(time3*1.7*2.));*/
	DofQ(estQuat, V(estState, 7), V(estState, 8), V(estState, 9), V(estState, 10));
	DofQ(desQuat, V(desState, 7), V(desState, 8), V(desState, 9), V(desState, 10));
	/*DofRPY(estQuat, rollval, 0., yawval);*/
	DrawWorld();
}
void	Close3D(void) 
{
    CloseWorld();
}
void	CamFunc(int i){
char	str[128];
Matrix Identity = { 1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1 };
float	x, y;
    x = dem.longitude[0]/3600.+xpo/map.scx/dem.col;
    y = dem.latitude[0]/3600.+ypo/map.scy/dem.col;
    RGBcolor(0, 100, 255);
    cmov(-1.3, -.93, -2.5);
    if(ViewMode == -1) {sprintf(str, "HOLD"); charstr(str);}
    if(ViewMode == 0) {sprintf(str, "MOVE"); charstr(str);}
    if(ViewMode == 1) {sprintf(str, "SCALE"); charstr(str);}
/*
    cmov(.5, -.8, -2.5);
    sprintf(str, "Longitude =>  %f", x); charstr(str);
    cmov(.5, -.85, -2.5);
    sprintf(str, " Latitude => %f", y); charstr(str);
*/
    RGBcolor(0, 100, 255);
    cmov(-2.5*sin(-CamPan), -.9, -2.5*cos(-CamPan));
    sprintf(str, "<N>", CamPan); charstr(str);
    cmov(-2.5*sin(-CamPan+PI), -.9, -2.5*cos(-CamPan+PI));
    sprintf(str, "<S>", CamPan); charstr(str);
    cmov(-2.5*sin(-CamPan-PI/2.), -.9, -2.5*cos(-CamPan-PI/2.));
    sprintf(str, "<E>", CamPan); charstr(str);
    cmov(-2.5*sin(-CamPan+PI/2.), -.9, -2.5*cos(-CamPan+PI/2.));
    sprintf(str, "<W>", CamPan); charstr(str);
    cmov(-2.5*sin(-CamPan-PI/4.), -.9, -2.5*cos(-CamPan-PI/4.));
    sprintf(str, "<NE>", CamPan); charstr(str);
    cmov(-2.5*sin(-CamPan+PI/4.), -.9, -2.5*cos(-CamPan+PI/4.));
    sprintf(str, "<NW>", CamPan); charstr(str);
    cmov(-2.5*sin(-CamPan-PI*3./4.), -.9, -2.5*cos(-CamPan-PI*3./4.));
    sprintf(str, "<SE>", CamPan); charstr(str);
    cmov(-2.5*sin(-CamPan+PI*3./4.), -.9, -2.5*cos(-CamPan+PI*3./4.));
    sprintf(str, "<SW>", CamPan); charstr(str);
}
/*DofRPY(buoyPos,x*.1,y*.1,x*.2);*/
void	SetPos(void){
    if(ViewMode == 0){
	xMouse = 300*(2.0*(getvaluator(MOUSEX)-xorigin)/xsize-1.0)/2.0;
	yMouse = 300*(2.0*(getvaluator(MOUSEY)-yorigin)/ysize-1.0)/2.0;
	xMouse -= xDown;
	yMouse -= yDown;
	xpo += .01*1.*(xMouse/4.*cos((double)CamPan) - yMouse/4.*sin((double)CamPan));
	ypo += .01*1.*(yMouse/4.*cos((double)CamPan) + xMouse/4.*sin((double)CamPan));
	/*zpo=V(PosVec(map, xpo, ypo), 3)+HEAD_Height;*/
	zpo=0.;
	Range = 0.;
    }
    if(ViewMode == 1){
	xMouse = 300*(2.0*(getvaluator(MOUSEX)-xorigin)/xsize-1.0)/2.0;
	yMouse = 300*(2.0*(getvaluator(MOUSEY)-yorigin)/ysize-1.0)/2.0;
	xMouse -= xDown;
	yMouse -= yDown;
	rollval = .1*1.*(xMouse/4.*cos((double)CamPan) - yMouse/4.*sin((double)CamPan));
	yawval = .1*1.*(yMouse/4.*cos((double)CamPan) + xMouse/4.*sin((double)CamPan));
    }
    CamTranslateX=xpo;
    CamTranslateY=ypo-Range;
    CamTranslateZ=zpo;
}
void	SetAttitude(){
float	xah, yah, zah;
Vector	Vahead,  Vup;
    if(ViewMode == 0){
	xah = xpo+40.*(cos((double)CamPan) - sin((double)CamPan));
	yah = ypo+40.*(cos((double)CamPan) + sin((double)CamPan));
	zah = V(PosVec(map, xah, yah), 3)+HEAD_Height;
		
	Vahead = UnitV(VectorInit(3, xah-xpo, yah-ypo, 0.));
	Vup = UnitV(VectorInit(3, xah-xpo, yah-ypo, zah-zpo));
	/*PrintV(Vahead);
	PrintV(Vup);*/
	if(zah==zpo){
	    CamTilt = 3.141592/2.;;
	    /*printf("-----------\n\r");*/
	}else{
	    if(zah>zpo)CamTilt = 3.141592/2.+AngleVV(Vahead, Vup);
	    else CamTilt = 3.141592/2.-AngleVV(Vahead, Vup);
	}
	CamRoll = 0.;
    }
    if(ViewMode == 1){
	CamTilt = 3.141592/4.;
	CamRoll = 0.;
    }
}
void	CheckButtons(void){
    if((getbutton(MIDDLEMOUSE)== TRUE)&&(MIDDLE_DOWN == FALSE)){
	ViewMode++;
	if(ViewMode == 2) ViewMode = -1;
	xDown = 300*(2.0*(getvaluator(MOUSEX)-xorigin)/xsize-1.0)/2.0;
	yDown = 300*(2.0*(getvaluator(MOUSEY)-yorigin)/ysize-1.0)/2.0;
	MIDDLE_DOWN = TRUE;
    }
    if((getbutton(MIDDLEMOUSE)== FALSE)&&(MIDDLE_DOWN == TRUE)){
	MIDDLE_DOWN = FALSE;
    } 
    if(ViewMode == 0){
        if(getbutton(LEFTMOUSE)== TRUE){
	    CamPan += .045;
        }
        if(getbutton(RIGHTMOUSE)== TRUE){
	    CamPan -= .045;
        }
    }  
    if(ViewMode == 1){
    }  
}
WMbuf	wmNewBuffer(int s){
WMbuf	buf;
    buf = (WMbuf)malloc(sizeof(struct WMbufstr));
    buf->pos = 0;
    buf->lastreadpos = 0;
    buf->size = s;
    buf->v = (float*)malloc(s*sizeof(float));
    return(buf);    
}
void	wmClearBuffer(WMbuf buf){
int i;
    buf->pos = 0;
    buf->lastreadpos = 0;
    for(i=0; i<buf->size; i++) buf->v[i] = 0;
}
void	wmResetBufferRead(WMbuf buf){
    buf->lastreadpos = buf->pos;
}
void	wmAddToBuffer(WMbuf buf, float x){
    buf->v[buf->pos] = x;
    buf->pos++;
    if(buf->pos >= buf->size) buf->pos=0;
}
float	wmReadFromBuffer(WMbuf buf){
float	x;
    buf->lastreadpos--;
    if(buf->lastreadpos < 0) buf->lastreadpos = buf->size-1;
    x = buf->v[buf->lastreadpos];
    return(x);
}
/* back of pm */
scale	.083333333333	.083333333333	.083333333333	
textureSize	512	512
quadTEX	-35	10	20	165	29	
	-35	-10	20	11	28
	-35	10	-20	168	332	
	-35	-10	-20	15	337
/* top of pm */
quadTEX	-35	10	-20	437	320	
	-35	-10	-20	287	322
	-10	10	-20	437	504	
	-10	-10	-20	292	505
quadTEX	-10	10	-20	502	453	
	-10	-10	-20	502	351
	-10	10	-10	454	453	
	-10	-10	-10	454	351
quadTEX	-10	10	-10	502	453	
	-10	-10	-10	502	351
	0	10	-10	454	453	
	0	-10	-10	454	351
/* top of em */
quadTEX	0	10	-10	506	133	
	0	-10	-10	504	282
	30	6	-6	281	163	
	30	-6	-6	280	253
/* top of mm */
quadTEX	30	6	-6	273	257	
	30	-6	-6	183	258
	42	6	-6	274	347	
	42	-6	-6	183	346
/* front of mm */
quadTEX	42	6	-6	172	100	
	42	-6	-6	263	100
	42	6	6	172	10	
	42	-6	6	262	10
/* bottom of mm */
quadTEX	42	6	6	273	257	
	42	-6	6	183	258
	30	6	6	274	347	
	30	-6	6	183	346
/* bottom of em */
quadTEX	30	6	6	498	127	
	30	-6	6	496	33
	0	10	10	269	155	
	0	-10	10	269	4
quadTEX	0	10	10	502	453	
	0	-10	10	502	351
	-10	10	10	454	453	
	-10	-10	10	454	351
quadTEX	-10	10	10	502	453	
	-10	-10	10	502	351
	-10	10	20	454	453	
	-10	-10	20	454	351
/* bottom of pm */
quadTEX	-10	10	20	437	320	
	-10	-10	20	287	322
	-35	10	20	437	504	
	-35	-10	20	292	505
/* port bb */
quadTEX	-35	-10	-10	168	104	
	-35	-10	10	168	257
	-35	-22.5	-10	257	105	
	-35	-22.5	10	260	252
quadTEX	-35	-22.5	-10	280	504	
	-35	-22.5	10	276	356
	0	-22.5	-10	16	506	
	0	-22.5	10	15	362 
quadTEX	0	-22.5	-10	257	104	
	0	-22.5	10	260	252
	0	-10	-10	185	104	
	0	-10	10	185	254
/* port em */
quadTEX	0	-10	-10	274	155	
	0	-10	10	275	6
	30	-6	-6	498	127	
	30	-6	6	498	35
/* port mm */
quadTEX	30	-6	-6	273	257	
	30	-6	6	183	258
	42	-6	-6	274	347	
	42	-6	6	183	346
/* starboard bb */
quadTEX	-35	10	10	168	104	
	-35	10	-10	168	257
	-35	22.5	10	257	105	
	-35	22.5	-10	260	252
quadTEX	-35	22.5	10	276	356
	-35	22.5	-10	280	504
	0	22.5	10	15	362
	0	22.5	-10	17	506 
quadTEX	0	22.5	10	260	254	
	0	22.5	-10	258     105
	0	10	10	168	258	
	0	10	-10	168     103
/* starboard em */
quadTEX	30	6	-6	275	6
	30	6	6	274	156	
	0	10	-10	498	36
	0	10	10	498	126	
/* starboard mm */
quadTEX	30	6	6	273	257	
	30	6	-6	183	258
	42	6	6	274	347	
	42	6	-6	183	346
/* port pm upper */
quadTEX	-10	-10	-20	509	437	
	-35	-10	-20	509	294
	-10	-10	-10	447	437	
	-35	-10	-10	447	294
/* port bb upper */
quadTEX	0	-10	-10	405	276
	-35	-10	-10	280	276
	0	-22.5	-10	405	316	
	-35	-22.5	-10	281	318
/* port bb lower */
quadTEX	0	-22.5	10	405	316	
	-35	-22.5	10	281	318
	0	-10	10	405	276	
	-35	-10	10	280	276
/* port pm lower */
quadTEX	-10	-10	10	451	357	
	-35	-10	10	445	499
	-10	-10	20	508	357	
	-35	-10	20	507	499
/* starboard pm upper */
quadTEX	-35	10	-20	510	496	
	-10	10	-20	507	354
	-35	10	-10	446	496	
	-10	10	-10	450	354
/* starboard bb upper */
quadTEX	-35	10	-10	405	276	
	0	10	-10	280	276
	-35	22.5	-10	405	316	
	0	22.5	-10	281	318
/* starboard bb lower */
quadTEX	-35	22.5	10	405	316	
	0	22.5	10	281	318
	-35	10	10	405	276	
	0	10	10	280	276
/* starboard pm lower */
quadTEX	-35	10	10	448	295	
	-10	10	10	507	446
	-35	10	20	509	295	
	-10	10	20	507	448
scale	12	12	12	
end