Skip to main content
gamedev.net
🔒 Locked

Alpha ! how to........

Started by robert_s 37 replies 2.1k views
Original Post
robert_s
robert_s
HI! this is my bit of terrain! and this is my main cockpit panel! Now I am trying to get rid of the white colour from the panel image! I know that I should use Alpha blending which I used but it works in the other way round now!! ie. white colour still stays solid but the areas which display the panel are transparent!! How do I do that it will ignore completely the white background colour (ie. white not visible at all) and the panel will be completely solid (visible) ie. not transprent at all clearly visible! this is what I wrote: glEnable(GL_BLEND); // Enable Blending glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Select The Type Of Blending glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glPushMatrix(); // Store The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix gluOrtho2D(0, winWidth,0,winHeight); // Set Up An Ortho Screen glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glPushMatrix(); // Store The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix glEnable(GL_TEXTURE_2D); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.9f); //display map HUD glBindTexture(GL_TEXTURE_2D, cockpitPanel); //draw the panel at the bottom of the window glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2d( 0.0, 0.0); // Bottom Left Of The Texture and Quad glTexCoord2f(1.0f, 0.0f); glVertex2d( winWidth, 0.0); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex2d( winWidth, winHeight); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex2d( 0.0, winHeight); // Top Left Of The Texture and Quad glEnd(); glDisable(GL_TEXTURE_2D); glDisable(GL_ALPHA_TEST); glDisable(GL_BLEND); ...........etc.. thanks for your time!! PS: please see if the order I have nested those alpha functions are correct!? ie. glAlphaFunc and glBlendFunc. Edited by - robert_s on November 17, 2001 1:47:52 PM Edited by - robert_s on November 17, 2001 1:50:25 PM Edited by - robert_s on November 17, 2001 1:53:13 PM
DarkHunter
DarkHunter
DOnt know how to help you. It looks like a old game i ahve called F-15.... are you copying/recreating it? or just making a new one that just happens to look exactly the same?
robert_s
robert_s
----------------------------------------------------------
Anonymous Poster
This is wrong then : glBlendFunc(GL_SRC_ALPHA, GL_ONE);
you want to use colorkey instead of alpha blending unless you actually add an alpha channel to your pic.
------------------------------------------------------------

ok! thx! but whats actually colorkey? could you exaplin please?

--------------------------------------------------------------
DarkHunter
DOnt know how to help you. It looks like a old game i ahve called F-15.... are you copying/recreating it? or just making a new one that just happens to look exactly the same?
----------------------------------------------------------------

Sorry DarkHunter but I don't even know what game do you really mean! I do not copy anything! this is just a plain chunk of a terrain covered with textures which even hasn't got normals calculated yet and a simple skybox with texs too. so I don't really understand what's wrong with it? if you know where i could get some source code for that game you mentioned I'll be more than pleased to know the link from u! haha!!
About textures: you're right! the panel was taken from a game but not the one you mentioned!! sky is just a real picture! thats all!! the terrain texture is just one 128x128 texture created in about 5 min on Photoshop! thats it! so, whole secret revealed!! haha!!!
Obviously I am not going to create panel and sky for this engine on my own as I don't have time for this!! My point is to understand the design and learn game programming techniques but not wasting my time on creating textures which are not relevent for me at this stage!! When I completely finish the main design I'll then concentrate on texs but not now!!!
Anyway this is not going to be a commercial software that you can buy!! this is just for my fun!
The panel I did not draw "with a pencil and I am not going to" . I am sure that anyone out there who wants to have a nice looking panel does a similar thing ie. converts a real image on photoshop or takes from some other sources and slightly converts it on photoshop!!thats it!

ok! sorry I got a bit annoyed!
Anyway I am still looking for someone who can help me!
Anyone?!

Edited by - robert_s on November 17, 2001 4:37:28 PM
robert_s
robert_s
Yes !! you''re right it doesn''t have an alpha channel!! How do I add it? is is a lot of work? please explain!
(Thanks for reply! )
robert_s
robert_s
Gosh! that means I have to implement an algorith to load TGAs as my one reads only BMPs! so it won''t work!!
Ok once I''ve done that do I just use same code as above? or needs something more??
baldurk
baldurk
It might not be the best answer but try putting

