# 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.

## 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
if (fileSurface == NULL)
{
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)
{
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 on other sites
Could be that the data is in BGRA format instead. I had that issue 6 years ago when I was doing video acquisition.

##### Share on other sites
Quote:
 Original post by FrunyCould 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 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 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 on other sites
Quote:
 Original post by deavikActually 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.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633395
• Total Posts
3011653
• ### Who's Online (See full list)

There are no registered users currently online

×