[SDL] Alpha-blending with OpenGL in SDL (multiple textures)

Stephen Anthony stephena at roadrunner.nf.net
Sun Nov 2 14:21:00 PST 2003


On Sunday 02 November 2003 06:20 pm, Mikkel Gjøl wrote:

> First of all, comp.graphics.api.opengl is great for answering
> questions like this.

Noted, and I will be following up on this message there.

> You need to set the alpha-channel of the image to values between 0
> and 1. If you do this, you should set the blendfunc to:
>
> glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
> - which is a linear interpolation between the color of the texture
> you draw with, and the color that is already in the framebuffer.
>
> If you don't want to mess about with blendfuncs, you might use
> glAlphaFunc(GL_GREATER, 0.5f);
> - but this requires actually setting an alpha-value for the texture.
>
>
> You _could_ blend according to the color of the texture though, using
> glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
>
> - which would give the same result for monocrome textures.

OK, here is some sample code that was lifted from a few tutorials.  It 
doesn't seem to work either.

  GLubyte subimage[100][100][4];
  for(int i=0;i<100;i++)
  {
    for(int j=0;j<50;j++)
    {
      subimage[i][j][0] = (GLubyte) 0xff;
      subimage[i][j][1] = (GLubyte) 0;
      subimage[i][j][2] = (GLubyte) 0;
      subimage[i][j][3] = (GLubyte) 0xff;
    }
    for(int j=50;j<100;j++)
    {
      subimage[i][j][0] = (GLubyte) 0xff;
      subimage[i][j][1] = (GLubyte) 0;
      subimage[i][j][2] = (GLubyte) 0;
      subimage[i][j][3] = (GLubyte) 0;
    }
  }

  GLuint tex;
  glEnable(GL_TEXTURE_2D);
  glGenTextures(1, &tex);
  glBindTexture(GL_TEXTURE_2D, tex);
  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 100, 100,
                  GL_RGBA, GL_UNSIGNED_BYTE, subimage);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

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

  glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex2i(20,  60 );
    glTexCoord2f(1.0, 0.0); glVertex2i(120, 60 );
    glTexCoord2f(0.0, 1.0); glVertex2i(120, 160);
    glTexCoord2f(1.0, 1.0); glVertex2i(20,  160);
  glEnd();


Now from my (limited) understanding, this should create a texture that 
is fully opaque and red in one half, and fully transparent in the other 
half.  That's the type of code I need.  But that's not what's 
happening.  Instead, the texture is drawn totally opaque in a color 
that I set elsewhere previously (light grey).  And if I change the 
blending to 'glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR)', then 
the texture is drawn somewhat transparently, but still in the light 
grey color.

Obviously, it isn't using the colors from the texture itself, and thats 
what I need to know how to do.

> I can't tell if you need to draw several letters on top of each
> other, but if you do, you should remember to draw them "back to
> front", ie. draw the letter you actually want to be shown on the
> screen, last.

No, I wouldn't need to draw over characters.  But the character textures 
are drawing over the main window texture.

Thanks for any help,
Steve





More information about the SDL mailing list