Simple transparency: Jeff Molofee (Nehe) OpenGL Tutorial - Eight Lesson

zhaozj2021-02-08  229

Jeff Molofee (Nehe) OpenGL Tutorial Eight lesson Translated by CKER Simple transparent OpenGL is related to certain types of (color) mixing. The definition of mixing is, combined with the color of a pixel and the plotted pixel color corresponding to its corresponding pixel colors. As for how to combine these two colors, depending on the component value of the Alpha channel, and / or the mixing function used. Alpha is typically the amount of 4th color group located at the end of the color value. In the previous lesson, we all use GL_RGB to specify the three components of the color. The corresponding GL_RGBA can specify the value of the Alpha component. Further, we can use Glcolor4f () to replace GLCOLOR3F (). Most people think that Alpha components represent the transparency of materials. That is to say, the materials represented by the alpha value of 0.0 are completely transparent. The materials represented by the Alpha value of 1.0 are completely opaque. Mixed formula If you don't catch a cold in mathematics, just want to see how to achieve transparency, please skip this section. If you want to understand (color) mixed working principle, this section should suit you. "Translator Note: It is not difficult to ^ - ^. The formula in the original text is as follows, and CKer is more embracing. In fact, the basic principle of mixing is to separate the color and background color of the image that will be divided into, according to the RGB rule, according to the RGB color component * alpha value background RGB color components * (1-alpha value) ) - After a simple formula is mixed, the mixed RGB component will be reconfigured. The formula is as follows: (RS SR RD DR, GS SG GD DG, BS SB BD DB, AS SA AD DA) OpenGL calculates the color mixing results of the two pixels in accordance with the formulas above. Small-written S and R represent the source pixels and target pixels, respectively. Uprising s and d are corresponding mixing factors. These determine how you mix these pixels. In most cases, the alpha mixing color value of each color channel is the same, so that there is (AS, AS, AS, AS), and the target pixels are 1, 1, 1, 1) - (AS, AS, AS, AS). The above formula becomes the following appearance: (RS AS RD (1 - As), GS AS GD (1 - As), BS AS BS (1 - As), AS AD (1 - as) This formula generates a transparent / translucent effect. The steps in OpenGL implementation in OpenGL are similar to the OpenGL process we mentioned earlier. The formula is then set and the write depth cache is turned off when drawing a transparent object. Because we want to draw objects behind translucent graphics. This is not the correct mixing method, but most of this approach works very well in a simple project. Rui Martins Supplement: The correct mixing process should be a transparent graphic after drawing all scenes. And to draw (the farthest object) in the opposite order of the depth cache. Consider mixing two polygons (1 and 2) Alpha mix, different drawings will be different results. (Here, it is assumed that the polygon 1 is recently, then the correct process should first draw polygon 2, then draw polygon 1. As you see it again, from these two The light behind the two "polygons Always pass through the polygon 2, then pass through the polygon 1, finally arrive at the eye of the observer.) When the depth cache is enabled, you should sort the transparent graphics according to the depth, and draw these transparenches after all the scene plots object. Otherwise you will get an incorrect result. I know that something is very painful, but this is the right way. We will use the code of the seventh lesson. At first, first add two new variables at the beginning of the code. I have rewritten the whole code for the clear start. #include // // windows header file

#include // Standard input and output library header file

#include // OpenGL32 library header file

#include // Glu32 library header file

#include // GLAUX library header file

HDC HDC = NULL; // Private GDI Device Description Table

HGLRC HRC = NULL; // Permanent Coloring Description Table

HWND HWND = NULL; / / Save our window handle

Hinstance hinstance; // Save the program's instance

Bool Keys [256]; // A number of groups for keyboard routines

Bool Active = true; // Events flag of the window, default TRUE

Bool fullscreen = true; // full screen sign default setting into full screen mode

BOOL LIGHT; / / Light Source Over / Off // Lighting ON / OFF

BOOL BLEND; // Blending Off / ON? (Add)

BOOL LP; / / L button?

BOOL FP; / / F button presses?

BOOL BP; // B button? (Add)

GLFLOAT XROT; // X Rotate

GLFLOAT YROT; // Y Rotate

GLFLOAT XSPEED; // X Rotation Speed

