Skip to main content
GameDev.net gamedev.net
🔒 Locked

Sprites

Started by fearyourself May 13, 2003 at 5:25 PM 9 replies 3.3k views
Original Post
fearyourself
fearyourself
A little problem i have: I can''t seem to put in sprites in my engine, i''m able to have a nice image but with the black around or the black gone but then the sprite is a bit transparent (I want it totally opaque). I''ve been juggling with the Alpha_Test and the Blend functions but i can''t seem to get them right Any solutions?
Cifko
Cifko
disable GL_BLEND
enable only GL_ALPHA_TEST
then create your sprites in 32bit
in the 4. plane set only FF where you want it to be drawn
and 00 where you want it to be hidden

glAlphaFunc(GL_GREATER,0.9);
when you are to close to that sprite and you have bi-linear or better filtering some colors would have alpha greater then 0 and less then 1, so choose the right alpha for glAlphaFunc, but it''s very small effect


fearyourself
fearyourself
I''m probably missing something but i''ve tried taking off the blend but if I do that, the black part gets drawn. Only when i turn on blending does it disappear but then the sprite is also transparent which is not what i want
Myopic Rhino
Myopic Rhino
Are you doing what he suggested with the alpha values? If so, then the alpha test should do what you want.

Since you want black to be transparent, just load your bitmap normally, and then loop through it, setting the alpha value to zero where it''s black and FF otherwise.
fearyourself
fearyourself
Yeah I do exactly what he says and yeah my bitmap is correctly loaded... it''s pretty weird...

Basically is this what you''re suppose to do:

Load your bitmap and put the alpha channel
Draw your scene and when u want to put the sprites
Disable blend and put up the alpha test with the function GL_GREATER,0.9 and then normally what''s not black should be drawn totally opaque
Turn off the alpha test...

But like I said, i draw the sprites last and if I don''t put the blending, the black is still draw... How do u put up a picture here so that i can show u?
fearyourself
fearyourself
Ok i'll try to give u the code that matters (there's too much around it to give it all)

This is the code i use to render my scene (i'll put in all the opengl function calls)

  
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
...
//we translate and rotate the world

glTranslatef(0.0f,0.0f,z);
//we rotate the x axis, y axis and z axis

glRotatef(rx,1.0f,0.0f,0.0f);
glRotatef(ry,0.0f,1.0f,0.0f);
glRotatef(rz,0.0f,0.0f,1.0f);
//we scale

glScalef(scale,scale,scale);
...

//we draw the terrain... to bind the texture

glBindTexture(GL_TEXTURE_2D,txt.get_Texture_Coord(terrain[i][j].texture,coord));
...

//we draw the water

glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glBindTexture(GL_TEXTURE_2D,txt.get_Texture_Coord(WATER,coord));
glColor4f(0.5f,0.5f,1.0f,0.7f);
...
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
glColor3f(1.0f,1.0f,1.0f);

//we draw the trees (THIS IS THE PROBLEM THE SPRITE OF MY TREE

//DOESN'T WORK WELL, THE BLACK PART IS DRAWN INSTEAD OF BEING

//TRANSPARENT

glDepthMask(GL_FALSE);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0.9);
//to bind the texture

glBindTexture(GL_TEXTURE_2D,txt.get_Texture(5));
...
glDepthMask(GL_TRUE);
glDisable(GL_ALPHA_TEST);




There that's it... i hope this can help you help me =>
Myopic Rhino
Myopic Rhino
Post the code you''re using to set up the texture, including the texture environment.
fearyourself
fearyourself
Good call Myopic Rhino, it was the Texture loading I had (before i wanted to put in the sprites):

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bih.biWidth,bih.biHeight,0,GL_RGB,GL_UNSIGNED_BYTE,tmp);

But when i wanted to put in the alpha channel i transformed it to:

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bih.biWidth,bih.biHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,tmp);

Forgetting the 3 parameter... Thx all for ur help
fearyourself
fearyourself
As I said, it works now but maybe u want to see it:

  
unsigned char* LoadBitmapFile(char *f, BITMAPINFOHEADER *bih)
{
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;

//open filename

filePtr = fopen(f,"rb");
if(filePtr == NULL)
write_err("Error opening bitmap file ",f,".");

//read header

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

//check if bitmap

if(bitmapFileHeader.bfType != 0X4d42)
{
fclose(filePtr);
}

//read info header

fread(bih,sizeof(BITMAPINFOHEADER),1,filePtr);

//move file pointer to beginning

fseek(filePtr,bitmapFileHeader.bfOffBits,SEEK_SET);

//alocate enough mem

bitmapImage = (unsigned char*) malloc(bih->biSizeImage);

if(!bitmapImage)
{
fclose(filePtr);
}

//read bitmap

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

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

//swap R and B values

for(imageIdx=0; imageIdx < (int) bih->biSizeImage;imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx+2] = tempRGB;
}

fclose(filePtr);
return bitmapImage;
}

unsigned char *Texture::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;
}


There you go

[edited by - fearyourself on May 15, 2003 1:54:34 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.