Sparky
Sparky

Reputation: 152

Odd happenings of OpenGL Texturing

I am fairly certain my textures are loaded without problem, however, I cannot get them to render!

I enable GL_TEXTURE_2D in when I initialize GL. I then load the texture with this function:

GLuint loadTexture(std::string filepath)
{
    SDL_Surface *image;
    if ( image = SDL_LoadBMP( filepath.c_str() ) )
    {
        GLuint tex_id;
        glGenTextures( 1, &tex_id );
        glBindTexture( GL_TEXTURE_2D, tex_id );

        glTexImage2D(   GL_TEXTURE_2D, 0, 3, image->w,
                        image->h, 0, GL_RGB,
                        GL_UNSIGNED_BYTE, image->pixels );

        SDL_FreeSurface(image);

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

        textures.emplace(getFilename(filepath), tex_id);

        glBindTexture(GL_TEXTURE_2D, 0);

        return tex_id;
    }
    else
    {
        // std::cout << "[!!] Could not open " << filepath << "! \n";
        return -1;
    }
}

The texture is loaded, and then the texture name is placed in a map with the file name as a key. The map is a global defined as std::map <std::string, GLuint> textures. I can iterate through the map and verify that GL is assigning texture names properly. Finally, I go on to draw my quads as such:

glBindTexture(GL_TEXTURE_2D, textures["MY_IMAGE.BMP"]);

glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex3d(0.0,  0.0, iNode->verticies[1]);

glTexCoord2f(1.0, 0.0);
glVertex3d(1.0,  0.0, iNode->verticies[2]);

glTexCoord2f(1.0, 1.0);
glVertex3d(1.0,  1.0, iNode->verticies[3]);

glTexCoord2f(0.0, 1.0);
glVertex3d(0.0,  1.0, iNode->verticies[4]);

glEnd();

A few things worth noting:

Is the texture being somehow deleted internally? Am I doing something wrong with my rendering?

I am willing to supply more code if needed.

EDIT: I have no idea what I did... Now my renders look like this: enter image description here

Upvotes: 4

Views: 135

Answers (1)

kakTuZ
kakTuZ

Reputation: 562

From the SDL_LoadBMP documentation:

Note: When loading a 24-bit Windows BMP file, pixel data points are loaded as blue, green, red, and NOT red, green, blue (as one might expect).

Therefore the format parameter provided to glTexImage2D needs to be GL_BGR.

Upvotes: 1

Related Questions