| 1 |
#include <GL/glut.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <string.h> |
| 5 |
#include <math.h> |
| 6 |
#define WINDOW "image" |
| 7 |
#define UP 101 |
| 8 |
#define DOWN 103 |
| 9 |
#define RIGHT 102 |
| 10 |
#define LEFT 100 |
| 11 |
#define MAX 200 |
| 12 |
|
| 13 |
int NX, NY, MAXNZ, sets; |
| 14 |
int foo; |
| 15 |
|
| 16 |
void do_byteswap_f32( float arr[], int nel ), global(), local( int, int ); |
| 17 |
void readnames( char[] ), readarray( float[], char[], int ), readjet(); |
| 18 |
void TimerFunction( int ); |
| 19 |
|
| 20 |
float data[MAX*MAX], mxval, mnval, jet[MAX][MAX]; |
| 21 |
float globalmx=0, globalmn=100; |
| 22 |
int win[MAX], ilev=1, howmany, count=0, glo=0, usr=0, anim=0; |
| 23 |
char fns[MAX][MAX]; |
| 24 |
|
| 25 |
void menu(int value){ |
| 26 |
int i; |
| 27 |
|
| 28 |
switch( value ){ |
| 29 |
case 1: usr=glo=0; // unset glo & usr, sets local max/min |
| 30 |
local( count, ilev ); // reads new array, set local max/min |
| 31 |
break; |
| 32 |
case 2: glo=1; // enables global max/min |
| 33 |
usr=0; // unsets usr |
| 34 |
mxval=globalmx; // sets max to globalmx |
| 35 |
mnval=globalmn; // sets min to globalmn |
| 36 |
break; |
| 37 |
case 3: usr=1; // switch to user-set max/min |
| 38 |
glo=0; // unset glo |
| 39 |
printf( "Max=" ); scanf( "%f", &mxval ); // prompt for new max/min |
| 40 |
printf( "Min=" ); scanf( "%f", &mnval ); |
| 41 |
break; |
| 42 |
} |
| 43 |
for( i=0; i<sets; i++){ |
| 44 |
glutSetWindow(win[i]); |
| 45 |
glutPostRedisplay(); // display with new values |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
void display(void){ |
| 50 |
int i, j, ioff; |
| 51 |
float r, g, b, num, k, y; |
| 52 |
char str[MAX]; |
| 53 |
glClear( GL_COLOR_BUFFER_BIT ); // background to black |
| 54 |
|
| 55 |
ioff=0; // ioff will count thru both i&j b/c data is 1D |
| 56 |
for( j=0; j<NY; j++ ) |
| 57 |
for( i=0; i<NX; i++ ){ |
| 58 |
r=g=b=0; // set color values to black |
| 59 |
if( data[ioff]==0 ); // if data=0, values stay black |
| 60 |
else{ |
| 61 |
if( data[ioff]<mnval ) num=0; // if data is less than min, =min |
| 62 |
else{ |
| 63 |
if( data[ioff]>mxval ) num=63; // if data is more than max, =max |
| 64 |
else |
| 65 |
num=63*( data[ioff]-mnval )/( mxval-mnval ); //scale num from 0-63 |
| 66 |
} |
| 67 |
r=jet[(int)num][0]; // set red val |
| 68 |
g=jet[(int)num][1]; // set green val |
| 69 |
b=jet[(int)num][2]; // set blue val |
| 70 |
} |
| 71 |
|
| 72 |
glColor3f( r, g, b ); // put r, g, b into effect |
| 73 |
glRectf( i, j, i+1, j+1 ); // draws a square for data value |
| 74 |
ioff++; |
| 75 |
} |
| 76 |
|
| 77 |
glColor3f( 1, 1, 1 ); // set color to white |
| 78 |
glRectf( NX, 0, NX+1, NY+1 ); // draws a border |
| 79 |
for( i=0; i<64; i++ ){ //draws color bar |
| 80 |
glColor3f( jet[i][0], jet[i][1], jet[i][2]); //sets color |
| 81 |
k=(float)i; // turns i into a float |
| 82 |
y=(float)NY/64; // sets height of rectangles |
| 83 |
glRectf( NX+10, y*k, NX+20, (k+1)*y ); // draws rectangle |
| 84 |
} |
| 85 |
|
| 86 |
glColor3f( 1, 1, 1 ); // color to white |
| 87 |
|
| 88 |
sprintf( str, "%.4e", mxval ); // labels color bar with max val |
| 89 |
glRasterPos2f( NX+2, NY-1 ); // sets where to write to |
| 90 |
for( i=0; i<strlen( str ); i++) // writes each character |
| 91 |
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, str[i] ); |
| 92 |
|
| 93 |
str[0]='\0'; // sets str to null |
| 94 |
|
| 95 |
sprintf( str, "%.4e", mnval ); // labels color bar with min val |
| 96 |
glRasterPos2f( NX+2, 1 ); // sets where to write to |
| 97 |
for( i=0; i<strlen( str ); i++ ) // writes each character |
| 98 |
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, str[i] ); |
| 99 |
|
| 100 |
glRectf( 0, NY, NX, NY+1 ); // draws a border |
| 101 |
glRasterPos2f( 1, NY+6 ); // sets where to write to |
| 102 |
for( i=0; i<strlen( fns[count] ); i++ ) // labels current file name |
| 103 |
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, fns[count][i] ); |
| 104 |
|
| 105 |
str[0]='\0'; // sets str to null |
| 106 |
|
| 107 |
sprintf( str, "Level %d", ilev ); // labels current level |
| 108 |
glRasterPos2f( 1, NY+3 ); // sets where to write to |
| 109 |
for( i=0; i<strlen( str ); i++ ) // writes each character |
| 110 |
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, str[i] ); |
| 111 |
|
| 112 |
str[0]='\0'; //sets str to null |
| 113 |
|
| 114 |
if( glo ) // labels how max/min have been set |
| 115 |
sprintf( str, "Global" ); // if glo is set, display Global |
| 116 |
if( usr ) |
| 117 |
sprintf( str, "User-set" ); // if usr is set, display User-set |
| 118 |
if( !usr && !glo ) |
| 119 |
sprintf( str, "Local" ); // else display Local |
| 120 |
glRasterPos2f( NX+12, NY+3); |
| 121 |
for( i=0; i<strlen( str ); i++ ) // write each char of label |
| 122 |
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, str[i] ); |
| 123 |
|
| 124 |
str[0]='\0'; |
| 125 |
|
| 126 |
if( anim ){ // tell user if autoplay is on |
| 127 |
sprintf( str, "Autoplay" ); |
| 128 |
glRasterPos2f( NX-8, NY+3 ); |
| 129 |
for(i=0; i<strlen( str ); i++ ) // write each char of label |
| 130 |
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, str[i] ); |
| 131 |
} |
| 132 |
|
| 133 |
glutSwapBuffers(); |
| 134 |
glFlush(); |
| 135 |
} |
| 136 |
|
| 137 |
void key( unsigned char key, int x, int y ){ //called on key press |
| 138 |
int i; |
| 139 |
|
| 140 |
switch(key){ |
| 141 |
case 'q': exit(0); //quits on 'q' |
| 142 |
break; |
| 143 |
case 'r': count=0; |
| 144 |
ilev=1; |
| 145 |
if( glo || usr ) |
| 146 |
readarray( data, fns[count], ilev ); |
| 147 |
else |
| 148 |
local( count, ilev ); |
| 149 |
for( i=0; i<sets; i++){ |
| 150 |
glutSetWindow(win[i]); |
| 151 |
glutPostRedisplay(); |
| 152 |
} |
| 153 |
break; |
| 154 |
case 'a': anim++; // switches autoplay off/on |
| 155 |
anim=anim%2; // when a is pressed repeatedly |
| 156 |
for( i=0; i<sets; i++){ |
| 157 |
glutSetWindow(win[i]); |
| 158 |
glutPostRedisplay(); |
| 159 |
} |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void TimerFunction( int value ){ |
| 165 |
int i; |
| 166 |
|
| 167 |
switch(value){ // increments in the correct direction |
| 168 |
case DOWN : if( ilev<MAXNZ ) |
| 169 |
ilev++; // if down arrow pressed, moves down levels |
| 170 |
break; |
| 171 |
case UP : if( ilev>1 ) |
| 172 |
ilev--; // if up arrow is pressed, moves up levels |
| 173 |
break; |
| 174 |
case LEFT : if( count>0 ) |
| 175 |
count--; // if left arrow is pressed, moves back in time |
| 176 |
break; |
| 177 |
case RIGHT: if( count<howmany-1 ) |
| 178 |
count++; // if right arrow is pressed, moves forward in time |
| 179 |
break; |
| 180 |
} |
| 181 |
|
| 182 |
if( glo || usr ) // if glo or usr is set, read new array w/o |
| 183 |
readarray( data, fns[count], ilev ); // calculating new max/min |
| 184 |
else // if local max/min is set |
| 185 |
local( count, ilev ); // read new array, calculate new max/min |
| 186 |
|
| 187 |
for( i=0; i<sets; i++){ |
| 188 |
glutSetWindow(win[i]); |
| 189 |
glutPostRedisplay(); // display with new values |
| 190 |
} |
| 191 |
|
| 192 |
switch( value ){ |
| 193 |
case DOWN : if( ilev<MAXNZ ) // sets limit that you can go down |
| 194 |
glutTimerFunc( 100, TimerFunction, value ); // recalls itself |
| 195 |
break; |
| 196 |
case UP : if( ilev>1 ) // sets limit that you can go up |
| 197 |
glutTimerFunc( 100, TimerFunction, value ); // recalls itself |
| 198 |
break; |
| 199 |
case LEFT : if( count>0 ) // sets limit that you can go left |
| 200 |
glutTimerFunc( 100, TimerFunction, value ); // recalls itself |
| 201 |
break; |
| 202 |
case RIGHT: if( count<howmany-1 ) // sets limit that you can go right |
| 203 |
glutTimerFunc( 100, TimerFunction, value ); // recalls itself |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void specialkey( int key, int x, int y ){ |
| 209 |
int i; |
| 210 |
|
| 211 |
if( anim ) // if animation is set, call the timer function |
| 212 |
glutTimerFunc( 100, TimerFunction, key); |
| 213 |
|
| 214 |
switch(key){ |
| 215 |
case DOWN : if( ilev<MAXNZ ) // if you haven't reached the bottom |
| 216 |
ilev++; // keep going down |
| 217 |
break; |
| 218 |
case UP : if( ilev>1 ) // if you haven't reached the top |
| 219 |
ilev--; // keep going up |
| 220 |
break; |
| 221 |
case RIGHT : if( count<howmany-1 ) // if you haven't reached the last file |
| 222 |
count++; // keep going right |
| 223 |
break; |
| 224 |
case LEFT : if( count>0 ) // if you haven't reached the first file |
| 225 |
count--; // keep going left |
| 226 |
break; |
| 227 |
} |
| 228 |
if( glo || usr ) // if global or user-set max/min |
| 229 |
readarray(data, fns[count], ilev); // read new array w/o new max/min |
| 230 |
else // if local max/min set |
| 231 |
local(count, ilev); // read new array and calculate local max/min |
| 232 |
|
| 233 |
for(i=0; i<sets; i++){ |
| 234 |
glutSetWindow(win[i]); |
| 235 |
glutPostRedisplay(); // display with new values |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void global(){ // calculates the max/min for the total data set |
| 240 |
FILE* fp; |
| 241 |
int i, j; |
| 242 |
|
| 243 |
for( i=0; i<howmany; i++ ) // cycles through each file |
| 244 |
for( j=0; j<MAXNZ; j++ ){ // cycles through each level for each file |
| 245 |
local( i, j ); // calculates local/max/min for specific data set |
| 246 |
if( mxval > globalmx ) globalmx = mxval; // sets highest value to globalmx |
| 247 |
if( mnval < globalmn ) globalmn = mnval; // sets lowest value to globalmn |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
void local( int time, int lev ){ // calculates local max/min |
| 252 |
int i; |
| 253 |
|
| 254 |
mxval=0; |
| 255 |
mnval=100; |
| 256 |
readarray( data, fns[time], lev ); // read new array of data |
| 257 |
for( i=0;i<NX*NY;i++ ){ |
| 258 |
if( data[i] > mxval ) mxval=data[i]; // set largest val to mxval |
| 259 |
if( data[i] < mnval ) mnval=data[i]; // set smallest val to mnval |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
void readnames( char filename[] ){ // reads in list of filenames containing data |
| 264 |
FILE* fp; |
| 265 |
int i=0; |
| 266 |
|
| 267 |
fp=fopen( filename, "r" ); // opens list |
| 268 |
while( !feof(fp) ){ // reads until the end of the file |
| 269 |
fscanf( fp, "%s",fns[i] ); // places filename into an array of strings |
| 270 |
i++; // counts how many filenames there are |
| 271 |
} |
| 272 |
fclose( fp ); // close file |
| 273 |
howmany=i-1; |
| 274 |
} |
| 275 |
|
| 276 |
void readjet(){ //reads in color scale values |
| 277 |
|
| 278 |
FILE* fp; |
| 279 |
int i, j; |
| 280 |
|
| 281 |
fp=fopen( "jet.dat", "r" ); // opens file containing values |
| 282 |
for( i=0; i<64; i++ ) // reads in 64 sets of r, g, b values |
| 283 |
for( j=0; j<3; j++ ) |
| 284 |
fscanf( fp, "%f", &jet[i][j] ); |
| 285 |
fclose( fp ); // closes file |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
void readarray( float arr[], char filename[], int il ){ // reads new data |
| 290 |
FILE *fp; |
| 291 |
int i; |
| 292 |
|
| 293 |
fp=fopen( filename, "r" ); // opens the file containing data |
| 294 |
fseek( fp, (il-1)*NX*NY*4, SEEK_SET ); // seeks to the correct level using ilev |
| 295 |
fread( arr, sizeof( arr[0] ), NX*NY, fp ); // reads in data to fill one level |
| 296 |
fclose( fp ); // close file |
| 297 |
do_byteswap_f32( arr,NX*NY ); // swaps the binary data |
| 298 |
} |
| 299 |
|
| 300 |
void do_byteswap_f32( float arr[], int nel ) // switches endian |
| 301 |
{ |
| 302 |
int i; |
| 303 |
char src[4]; |
| 304 |
char trg[4]; |
| 305 |
for( i=0; i<nel; i++ ){ |
| 306 |
memcpy( src, &(arr[i]), 4); |
| 307 |
trg[0]=src[3]; |
| 308 |
trg[1]=src[2]; |
| 309 |
trg[2]=src[1]; |
| 310 |
trg[3]=src[0]; |
| 311 |
memcpy( &(arr[i]), trg, 4); |
| 312 |
} |
| 313 |
} |
| 314 |
int main( int argc, char *argv[] ){ |
| 315 |
int i, x, y, tmp; |
| 316 |
char filename[MAX]; |
| 317 |
|
| 318 |
printf( "Please enter x, y and z dimensions\n" ); |
| 319 |
scanf( "%d %d %d", &NX, &NY, &MAXNZ ); //user-set NX, NY, NZ |
| 320 |
printf( "Please enter filename\n" ); |
| 321 |
scanf( "%s", filename ); //filename contains names of data files |
| 322 |
printf( "how many data sets?\n" ); |
| 323 |
scanf( "%d", &sets); |
| 324 |
|
| 325 |
readjet(); // stores color values |
| 326 |
readnames( filename ); // gets list of filenames to read from |
| 327 |
global(); // calculates max and min for all data |
| 328 |
local( count, ilev ); // sets curr data array, finds local max/min |
| 329 |
|
| 330 |
glutInit( &argc, argv ); // initializes stuff |
| 331 |
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 332 |
glutInitWindowSize( 1400/3, ((NY+15)*1400)/((NX+25)*3) ); // sets window size from NX, NY |
| 333 |
|
| 334 |
for(i=0; i<sets; i++){ |
| 335 |
x=(i%3)*(1400/3); |
| 336 |
y=(i/3)*((NY+15)*1400)/((NX+25)*3); |
| 337 |
glutInitWindowPosition( x, y ); |
| 338 |
win[i]=glutCreateWindow( WINDOW ); // window is created |
| 339 |
glClearColor( 0, 0, 0, 0 ); // sets clear color to black |
| 340 |
gluOrtho2D( 0, NX+25, 0, NY+15 ); // sets how data is mapped to window |
| 341 |
glutDisplayFunc( display ); |
| 342 |
glutKeyboardFunc( key ); // called on key press |
| 343 |
glutSpecialFunc( specialkey ); // called on special key press (arrow keys) |
| 344 |
|
| 345 |
glutCreateMenu( menu ); // adds a menu |
| 346 |
glutAddMenuEntry( "Local Color Scale", 1 ); // describes choices |
| 347 |
glutAddMenuEntry( "Global Color Scale", 2 ); |
| 348 |
glutAddMenuEntry( "User-Set Color Scale", 3 ); |
| 349 |
glutAttachMenu( GLUT_RIGHT_BUTTON ); // menu called on right click |
| 350 |
} |
| 351 |
|
| 352 |
glutMainLoop(); |
| 353 |
return 0; |
| 354 |
} |
| 355 |
|