glBlendFunc(GL_DST_ALPHA,GL_SRC_COLOR);

in InitGL instead of the current one.
That makes black have full alpha and white have no alpha, try that although the best answer is to use TGAs

------------------------------
Baldur K
Locke
Locke
How do I modify the Alpha Channel using Photoshop, I have no idea how to do anything advanced in it, is it simple?
Brendan ''Locke'' Hennessy"I heard life sucks, so that''s why I don''t have one"
Fruny
Fruny
BLASPHEMY! DO NOT USE ALPHA!!! This is what the stencil buffer is for. (pp 441-446 in the Red Book)

First clear the stencil buffer, then paint 1's where you want your GL scene to be rendered. e.g. with DrawPixels() since you have a bitmap (should be a black/white bitmap to make things easier).

    
glutInitDisplayMode( GLUT_STENCIL ... ); // use a stencil buffer


// Initialize the stencil buffer - once only.


glEnable( GL_STENCIL_TEST ); // paint in the color buffer only if the stencil test passes;

glStencilFunc( GL_ALWAYS, 0x1, 0x1 ); // always pass the test

glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE ); // replace the stencil value regardless of the test result;

glDrawPixel( ... ); // Draw your bitmap;


.
.
.

glStencilFunc( GL_EQUAL, 0x1, 0x1 ); // pass the test if the stencil & 0x1 == 0x1 (i.e. not black)

glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP ); // we're not modifying the stencil buffer anymore.


/* YOUR 3D SCENE HERE */



That way, only pixels corresponding with a non-black stencil pixel will be painted. And you can still use alpha for other purposes. And it is faster ('cause that's what it is designed for). And to render on the whole screen, disable the stencil test. Or use different masks ( 3rd parameter ) to separately control your 3 displays (outside view and 2 controls)

Like this :

paint all 0's on the dashboard
paint bit 1 on the outside
paint bit 2 on the left control
paint bit 3 on the right control

Then check

   

glStencilFunc( GL_EQUAL, 0x1, 0x1 ); // outside

glStencilFunc( GL_EQUAL, 0x2, 0x2 ); // left

glStencilFunc( GL_EQUAL, 0x4, 0x4 ); // right



Edited by - Fruny on November 18, 2001 5:54:26 AM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Maximus
Maximus
quote:
Original post by Fruny
BLASPHEMY! DO NOT USE ALPHA!!! This is what the stencil buffer is for.



Remember that not every card has a hardware stencil buffer, and nVidia cards only have one in 32bit colour. It is MUCH faster to use alpha, and much easier as well.

  
glDisable(GL_BLEND);
glEnable(GL_ALPHA_TEST);

DrawHUDImage();

glDisable(GL_ALPHA_TEST);


Thats basically it.
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
robert_s
robert_s
hmmm... now I am confused which technique to use!?? I think I''ll still experiment with alpha but I am quite interested with FRUNYs idea! do you think its faster than Alpha Fruny?
Maximus I can see that you disabling BLENDing before you use ALPHA_TEST is it right? look at the top of this page at my code or below.
I put alpha_test between glEnable(GL_BLEND) and glDisable(GL_BLEND) is it wrong? (nested)



this is what I wrote:

glEnable(GL_BLEND); // Enable Blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Select The Type Of Blending
 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
 glEnable(GL_ALPHA_TEST);
 glAlphaFunc(GL_GREATER, 0.9f);
  //draw the panel at the bottom of the window
 glDisable(GL_ALPHA_TEST);
glDisable(GL_BLEND);

Fruny
Fruny
Apparently, you want to add the bitmap OVER the picture, covering (read wasting) some of the rendering you have already done.

The stencil buffer works the other way round : your scene is only rendered where the stencil buffer says so.

To render your image
a) disable stencil test
b) render the cockpit (bit blast, quick and dirty)
c) enable stencil test
d) render the 3D

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Maximus
Maximus
Using the stencil buffer, it has to check the stencil value of every pixel before drawing it in much the same way the depth buffer works. If you use alpha blending you can just dump the image over the top without depth testing.
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
balloon
balloon
Few ideas:

