PDA

View Full Version : Texture looks strange?



tommed
01-06-2011, 11:34 AM
Hi Guys,

I have a function for loading PPM images, the image loaded into GIMP looks like this:

http://dl.dropbox.com/u/577250/forums/opengl-shot1.png

But when it's running in my opengl app, it looks like this:

http://dl.dropbox.com/u/577250/forums/opengl-shot2.png

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\n", 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", &amp;w, &amp;h, &amp;d);
break;
case 1:
i += sscanf(head, "%d %d", &amp;h, &amp;d);
break;
case 2:
i += sscanf(head, "%d", &amp;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\n", 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!!

mobeen
01-06-2011, 09:04 PM
Hi,
Call


glPixelStorei(GL_UNPACK_ALIGNMENT,1);


before you call glTexParameteri funcs.
Reason: Opengl's default alignment is 4 bytes. if say your image has a width of 397, then in each line three bytes will be added to make it divisible by 4. Calling the above func. tells opengl that your data is tightly packed.
Source: Opengl super bible 5th ed. (page 182).

tommed
01-07-2011, 04:39 AM
Brilliant, works a charm! Thank you very much! :)