Jump to content
  • Advertisement
Sign in to follow this  
Benjamin Heath

OpenGL What once was blue is red instead (solved)

This topic is 4581 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 loading an image to display as a background, and I'm using OpenGL via SDL. The image is loaded via SDL_image, and converted to the display format before a texture object is created out of it. All of this is encapsulated into a class called Surface. The problem? The colors do not look right. I'm trying to load this van Gogh painting. Note the blue sky in the background. Well, when I load it, it looks red. Something is wrong with the pixel format here. Here's the relevant code for that:
Surface::Surface(std::string filename)
:	tex(0), w(0), h(0), nRefs(1)
{
	InitPosition();

	std::cout << "Loading surface from file \"" << filename << "\"." << std::endl;


	// Step 1: load image from file into fileSurface
	SDL_Surface *fileSurface = IMG_Load(filename.c_str());
	if (fileSurface == NULL)
	{
		// report error
		std::cout << "Couldn't load " << filename << ": " << IMG_GetError() << std::endl;
	}

	// Step 2: convert fileSurface to display format
	SDL_Surface *formattedSurface = SDL_DisplayFormat(fileSurface);
	if (formattedSurface == NULL)
	{
		// report error
		std::cout << "Couldn't convert surface to display format: " << SDL_GetError() << std::endl;
	}

	// Step 3: free fileSurface and make a texture out of formattedSurface
	SDL_FreeSurface(fileSurface);
	fileSurface = NULL;
	w = (GLsizei)formattedSurface->w; // these are some members for this class
	h = (GLsizei)formattedSurface->h;

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, formattedSurface->pixels);
	SDL_FreeSurface(formattedSurface);
	formattedSurface = NULL;
}


Note that I'm using GL_RGBA as the format. Should I be doing anything else, is there something I need to change? What can you see is wrong? Thanks ahead of time, Benji PS: Actually, having the blue sky look red looks pretty dang cool [smile], but a problem's a problem, and I'd like to fix it. [Edited by - Benjamin Heath on May 5, 2006 8:48:07 PM]

Share this post


Link to post
Share on other sites
Advertisement
Could be that the data is in BGRA format instead. I had that issue 6 years ago when I was doing video acquisition.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Could be that the data is in BGRA format instead. I had that issue 6 years ago when I was doing video acquisition.
That was my first guess as well, but then everything is solid white. :(

Share this post


Link to post
Share on other sites
Problem Solved!

Actually, you had it right, Fruny. :P The problem was that I was doing this:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, formattedSurface->pixels);


Well, for one thing, I'm passing GL_RGBA twice and not telling how many components there are. Secondly, GL_RGBA, as you said Fruny, is incorrect. I changed that line as such to this:

glTexImage2D(GL_TEXTURE_2D, 0, 3, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, formattedSurface->pixels);


Works great now. :)

Thanks again.

Share this post


Link to post
Share on other sites
Actually the 3rd parameter should indeed be GL_RGBA, or even more preferably, shoudl be specific internal format such as GL_RGBA8. The 3 works as GL_RGB for backward compatibility reason, IIRC.

Share this post


Link to post
Share on other sites
Quote:
Original post by deavik
Actually the 3rd parameter should indeed be GL_RGBA, or even more preferably, shoudl be specific internal format such as GL_RGBA8. The 3 works as GL_RGB for backward compatibility reason, IIRC.
That seems to work well. Thank you.

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!