0
votes

i have a simple plain (square) with lighting and material on it, i want to put an earth texture on it. but nothing appears. I've checked, and think it loads the RGB data correctly so my guess is that there is a problem with this line (data is *char and the image is BMP24)

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)data);

here is a .bmp load function:

GLuint LoadTexture(const char* imagepath) {
    printf("Reading image %s\n", imagepath);
    unsigned int outWidth = -1;
    unsigned int outHeight = -1;
    unsigned char header[54];
    unsigned int dataPos;
    unsigned int imageSize;

    FILE* file;
    if (errno_t err = fopen_s(&file, imagepath, "rb") != 0) {
        perror("Can't Open file");
        return 0;
    }

    // If less than 54 byes are read, problem
    if (fread(header, 1, 54, file) != 54) {
        printf("Not a correct BMP file\n");
        return NULL;
    }
    // A BMP files always begins with "BM"
    if (header[0] != 'B' || header[1] != 'M') {
        printf("Not a correct BMP file\n");
        return NULL;
    }
    // Make sure this is a 24bpp file
    if (*(int*)&(header[0x1E]) != 0) { printf("Not a correct BMP file\n");    return NULL; }
    if (*(int*)&(header[0x1C]) != 24) { printf("Not a correct BMP file\n");    return NULL; }

    // Read the information about the image
    dataPos = *(int*)&(header[0x0A]);
    outWidth = *(int*)&(header[0x12]);
    outHeight = *(int*)&(header[0x16]);
    imageSize = *(int*)&(header[0x22]);

    // Some BMP files are misformatted, guess missing information
    if (imageSize == 0)    imageSize = outWidth * outHeight * 3; // 3 : one byte for each Red, Green and Blue component
    if (dataPos == 0)      dataPos = 54; // The BMP header is done that way

    // Read the actual data from the file into the buffer
    unsigned char* data = new unsigned char[imageSize];
    fread(data, 3, imageSize, file);


    // Everything is in memory now, the file wan be closed
    fclose(file);

    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)data);

    return tex;
}

here's the main and display function:

void render(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    float ratio = 0.75;
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, earthTextureData);

    glBegin(GL_QUADS);
    glVertex3f(-0.5, -0.5, 0);
    glVertex3f(-0.5, 0.5, 0);
    glVertex3f(0.5, 0.5, 0);
    glVertex3f(0.5, -0.5, 0);
    glEnd();

    glDisable(GL_TEXTURE_2D);

    glutSwapBuffers();
}


int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(1200, 800);
    glutCreateWindow("app");

    init(); // custome initilizing
    initLighting();

    earthTextureData = LoadTexture("Texture/globe.bmp");

    glutDisplayFunc(render);
    glutReshapeFunc(reshape);
    glutMotionFunc(mouseMove);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);

    glutMainLoop();
}

I've played around with the code, and sometimes (when i use int instead of char*) i get a red material covering everything but don't know exactly what causes it

UPDATE: had to change a few lines to make it work (for bmp24 files):

1-edit this line: fread(data, 1, imageSize, file);

2- add this line: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

3- edit this line (for colors to show properly): glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, (GLvoid*)data);

4-add UV coordinates (lookup glTexCoord)

2

2 Answers

0
votes

You missed to set the Set the texture parameters by glTexParameter.

If you do not generate mipmaps (by glGenerateMipmap), then setting the GL_TEXTURE_MIN_FILTER is important. Since the default filter is GL_NEAREST_MIPMAP_LINEAR the texture would be mipmap incomplete, if you don not change the minifying function to GL_NEAREST or GL_LINEAR.

When the image is loaded to a texture object, then GL_UNPACK_ALIGNMENT has to be set to 1.
By default GL_UNPACK_ALIGNMENT is 4, so each line of an image is assumed to be aligned to 4 bytes. The pixels of a BMP-file in common have a size of 3 bytes and are tightly packed, this would cause a misalignment.

GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)data);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

Of course you have to set the texture parameter by glTexCoord, before specifying the vertex coordinate (glVertex) as mention in the other answer:

glBindTexture(GL_TEXTURE_2D, earthTextureData);

glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);

glTexCoord2f(0.0, 1.0);
glVertex3f(-0.5, 0.5, 0);

glTexCoord2f(1.0, 1.0);
glVertex3f(0.5, 0.5, 0);

glTexCoord2f(1.0, 0.0);
glVertex3f(0.5, -0.5, 0);

glEnd();
glDisable(GL_TEXTURE_2D);
0
votes

You have to tell OpenGL the UV coordinates on where and how to place that texture. This can be done in your render function.

UV coordinates range from 0.0 - 1.0 and tell OpenGL on how to place the texture onto a render.

Here is a good picture that illustrates uv coordinates well

Something like this should work:

void render(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    float ratio = 0.75;
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, earthTextureData);

    glBegin(GL_QUADS);
    glTexCoord3f(0.0, 0.0, 0.0);
    glVertex3f(-0.5, -0.5, 0);

    glTexCoord3f(0.0, 1.0, 0.0);
    glVertex3f(-0.5, 0.5, 0);

    glTexCoord3f(1.0, 1.0, 0.0);
    glVertex3f(0.5, 0.5, 0);

    glTexCoord3f(1.0, 0.0, 0);
    glVertex3f(0.5, -0.5, 0);
    glEnd();

    glDisable(GL_TEXTURE_2D);

    glutSwapBuffers();
}