Jump to content

  • Log In with Google      Sign In   
  • Create Account


OpenGL Blending Help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 23 May 2012 - 05:17 AM

Hi Guys

I'm currently creating a menu for my project and I'm trying to get the textures to blend so i'm only left with the actual Image on the texture and not the background. The problem is the whole texture is somewhat transparent, it's not just removing the background.

My RGBA texture looks like

Posted Image

And the black background needs to be removed from the image.

I was using GL_SRC_ALPHA, GL_ONE_MINUS_SRC,ALPHA for my blend function but it wasn't blending anything. I changed to GL_ONE, GL_ONE and now i'm at where i am now.


Posted Image


You ca see the text is there but its also transparent, but the background has been removed which is good.


This is how i'm drawing my button. The world behind it is drawn after (i've tried switching order didnt change anything) and it's drawn using VBO's whereas the Buttons are drawn in immediate mode.


[left]
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);[/left]
  // get button pos
  Point3<float> pos = button->getPos();
  // get button dimensions
  int width = button->getWidth();
  int height = button->getHeight();
  // bind button texture and draw quad
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, button->getState() ? button->getDownTex()->getTextureID() : button->getUpTex()->getTextureID());
  glBegin(GL_QUADS);
   glTexCoord2i(0, 1); glVertex3f(pos.x, pos.y, 0.0f);
   glTexCoord2i(1, 1); glVertex3f(pos.x + width, pos.y, 0.0f);
   glTexCoord2i(1, 0); glVertex3f(pos.x + width, pos.y + height, 0.0f);
   glTexCoord2i(0, 0); glVertex3f(pos.x, pos.y + height, 0.0f);
  glEnd();

I'm using sdl to load the texture
SDL_Surface* image = IMG_Load(textureName);
data.m_w = image->w;
data.m_h = image->h;
data.m_bitsPerPixel = image->format->BitsPerPixel;
data.m_alpha = image->format->alpha;
int colourMode = image->format->BytesPerPixel;
if (colourMode == 4)
internalFormat = GL_RGBA;
if (colourMode == 3)
internalFormat = GL_RGB;
if (colourMode == 1)
internalFormat = GL_LUMINANCE;
if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
if (colourMode == 4)
  format = GL_BGRA;
else
  format = GL_BGR;
}
else
{
if (colourMode == 4)
  format = GL_RGBA;
else
  format = GL_RGB;
}
GLuint texture = -1; // create texture handle
glGenTextures(1, &texture);    // gen texture
bindTexture(GL_TEXTURE_2D, texture);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, image->w, image->h, 0, format, GL_UNSIGNED_BYTE, image->pixels); // normal texture

Thanks for any help.

Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7235

Like
2Likes
Like

Posted 23 May 2012 - 05:25 AM

I was using GL_SRC_ALPHA, GL_ONE_MINUS_SRC,ALPHA for my blend function but it wasn't blending anything. I changed to GL_ONE, GL_ONE and now i'm at where i am now.

Blending with GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA is the correct way. You should check your image, I think, that you don't have a proper alpha channel (alpha=0 for all pixels).

#3 Brother Bob   Moderators   -  Reputation: 8089

Like
0Likes
Like

Posted 23 May 2012 - 05:30 AM

The alpha channel isn't showing in the image you posted. What does it look like?

#4 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 23 May 2012 - 05:32 AM

Thanks for the reply, do you know how i can create the proper image with alpha in photoshop? I've done a tutorial but thats what i've been using now and it isnt working.

#5 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 23 May 2012 - 05:34 AM

The alpha channel isn't showing in the image you posted. What does it look like?


This is what it looks like in photoshop, the alpha channel is just all black, do i need to make it a mask of the upper layer?

Posted Image

#6 szecs   Members   -  Reputation: 2120

Like
1Likes
Like

Posted 23 May 2012 - 05:46 AM

Yes, you have to make a mask.

1.Copy the content of the color channel to the alpha channel.
2.Invert the color of the alpha channel, since 0 (black) is fully transparent and 255 (white, or 1.0 with float colors) is fully opaque.
3.Probably you can play around with brightness/contrast to get these two values, just make sure the background is all 0.
4.Make the color channels to be the desired color, but fill the whole rectangle with the color. Otherwise in the game, you will have a small blackish hollow around the text.

I hope that's clear, the photoshop screenshot tells me that this should work. EDIT: some current photoshop versions doesn't support that, since there's some idiotic opacity thing instead. If that's the case, there is a plugin to fix that, google your photoshop version with "alpha", that should give you good results.

And of course use a file format with an alpha channel. Targa for example.

Edited by szecs, 23 May 2012 - 05:52 AM.


#7 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 23 May 2012 - 06:08 AM

Yes, you have to make a mask.

1.Copy the content of the color channel to the alpha channel.
2.Invert the color of the alpha channel, since 0 (black) is fully transparent and 255 (white, or 1.0 with float colors) is fully opaque.
3.Probably you can play around with brightness/contrast to get these two values, just make sure the background is all 0.
4.Make the color channels to be the desired color, but fill the whole rectangle with the color. Otherwise in the game, you will have a small blackish hollow around the text.

I hope that's clear, the photoshop screenshot tells me that this should work. EDIT: some current photoshop versions doesn't support that, since there's some idiotic opacity thing instead. If that's the case, there is a plugin to fix that, google your photoshop version with "alpha", that should give you good results.

And of course use a file format with an alpha channel. Targa for example.


Legend! Worked like a charm, thanks!

Just to clarify i am using

GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS