scene=!scene; // Toggle From One Scene To The Other
}
The code below checks to see if we have released the spacebar (if NOT ' '). If the spacebar has been released, we set sp to FALSE letting our program know that the spacebar is NOT being held down. By setting sp to FALSE the code above will check to see if the spacebar has been pressed again, and if so the cycle will start over.
if (!keys[' ']) // Has Spacebar Been Released?
{
sp=FALSE; // Tell Program Spacebar Has Been Released
}
The next section of code checks to see if the 'M' key is being pressed. If it is being pressed, we set mp to TRUE, telling our program not to check again until the key is released, and we toggle masking from TRUE to FALSE or FALSE to TRUE. If masking is TRUE, the drawing code will turn on masking. If it is FALSE masking will be off. If masking is off, the object will be blended to the screen using the old fashioned blending we've been using up until now.
if (keys['M'] && !mp) // Is M Being Pressed?
{
mp=TRUE; // Tell Program M Is Being Held
masking=!masking; // Toggle Masking Mode OFF/ON
}
The last bit of code checks to see if we've stopped pressing 'M'. If we have, mp becomes FALSE letting the program know that we are no longer holding the 'M' key down. Once the 'M' key has been released, we are able to press it once again to toggle masking on or off.
if (!keys['M']) // Has M Been Released?
{
mp=FALSE; // Tell Program That M Has Been Released
}
Like all the previous tutorials, make sure the title at the top of the window is correct.
if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("NeHe's Masking Tutorial", 640, 480, 16, fullscreen)) {
return 0; // Quit If Window Was Not Created
}
}
}
}
}
// Shutdown
KillGLWindow(); // Kill The Window
return (msg.wParam); // Exit The Program
}
Creating a mask isn't to hard. A little time consuming. The best way to make a mask if you already have your image made is to load your image into an art program or a handy program like infranview, and reduce it to a gray scale image. After you've done that, turn the contrast way up so that gray pixels become black. You can also try turning down the brightness, etc. It's important that the white is bright white, and the black is pure black. If you have any gray pixels in your mask, that section of the image will appear transparent. The most reliable way to make sure your mask is a perfect copy of your image is to trace over the image with black. It's also very important that your image has a BLACK background and the mask has a WHITE background! If you create a mask and notice a square shape around your texture, either your white isn't bright enough (255 or FFFFFF) or your black isn't true black (0 or 000000). Below you can see an example of a mask and the image that goes over top of the mask. the image can be any color you want as long as the background is black. The mask must have a white background and a black copy of your image.
This is the mask→
This is the image→
Eric Desrosiers pointed out that you can also check the value of each pixel in your bitmap while you load it. If you want the pixel transparent you can give it an alpha value of 0. For all the other colors you can give them an alpha value of 255. This method will also work but requires some extra coding. The current tutorial is simple and requires very little extra code. I'm not blind to other techniques, but when I write a tutorial I try to make the code easy to understand and easy to use. I just wanted to point out that there are always other ways to get the job done. Thanks for the feedback Eric.
In this tutorial I have shown you a simple, but effective way to draw sections of a texture to the screen without using the alpha channel. Normal blending usually looks bad (textures are either transparent or they're not), and texturing with an alpha channel requires that your images support the alpha channel. Bitmaps are convenient to work with, but they do not support the alpha channel this program shows us how to get around the limitations of bitmap images, while demonstrating a cool way to create overlay type effects.
Thanks to Rob Santa for the idea and for example code. I had never heard of this little trick until he pointed it out. He wanted me to point out that although this trick does work, it takes two passes, which causes a performance hit. He recommends that you use textures that support the alpha channel for complex scenes.
I hope you enjoyed this tutorial. If you had any problems understanding it, or you've found a mistake in the tutorial please let me know. I want to make the best tutorials available. Your feedback is important!
* DOWNLOAD Visual C++ Code For This Lesson.
* DOWNLOAD Borland C++ Code For This Lesson. (Conversion by Patrick Salmons)
* DOWNLOAD Cygwin Code For This Lesson. (Conversion by Stephan Ferraro)
* DOWNLOAD Delphi Code For This Lesson. (Conversion by Marc Aarts)
* DOWNLOAD Irix / GLUT Code For This Lesson. (Conversion by Rob Fletcher)
* DOWNLOAD Java Code For This Lesson. (Conversion by Jeff Kirby)
* DOWNLOAD Linux Code For This Lesson. (Conversion by Daniel Davis)
* DOWNLOAD Linux/GLX Code For This Lesson. (Conversion by Mihael Vrbanec)
* DOWNLOAD Linux/SDL Code For This Lesson. (Conversion by Ti Leggett)
* DOWNLOAD Mac OS Code For This Lesson. (Conversion by Anthony Parker)
* DOWNLOAD Mac OS X/Cocoa Code For This Lesson. (Conversion by Bryan Blackburn)
* DOWNLOAD Visual C++ / OpenIL Code For This Lesson. (Conversion by Denton Woods)
* DOWNLOAD Visual Basic Code For This Lesson. (Conversion by Edo)