Archived

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

alpha textueres problem

This topic is 4950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm having a weird problem with drawing a texture with alpha, i could'nt get my code to work, so i coppied the code right off the opengl game programing book, cacuts example and i still can't get it working right. here's the texture loading code, it works.
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
	FILE *filePtr;
	BITMAPFILEHEADER bitmapFileHeader;
	unsigned char *bitmapImage;
	int imageIndex = 0;
	unsigned char tempRGB;

	filePtr = fopen(filename, "rb");
	if (filePtr == NULL)
	{
		return NULL;
	}

	fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);

	if (bitmapFileHeader.bfType != BITMAP_ID)
	{
		fclose(filePtr);
		return NULL;
	}

	fread(bitmapInfoHeader,sizeof(BITMAPINFOHEADER), 1, filePtr);
	fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
	bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

	if(!bitmapImage)
	{
		free(bitmapImage);
		fclose(filePtr);
		return NULL;
	}

	fread(bitmapImage,1,bitmapInfoHeader->biSizeImage,filePtr);

	if (bitmapImage == NULL)
	{
		fclose(filePtr);
		return NULL;
	}

	for (imageIndex= 0; imageIndex < bitmapInfoHeader->biSizeImage; imageIndex += 3)
	{
		tempRGB = bitmapImage[imageIndex];
		bitmapImage[imageIndex] = bitmapImage[imageIndex + 2];
		bitmapImage[imageIndex + 2] = tempRGB;
	}

	fclose(filePtr);
	return bitmapImage;
}  
here's the alpha texture code, it does not work and i coppied and pasted it.
unsigned char *LoadBitmapFileWithAlpha(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
  unsigned char *bitmapImage = LoadBitmapFile(filename, bitmapInfoHeader);
  unsigned char *bitmapWithAlpha = (unsigned char *)malloc(bitmapInfoHeader->biSizeImage * 4 / 3);

  if (bitmapImage == NULL || bitmapWithAlpha == NULL)
    return NULL;

  // loop through the bitmap data

  for (unsigned int src = 0, dst = 0; src < bitmapInfoHeader->biSizeImage; src +=3, dst +=4)
  {
    // if the pixel is black, set the alpha to 0. Otherwise, set it to 255.

    if (bitmapImage[src] == 0 && bitmapImage[src+1] == 0 && bitmapImage[src+2] == 0)
      bitmapWithAlpha[dst+3] = 0;
    else
      bitmapWithAlpha[dst+3] = 0xFF;

    // copy pixel data over

    bitmapWithAlpha[dst] = bitmapImage[src];
    bitmapWithAlpha[dst+1] = bitmapImage[src+1];
    bitmapWithAlpha[dst+2] = bitmapImage[src+2];
  }

  free(bitmapImage);

  return bitmapWithAlpha;
}   
and here's my initilization code.
BOOL Initialize (GL_Window* window, Keys* keys)					
{
	g_window	= window;
	g_keys		= keys;

	angle		= 0.0f;											

	glClearColor (0.0f, 0.0f, 0.0f, 0.5f);
	glClearDepth (1.0f);										
	glDepthFunc (GL_LEQUAL);									
	glEnable (GL_DEPTH_TEST);									
	glShadeModel (GL_SMOOTH);									
	glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	

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

	glEnable(GL_TEXTURE_2D);
	bitmapData = LoadBitmapFileWithAlpha("checker2.bmp",&bitmapInfoHeader);
		glGenTextures(1, &texture);
		glBindTexture(GL_TEXTURE_2D, texture);

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

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);

	BuildFont();
	log("<p>Initilization Completed.");
	log("<p>test %f", angle);

	return TRUE;												
}  
and here's my drawing code.
void Draw (void)
{	
	static float y;
	static float x = 0.25;
	if (y > 180 || y < 0)
	{
		x = x / -1;
	}
	y +=x;


	time(&start);
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
	glLoadIdentity ();											
	glTranslatef (0.0f, 0.0f, -6.0f);							
	glRotatef (angle, 0.0f, 1.0f, 0.0f);
	glRotatef(y,1,0,0);
	glColor3f(1,1,1);
	
	DrawTextureCube(0,0,0);
	gui();
	glFlush();	
	time(&end);
	dif = difftime(end,start);
}
  
and finaly here's the output, incase angelfire screws me over with image hosting. http://www.angelfire.com/d20/cibressus/alpha_texture_err.jpg [edited by - cibressus on May 29, 2004 3:06:30 PM] [edited by - cibressus on May 29, 2004 3:07:46 PM]

Share this post


Link to post
Share on other sites
Hi. There's a much better way to debug than this - especially for simple programs. By "this" I mean asking others/posting on forums; it's much more productive to figure it out yourself. Try the following: First, try to get blending working without texturing via a sample program that first draws a red quad and then enable and setup your blending. Then, draw a blue quad in front of and bigger than the red quad with an alpha of your choosing (I'd recomend first getting it to work with 0.5 alpha). You should notice blending if this is done right. (Make sure you setup blending correctly via glBlendFunc...experiment with it. )

Next, when you have that blending working, try doing the same thing, but instead of drawing the blue quad, draw a textured quad. Make sure you set the color to white before drawing the textured quad so that your texture gets drawn properly.

Hope this helps you out and leads to some fun learning. Good luck!

~Urayami

[edited by - urayami on May 29, 2004 3:25:47 PM]

Share this post


Link to post
Share on other sites
Alrighty.

The LoadBitmapWithAlpha() code seems fine...I''m a little confused about what you are doing here in LoadBitmapFile():


for (imageIndex= 0; imageIndex < bitmapInfoHeader->biSizeImage; imageIndex += 3)
{
tempRGB = bitmapImage[imageIndex];
bitmapImage[imageIndex] = bitmapImage[imageIndex + 2];
bitmapImage[imageIndex + 2] = tempRGB;
}


~Urayami

Share this post


Link to post
Share on other sites
Well I can give you a clue on this one and you''ll probably kick yourself

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);

you probably want GL_RGBA

Share this post


Link to post
Share on other sites