File: app.c

package info (click to toggle)
acm 5.0-23.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,364 kB
  • ctags: 4,793
  • sloc: ansic: 42,444; makefile: 706; cpp: 293; perl: 280; sh: 198
file content (394 lines) | stat: -rw-r--r-- 8,772 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 *  acm : an aerial combat simulator for X
 *  Copyright (C) 1991,1992  Riley Rainey
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 dated June, 1991.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program;  if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave., Cambridge, MA 02139, USA.
 */
#include "../lib/Vlib.h"
#include <X11/Xutil.h>
#include <math.h>
#include <sys/time.h>

#define VIEW1

extern Display  *dpy;
extern Window   win;
extern GC       curGC;
extern XSizeHints xsh;
extern int	mono;

VPolygon *poly[2048];

double lastTime = 1000.0;
double frameCount = 0.0;
double minInterval = 10000.0;
double maxInterval = 0.0;
double elapsedTotal = 0.0;

static VPoint origin = { 0.0, 0.0, 0.0 };

void TimeIntervalAccounting() {

	struct	itimerval itv;
	double	thisTime, elapsedTime;

	getitimer (ITIMER_REAL, &itv);

	thisTime = (double) itv.it_value.tv_sec +
		(double) itv.it_value.tv_usec / 1000000.0;

	elapsedTime = lastTime - thisTime;

	if (elapsedTime > maxInterval)
		maxInterval = elapsedTime;

	if (elapsedTime < minInterval)
		minInterval = elapsedTime;

	elapsedTotal += elapsedTime;
	frameCount += 1;
	lastTime = thisTime;
}

void PrintStatistics () {

	printf ("Total time for %g frames was %g seconds.\n\n", frameCount,
		elapsedTotal);
	printf ("Average frame rate was %g frames per second\n",
		frameCount / elapsedTotal);
	printf ("Maximum frame rate was %g frames per second\n",
		1.0 / minInterval);
	printf ("Minimum frame rate was %g frames per second\n",
		1.0 / maxInterval);
}

VPoint	*normal;
Viewport *vx;

void placeObject (v, obj, loc, roll, pitch, yaw, poly, cnt)
Viewport *v;
VObject *obj;
VPoint  loc;
double roll;
double pitch;
double yaw;
VPolygon **poly;
int	 *cnt; {

	int	 i, j, k, offset, aspect, n;
	VPoint	 *q, tmp, center, nc;
	VMatrix	 mtx, mtx1;
	register double dist;
	VPolygon **p;

	j = *cnt;

	VIdentMatrix (&mtx);
	if (roll != 0.0)
		VRotate (&mtx, XRotation, roll);
	if (pitch != 0.0)
		VRotate (&mtx, YRotation, pitch);
	if (yaw != 0.0)
		VRotate (&mtx, ZRotation, yaw);
	VTranslatePoint (&mtx, loc);

	VMatrixMult (&mtx, &v->eyeSpace, &mtx1);

	VTransform (&obj->center, &mtx, &tmp);
	VTransform (&tmp, &v->eyeSpace, &center);
	for (i=0; i<4; ++i) {
		dist = VPointToClipPlaneDistance (&center, &(normal[i]));
		if (dist > obj->extent) {
			return;
		}
	}

	n = obj->numPolys;
	p = obj->polygon;
	if (obj->order) {
		VTransform (&origin, &mtx1, &nc);
		VTransform_ (&_VUnitVectorI, &mtx1, &obj->xaxis);
		VTransform_ (&_VUnitVectorJ, &mtx1, &obj->yaxis);
		VTransform_ (&_VUnitVectorK, &mtx1, &obj->zaxis);
		aspect = VComputeObjectAspect (obj, &nc);
#ifdef notdef
		printf ("%s: %s\n", obj->name, VGetAspectName(aspect));
#endif
		offset = aspect * n;
	}

	for (i=0; i<n; ++i) {

		if (poly[j] != (VPolygon *) NULL)
			VDestroyPolygon (poly[j]);

		poly[j] = (obj->order) ?
			VCopyPolygon(p[obj->order[offset + i]]) :
			VCopyPolygon(p[i]);
		for ((k=0, q=poly[j]->vertex); k<poly[j]->numVtces; (++k, ++q)) {
			VTransform(q, &mtx1, &tmp);
			*q = tmp;
		}
	        if (poly[j]->flags & PolyNormalValid) {
		    VTransform_ (&poly[j]->normal, &mtx1, &tmp);
		    poly[j]->normal = tmp;
		}
		++j;
	}

	*cnt = j;
}

