One function to load image and create openGL texture from file, in C++
loadImage(string fileName, GLuint *texture) { assert(texture != NULL); SDL_Surface *surface; GLenum texture_format; GLint nOfColors; if ( (surface = IMG_Load(fileName.c_str())) ) { nOfColors = surface->format->BytesPerPixel; if (nOfColors == 4) { if (surface->format->Rmask == 0x000000ff) texture_format = GL_RGBA; else texture_format = GL_BGRA; } else if (nOfColors == 3) { if (surface->format->Rmask == 0x000000ff) texture_format = GL_RGB; else texture_format = GL_BGR; } glGenTextures( 1, texture ); // Bind the texture object glBindTexture( GL_TEXTURE_2D, *texture ); // Set the texture's stretching properties glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels ); } else { cout << "cannot load the file: " << fileName << endl; } if ( surface ) SDL_FreeSurface( surface); }
Log in to answer.
leothenerd 2:58 am on November 18, 2009