PDA

View Full Version : Alpha Test



Foxabilo
10-27-2005, 08:47 PM
Howdy people, appols for posting this in 2 locations but no joy as yet and i wondered if it was in the right place, anyways..

OK, I am writting a "small" (getting bigger by the second tho :) ) app for me and a few freinds to be able to play chess via tcp/ip and as I am a glutten for punishment I decided to code it myself from scratch in a language I have never used before and with OpenGL to do the graphics with, Dumb huh ! Meh I like a challenge :)

OK here is my problem I will include the code at the bottom, but please be gentle with my pathetic coding skills as up until 2 days ago i did not even have a c++ compiler and OpenGL was somthing that half life had a mode for, now i am attempting to code in both so the code is sloppy and somwhat hard coded etc etc, I realise it makes it a pain to look through but is any expert does have the odd few mins to take a look at it and work out whats wrong I would be very greatfull.

Right onto the problem itself:

I have created a number of chess piece bmp files in photoshop with an alpha channel to mask the bits of the texture i do not want to be displayed, these bmp files are loaded in and mapped to gldisk's just for what of somthing better to texture them to, the disks form the playing pieces what i wanted to happen is the alpha test to remove the unwanted texture from around saw a pawn or a king and just leave the texture almost like a sprite, but alas this is not happening.

What is happening is that the disk is still visiable no matter what set of blends or alpha tests i do i have tried just about every combination out there so i have ugly round disks with nice textures on them sitting on top of a chess board and (pulling my hair out) i want the part of the disk that is not textures and alpha mapped to go away to actualy go away :)

OK here is a link to a screen shot of the problem I have deliberatly coloured the bishops in red so you can see the problem the bishop is currently the only peice to have an alpha channel in the BMP the rest i will do once i get this one working

http://img457.imageshack.us/my.php?image=chess7ou.jpg

http://img457.imageshack.us/img457/9093/chess7ou.jpg

The blue square is just the cursor for piece movment

and here is the ugly code DONT LAUGH !!! ;)


void __fastcall TFormMain::DrawPiece(int Piece,float x,float y, float Depth, float Rotation)
{

glPushMatrix();

//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glColor4f(0, 0, 0,0.1);
//glEnable (GL_BLEND);

//glBlendFunc (GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

//glBlendFunc (GL_SRC_COLOR, GL_DST_COLOR);

//glEnable(GL_ALPHA_TEST);
//glAlphaFunc(GL_GREATER, 0.2);


glEnable(GL_TEXTURE_2D);
glAlphaFunc(GL_GREATER, 0.5);
glEnable(GL_ALPHA_TEST);
//glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
//glBlendFunc(GL_ONE, GL_ONE);
//glEnable(GL_POLYGON_OFFSET);
glPolygonOffset(0.0, -3);

//gluDisk(WhiteBishopMap, 0, 7, 15, 1);

switch (Piece)
{
case 1 : glBindTexture(GL_TEXTURE_2D, WhiteRookTexture);break;
case 2 : glBindTexture(GL_TEXTURE_2D, WhiteKnightTexture);break;
case 3 : glBindTexture(GL_TEXTURE_2D, WhiteBishopTexture);break;
case 4 : glBindTexture(GL_TEXTURE_2D, WhiteQueenTexture);break;
case 5 : glBindTexture(GL_TEXTURE_2D, WhiteKingTexture);break;
case 11 : glBindTexture(GL_TEXTURE_2D, WhitePawnTexture);break;
case 12 : glBindTexture(GL_TEXTURE_2D, WhitePawnTexture);break; // en passant pawn
case 31 : glBindTexture(GL_TEXTURE_2D, BlackRookTexture);break;
case 32 : glBindTexture(GL_TEXTURE_2D, BlackKnightTexture);break;
case 33 : glBindTexture(GL_TEXTURE_2D, BlackBishopTexture);break;
case 34 : glBindTexture(GL_TEXTURE_2D, BlackQueenTexture);break;
case 35 : glBindTexture(GL_TEXTURE_2D, BlackKingTexture);break;
case 21 : glBindTexture(GL_TEXTURE_2D, BlackPawnTexture);break;
case 22 : glBindTexture(GL_TEXTURE_2D, BlackPawnTexture);break; // en passant pawn
case 100 : glBindTexture(GL_TEXTURE_2D, BoardLightTexture);break;
case 101 : glBindTexture(GL_TEXTURE_2D, BoardDarkTexture);break;
case 102 : glBindTexture(GL_TEXTURE_2D, BoardGreenTexture);break;
case 103 : glBindTexture(GL_TEXTURE_2D, BoardBlueTexture);break;
case 104 : glBindTexture(GL_TEXTURE_2D, BoardRedTexture);break;
}
//glEnable(GL_BLEND);
//glBlendFunc (GL_ONE, GL_ONE);
//glDisable(GL_DEPTH_TEST);
//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glEnable(GL_ALPHA_TEST);
//glAlphaFunc(GL_GREATER, 0.5);
//glPushMatrix();
//glDisable(GL_CULL_FACE);
//glColor4f(1, 1, 1,0.51);
//glDisable(GL_DEPTH_TEST);
glTranslatef(y, x, Depth);
glRotatef(Rotation, 0.0, 0.0, 1.0);
glCallList(startoflist+Piece);

//glEnable(GL_CULL_FACE);
//glEnable (GL_BLEND);

//glBlendFunc (GL_ONE, GL_ONE);
//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glEnable(GL_RGBA);


glPopMatrix();




}




WhiteKnightMap = gluNewQuadric();

gluQuadricTexture(WhiteKnightMap, GL_TRUE);
gluQuadricDrawStyle(WhiteKnightMap, GLU_FILL);
gluQuadricNormals(WhiteKnightMap, GL_LINEAR);

glNewList(startoflist +2, GL_COMPILE); //King is plus 1 in GL element List , Board is 0
gluDisk(WhiteKnightMap, 0, PieceSize, 15, 1);
glEndList();


filePtr = fopen("WhiteBishop copy.bmp","rb");
fread(buff, 10,1,filePtr);
fread(buff, 4,1, filePtr);
offset = buff[0];
fread(buff, offset-11,1, filePtr);
fread(picbuff,(128*128*4),1,filePtr);
fclose(filePtr);

f =0;
for (int x=0; x<128; x++)
{
for (int y=0; y<128; y++)
{
a = picbuff[f+1];
b = picbuff[f+2];
c = picbuff[f+3];
d = picbuff[f+0];
pixel = (a)+(b*256)+(c*65536);
bits[x][127-y][2] = a;
bits[x][127-y][1] = b;
bits[x][127-y][0] = c;
bits[x][127-y][3] = 255-d;
f = f + 4;

}
}

glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGenTextures(1, &amp;WhiteBishopTexture);
glBindTexture(GL_TEXTURE_2D, WhiteBishopTexture);
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_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);

zeckensack
10-28-2005, 01:06 AM
The code doesn't look so bad. If this is really your third day of being exposed to C++ you should be very proud of yourself :)

You said you've tried lots of combinations of alpha test without success, but I'd still like to ask if you tried the reverse of what's in the version you posted. I.e. glAlphaFunc(GL_LESS,0.5).

Otherwise there's no real problem with what you're doing. It should work. This may actually be a problem with your bmp data. To verify that you have the correct alpha channel data in there, in the part where you shuffle the channels around, try moving the supposed alpha to e.g. the green color channel for visual inspection.

Your alpha channel may be inverted or it may not have been saved at all.

There are a few ... interesting things in your code, foremost using a gluDisk as your carrier for the textures when you actually don't care about the geometric shape, but we'll save that for another occasion.

But there's one real no-no: when reading the bmp, you seek ahead by reading some amount of whatever to a buffer. That may turn into a bad and dangerous habit, so I'll point out right now that there's an fseek function which you should use instead.

sqrt[-1]
10-28-2005, 01:16 AM
I don't see anything obvious that is wrong. If you post a link to compiled exe with alpha test enabled with a alpha reference value (set to GL_GREATER), I'll have a look. (also if you can include source that would also help)

Foxabilo
10-28-2005, 08:49 AM
Thanks for the feed back chaps/chapesses

Some bright spark spotted the problem in my code posted in the beginners section

I was using GL_DECAL texture enviroment and blending my alpha value with the texture !! swapped that to GL_REPLACE and its all working now ! Wooooooooot

Thankyou Soooo much guys'n'Gals for spending time on this

knackered
10-28-2005, 09:48 AM
smug sod.

Foxabilo
10-28-2005, 01:24 PM
Originally posted by knackered:
smug sod.LOL, absolutly no offence intented I really do appreciate the help that has been given on this forum, I was at my witts end and now its all smiles und sunshine :)

Thanks

Foxabilo
10-28-2005, 01:31 PM
Originally posted by zeckensack:
The code doesn't look so bad. If this is really your third day of being exposed to C++ you should be very proud of yourself :)

You said you've tried lots of combinations of alpha test without success, but I'd still like to ask if you tried the reverse of what's in the version you posted. I.e. glAlphaFunc(GL_LESS,0.5).

Otherwise there's no real problem with what you're doing. It should work. This may actually be a problem with your bmp data. To verify that you have the correct alpha channel data in there, in the part where you shuffle the channels around, try moving the supposed alpha to e.g. the green color channel for visual inspection.

Your alpha channel may be inverted or it may not have been saved at all.

There are a few ... interesting things in your code, foremost using a gluDisk as your carrier for the textures when you actually don't care about the geometric shape, but we'll save that for another occasion.

But there's one real no-no: when reading the bmp, you seek ahead by reading some amount of whatever to a buffer. That may turn into a bad and dangerous habit, so I'll point out right now that there's an fseek function which you should use instead.Thankyou very much for your feedback I have now implemented fseek as you sugested and have changed the Disk to a simple Quad, slowly getting the hang of getting back into coding its been some 15 years, was all green screen and DOS in my day ;)