11 Replies - 2052 Views - Last Post: 20 April 2011 - 05:59 AM Rate Topic: -----

#1 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

SDL opengl colour problem

Posted 19 April 2011 - 08:19 PM

I am sorry for posting this in game programming, but out of all of the forums this seemed the most appropriate, and I couldn't think of where it should properly go. But hey

My problem is like this: When I load a PNG image with the SDL_image library and try and bind it to opengl, the colours go strange. I am not sure what is causing this, but I will post the loading code in case there is something to spot

SDL_Surface * temp = IMG_Load("example.png");
SDL_Surface * image = SDL_DisplayFormatAlpha(temp);
SDL_FreeSurface(temp);
temp = 0;

GLuint texture;
GLuint nOfColours = image->format->BytesPerPixel;
GLenum textureFormat;

if(nOfColours == 3)
{
    textureFormat = GL_RGB;
}
else
{
    textureFormat = GL_RGBA;
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

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

glTexImage2D(GL_TEXTURE_2D, 0, nOfColours, image->w, image->h, 0, textureFormat, GL_UNSIGNED_BYTE, image->pixels);



I hope someone can shed light on this problem, because today google has failed me, and experimenting has left me in the same mess no closer to the answer :(

Is This A Good Question/Topic? 0
  • +

Replies To: SDL opengl colour problem

#2 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SDL opengl colour problem

Posted 20 April 2011 - 02:06 AM

How the colors go strange can tell you what the problem could be. Can you post an image? :)
Was This Post Helpful? 0
  • +
  • -

#3 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: SDL opengl colour problem

Posted 20 April 2011 - 03:15 AM

I just created this to show the difference. This is a before and after shot of a new image. As you can see red seems to be the only colour there
Posted Image

This post has been edited by sk1v3r: 20 April 2011 - 04:04 AM

Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: SDL opengl colour problem

Posted 20 April 2011 - 03:59 AM

GLuint TextureID = 0;

SDL_Surface* pSurface = IMG_Load("yourimage.png");

glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);

int Mode = GL_RGB;

if(pSurface->format->BytesPerPixel == 4) {
    Mode = GL_RGBA;
}

glTexImage2D(GL_TEXTURE_2D, 0, Mode, pSurface->w, pSurface->h, 0, Mode, GL_UNSIGNED_BYTE, pSurface->pixels);

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



This is how I do it in my code, maybe you could compare :)

This post has been edited by stayscrisp: 20 April 2011 - 04:00 AM

Was This Post Helpful? 1
  • +
  • -

#5 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: SDL opengl colour problem

Posted 20 April 2011 - 04:29 AM

I made my code practically identical to yours for the sake of trying, but no dice. I then messed about with a PNG to see the results, here are all three images. The first is the image before being put into the program. The second is it being shown in the program with IMG_load and SDL_DisplayFormatAlpha, and the third is just the image being loaded with IMG_Load.

White in here denotes transparency.

Posted Image

I am wondering if it is my OS? I am using Ubuntu if that helps in any way.
Or it could be my initializing.


EDIT: I have just been experimenting and found that there is also a problem when I load shapes without pngs aswell. Sorry for not noticing that earlier. It appears that it is all of my colours that are screwing up :(

EDIT2: When I stop showing shapes without img's the png works (albeit without transparency).
I think I have found the reason for it aswell.

I am drawing the random rectangles that I want to appear after I draw my image. I guess as I was drawing a red rectangle it affected the image as it was still bound I believe. Sorry for the inconvenience

This post has been edited by sk1v3r: 20 April 2011 - 04:36 AM

Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: SDL opengl colour problem

Posted 20 April 2011 - 05:25 AM

I don't know if it would help but you could try using GL_BGRA as opposed to GL_RGBA to test if it is your OS as I don't know what the endianness of your system is.

*edit*
Actually there is an SDL way of checking a systems endianness

SDL_BYTE_ORDER



You can compare it like so
if(SDL_BYTE_ORDER == SDL_BIG_ENDIAN)



so if it's big endian you can use BGR and if it's little endian use RGB.
SDL saves the day again :)

This post has been edited by stayscrisp: 20 April 2011 - 05:34 AM

Was This Post Helpful? 1
  • +
  • -

#7 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: SDL opengl colour problem

Posted 20 April 2011 - 05:30 AM

Nevermind, Problem solved :)
The simplest solutions are always the ones you spend hours on :P
Turns out all I had to do was glColor3f(1.0f, 1.0f, 1.0f); before I Drew the vertexes in the quad. That took too long :P
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: SDL opengl colour problem

Posted 20 April 2011 - 05:36 AM

Oh well, at least you can use the SDL_BYTE_ORDER function to make your code more portable and robust :)
Was This Post Helpful? 0
  • +
  • -

#9 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: SDL opengl colour problem

Posted 20 April 2011 - 05:42 AM

:)

Quick question, with your loading, do you get transparency?
Was This Post Helpful? 0
  • +
  • -

#10 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: SDL opengl colour problem

Posted 20 April 2011 - 05:45 AM

You have to use OpenGL's blend functions to use transparency.

Something like this:

glEnable(GL_BLEND); 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


Was This Post Helpful? 1
  • +
  • -

#11 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: SDL opengl colour problem

Posted 20 April 2011 - 05:50 AM

brilliant !
Thank you so much :D
Was This Post Helpful? 0
  • +
  • -

#12 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: SDL opengl colour problem

Posted 20 April 2011 - 05:59 AM

No problem :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1