Transparency

Having read all transparency-subjects at the forum, I can still not solve my problem. The thing is, I have my own font class with bitmaps, black/blue. Now, I’d like to make the black color transparent, making it possible to use background images. Any suggestions or pseudocodes that I might use?
Regards
/M

look up blending, that´s what you need…
first you draw the background and later on draw your text with glColor4f(r,g,b,alpha) the alpha value gives you the transparency…

some functions:
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

and enable the blending with glEnable(GL_BLEND);

/grodslukaren

ok…but it still doesn’t work…
I tried this:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glColor4f(0.0f, 0.0f, 1.0f, 0.0f);
glBegin(“my drawing”)…
shouldn’t that make the blue color transparent?
i also tried glColor4f(0.0f, 0.0f, 0.0f, 0.0f) which I thought would make the black color transparent, but instead nothing was visible…

further help, grodslukaren???

Ok, first of all you said you were using bitmaps for your font? Do you mean a true bitmap (0-bit off 1-bit on) or do you mean a BMP file for a texture. If you mean a true bitmap then the color used to display the 1-bits will be the current color, which is set with glColor*. The 0-bits are not drawn. So… if you want the font to be blended you set the current color to something with an alpha value. You seem to be under the impression that using glColor4f will assign an alpha value to that color. That is not the case. It simply sets the current color that will be used for drawing. One other thing that’s important to note is that if you are using lighting, the current color is not used. With lighting enabled, the current material properties are used. If you enable GL_COLOR_MATERIAL you can have the current color track one of the material properties, but it is still basically using materials.

first you should draw your opaque objects, or the bitmap you want as background, then turn on:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glColor4f(0.0f, 0.0f, 1.0f, 0.5f); //0.5 makes it 50% transparent
glBegin(“your transparent stuff”)…
glDisable(GL_BLEND);

<<shouldn’t that make the blue color <<transparent?
<<i also tried glColor4f(0.0f, 0.0f, 0.0f, <<0.0f) which I thought would make the black <<color transparent, but instead nothing was <<visible…

you have to set the alpha value to something between 0 and 1, zero is totally transparent and 1 is totally opaque… with zero the object is there, but you can´t see it…

hope it helps…

/grodslukaren

hmmm…i use BMP files…
Is the code diffícult to write? Would it be possible to send you the code by mail, and you might just comment it…what should be written where and so on?
Gee, was that a lazy one or what?

>grodslukaren
That code snippet made the hole bitmap 50% transparent, not only the blue color…

as far as i know you can’t simply make a certain color transparent (well you actually can make blue transparent using register combiners). So why don’t just make give your bitmaps fonts a transparent part, its much easier than letting your opengl programm transform anything.

Chris

>DaViper
Ok…does that mean that I can’t use BMP? I have a LoadJPG class…will the transparency work “automatically” if I give the fonts a transparent part or do I have to modify the class? Feeling stupid

i think tiff would be the best format, because jpeg doesn’t support transparency

I haven´t worked with bitmaps, so I´m not sure how to do it. But I send you a part of my Render() that works with objects…

good luck!!

// draw dipoles if exist any, opaque
if (m_bDipoles)
glDisable(GL_BLEND);
{
for (dipole=0; dipole<m_numNbrOfDipoles; dipole++)
{
glPushMatrix();
glTranslatef(m_Dipoles[dipole].posX360,m_Dipoles[dipole].posY(-360),m_Dipoles[dipole].posZ);
glPushName(dipole);
glColor3f(0.3f, 0.3f, 1.0f); // dipoles drawn blue
glCallList(DIPOLE); // draws a dipole
glPopName();
glPopMatrix();
}
}

glEnable(GL_BLEND);
// draw the brain, the scalp and the face from the displaylists blended to 33%
glPushMatrix();
	glTranslatef(-0.00950363f, 84.0311f,-16.7920f);
	glColor4f(0.3f, 1.0f, 0.3f, 0.33f);
	glCallList(BRAIN);
glPopMatrix();
glColor4f(1.0f, 0.4f, 0.3f, 0.33f);
glCallList(DISP_FACE);	// draws the face
glCallList(DISP_REAL_SCALP);	// draws the scalp with the same color as the face	

glFlush();
SwapBuffers();

Sounds like you just need to convert your image to an RGBA image from RGB. Then for any given color you can set the A (alpha channel) to anything you like to make only that color transparent. Here’s a quick code snippet that should help explain what I mean.

unsigned char* pRGBImage;
unsigned char* pRGBAImage;
int width, height;

pRGBImage = GetImageData();
width = GetWidth();
height = GetHeight();

pRGBAImage = new unsigned char[widthheight4];
for (int y=0;y<height;y++)
{
for (int x=0;x<width;x++)
{
int baseindex = y*width + x;

  // assign current RGB data
  pRGBAImage[baseindex*4] = pRGBImage[baseindex*3];
  pRGBAImage[baseindex*4+1] = pRGBImage[baseindex*3+1];
  pRGBAImage[baseindex*4+2] = pRGBImage[baseindex*3+2];

  // Now assign blue alpha of 128 everything else 255
  if (pRGBImage[baseindex*3]==0 && pRGBImage[baseindex*3+1]==0 && pRGBImage[baseindex*3+2]==255)
      pRGBAImage[baseindex*4+3]=128;
  else
      pRGBAImage[baseindex*4+3]=255;

}
}