app(background)
char *background; {

	int	i, cnt, black;
	unsigned int j, curPixel = 0;
	char	*str;
	FILE	*file;
	VObject	*obj, *ftr, *mig;
	VPoint	viewPt, centerInt, up, ftrLoc, ftrLoc2, ftrLoc3, migLoc1, migLoc2;
	Viewport *v;
	double	dist, p, migV, v1, a, migRoll;
	double	updateRate = 10.0;
	Colormap cmap;
	long	screen;
	ZInfo	z;
	VColor	*vsky;
	unsigned long skypixel;
	struct	 itimerval timeval;
	XSegment seg[2];

	str = "../../objects/rwy-and-gnd.obv";

	if ((file = fopen(str, "r")) == (FILE *) NULL) {
		perror ("Cannot open object file");
		exit (1);
	}

	if ((obj = VReadObject(file)) == (VObject *) NULL) {
		fprintf (stderr, "Error reading the object definition.\n");
		exit (1);
	}

	fclose (file);

	str = "../../objects/f16.obv";

	if ((file = fopen(str, "r")) == (FILE *) NULL) {
		perror ("Cannot open object file");
		exit (1);
	}

	if ((ftr = VReadObject(file)) == (VObject *) NULL) {
		fprintf (stderr, "Error reading the object definition.\n");
		exit (1);
	}

	fclose (file);
	

	str = "../../objects/mig29.obv";

	if ((file = fopen(str, "r")) == (FILE *) NULL) {
		perror ("Cannot open object file");
		exit (1);
	}

	if ((mig = VReadObject(file)) == (VObject *) NULL) {
		fprintf (stderr, "Error reading the object definition.\n");
		exit (1);
	}

	fclose (file);
	
	 _VDefaultWorkContext->usePixmaps = 0;

	screen = DefaultScreen (dpy);
	cmap = DefaultColormap (dpy, screen);

	v = VOpenViewport (dpy, screen, win, cmap,
		DefaultVisual (dpy, screen), UNITS_FEET,
		1.5, 1.0, xsh.width, xsh.height);

	normal = v->clipNormals;
	vx = v;

	vsky = VAllocColor ("skyblue");

	if (VBindColors (v, background) < 0) {
		fprintf (stderr, "Error in binding colors.\n");
		exit (1);
	}

	skypixel = v->pixel[vsky->cIndex];

	ftrLoc.x = 80.0;
	ftrLoc.y = -110.0;
	ftrLoc.z = -7.0;

	ftrLoc2.x = 475.0;
	ftrLoc2.y = 3.7;
	ftrLoc2.z = -7.0;

	migLoc1.z = -310.0;
	migLoc2.z = -320.0;
	migV = 370.0 / 3600.0 * 5280.0 / updateRate;	/* mig speed in fps */
	migV = migV / (2500.0 * 3.14 * 2.0); /* mig speed in rad/update */
	migRoll = 28.0 * 3.14 / 180.0;
	a = 90.0 * 3.14 / 180.0;

	v1 = 130.0 / 3600.0 * 5280.0 / updateRate;

	centerInt.x = 80.0;
	centerInt.y = -90.0;
	centerInt.z = -6.0;

	dist = -10000.0; p = 0;

	getitimer (ITIMER_REAL, &timeval);
	timeval.it_value.tv_sec = 1000;
	timeval.it_value.tv_usec = 0;
	setitimer (ITIMER_REAL, &timeval, (struct itimerval *) NULL);

	mono = 0;

	while (1) {

		if (mono == 0) {

			z.depth = MaxDepth;
			z.color = skypixel;
			FillRectangle (v->w, 0, 0, xsh.width, xsh.height, &z);

		}

		ftrLoc3.x = dist+150.0;
		ftrLoc3.y = -15.0;
		ftrLoc3.z = (dist+150.0) * 50.0 / 1000.0 - 40.0;
		if (ftrLoc3.z > -13.0)
			ftrLoc3.z = -13.0;
		viewPt.x = dist;
		viewPt.y = 15.0;
		viewPt.z = dist * 50.0 / 1000.0 - 50.0;
		if (viewPt.z > -13.0)
			viewPt.z = -13.0;
		up = viewPt;
		up.z = up.z - 1.0;
		if (viewPt.z < -14.0)
			up.y = up.y + 0.1 * sin(p);
		p = p + 0.03;
		dist = dist + v1;
		if (dist > 10000.0) {
			PrintStatistics ();
			exit (0);
		}

		migLoc1.x = 0.0 + 2200.0 * cos (a);
		migLoc1.y = -800.0 + 2200.0 * sin (a);
		migLoc2.x = 0.0 + 2225.0 * cos (a-0.04);
		migLoc2.y = -800.0 + 2225.0 * sin (a-0.04);
		a = a + migV;

/*
 *  Calculate eye space information based on our current viewpoint.
 */

#ifdef VIEW1
	VGetEyeSpace (v, viewPt, centerInt, up);
#endif
#ifdef VIEW2
	up = migLoc2;
	up.z = up.z - 1.0;
	VGetEyeSpace (v, migLoc2, viewPt, up);
#endif
#ifdef VIEW3
	up = ftrLoc;
	up.z = up.z - 1.0;
	VGetEyeSpace (v, ftrLoc, viewPt, up);
#endif

/*
 *  Now create a vector containing all polygons from the objects.
 */

	cnt = 0;
	placeObject (v, obj, origin, 0.0, 0.0, 0.0, poly, &cnt);

#ifndef VIEW3
	placeObject (v, ftr, ftrLoc, 0.0, 0.0, 90.0*3.14/180.0, poly, &cnt);
#endif
	placeObject (v, ftr, ftrLoc2, 0.0, 0.0, -3.0*3.14/180.0, poly, &cnt);
	placeObject (v, ftr, ftrLoc3, 0.0, -11.0*3.14/180.0, 0.0, poly, &cnt);
#ifndef VIEW1
	placeObject (v, ftr, viewPt, 0.0, -11.0*3.14/180.0, 0.0, poly, &cnt);
#endif
	placeObject (v, mig, migLoc1, migRoll, 0.0, a+90.0*3.14/180.0, poly, &cnt);
#ifndef VIEW2
	placeObject (v, mig, migLoc2, migRoll, 0.0, a+90.0*3.14/180.0, poly, &cnt);
#endif

	black = BlackPixel(dpy, 0);


/*
 *  First clip, then draw each polygon.
 */

	for (i=0; i<cnt; ++i) {

		if (mono)
			XSetForeground (dpy, curGC, black);

		poly[i] = VClipSidedPolygon(v, poly[i], v->clipPoly);

		if (poly[i]) {
#ifdef notdef
		    if (mono == 0 &&
			curPixel != (j=v->pixel[poly[i]->color->cIndex])) {
			XSetForeground (dpy, curGC, j);
			curPixel = j;
		    }
#endif
		    if (mono)
		        VDrawPolygon (v, win, curGC, poly[i]);
		    else
		        VFillPolygon (v, win, curGC, poly[i]);
		}

	}

	VExposeBuffer (v, curGC);

	TimeIntervalAccounting();

/*
 * Erase the un-displayed planes.
 */

#ifdef notdef
	if (mono == 0) {
		curPixel = *(v->pixel);
        	XSetForeground (dpy, curGC, curPixel);
		if ( _VDefaultWorkContext->usePixmaps == 1)
        	XFillRectangle (dpy, v->monoPixmap, curGC, 0, 0, xsh.width, xsh.height);
		else
        	XFillRectangle (dpy, win, curGC, 0, 0, xsh.width, xsh.height);
	}
#endif


        }

}