SDL_image to OpenGL texture

Started by
1 comment, last by V-man 15 years, 9 months ago
How to load OpenGL texture using SDL_image? If you can, please give me a code sample :)
Advertisement
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

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

//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
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement