SDL Image Resizing

May 14, 2012 at 12:51pm
I'm attempting to resize (not "stretch", you'll see what I mean in the code below) an SDL_Surface such that it has dimensions that conform to a power of 2. When attempting to render this texture using OpenGL, no image appears on the screen. The rendering code works fine with textures that haven't been touched by the resizing code, but otherwise there is no change to the contents of the screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Resize, if necessary
if (m_Width != m_TexWidth || m_Height != m_TexHeight)
{
	SDL_Surface * newSurface =
		SDL_CreateRGBSurface (SDL_SWSURFACE, m_TexWidth, m_TexHeight,
		m_pImage->format->BitsPerPixel,
		m_pImage->format->Rmask, m_pImage->format->Gmask, m_pImage->format->Bmask, m_pImage->format->Amask);

	SDL_BlitSurface (m_pImage, NULL, newSurface, NULL);

	SDL_FreeSurface (m_pImage);
	m_pImage = newSurface;
}


Does anyone see anything wrong here?
May 15, 2012 at 8:44pm
No takers?
May 15, 2012 at 8:47pm
I don't see anything wrong with the code you have posted. Are you sure the problem is with this code?

Somewhere you have to convert the m_pImage surface to an OpenGL texture. Are you doing that correctly?
May 15, 2012 at 9:25pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
m_pImage = IMG_Load (C_STRING (fname));
if (m_pImage == NULL)
	ore::Diagnostics::Warning (true)
		.Append ("texture.cpp | CTexture::LoadFromFile - Unable to load file ")
		.Append (fname)
		.Display ();

//	... Eh
//	TODO: Make format detection less "... Eh"
int imgFormat;
if (m_pImage->format->BitsPerPixel == 32)
	imgFormat = GL_RGBA;
else
	imgFormat = GL_RGB;

m_Width = m_pImage->w;
m_Height = m_pImage->h;

m_TexWidth = powf (2, ceilf (log2 (m_pImage->w)));
m_TexHeight = powf (2, ceilf (log2 (m_pImage->h)));

//	Resize, if necessary
if (m_Width != m_TexWidth || m_Height != m_TexHeight)
{
	SDL_Surface * newSurface =
		SDL_CreateRGBSurface (SDL_SWSURFACE, m_TexWidth, m_TexHeight,
			m_pImage->format->BitsPerPixel,
			m_pImage->format->Rmask, m_pImage->format->Gmask, m_pImage->format->Bmask, m_pImage->format->Amask);

	SDL_BlitSurface (m_pImage, NULL, newSurface, NULL);

	SDL_FreeSurface (m_pImage);
	m_pImage = newSurface;
}

glGenTextures (1, &m_Texture);
glBindTexture (GL_TEXTURE_2D, m_Texture);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//	Interpolation method for scaling
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// the texture wraps over at the edges (repeat)
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// For good measure
SDL_LockSurface (m_pImage);
Uint8 * data = (Uint8 *)m_pImage->pixels;

glTexImage2D (GL_TEXTURE_2D, 0, imgFormat, m_TexWidth, m_TexHeight, 0, imgFormat, GL_UNSIGNED_BYTE, data);
SDL_UnlockSurface (m_pImage);


I'm not concerned about it being a problem with image format - it would just display garbage, and I could correct that. SDL_GetError and IMG_GetError return 0-length strings, and glGetError returns GL_NO_ERROR.
Last edited on May 15, 2012 at 9:25pm
Topic archived. No new replies allowed.