Archived

This topic is now archived and is closed to further replies.

SERPENT

Transparency with bitmap resource as a texture

Recommended Posts

hey, this is my first time here, and I think you might like to know that i am the n00biest of all n00bs. Anyway, I''ve been programming this little cube with a bitmap texture as a win32 resource in msvc++ 6. My problem is, I can''t get the transparency to work. Heres my LoadGLTextures:
int LoadGLTextures()									// Load Bitmaps And Convert To Textures

{
	
HWND hwnd;
	int Status=TRUE;									// Status Indicator


   // load bitmap from resource file

   HBITMAP bitmap  = LoadBitmap(GetModuleHandle(NULL), 
      MAKEINTRESOURCE(IDB_BITMAP2));


   // setup 24 bits bitmap structure

   // works on 8 bits bitmaps also!



   BITMAPINFO info;
   BITMAPINFOHEADER header;
   header.biSize = sizeof(BITMAPINFOHEADER);     
   header.biWidth = 256;     
   header.biHeight = 256; 
   header.biPlanes = 1;     
   header.biBitCount = 24;     
   header.biCompression = BI_RGB;
   header.biSizeImage = 0;     
   header.biClrUsed = 0;     
   header.biClrImportant = 0; 

   info.bmiHeader = header;
   info.bmiColors->rgbRed      = NULL;
   info.bmiColors->rgbGreen    = NULL;
   info.bmiColors->rgbBlue     = NULL;
   info.bmiColors->rgbReserved = NULL;


   // store bitmap data in a vector

   const int size = 256*256*3;
   unsigned char data[size];  

   HDC hdc = GetDC(hWnd);
      GetDIBits(hdc, bitmap,  0, 256,  &data,  &info, DIB_RGB_COLORS);
  
	  ReleaseDC(hWnd, hdc);
  


   // convert from BGR to RGB

   unsigned char buff;
   for(int i=0; i<256*256; i++)
   {
      buff = data[i*3];
      if(i>=3)
      {
         data[i*3]   = data[i*3+2];
         data[i*3+2] = buff;
      }
   }
 unsigned char  *src_bitmap = data;
   unsigned char *alpha_bitmap = (unsigned char *)malloc(((256)*(256)) *4) ;
   for (unsigned int src = 0, dst = 0; src < (256)*3; src +=3, dst +=4)
{
// if the pixel is black, set the alpha to 0. Otherwise, set it to 255.

	
	if( src_bitmap[src] == 0xFF && src_bitmap[src+1] == 0 && src_bitmap[src+2] == 0xFF )
{
alpha_bitmap[dst+3] = 0;
PostQuitMessage(0); //I tell it to quit, and it WONT!

}
else
alpha_bitmap[dst+3] = 0xFF;

// copy pixel data over

alpha_bitmap[dst] = src_bitmap[src];
alpha_bitmap[dst+1] = src_bitmap[src+1];
alpha_bitmap[dst+2] = src_bitmap[src+2];
}

   // create one texture

   glGenTextures(1, &m_texture[0]);
				
   // select texture

   glBindTexture(GL_TEXTURE_2D, m_texture[0]);

	
   // SetTextureParameters

		glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, &data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);

   gluBuild2DMipmaps(GL_TEXTURE_2D, 4, 256, 256,
GL_RGBA, GL_UNSIGNED_BYTE, alpha_bitmap);
		// generate texture

   glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, &data);
//free all the stuff, etc.

and within my DrawScene(), i have:
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0);
right before i draw my cube. My cube''s texture has a large pink(255,0,255) square on it that should go transparent, but it doesn''t. Help me, please. Thank you all! http://www.WoodmanStudios.tk

Share this post


Link to post
Share on other sites
can somebody please help me out here?
if you don''t understand my question, basically i''m just saying that my simple bitmap, with a large pink square on it, won''t go transparent, and that it''s a win32 resource.

Share this post


Link to post
Share on other sites
I believe the reason this doesn''t work has something to do with OpenGL''s internal texture format (probably RGB8, not RGBA8) although i am not really sure about this. One thing you should do is check out the masking tutorial on this site (nehe.gamedev.net) this will show you an easy way to mask your textures with a transparency channel.

Share this post


Link to post
Share on other sites
I''d really rather not have to make two images for the price of one, if you know what i mean. I at one point HAD this going, but the texture was from a file(nehe.bmp). Then I turned it into a resource, so i don''t have to use a file, now the transparency doesn''t work. It should still work though, right?

http://www.WoodmanStudios.tk

Share this post


Link to post
Share on other sites

// if the pixel is black, set the alpha to 0. Otherwise, set it to 255.
if( src_bitmap[src] == 0xFF && src_bitmap[src+1] == 0 && src_bitmap[src+2] == 0xFF )
{
alpha_bitmap[dst+3] = 0;
PostQuitMessage(0); //I tell it to quit, and it WONT!
}


Your comment states that you want black to be transparent, and yet the color you are checking for is 255,0,255. Pure black should be 0,0,0.

That is the only obvious error that I can see.

J.W.

Share this post


Link to post
Share on other sites
He sais he wants pink to be transparent, which he does right, the problem is i think that you''d have to set the internal texture format of opengl to 4-channel.

Share this post


Link to post
Share on other sites
NeHe''s IPicture code does what you want...it doesn''t get the bmp from a resource, but it does the transparency stuff, so you should be able to find what you want in there, e.g. setting the texture format to RGBA8 as Tree Penguin suggests (if that is the problem).

Share this post


Link to post
Share on other sites
I''m able to do transparency from files ok. but with resources, the transparency just doens''t seem to go through. Can someone provide me with code that will take in a bitmap from a resource and use transparency? please! i''ll be your best friend!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The line:
glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, &data);


Should be
glTexImage2D(GL_TEXTURE_2D, // Gotta put this.
0, // Top level image.
4, // You have four colour components in your data.
256, 256, // X and Y sizes.
0, // No border.
GL_RGBA, // Final texture will have alpha channel.
GL_UNSIGNED_BYTE, // Source data is in bytes.
alpha_bitmap // This is the correct image source.
);


Share this post


Link to post
Share on other sites