Hi Guys,
I have a function for loading PPM images, the image loaded into GIMP looks like this:
But when it’s running in my opengl app, it looks like this:
What have I done wrong…?
I got the code for loading PPM images from another web site, it looks like this:
int environ_load_texture_ppm(const char *filename, GLuint *texid) {
FILE* fp;
int i, w, h, d;
unsigned char* image;
char head[70]; /* max line <= 70 in PPM (per spec). */
fp = fopen(filename, "rb");
if (!fp) {
perror(filename);
return -1;
}
/* grab first two chars of the file and make sure that it has the
correct magic cookie for a raw PPM file. */
fgets(head, 70, fp);
if (strncmp(head, "P6", 2)) {
fprintf(stderr, "%s is not a raw PPM file
", filename);
return -1;
}
/* grab the three elements in the header (width, height, maxval). */
i = 0;
while (i < 3) {
fgets(head, 70, fp);
if (head[0] == '#') {
/* skip comments. */
continue;
}
switch (i) {
case 0:
i += sscanf(head, "%d %d %d", &w, &h, &d);
break;
case 1:
i += sscanf(head, "%d %d", &h, &d);
break;
case 2:
i += sscanf(head, "%d", &d);
break;
}
}
/* grab all the image data in one fell swoop. */
image = (unsigned char*)malloc(sizeof(unsigned char)*w*h*3);
fread(image, sizeof(unsigned char), w*h*3, fp);
fclose(fp);
/* Bind */
glGenTextures(1, texid);
glBindTexture(GL_TEXTURE_2D, *texid);
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
/* Set parameters */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* Build mipmaps */
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB,
w, h,
GL_RGB, GL_UNSIGNED_BYTE,
image);
free(image);
printf("loaded %s as a texture
", filename);
return 0;
}
Then, when I want to draw the texture to a quad surface (whilst in ortho projection), I do this:
glPolygonMode(GL_FRONT, GL_FILL);
glColor3f(1, 1, 1);
glTranslatef(width-294-20, height-64-20, 0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureBanner);
glBegin(GL_QUADS);
{
glTexCoord2f(0, 1); glVertex2f(0, 0);
glTexCoord2f(1, 1); glVertex2f(294, 0);
glTexCoord2f(1, 0); glVertex2f(294, 64);
glTexCoord2f(0, 0); glVertex2f(0, 64);
}
glEnd();
glDisable(GL_TEXTURE_2D);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
What’s wrong with this? Many thanks!!