Skip to main content
gamedev.net
🔒 Locked

Loading Textures - "Negative" textures when using SDL

Started by JCK Apr 10, 2005 at 7:04 PM 13 replies 2.4k views
Original Post
JCK
JCK
Yeah, it's me again. LOL Not quite sure if this belongs in an SDL forum, but it does have relevance to OGL. I am getting having texture problems when loading them with SDL. Source:
SDL_Surface *LoadBMP(const char *Filename)							// Loads A Bitmap Image
{
	FILE *File=NULL;												// File Handle

	if (!Filename)													// Make Sure A Filename Was Given
	{
		return NULL;												// If Not Return NULL
	}

	File=fopen(Filename,"r");										// Check To See If The File Exists

	if (File)														// Does The File Exist?
	{
		fclose(File);												// Close The Handle
		return SDL_LoadBMP(Filename);							    // Load The Bitmap And Return A Pointer
	}

	return NULL;													// If Load Failed Return NULL
}

GLuint LoadGLTexture( const char *filename )						// Load Bitmaps And Convert To Textures
{
	SDL_Surface *pImage = LoadBMP(filename);						// Create Storage Space For The Texture
	GLuint textureID = 0;											// Texture ID

	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
	if ( pImage != NULL && pImage->pixels != NULL)					// If Texture Image Exists
	{

		// Typical Texture Generation Using Data From The Bitmap
		glGenTextures(1, &textureID);								// Create The Texture
		glBindTexture(GL_TEXTURE_2D, textureID);

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

		glTexImage2D(GL_TEXTURE_2D, 0, 3, pImage->w, pImage->h, 0, GL_RGB, GL_UNSIGNED_BYTE, pImage->pixels);

		SDL_FreeSurface(pImage);									// Free The Image Structure
	}

	return textureID;												// Return The Status
}
The textures are coming out reversed, like a photo negative. What am I doing wrong?
Drew_Benton
Drew_Benton
Quote:
Original post by Captain Tex
Try changing GL_RGB to GL_BGR.


I think that would have to be GL_BGR_EXT [wink], but you are indeed right I believe. There was an article on doing this a while back. Here it is for additional references. (Esp, when the texture is displayed mirrored and flipped)
Drew_Benton
Drew_Benton
Quote:
Original post by Captain Tex
Why the EXT?


I can't explain it, but I don't think it will compile without the _EXT. I will have to check to be 100% sure, but in just about everything it says you need to use the EXT.
Quote:
In order for OpenGL to understand BGR use GL_BGR_EXT instead of GL_RGB in your call to glTexImage2D or gluBuild2DMipmaps:

That was one example from here. IIRC from my own experiences, I've had to use EXT to get it to compile with Windows. That's what this tutorial did as well.
Ademan555
Ademan555
To maintain sanity on the openGL side, is there a way to make SDL flip the Red and Blue bytes (i would reccomend that rather than relying on an extension)? I would assume there is

(im actually just beggining to use SDL myself because i was tired of writing a window framework that would become totally useless on another platform lol)

i may look into making SDL flip the bytes myself, but if i dont edit this, you might as well look yourself

cheers
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
_the_phantom_
_the_phantom_
its EXT as it wasnt present in core OpenGL1.1, however it was later added to the core, so if you have an upto date extension header it should show it as both GL_BGR_EXT and GL_BGR, both of which have the same values (GLee for example has both)

It should be possible to flip the bits in SDL, but I wouldnt worry too much about the extension not being supported as it became 'core' some time ago and even if the gfx card doesnt natively support BGR it will just fiddle it on upload to an internal format it can handle
JCK
JCK
You know, you guys are da best! I <3 teh OGL forums
AndyTX
AndyTX
As extra info, you CAN get SDL to flip the order for you (from "whatever it is" to a target format) using "SDL_ConvertSurface".

For example something like this code should do you nicely and work cross-platform (and also with any file format if you're loading with SDL_image or equivalent):

SDL_PixelFormat RGBA8Format;memset(&RGBA8Format, 0, sizeof(SDL_PixelFormat));RGBA8Format.BitsPerPixel = 32;#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */	RGBA8Format.Rmask = 0x000000FF;	RGBA8Format.Gmask = 0x0000FF00;	RGBA8Format.Bmask = 0x00FF0000;	RGBA8Format.Amask = 0xFF000000;#else	RGBA8Format.Rmask = 0xFF000000;	RGBA8Format.Gmask = 0x00FF0000;	RGBA8Format.Bmask = 0x0000FF00;	RGBA8Format.Amask = 0x000000FF;#endif...SDL_Surface *Convert = SDL_ConvertSurface(Image, &RGBA8Format, 0);glPixelStorei(GL_UNPACK_ALIGNMENT, 1);gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8,  Convert->w, Convert->h, GL_RGBA, GL_UNSIGNED_BYTE, Convert->pixels);SDL_FreeSurface(Convert);


Hope that helps.
Captain Tex
Captain Tex
Quote:
Original post by Drew_Benton
Quote:
Original post by Captain Tex
Why the EXT?


I can't explain it, but I don't think it will compile without the _EXT. I will have to check to be 100% sure, but in just about everything it says you need to use the EXT.


fwiw, I use it without the EXT and it works fine.
tolaris
tolaris
Well, the BGRA formats made it to the core specifications with version 1.2:

"Appendix D Version 1.2

D.2 BGRA Pixel Formats
BGRA extends the list of host-memory color formats. Specifically, it provides a
component order matching file and framebuffer formats common on Windows platforms.
The additions match those of the EXT_bgra extension."

and the new formats are listed in table 3.6 (DrawPixels and ReadPixels formats) as "BGR" and "BGRA" ... so i guess you're pretty safe to use the version without _EXT on anything moderately up to date.
zedzeek
zedzeek
theyre exactly the same

#define GL_BGR_EXT 0x80E0
#define GL_BGR 0x80E0

EXT stands for extension, as has been mentioned since its part of the core now the ext has been dropped same with
GL_TEXTURE0_ARB -> GL_TEXTURE0 or
glDrawBuffersARB -> glDrawBuffers
i myself also use the shorter version (less typing and looks cleaner)
Drew_Benton
Drew_Benton
Yea, I think it was just the last time that I used the GL_BGR_EXT, the GL_BGR was not defined for OpenGL on the Windows platform. That or I was just too dumb to get it to work [grin] Thanks for all the research guys! I'll give this a try a bit later.
Simian Man
Simian Man
If you inclue instead of you don't have to worry about extensions!
JCK
JCK
Quote:
Original post by Simian Man
If you inclue instead of you don't have to worry about extensions!


I do indeed use the SDL header for OGL since it lets me get rid of windows.h and I also dont' have to include GLU, only have to link to the OGL libs ^_^. Very handy.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.