GLFLOAT YSPEED; // Y Rotation Speed

GLFLOAT Z = -5.0F; // Deepen the distance from the screen

GLFLOAT LIGHTAMBIENT [] = {0.5F}; // Environmental light parameters

GLFLOAT LIGHTDIFFUSE [] = {1.0F}; // diffuse light parameters

GLFLOAT LIGHTPSITION [] = {0.0F}; // Light source location

Gluint Filter; // Filter Type

Gluint Texture [3]; // 3 storage space for texture

LResult Callback WndProc (HWND, UINT, WPARAM, LPARAM); // WNDPROC Definition

Then move down to loadgltextures () here. Find "IF (TextureImage [0] = LoadBMP (" Data / Crate.bmp ")" "" We are now using a colored glass texture to replace the wooden box texture in the previous lesson. IF (TextureImage [0] = LoadBMP ("DATA / Glass.BMP")); / / Load Glass Bitmap (Modified)

The following two rows are added to the initgl () code section. The first line draws this object with full brightness and 50% Alpha mixed (translucent). When the mixing option is turned on, this object will generate a 50% transparent effect. The second line setting is used in the mixing type. RUI Martins Supplement: The value of the Alpha channel is 0.0 means that the material material is completely transparent. 1.0 means completely opaque. Glcolor4f (1.0F, 1.0F, 1.0F, 0.5F); // full brightness, 50% alpha mix (new)

GLBLENDFUNC (GL_SRC_ALPHA, GL_ONE); // Based on the translucent hybrid function based on source pixel ALPHA channel value (new)

Find the following code segment in the place close to the seventh class. IF (Keys [vk_left]) // LEFT direction button presses?

{

YSPEED- = 0.01f; // If yes, reduce YSPEED

}

The above code, we increase the following code. These lines monitor if the B key is pressed. If so, the computer checks if the mixing option has been opened. Then set it to the opposite state. IF (Keys ['B'] &&! BP) // B Jushi and BP is false? {

BP = true; // If it is, BP is set to TRUE

Blend =! blend; // True / false to switch mix options

What is IF (BLEND) / / mix?

{

Glenable (gl_blend); // Open the mix

GLDISABLE (GL_DEPTH_TEST); // Turn off depth test

}

ELSE / / otherwise

{

GLDISABLE (GL_BLEND); // Close mix

Glenable (GL_DEPTH_TEST); // Open depth test

}

}

IF (! keys ['b']) // b key loose?

{

BP = false; // If it is, BP is set to FALSE

}

But how can I specify the color when using the texture map? It is very simple, when adjusting the map mode, the color of each pixel point of the document map is multiplied by the alpha channel parameters with the current pixel color. owned. For example, the color drawn is (0.5, 0.6, 0.4), we will get the color by color (0.5, 0.6, 0.4, 0.2) (the alpha parameter is not specified, default is zero). So is this! OpenGL is really simple to achieve Alpha mix! Original note (11/13/99) I (nehe) mixed code has been modified to make the displayed object look more realistic. At the same time, the source pixels and destination pixels are mixed using Alpha parameters, which will cause the artificial traces of the object look obvious. It will cause the back of the object to be darker along the side. Basically, the physical experience looks very weird. The mixing method I used may not be the best, but it is indeed possible. After the light source is enabled, the object looks realistic. Thanks to the original code provided by TOM, the mixing method he use is correct, but the object looks not as the appeal :) The code is again modified because the gldepthmask () function is addressed in some graphics cards. problem. This command does not seem to be very valid when the depth buffer test is enabled or turned off on some cards, so I have enabled or shut down the code for depth buffering tests into old-fashioned Glenable and GLDISABLE. The ALPHA mixture of texture maps is used for texture maps that can be read from the problem maps like colors. The method is as follows, you need to obtain Alpha parameters while loading the required material. Then use the color format of GL_RGBA when you call GLTexImage2D (). "Translator: NEHE's document seems very simple, it seems very rushed. But if you want to come to such a master, have you seen a few? Or that sentence, I am not a master, I hope you are sincere. Here is the source code download link.

转载请注明原文地址:https://www.9cbs.com/read-2206.html

New Post(0)