If I right remember there''s simple texture tool at nvidia developer page. You can add alpha channel with it.

Also you can at load time create alpha channel for your texture. Check each colorvalue in the texture. If it''s white se alpha to 0xff else set it to 0xff.
HellRiZZer
HellRiZZer
So, finally, how to draw an image (TGA 32 bit with alpha channel)
without black/white pixels on quad?
1)Use GL_RGBA for TGA load
2)Then what?
How to render (what steps) e.g
glDisable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
DrawHUDImage();
glDisable(GL_ALPHA_TEST);
Is that drawHUDImage() is rendering image on somewhere(quad)?
So, I have to go:

glDisable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glBindTexture(GL_TEXTURE_2D, m_texture.ID);
glBegin();
...
glVertex3f(etc);
...
glEnd();
glDisable(GL_ALPHA_TEST);

Is that right?
aleks_1661
aleks_1661
its probably noi the most efficient way of doint things but i use this method to mask images, i use two textures, a mask and the texture. i use these little functions to do it.

using a TGA file with apha channel is a much sweeter way to do it.

texture 1 is the image, 2 is the mask


void Blend()
{
glEnable(GL_BLEND); // Turn Blending On
glDisable(GL_DEPTH_TEST); // Turn Depth Testing Off
}

void Mask()
{
glBlendFunc(GL_DST_COLOR,GL_ZERO);
}

void Copy()
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
}

void DontBlend()
{
glDisable(GL_BLEND); // Turn Blending Off
glEnable(GL_DEPTH_TEST); // Turn Depth Testing On
}



void DrawShip()
{

Blend();

glColor4f(1.0f,1.0f,1.0f,1.0f);

Mask();
glBindTexture(GL_TEXTURE_2D, texture[2]);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.5f, -1.5f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.5f, -1.5f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.5f, 1.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.5f, 1.5f, 0.0f);
glEnd();

Copy();
glBindTexture(GL_TEXTURE_2D, texture[1]);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.5f, -1.5f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.5f, -1.5f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.5f, 1.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.5f, 1.5f, 0.0f);
glEnd();

DontBlend();
}


"For Queen and Country!"

Edited by - aleks_1661 on November 19, 2001 8:10:10 AM
Twitter: [twitter]CaffinePwrdAl[/twitter] Website: (Closed for maintainance and god knows what else)
Maximus
Maximus
This is how I draw a masked image.

After switching to an orthographic projection matrix, I call:

glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glEnable(GL_ALPHA_TEST);

Then I just draw the image onto triangles as you would any other polygon soup. The alpha test will ignore any pixels that have an alpha value < 1 (you may need to set that occlusion method with glAlphaFunc, depending on how your alpha channels are setup in the images).

Just make sure your image has an alpha channel, and that you are loading it correctly, and you should have no problems.
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
robert_s
robert_s
Ok Guys!! Your answers are nice and clear!!
But.. what do I need the mask texture for? if I have already alpha channel which will enable me to ignore the unvanted pixels?! is it necessary to use the mask too?
miles vignol
miles vignol
Okay, here''s a couple of things to keep in mind:

First off, you probably ought to roughly outline your HUD rather than draw a quad the size of the screen. Just use a few polygons to approximate the area. The texture coordinates for the vertices making up the HUD display will be based on the x,y coordinates for each point as it relates to the overall height/width of the object.

Secondly, if you''re ever going to create artwork that is to be viewed as a transparent overlay, NEVER USE A COLOR IN THE BACKGROUND!!!! Always make your artwork over BLACK. This way, you can add your alpha channel (or better yet, create it at the same time you''re creating your artwork) and do a simple glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) for blending. Even if you add an alpha to your current image, you''ll likely have a white halo around your edges from the blending. You''ll also need to use glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) which (to me anyway) is a little less straight forward for artwork.

How did you create your artwork? Maybe you need to go back a couple steps.
feagle814
feagle814
Anybody ever thought about using alpha on an OVERLAY plane?
Maximus
Maximus
Heh I meant using the alpha as the mask, which is removed from being rendered when you draw the image ^_^
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack

Topic Locked

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

Sign in to reply to this topic.