File: cpinshorizon.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (425 lines) | stat: -rw-r--r-- 9,214 bytes parent folder | download | duplicates (6)
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef __MSW__
# include <windows.h>
#endif
#include <GL/gl.h>
#include "matrixmath.h"
#include "gw.h"
#include "v3dtex.h"
#include "cpvalues.h"
#include "cpins.h"
#include "cpinshorizon.h"
#include "cp.h"


CPIns *CPInsHorizonNew(void *cp);
static void CPInsHorizonDrawHorizonPlate(
	GLfloat p, GLfloat b,                   /* In radians. */
	GLfloat width, GLfloat height,
	int deg_visible,			/* Degrees visible. */
	GLfloat bank_notch_radius,		/* 0.0 to 1.0 */
	CPColor *bg, CPColor *fg, CPColor *text
);
static void CPInsHorizonValuesChanged(
	CPIns *ins, ControlPanelValues *v, void *data
);
static void CPInsHorizonManage(
	CPIns *ins, ControlPanelValues *v, void *data
);
static void CPInsHorizonDelete(CPIns *ins, void *data);


#ifndef PI
# define PI     3.14159265
#endif

#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? (float)atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))

#define RADTODEG(r)     ((r) * 180.0 / PI)
#define DEGTORAD(d)     ((d) * PI / 180.0)

#define NORMAL3F(x,y,z) (glNormal3f(    \
			 (GLfloat)(x),  \
			 (GLfloat)(z),  \
			 (GLfloat)-(y)  \
			))
#define TEXCOORD2F(x,y) (glTexCoord2f(          \
			 (GLfloat)(x),          \
			 (GLfloat)(1.0 - (y))   \
			))
#define VERTEX3FC(x,y,z) (glVertex3f(			\
			 (GLfloat)((x) / 100.0),        \
			 (GLfloat)((z) / 100.0),        \
			 (GLfloat)((y) / -100.0)        \
			))
#define ROTATEH(r)      (glRotatef((GLfloat)-RADTODEG(r), \
			0.0f, 1.0f, 0.0f))
#define ROTATEP(r)      (glRotatef((GLfloat)-RADTODEG(r), \
			1.0f, 0.0f, 0.0f))
#define ROTATEB(r)      (glRotatef((GLfloat)-RADTODEG(r), \
			0.0f, 0.0f, 1.0f))
#define TRANSLATE(x,y,z)        (glTranslatef(          \
			 (GLfloat)((x) / 100.0),        \
			 (GLfloat)((z) / 100.0),        \
			 (GLfloat)((y) / -100.0)        \
			))


/*
 *      Creates a new artificial horizon instrument.
 */
CPIns *CPInsHorizonNew(void *cp)
{
	CPInsHorizon *insv;
	CPIns *ins = CPInsNew(
	    sizeof(CPInsHorizon),
	    cp,
	    CPINS_TYPE_HORIZON,
	    "Horizon"
	);
	if(ins == NULL)
	    return(NULL);

	insv = CPINS_HORIZON(ins);
	insv->deg_visible = 40;
	insv->bank_notch_radius = 0.85f;

	CPInsSetFunction(
	    ins, "values_changed",
	    CPInsHorizonValuesChanged, NULL
	);
	CPInsSetFunction(
	    ins, "manage",
	    CPInsHorizonManage, NULL
	);
	CPInsSetFunction(
	    ins, "delete",
	    CPInsHorizonDelete, NULL
	);

	return(ins);
}

/*
 *	Draws the horizon ticks and plates.
 */
static void CPInsHorizonDrawHorizonPlate(
	GLfloat p, GLfloat b,			/* In radians. */
	GLfloat width, GLfloat height,
	int deg_visible,			/* Degrees visible. */
	GLfloat bank_notch_radius,		/* 0.0 to 1.0 */
	CPColor *bg, CPColor *fg, CPColor *text
)
{
	int 	ppd;			/* Pixels per degree. */
	float	p_deg,			/* Delta pitch in degrees. */
		half_width = width / 2,
		half_height = height / 2;
	double a[3 * 1], r[3 * 1];

	/* All calculations will be relative to center 0, 0 and
	 * then translated (and y axis flipped) just before passing
	 * to each drawing function.
	 */

	/* Calculate pixels per degree, currently we want to show
	 * a range of 40 degrees.
	 */
	ppd = (int)MAX(height / deg_visible, 1);

	/* Calculate delta pitch in degrees where negative pitch is down
	 * and positive pitch is up. (90.0, -90.0).
	 */
	if(p > DEGTORAD(180))
	    p_deg = (float)(360.0 - RADTODEG(p));
	else
	    p_deg = (float)-RADTODEG(p);


/* Takes vector a and rotates it by the bank b (in radians) and sets
 * its vertex (applying appropriate offsets).
 * The vector a must be centered at around 0,0.
 */
#define DO_SET_VERTEX	{		\
 MatrixRotateHeading3(a, -b, r);	\
 glVertex2f(				\
  (GLfloat)(r[0] + half_width),		\
  (GLfloat)(half_height - r[1])		\
 );					\
}


	/* Draw sky. */
	glColor3f(bg->r, bg->g, bg->b);
	glBegin(GL_QUADS);
	{
	    glVertex2f(0.0f, 0.0f);
	    glVertex2f(0.0f, height);
	    glVertex2f(width, height);
	    glVertex2f(width, 0.0f);
	}
	glEnd();


	/* Draw ground. */
	if(p_deg <= deg_visible)
	{
	    a[2] = 0.0;

	    glColor3f(fg->r, fg->g, fg->b);
	    glBegin(GL_QUADS);
	    {
		/* Upper left. */
		a[0] = -width;
		a[1] = MIN((0.0 - p_deg), deg_visible) * ppd;
		DO_SET_VERTEX

		/* Lower left. */
		a[0] = -width;
		a[1] = MAX((-130.0 - p_deg), -deg_visible) * ppd;
		DO_SET_VERTEX

		/* Lower right. */
		a[0] = width;
		a[1] = MAX((-130.0 - p_deg), -deg_visible) * ppd;
		DO_SET_VERTEX

		/* Upper right. */
		a[0] = width;
		a[1] = MIN((0.0 - p_deg), deg_visible) * ppd;
		DO_SET_VERTEX
	    }
	    glEnd();

	    glColor3f(text->r, text->g, text->b);
	    glBegin(GL_LINES);
	    {
		a[0] = -0.01 * width;
		a[1] = (0.0 - p_deg) * ppd;
		DO_SET_VERTEX
		a[0] = -1.0 * width;
		a[1] = (-40.0 - p_deg) * ppd;
		DO_SET_VERTEX

		a[0] = 0.01 * width;
		a[1] = (0.0 - p_deg) * ppd;
		DO_SET_VERTEX
		a[0] = 1.0 * width;
		a[1] = (-40.0 - p_deg) * ppd;
		DO_SET_VERTEX

		a[0] = -0.2 * width;
		a[1] = (0.0 - p_deg) * ppd;
		DO_SET_VERTEX
		a[0] = -1.0 * width;
		a[1] = (-20.0 - p_deg) * ppd;
		DO_SET_VERTEX
		a[0] = 0.2 * width;
		a[1] = (0.0 - p_deg) * ppd;
		DO_SET_VERTEX
		a[0] = 1.0 * width;
		a[1] = (-20.0 - p_deg) * ppd;
		DO_SET_VERTEX
	    }
	    glEnd();

	}


	/* Draw bank notch. */
	glColor3f(text->r, text->g, text->b);
	glBegin(GL_TRIANGLES);
	{
	    a[0] = 0.0;
	    a[1] = bank_notch_radius * half_height;
	    DO_SET_VERTEX
	    a[0] = -0.05 * half_width;
	    a[1] = (bank_notch_radius - 0.15) * half_height;
	    DO_SET_VERTEX
	    a[0] = 0.05 * half_width;
	    a[1] = (bank_notch_radius - 0.15) * half_height;
	    DO_SET_VERTEX
	}
	glEnd();

	/* Begin drawing ticks. */
	glColor3f(text->r, text->g, text->b);
	glBegin(GL_LINES);
	{
	    int i, d, degree = 0;

	    a[2] = 0.0;

	    /* Horizon (0 degree) line. */
	    a[0] = -0.35 * width;
	    a[1] = (degree - p_deg) * ppd;
	    DO_SET_VERTEX
	    a[0] = -0.1 * width;
	    a[1] = (degree - p_deg) * ppd;
	    DO_SET_VERTEX
	    a[0] = 0.1 * width;
	    a[1] = (degree - p_deg) * ppd;
	    DO_SET_VERTEX
	    a[0] = 0.35 * width;
	    a[1] = (degree - p_deg) * ppd;
	    DO_SET_VERTEX

	    /* Begin drawing 10 degree ticks, start degree off to the
	     * nearest 10th degree.
	     */
	    degree = (int)(p_deg / 10) * 10;
	    for(i = 0, d = degree - (2 * 10);
		i < 6;
		i++, d += 10
	    )
	    {
		if(d == 0)
		    continue;

		a[0] = -0.2 * width;
		a[1] = (d - p_deg) * ppd;
		DO_SET_VERTEX
		a[0] = 0.2 * width;
		a[1] = (d - p_deg) * ppd;
		DO_SET_VERTEX
	    }

	    /* Begin drawing 5 degree ticks. */
	    for(i = 0, d = degree - (2 * 10) + 5;
		i < 5;
		i++, d += 10
	    )
	    {
		if(d == 0)
		    continue;

		a[0] = -0.12 * width;
		a[1] = (d - p_deg) * ppd;
		DO_SET_VERTEX
		a[0] = 0.12 * width;
		a[1] = (d - p_deg) * ppd;
		DO_SET_VERTEX
	    }
	}
	glEnd();



#undef DO_SET_VERTEX
}

/*
 *	"values_changed" callback.
 */
static void CPInsHorizonValuesChanged(
	CPIns *ins, ControlPanelValues *v, void *data
)
{
	ControlPanel *cp;
	gw_display_struct *display;
	int cstate;
	GLfloat width, height;
	v3d_texture_ref_struct *tex_bg, *tex_fg;
	CPInsHorizon *insv = CPINS_HORIZON(ins);
	if((insv == NULL) || (v == NULL))
	    return;

	cp = CONTROL_PANEL(ins->cp);
	display = CONTROL_PANEL_DISPLAY(cp);
	if(display == NULL)
	    return;

	cstate = v->color_state;

	/* At this point the GL perspective matrix should already be
	 * orthoginal and it's size set to match the size of the
	 * frame buffer.  Now get the size of the frame buffer.
	 */
	width = (float)ins->res_width;
	height = (float)ins->res_height;

	/* Get instrument background and foreground textures. */
	tex_bg = ins->tex_bg;
	tex_fg = ins->tex_fg;


	/* Begin drawing. */
/*
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT);
 */

	/* Draw background. */



	/* Draw horizon. */
	StateGLDisable(&display->state_gl, GL_TEXTURE_2D);
	CPInsHorizonDrawHorizonPlate(
	    v->pitch, v->bank,
	    width, height,
	    MAX(insv->deg_visible, 1),
	    insv->bank_notch_radius,
	    &ins->color_bg[cstate],
	    &ins->color_fg[cstate],
	    &ins->color_text[cstate]
	);


	/* Draw foretround. */
	if(tex_fg != NULL)
	{
	    glColor3f(1.0f, 1.0f, 1.0f);
	    StateGLEnable(&display->state_gl, GL_TEXTURE_2D);
	    V3DTextureSelect(tex_fg);

	    glBegin(GL_QUADS);
	    {
		glTexCoord2f(0.0f, 0.0f);
		glVertex2f(0.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2f(0.0f, height);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2f(width, height);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2f(width, 0.0f);
	    }
	    glEnd();
	}

	CPInsRealizeTexture(ins);
}

/*
 *	"manage" callback.
 */
static void CPInsHorizonManage(
	CPIns *ins, ControlPanelValues *v, void *data
)
{
	CPInsHorizon *insv = CPINS_HORIZON(ins);
	if(insv == NULL)
	    return;



}

/*
 *      Deletes resources of instrument but not instrument itself.
 */
static void CPInsHorizonDelete(CPIns *ins, void *data)
{
	CPInsHorizon *insv = CPINS_HORIZON(ins);
	if(insv == NULL)
	    return;
}