SDL_image to OpenGL texture
bool SYM_TEXTURE::Load(string Filename) { //Prefix filename with texture path from engine config Filename = Engine->TexturesPath + Filename; //New SDL surface SDL_Surface *Surface; //Open the file FILE *File = fopen(Filename.c_str(), "r"); //Check if file exists if(!File) { Engine->Logger.AddEntry("Error: Could not load texture " + Filename); Engine->Console.PrintLine("> Error: Could not load texture " + Filename); return false; } //Close the file fclose(File); //Load the image Surface = IMG_Load(Filename.c_str()); //Check if image data loaded ok if(Surface == 0) { return false; } //Get dimentions Width = Surface->w; Height = Surface->h; // Check that the image's width is a power of 2 if ( (Surface->w & (Surface->w - 1)) != 0 ) { Engine->Logger.AddEntry("Warning: Non power-of-two texture loaded: " + Filename); } // Also check if the height is a power of 2 else if ( (Surface->h & (Surface->h - 1)) != 0 ) { Engine->Logger.AddEntry("Warning: Non power-of-two texture loaded: " + Filename); } //Check colour format if (Surface->format->BytesPerPixel == 4) { if (Surface->format->Rmask == 0x000000ff) { Mode = GL_RGBA; } else { Mode = GL_BGRA; } } else if (Surface->format->BytesPerPixel == 3) { if (Surface->format->Rmask == 0x000000ff) { Mode = GL_RGB; } else { Mode = GL_BGR; } } else { //Unsupported type Engine->Logger.AddEntry("Error: Unsupported texture colour format: " + Filename); SDL_FreeSurface(Surface); return false; } //Generate texure glGenTextures(1, &Texture); //Bind the texture glBindTexture(GL_TEXTURE_2D, Texture); //Copy all pixels in SDL surface to GL texture glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels); //Texture filters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST); //Generate mipmaps gluBuild2DMipmaps(GL_TEXTURE_2D, 0, Surface->w, Surface->h, Mode, GL_UNSIGNED_BYTE, Surface->pixels); //Free SDL surface SDL_FreeSurface(Surface); return true; }
Alter it to take endianness into account though.
&Texture is the GLuint.
Enjoy.
EDIT: Pasted the wrong code, fixed :P
//Generate texure glGenTextures(1, &Texture); //Bind the texture glBindTexture(GL_TEXTURE_2D, Texture); //Copy all pixels in SDL surface to GL texture glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels); //Texture filters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST); //Generate mipmaps gluBuild2DMipmaps(GL_TEXTURE_2D, 0, Surface->w, Surface->h, Mode, GL_UNSIGNED_BYTE, Surface->pixels); //Free SDL surface SDL_FreeSurface(Surface); return true;
Did you notice that this code calls glTexImage2D and also gluBuild2DMipmaps?
Also, if you want mipmaps, don't use gluBuild2DMipmaps.
http://www.opengl.org/wiki/index.php/Common_Mistakes#Creating_a_Texture
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement