Hi all,
This code is taken from Edward Angel’s book OpenGL Primer and was for a PPM reader. I’ve cut out all the unecessary parts of the reader and used it to display a data file which is just a series of numbers from 0 to 255 just like a PPM file. The data file used represents a grid 296 points (width) by 128 bits (height).
I have two questions:
i) How can I display the data files one after the other?
ii) I seem to get a grayscale image with this colormap. How do I change the color values so that I can show in my animation the densest parts as having a red color while the sparse parts have a blue color with green in between.
The code can be found below. Thanks for all your help.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
int n;
int m;
GLint counter = 0;
GLuint *image;
void myidle()
{
counter += 1;
if( counter <= 100 )
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(n, m, GL_RGB, GL_UNSIGNED_INT, image);
glutSwapBuffers();
}
void myreshape(int h, int w)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLfloat) n, 0.0, (GLfloat) m);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, h, w);
}
int main(int argc, char **argv)
{
FILE *fd;
int k, nm;
int one;
char filename1[20];
int i;
float s;
int red, green, blue;
for( counter = 0; counter <= 100; counter++ ) {
sprintf(filename1,“nldat2/%05d.dat”, counter );
if ( !( fd = fopen( filename1, “r”) ) )
printf ( "File cannot be opened
" );
else {
printf( "opened file %05d.dat
", counter );
n = 296;
m = 128;
k = 255;
nm = nm;
image = malloc(3sizeof(GLuint)*nm);
s = 255./k;
for(i = 0; i < nm; i++)
{
fscanf(fd, "%d
", &one );
red = one;
blue = one;
green = one;
image[3i + 3] = red;
image[3i + 2] = green;
image[3*i + 1] = blue;
}
}
printf( "finished loop %d
", counter);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(n, m);
glutInitWindowPosition(0, 0);
glutCreateWindow(“image”);
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glutIdleFunc(myidle);
glPixelTransferf(GL_RED_SCALE, s);
glPixelTransferf(GL_GREEN_SCALE, s);
glPixelTransferf(GL_BLUE_SCALE, s);
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);
glClearColor(1.0, 1.0, 1.0, 1.0);
glutMainLoop();
return 0;
}