/****************************************************************************
* Get texture image from pgm- or ppm-file.
* Format of input file:
* - line containing ascii file type, e.g. strings 'P5' or 'P6'
* - optional ascii comment lines starting with '#'
* - ascii line containing horizontal and vertical size separated by space
* - ascii line containing number '255'
* - binary pixel values: single byte per pixel with grayscale if type P5 (pgm)
* - triples of bytes with RGB values if type P6 (ppm)
***************************************************************************/
void getImageFromFile (char filename[])
{
#define inputLineLength 81
int i, j;
int byteValue, intensityLevels;
char inputLine[inputLineLength], typeOfFile[3];
FILE *f = fopen (filename, "r");
if (f == NULL) /* check if file exists */
{ fprintf (stderr, "\nERROR: Failed to open file.\n");
exit (1);
}
/* Get file type. */
do fgets (inputLine, inputLineLength-1, f); while (inputLine[0] == '#');
typeOfFile[0] = inputLine[0];
typeOfFile[1] = inputLine[1];
typeOfFile[2] = '\0';
printf (" file type: %s\n", typeOfFile);
/* Get horizontal en vertical size. */
do fgets (inputLine, inputLineLength-1, f); while (inputLine[0] == '#');
sscanf (inputLine, "%d %d", &texImageWidth, &texImageHeight);
printf (" horizontal: %d\n", texImageWidth);
printf (" vertical: %d\n", texImageHeight);
/* Allocate array that will contain the texture image. */
texImage1 =
(GLubyte *) malloc (texImageWidth*texImageHeight*4*sizeof(GLubyte));
/* Get number with maximum number of shades. */
do fgets (inputLine, inputLineLength-1, f); while (inputLine[0] == '#');
sscanf (inputLine, "%d", &intensityLevels);
printf (" int levels: %d\n", intensityLevels);
/* Copy binary pixel values into texture image array. */
for (i = texImageHeight-1; i >= 0; i--)
{ for (j = 0; j < texImageWidth; j++)
{ int position = (i*texImageWidth+j)*4;
/* read and store RGB or grayscale values*/
byteValue = getc (f); /* R-value or grayscale value */
texImage1[position] = (GLubyte) byteValue;
if (typeOfFile[1] == '6')
byteValue = getc (f); /* G-value if file type = R6 */
texImage1[position+1] = (GLubyte) byteValue;
if (typeOfFile[1] == '6')
byteValue = getc (f); /* B-value if file type = R6 */
texImage1[position+2] = (GLubyte) byteValue;
/* alpha-value */
texImage1[position+3] = (GLubyte) 255;
}
}
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
getImageFromFile ("ltgraf22.pgm");
}
void display3(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);
glRotated(5, 1, 0, 0); // x
glRotated(0, 0, 1, 0); // y
glRotated(-45, 0, 0, 1); // z
glTranslated(0, -10, 0);
for (int i = texImageHeight-1; i >= 0; i--)
{
for (int j = 0; j < texImageWidth; j++)
{
int p = (i*texImageWidth+j)*4;
int pi1 = ((i+1)*texImageWidth+(j))*4;
int pj1 = ((i)*texImageWidth+(j+1))*4;
glLineWidth(1);
glBegin(GL_LINES);
glVertex3d(j-50, i-50, -texImage1[p]+110);
glVertex3d(j+1-50, i-50, -texImage1[pj1]+110);
glEnd();
glBegin(GL_LINES);
glVertex3d(j-50, i-50, -texImage1[p]+110);
glVertex3d(j-50, i-50+1, -texImage1[pi1]+110);
glEnd();
}
}
glutSwapBuffers();
}
/****************************************************************************
* Reshape 3D windows
****************************************************************************/
void resize3D (int w, int h)
{
int w2 = w / 100;
int h2 = h / 100;
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
/* Original size of objects will be kept. */
glOrtho(-80, 80, -80, 80, 1, 146);
if ( w2 == h2)
pointsize = w2;
glFlush ();
}
/* Window 3d wire */
glutInitWindowSize(WINSIZE_X*2, WINSIZE_Y*2);
glutInitWindowPosition(WINPOS_X-WINSIZE_X*2-30, WINPOS_Y+WINSIZE_Y+30);
glutCreateWindow("3D gray");
init2();
glutDisplayFunc(display3);
glutKeyboardFunc(keyboard3D);
glutReshapeFunc(resize3D);