]> git.mxchange.org Git - flightgear.git/blob - Cockpit/panel.cxx
Incorporated Friedemann Reinhard's <mpt218@faupt212.physik.uni-erlangen.de>
[flightgear.git] / Cockpit / panel.cxx
1 /**************************************************************************
2  * panel.cxx -- routines to draw an instrument panel
3  *
4  * Written by Friedemann Reinhard, started June 1998.
5  *
6  * Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #ifdef HAVE_WINDOWS_H          
32 #  include <windows.h>
33 #endif
34
35 #include <GL/glut.h>
36 #include <XGL/xgl.h>
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #include <Aircraft/aircraft.h>
43 #include <Debug/fg_debug.h>
44 #include <Main/options.hxx>
45
46 #include "panel.hxx"
47
48
49 #define IMAGIC      0x01da
50 #define IMAGIC_SWAP 0xda01
51
52 #define SWAP_SHORT_BYTES(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
53 #define SWAP_LONG_BYTES(x) (((((x) & 0xff) << 24) | (((x) & 0xff00) << 8)) | \
54 ((((x) & 0xff0000) >> 8) | (((x) & 0xff000000) >> 24)))
55
56 typedef struct {
57     unsigned short imagic;
58     unsigned short type;
59     unsigned short dim;
60     unsigned short sizeX, sizeY, sizeZ;
61     unsigned long min, max;
62     unsigned long wasteBytes;
63     char name[80];
64     unsigned long colorMap;
65     FILE *file;
66     unsigned char *tmp[5];
67     unsigned long rleEnd;
68     unsigned long *rowStart;
69     unsigned long *rowSize;
70 } Image;
71
72
73 IMAGE *img;
74
75 static GLuint panel_list;
76 static GLuint panel_tex_id;
77 static GLubyte tex[512][256][4];
78
79
80 static double get_speed( void )
81 {
82         fgFLIGHT *f;
83
84         f = current_aircraft.flight;
85         return( FG_V_equiv_kts );    // Make an explicit function call.
86 }
87
88
89 /* image.c ,temporary hack, I know*/
90 static Image *ImageOpen(char *fileName)
91 {
92   Image *image;
93   unsigned long *rowStart, *rowSize, ulTmp;
94   int x, i;
95
96   image = (Image *)malloc(sizeof(Image));
97   if (image == NULL) 
98     {
99       fprintf(stderr, "Out of memory!\n");
100       exit(-1);
101     }
102   if ((image->file = fopen(fileName, "rb")) == NULL) 
103     {
104       perror(fileName);
105       exit(-1);
106     }
107   /*
108    *    Read the image header
109    */
110   fread(image, 1, 12, image->file);
111   /*
112    *    Check byte order
113    */
114   if (image->imagic == IMAGIC_SWAP) 
115     {
116       image->type = SWAP_SHORT_BYTES(image->type);
117       image->dim = SWAP_SHORT_BYTES(image->dim);
118       image->sizeX = SWAP_SHORT_BYTES(image->sizeX);
119       image->sizeY = SWAP_SHORT_BYTES(image->sizeY);
120       image->sizeZ = SWAP_SHORT_BYTES(image->sizeZ);
121     }
122
123   for ( i = 0 ; i <= image->sizeZ ; i++ )
124     {
125       image->tmp[i] = (unsigned char *)malloc(image->sizeX*256);
126       if (image->tmp[i] == NULL ) 
127         {
128           fprintf(stderr, "Out of memory!\n");
129           exit(-1);
130         }
131     }
132
133   if ((image->type & 0xFF00) == 0x0100) /* RLE image */
134     {
135       x = image->sizeY * image->sizeZ * sizeof(long);
136       image->rowStart = (unsigned long *)malloc(x);
137       image->rowSize = (unsigned long *)malloc(x);
138       if (image->rowStart == NULL || image->rowSize == NULL) 
139         {
140           fprintf(stderr, "Out of memory!\n");
141           exit(-1);
142         }
143       image->rleEnd = 512 + (2 * x);
144       fseek(image->file, 512, SEEK_SET);
145       fread(image->rowStart, 1, x, image->file);
146       fread(image->rowSize, 1, x, image->file);
147       if (image->imagic == IMAGIC_SWAP) 
148         {
149           x /= sizeof(long);
150           rowStart = image->rowStart;
151           rowSize = image->rowSize;
152           while (x--) 
153             {
154               ulTmp = *rowStart;
155               *rowStart++ = SWAP_LONG_BYTES(ulTmp);
156               ulTmp = *rowSize;
157               *rowSize++ = SWAP_LONG_BYTES(ulTmp);
158             }
159         }
160     }
161   return image;
162 }
163
164 static void ImageClose( Image *image)
165 {
166   int i;
167
168   fclose(image->file);
169   for ( i = 0 ; i <= image->sizeZ ; i++ )
170     free(image->tmp[i]);
171   free(image);
172 }
173
174 static void ImageGetRow( Image *image, unsigned char *buf, int y, int z)
175 {
176   unsigned char *iPtr, *oPtr, pixel;
177   int count;
178
179   if ((image->type & 0xFF00) == 0x0100)  /* RLE image */
180     {
181       fseek(image->file, image->rowStart[y+z*image->sizeY], SEEK_SET);
182       fread(image->tmp[0], 1, (unsigned int)image->rowSize[y+z*image->sizeY],
183             image->file);
184
185       iPtr = image->tmp[0];
186       oPtr = buf;
187       while (1) 
188         {
189           pixel = *iPtr++;
190           count = (int)(pixel & 0x7F);
191           if (!count)
192             return;
193           if (pixel & 0x80) 
194             {
195               while (count--) 
196                 {
197                   *oPtr++ = *iPtr++;
198                 }
199             } 
200           else 
201             {
202               pixel = *iPtr++;
203               while (count--) 
204                 {
205                   *oPtr++ = pixel;
206                 }
207             }
208         }
209     }
210   else /* verbatim image */
211     {
212       fseek(image->file, 512+(y*image->sizeX)+(z*image->sizeX*image->sizeY),
213             SEEK_SET);
214       fread(buf, 1, image->sizeX, image->file);
215     }
216 }
217
218 static void ImageGetRawData( Image *image, char *data)
219 {
220   int i, j, k;
221   int remain;
222
223   switch ( image->sizeZ )
224     {
225     case 1:
226       remain = image->sizeX % 4;
227       break;
228     case 2:
229       remain = image->sizeX % 2;
230       break;
231     case 3:
232       remain = (image->sizeX * 3) & 0x3;
233       if (remain)
234         remain = 4 - remain;
235       break;
236     case 4:
237       remain = 0;
238       break;
239     }
240
241   for (i = 0; i < image->sizeY; i++) 
242     {
243       for ( k = 0; k < image->sizeZ ; k++ )
244         ImageGetRow(image, image->tmp[k+1], i, k);
245       for (j = 0; j < image->sizeX; j++) 
246         for ( k = 1; k <= image->sizeZ ; k++ )
247           *data++ = *(image->tmp[k] + j);
248       data += remain;
249     }
250 }
251
252 static IMAGE *ImageLoad(char *fileName)
253 {
254   Image *image;
255   IMAGE *final;
256   int sx;
257
258   image = ImageOpen(fileName);
259
260   final = (IMAGE *)malloc(sizeof(IMAGE));
261   if (final == NULL) 
262     {
263       fprintf(stderr, "Out of memory!\n");
264       exit(-1);
265     }
266   final->imagic = image->imagic;
267   final->type = image->type;
268   final->dim = image->dim;
269   final->sizeX = image->sizeX; 
270   final->sizeY = image->sizeY;
271   final->sizeZ = image->sizeZ;
272
273   /* 
274    * Round up so rows are long-word aligned 
275    */
276   sx = ( (image->sizeX) * (image->sizeZ) + 3) >> 2;
277
278   final->data 
279     = (unsigned char *)malloc( sx * image->sizeY * sizeof(unsigned int));
280
281   if (final->data == NULL) 
282     {
283       fprintf(stderr, "Out of memory!\n");
284       exit(-1);
285     }
286
287   ImageGetRawData(image, final->data);
288   ImageClose(image);
289   return final;
290 }
291
292
293 void fgPanelInit ( void ) {
294     fgOPTIONS *o;
295     char tpath[256];
296     int x, y;
297
298     o = &current_options;
299
300 #ifdef GL_VERSION_1_1
301     xglGenTextures(1, &panel_tex_id);
302     xglBindTexture(GL_TEXTURE_2D, panel_tex_id);
303 #elif GL_EXT_texture_object
304     xglGenTexturesEXT(1, &panel_tex_id);
305     xglBindTextureEXT(GL_TEXTURE_2D, panel_tex_id);
306 #else
307 #  error port me
308 #endif
309
310     // glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
311     glPixelStorei(GL_UNPACK_ROW_LENGTH, 512);
312     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
313     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);   
314     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
315     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
316
317     /* load in the texture data */
318     tpath[0] = '\0';
319     strcat(tpath, o->fg_root);
320     strcat(tpath, "/Textures/");
321     strcat(tpath, "panel1.rgb");
322
323     if ( (img = ImageLoad(tpath)) == NULL ){
324         fgPrintf( FG_COCKPIT, FG_EXIT, 
325                   "Error loading cockpit texture %s\n", tpath );
326     }
327
328     for ( y = 0; y < 256; y++ ) {
329         for ( x = 0; x < 512; x++ ) { 
330             tex[x][y][0]=img->data[(y+x*256)*3];
331             tex[x][y][1]=img->data[(y+x*256)*3+1];
332             tex[x][y][2]=img->data[(y+x*256)*3+2];
333             if ( (tex[x][y][0] == 0) && (tex[x][y][1] == 0) && 
334                  (tex[x][y][2] == 0) ) {
335                 tex[x][y][3]=0;
336             } else {
337                 tex[x][y][3]=255;
338             }
339         }
340     }
341     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 256, 0, GL_RGBA, 
342                  GL_UNSIGNED_BYTE, (GLvoid *)(tex));
343     glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
344
345     printf("ALPHA=%d\n", tex[0][0][3]);
346     printf("ALPHA=%d\n", tex[512][0][3]);
347     printf("ALPHA=%d\n", tex[512][256][3]);
348     printf("ALPHA=%d\n", tex[0][256][3]);
349
350     panel_list = glGenLists (1);
351     glNewList(panel_list, GL_COMPILE);
352     glBegin(GL_POLYGON);
353     glTexCoord2f(0.0,0.0); glVertex2f(0.0,0.0);
354     glTexCoord2f(1.0,0.0); glVertex2f(640.0,0.0);
355     glTexCoord2f(1.0,1.0); glVertex2f(640.0,330.0);
356     // glTexCoord2f(0.6,1.0); glVertex2f(384.0,330.0); 
357     // glTexCoord2f(0.166666,0.91111); glVertex2f(106.66666,303.182);
358     // glTexCoord2f(0.0, 0.75769231); glVertex2f(0.0, 279.61); 
359     glTexCoord2f(0.0,1.0); glVertex2f(0.0,330.0); 
360     glEnd();
361     glEndList ();
362 }
363
364
365 void fgPanelUpdate ( void ) {
366     float alpha;
367     double speed;
368
369     glMatrixMode(GL_PROJECTION);
370     glPushMatrix();
371     glLoadIdentity();
372     //glViewport(0, 0, 640, 480);
373     gluOrtho2D(0, 640, 0, 480);
374     glMatrixMode(GL_MODELVIEW);
375     glPushMatrix();
376     glLoadIdentity();
377
378     glDisable(GL_DEPTH_TEST);
379     glDisable(GL_LIGHTING);
380     glEnable(GL_TEXTURE_2D);
381 #ifdef GL_VERSION_1_1
382     xglBindTexture(GL_TEXTURE_2D, panel_tex_id);
383 #elif GL_EXT_texture_object
384     xglBindTextureEXT(GL_TEXTURE_2D, panel_tex_id);
385 #else
386 #  error port me
387 #endif
388     glEnable(GL_BLEND);
389     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
390     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_BLEND);
391     glColor4f(1.0, 1.0, 1.0, 1.0);
392
393     glCallList(panel_list);
394
395     glPushMatrix();
396     glDisable(GL_TEXTURE_2D);
397     speed = get_speed();
398     alpha=((((float)(speed))/150)*270 + 20);
399     glTranslatef(130, 146, 0);
400     glRotatef(-alpha, 0.0, 0.0, 1.0);
401     glScalef(20, 23, 0.0);
402     glBegin(GL_POLYGON);
403     glColor4f(1.0, 1.0, 1.0, 1.0);
404     glVertex2f(0.0, 0.0);
405     glVertex2f(0.1, 0.2);
406     glVertex2f(0.0, 1.0);
407     glVertex2f(-0.1, 0.2);
408     glVertex2f(0.0, 0.0);
409     glEnd();  
410     glPopMatrix();   
411
412     glFlush();
413
414     glEnable(GL_DEPTH_TEST);
415     glEnable(GL_LIGHTING);
416     glDisable(GL_TEXTURE_2D);
417     glDisable(GL_BLEND);
418     glDisable(GL_ALPHA_TEST);
419
420     glMatrixMode(GL_PROJECTION);
421     glPopMatrix();
422     glMatrixMode(GL_MODELVIEW);
423     glPopMatrix();
424 }
425
426
427 /* $Log$
428 /* Revision 1.1  1998/06/27 16:47:54  curt
429 /* Incorporated Friedemann Reinhard's <mpt218@faupt212.physik.uni-erlangen.de>
430 /* first pass at an isntrument panel.
431 /*
432  */