1 /**************************************************************************
2 * panel.cxx -- routines to draw an instrument panel
4 * Written by Friedemann Reinhard, started June 1998.
6 * Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
42 #include <Aircraft/aircraft.h>
43 #include <Debug/fg_debug.h>
44 #include <Main/options.hxx>
50 #define IMAGIC_SWAP 0xda01
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)))
57 unsigned short imagic;
60 unsigned short sizeX, sizeY, sizeZ;
61 unsigned long min, max;
62 unsigned long wasteBytes;
64 unsigned long colorMap;
66 unsigned char *tmp[5];
68 unsigned long *rowStart;
69 unsigned long *rowSize;
75 static GLuint panel_list;
76 static GLuint panel_tex_id;
77 static GLubyte tex[512][256][4];
80 static double get_speed( void )
84 f = current_aircraft.flight;
85 return( FG_V_equiv_kts ); // Make an explicit function call.
89 /* image.c ,temporary hack, I know*/
90 static Image *ImageOpen(char *fileName)
93 unsigned long *rowStart, *rowSize, ulTmp;
96 image = (Image *)malloc(sizeof(Image));
99 fprintf(stderr, "Out of memory!\n");
102 if ((image->file = fopen(fileName, "rb")) == NULL)
108 * Read the image header
110 fread(image, 1, 12, image->file);
114 if (image->imagic == IMAGIC_SWAP)
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);
123 for ( i = 0 ; i <= image->sizeZ ; i++ )
125 image->tmp[i] = (unsigned char *)malloc(image->sizeX*256);
126 if (image->tmp[i] == NULL )
128 fprintf(stderr, "Out of memory!\n");
133 if ((image->type & 0xFF00) == 0x0100) /* RLE image */
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)
140 fprintf(stderr, "Out of memory!\n");
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)
150 rowStart = image->rowStart;
151 rowSize = image->rowSize;
155 *rowStart++ = SWAP_LONG_BYTES(ulTmp);
157 *rowSize++ = SWAP_LONG_BYTES(ulTmp);
164 static void ImageClose( Image *image)
169 for ( i = 0 ; i <= image->sizeZ ; i++ )
174 static void ImageGetRow( Image *image, unsigned char *buf, int y, int z)
176 unsigned char *iPtr, *oPtr, pixel;
179 if ((image->type & 0xFF00) == 0x0100) /* RLE image */
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],
185 iPtr = image->tmp[0];
190 count = (int)(pixel & 0x7F);
210 else /* verbatim image */
212 fseek(image->file, 512+(y*image->sizeX)+(z*image->sizeX*image->sizeY),
214 fread(buf, 1, image->sizeX, image->file);
218 static void ImageGetRawData( Image *image, char *data)
223 switch ( image->sizeZ )
226 remain = image->sizeX % 4;
229 remain = image->sizeX % 2;
232 remain = (image->sizeX * 3) & 0x3;
241 for (i = 0; i < image->sizeY; i++)
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);
252 static IMAGE *ImageLoad(char *fileName)
258 image = ImageOpen(fileName);
260 final = (IMAGE *)malloc(sizeof(IMAGE));
263 fprintf(stderr, "Out of memory!\n");
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;
274 * Round up so rows are long-word aligned
276 sx = ( (image->sizeX) * (image->sizeZ) + 3) >> 2;
279 = (unsigned char *)malloc( sx * image->sizeY * sizeof(unsigned int));
281 if (final->data == NULL)
283 fprintf(stderr, "Out of memory!\n");
287 ImageGetRawData(image, final->data);
293 void fgPanelInit ( void ) {
298 o = ¤t_options;
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);
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);
317 /* load in the texture data */
319 strcat(tpath, o->fg_root);
320 strcat(tpath, "/Textures/");
321 strcat(tpath, "panel1.rgb");
323 if ( (img = ImageLoad(tpath)) == NULL ){
324 fgPrintf( FG_COCKPIT, FG_EXIT,
325 "Error loading cockpit texture %s\n", tpath );
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) ) {
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);
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]);
350 panel_list = glGenLists (1);
351 glNewList(panel_list, GL_COMPILE);
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);
365 void fgPanelUpdate ( void ) {
369 glMatrixMode(GL_PROJECTION);
372 //glViewport(0, 0, 640, 480);
373 gluOrtho2D(0, 640, 0, 480);
374 glMatrixMode(GL_MODELVIEW);
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);
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);
393 glCallList(panel_list);
396 glDisable(GL_TEXTURE_2D);
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);
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);
414 glEnable(GL_DEPTH_TEST);
415 glEnable(GL_LIGHTING);
416 glDisable(GL_TEXTURE_2D);
418 glDisable(GL_ALPHA_TEST);
420 glMatrixMode(GL_PROJECTION);
422 glMatrixMode(GL_MODELVIEW);
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.