OpenGL Texture Loading issue with images that Crossed each other

Viewed 57

Problem Image:

Problem Image

I found some depart from image. I Think Texture location is problem, But I can't Solve this issue

My Code :

GLuint LoadTexture(const char * filename, int width, int height)

GLuint texture;
unsigned char * data;
FILE * file;

file = fopen(filename, "r");

if (file == NULL) return 0;

data = (unsigned char *)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);

// RGB
for (int i = 0; i < width * height; ++i)
{
    int index = i * 3;
    unsigned char B, R;
    B = data[index];
    R = data[index + 2];

    data[index] = R;
    data[index + 2] = B;
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); // GL_REPLACE에서 교체했음
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // GL_CLAMP
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

return texture;

and

    glBindTexture(GL_TEXTURE_2D, texture_image[2]);
    glBlendFunc(GL_ONE, GL_ONE);
    glBegin(GL_QUADS);
    glTexCoord2i(0, 0); glVertex3f(-32, -32, 0);
    glTexCoord2i(0, 1); glVertex3f(-32, 32, 0);
    glTexCoord2i(1, 1); glVertex3f(32, 32, 0);
    glTexCoord2i(1, 0); glVertex3f(32, -32, 0);
    glEnd();

This is the My first question here I dunno this is correct, but thank you.

2 Answers

A Windows Bitmap file has a file header of 54 bytes. I this header is the format of the file encoded.

If you know, that the file format is 24 bit RGB, then you can simply skip the header.

data = (unsigned char *)malloc(width * height * 3 + 54);
fread(data, width * height * 3 + 54, 1, file);
fclose(file);

.....

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data+54);

Further note, by default, each row of a texture is assumed to be aligned to 4 bytes.
At a 24bit RGB BMP image, each texel needs 24 bits or 3 byte and the pixels are tightly packed especially the rows of the texture. This means that the alignment of 4 bytes for the start of a line of the texture is disregarded (except 3 times the width of the texture is divisible by 4 without a remaining).

This not any problem in your case because you texture has a with of 64 pixel and 64*3 is dividable by 4. But if the number of bytes per row would not be dividable by 4, you would have to change the alignment to 1.

This means the GL_UNPACK_ALIGNMENT parameter has to be set to 1, before loading a tightly packed texture to the GPU (glTexImage2D).
Otherwise an offset of 0-3 bytes per line is gained, at texture lookup. This would causes a continuously twisted or tilted texture.

glPixelStore(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data+54);

One needs a naked GL_UNSIGNED_BYTE GL_RGB image, and that's probably not what that image is. OpenGL does not provide any file format support at all. You could save file as such using, eg, GIMP, but that will be a large file, (edit: uncompressed bitmap 24BPP, once you get past the BMP header, is one such format.) A more sustainable solution is to save the file in a known format and use a library to decompress it. Some of my favourites are,

Related