SDL/OpenGL Texture Padding

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

SDL/OpenGL Texture Padding

Post by JesseGuarascia »

So I've run into a bit a predicament. My friend and I are working on a new game, and I have to do all of the low level graphics stuff. Sadly, he's running an outdated graphics card. Thus, I have to write all of this padding code for non 2^n textures. My main problem is just taking the texture and padding it. Everything works fine, except for this function.

Code: Select all

SDL_Surface *Img::GetPaddedImage(SDL_Surface *image, Rect rect){
	// Generate a padded image using the given one
	SDL_Surface *temp = SDL_CreateRGBSurface(SDL_SWSURFACE, (int)rect.Width(), (int)rect.Height(), image->format->BitsPerPixel,
											  image->format->Rmask, image->format->Gmask, 
											  image->format->Bmask, image->format->Amask);

	SDL_FillRect(temp, NULL, 0xFF00FF);
	SDL_BlitSurface(image, NULL, temp, NULL);

	return temp;
}
This, along with some other stuff I don't feel necessary to show, are used to get a padded texture.

The code above just results in a blank image with no channels being rendered.

For the purposes of showing how textures are created (just in case), heres my Texture generation code:

Code: Select all

void Texture2D::Gen(SDL_Surface *sur){
	// Generate the texture using the image given
	glGenTextures(1, &tex);

	glBindTexture(GL_TEXTURE_2D, tex);

	GLenum texFormat = GL_RGBA; // Format used for the texture

	// Make sure the dimensions are of power of two
	if (!isPowerOfTwo(sur->w) || !isPowerOfTwo(sur->h)){
		pad.SetSize((float)sur->w, (float)sur->h);
		padded = true;

		sur = Img::GetPaddedImage(sur, Rect(0.0f, 0.0f, 
											(float)NextPowerOfTwo(sur->w),
											(float)NextPowerOfTwo(sur->h)));
	}

	// Decide which format to use
	if (sur->format->BytesPerPixel == 4){
		texFormat = GL_RGBA;
	} else if (sur->format->BytesPerPixel == 3){
		texFormat = GL_RGB;
	}

	// Create the texture
	glTexImage2D(GL_TEXTURE_2D, 0, 4, sur->w, sur->h, 
				0, texFormat, GL_UNSIGNED_BYTE, sur->pixels);

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

	// Get the sizes of the texture
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texWidth);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texHeight);

	DebugSys::Inst()->DebugAsset(this);
}
Any help/suggestions would be greatly appreciated :).
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

Re: SDL/OpenGL Texture Padding

Post by bnpph »

I don't exactly get what you're trying to do.

If you want to use non power of 2 textures, just gen texture that is next largest power of 2, then upload it with glTexSubImage2D.
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: SDL/OpenGL Texture Padding

Post by JesseGuarascia »

Yeah, but when I try to do the padding, the image turns out completely blank, and nothing renders.
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

Re: SDL/OpenGL Texture Padding

Post by bnpph »

Load the image with same code as you do for power of 2 textures.
The only thing that will be different is your OpenGL code.
Post Reply