Jump to content
  • Advertisement
Sign in to follow this  
LewsTherin

Problems converting SDL_Surface to OpenGL texture

This topic is 4944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to load a 128x256 image using SDL_image and then display it mapped to a quad. The current conversion code I'm using just gives me a completely black quad, here it is:
/* Convert an SDL_Surface to an OpenGL texture */
void texFromSurface( GLuint *texture, SDL_Surface* textureImage ) {
	if (textureImage) {
		glGenTextures( 1, texture );
		glBindTexture( GL_TEXTURE_2D, *texture );
		glTexImage2D( GL_TEXTURE_2D, 0, 3, textureImage->w, textureImage->h, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, textureImage->pixels );
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	}
}
What makes me think this code is wrong is that sometimes the quad is green or blue, when I use different colors in the texture.

Share this post


Link to post
Share on other sites
Advertisement
SiCrane, yes. Just tested that with printf().

mike25025: I'm pretty sure. The textures are mapped with this:

glTexCoord2d(0,0); glVertex3f(0.25f, 0, 0);
glTexCoord2d(0,1); glVertex3f(-0.25f, 0, 0);
glTexCoord2d(1,1); glVertex3f(-0.25f, 1, 0);
glTexCoord2d(1,0); glVertex3f(0.25f, 1, 0);

Share this post


Link to post
Share on other sites
SDL doesn't convert the image data when you load it... so you'll get different byte orders and so on depending on what endianness your system is, and what file type you load from. The chance of that directly corresponding to whatever OpenGL pixel format you decided upon is low.

So you can go down 1 of 2 routes:

1) check the image->format->Rmask/Gmask/Bmask etc, then choose the OpenGL format for glTexImage2D accordingly;
2) Create a new surface of the required format using SDL_CreateRGBSurface, then blit the loaded surface to that (letting SDL do the conversion internally), then call glTexImage2D on that copy.

Route 1 is faster but more complex. But if you're limited to 24bpp surfaces you should be ok. Try adding this:

GLenum externalFormat;
if (textureImage->format->Rmask == 0xff)
externalFormat = GL_RGB;
else
externalFormat = GL_BGR_EXT;


Then pass externalFormat to glTexImage2D instead of GL_BGR_EXT.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!