Principle of transparent sorting

Source: Internet
Author: User
What is deep cache first?

Is a two-dimensional array, usually 32 bits.

Determine whether the color cache is sent to the Pipeline Based on the depth.

You can customize judgment rules.

2. how to use that?

Deep cache:

It can be used to draw translucent images.

When creating a translucent image, enable hybrid, set the depth to read-only, and enable deep testing.

Deep testing: determines whether the current transparent pixel is sent to the color buffer.

To determine which part needs to be re-painted.

3. What is the problem?

In this way, the depth test only tests the depth of an opaque object.

It is equivalent to attaching all transparent objects to an opaque object.

4. How to think of the problem?

Rules for creating a translucent object: draw an opaque object first, and then draw a transparent object.

1. If it is overwritten, it will not be mixed.

2. If it is not covered, mix it.

5. Why is this order?

In fact, there is no order problem when a is mixed with B and then mixed with C.

The answer is no !!

Because the so-called mixing all uses Multiplication...

Conforms to the exchange law.

Ha ha, the primary school knowledge is used.

<P style = line-Height: 150%> Haha, these two lessons have been translated for a long time and have never been posted. You have waited for a long time (Can someone wait ?)

 

<P style = line-Height: 150%> simple and transparent
The vast majority of effects in OpenGL are related to some types of (color) mixing.
The definition of the mixed color is to combine the color of a certain pixel with the color of the corresponding pixel that has been drawn on the screen.
How to combine these two colors depends on the component values of the alpha channel of the color and/or the mixed color function used.
Alpha is usually the 4th color component at the end of the color value.
In the previous lessons, we used gl_rgb to specify three components of the color.
The corresponding gl_rgba can specify the Alpha component value.
Furthermore, we can use glcolor4f () instead of glcolor3f ().

 

<P style = line-Height: 150%> most people think that the Alpha component represents the transparency of the material.
This means that when the Alpha value is 0.0, the material is completely transparent.
When the Alpha value is 1.0, the material is completely opaque.

 

<P style = line-Height: 150%> color mixing formula
If you are not familiar with mathematics and want to see how to make it transparent, skip this section.
If you want to deeply understand the working principle of (color) mixing, this section should be suitable for you.
"The addition of cker is not difficult ^-^. In the original article, the formula is as follows.
In fact, the basic principle of mixing is that the color and background color of each pixel of the image to be divided are separated according to the RGB rules,
Based on the-RGB color component of the image * Alpha value + RGB color component of the background * (1-Alpha value)
-After such a simple formula is used for mixing, the RGB components obtained by mixing are re-merged .』

 

<P style = line-Height: 150%> the formula is as follows:
(RS Sr + rd dr, gs sg + gd dg, BS Sb + bd dB, as SA + AD da)
OpenGL calculates the mixed color of the two pixels according to the above formula.
The lower-case s and R indicate the source pixel and the target pixel respectively. Uppercase S and D are the corresponding color mixing factors.
These determine how you mix these pixels.
In most cases, the Alpha values of different color channels are the same,
In this way, the source pixel (as, ),
The target pixel is 1, 1, 1, 1)-(as, ).
The above formula is like the following:
(RS as + RD (1-As), GS as + Gd (1-As), BS as + BS (1-), as as + AD (1-))
This formula will generate transparent/translucent effects.

 

<P style = line-Height: 150%> color mixing in OpenGL
The steps for implementing color mixing in OpenGL are similar to the OpenGL process we mentioned earlier.
Then, set the formula and disable the write depth cache when creating a transparent object.
Because we want to draw objects behind a translucent image.
This is not the correct color mixing method, but most of the time this method works well in simple projects.

 

<P style = line-Height: 150%> supplement to Rui Martins: The correct color mixing process should be to draw all the scenes first and then draw transparent images.
In addition, the image must be drawn in the order opposite to the depth cache (first draw the farthest object ).
Consider mixing two polygon (1 and 2) with Alpha, and different draw orders will produce different results.
(Assuming that Polygon 1 is closest to the observer, draw polygon 2 and then polygon 1 in the correct process.
As you can see in reality,
The light from the two <transparent> polygon always first passes through polygon 2,
Then pass through polygon 1 to reach the observer's eyes .)
When deep cache is enabled, you should sort transparent images by depth,
And then draw these transparent objects after all the scenes are drawn. Otherwise, you will get incorrect results.
I know it is very painful to do so sometimes, but this is the right method.

 

<P style = line-Height: 150%> we will useCode.
Add two new variables at the beginning of the Code. For clarity, I have rewritten the entire code segment.

 

<P style = line-Height: 150%>}
VaR
H_rc: hglrc; // rendering context (coloring description table ).
H_dc: HDC; // device context (device description table)
H_wnd: hwnd; // window handle
H_instance: hinst ;//ProgramInstance ).
Keys: array [0 .. 255] of Boolean; // Array Used for keyboard routines

 

<P style = line-Height: 150%> light: Boolean; // on/off of the light source

 

<P style = line-Height: 150%> Blend: Boolean; // blending off/on? (New)

 

<P style = line-Height: 150%> LP: Boolean; // is the L key pressed?
FP: Boolean; // is the F key pressed?

 

<P style = line-Height: 150%> BP: Boolean; // is the key B pressed? (New)

 

<P style = line-Height: 150%> xrot: glfloat; // X Rotation
Yrot: glfloat; // y Rotation
Xspeed: glfloat; // X rotation speed
Yspeed: glfloat; // y rotation speed

 

<P style = line-Height: 150%> Z: glfloat =-5.0 F; // The distance between the two screens.
Lightambient: array [0 .. 3] of glfloat = (0.5, 0.5, 0.5, 1.0); // ambient light parameter (new)
Lightdiffuse: array [0 .. 3] of glfloat = (1.0, 1.0, 1.0, 1.0); // diffuse light parameter (added)
Lightposition: array [0 .. 3] of glfloat = (0.0, 0.0, 2.0, 1.0); // Light Source Position (new)
Filter: gluint; // Filter Type
Texture: array [0 .. 2] of gluint; // storage space of three textures

 

<P style = line-Height: 150%> procedure glgentextures (N: glsizei; var textures: gluint); stdcall; external
Opengl32;

 

<P style = line-Height: 150%> procedure glbindtexture (target: glenum; texture: gluint); stdcall; external
Opengl32;

 

<P style = line-Height: 150%> function glubuild2dmipmaps (target: glenum; components, width, height: glint;
Format, Atype: glenum; Data: pointer): integer; stdcall; External glu32 name
Glubuild2dmipmaps;

 

<P style = line-Height: 150%> {
Then move down to loadgltextures () here.
Find if (textureimage [0] = loadbmp (data/crate.bmp ))
This line. We now use colored glass textures to replace the wooden case textures in the previous lesson.
If (textureimage [0] = loadbmp ("Data/glass.bmp"); // load the glass Bitmap (modified)
}

 

<P style = line-Height: 150%> function loadtexture: Boolean; // load the bitmap and convert it to a texture.
VaR
Status: Boolean; // Status Indicator
Textureimage: array [0 .. 1] of ptaux_rgbimagerec; // create a texture Bucket
Begin
Status: = false;
Zeromemory (@ textureimage, sizeof (textureimage); // set the pointer to null
Textureimage [0]: = loadbmp (walls.bmp );
If textureimage [0] <> nil then
Begin
Status: = true; // set status to true
Glgentextures (1, texture [0]); // create a texture
// Create a nearest filter texture
Glbindtexture (gl_texture_2d, texture [0]);
// Generate texture
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_nearest); // (new)
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_nearest); // (new)

 

<P style = line-Height: 150%> glteximage2d (gl_texture_2d, 0, 3, textureimage [0]. sizex,
Textureimage [0]. sizey, 0, gl_rgb, gl_unsigned_byte,
Textureimage [0]. data );
Glbindtexture (gl_texture_2d, texture [1]); // use a typical texture generated from the bitmap data
// Generate texture
Glteximage2d (gl_texture_2d, 0, 3, textureimage [0]. sizex,
Textureimage [0]. sizey, 0, gl_rgb, gl_unsigned_byte,
Textureimage [0]. data );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear); // Linear Filter
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear); // Linear Filter
// Create a mipmapped texture
Glbindtexture (gl_texture_2d, texture [2]);
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter,
Gl_linear_mipmap_nearest); // (new)
Glubuild2dmipmaps (gl_texture_2d, 3, textureimage [0]. sizex,
Textureimage [0]. sizey, gl_rgb, gl_unsigned_byte,
Textureimage [0]. data); // (new )}
End;
If assigned (textureimage [0]) Then // whether the texture exists
If assigned (textureimage [0]. Data) Then // whether the texture image exists
Textureimage [0]. Data: = nil; // release the memory occupied by the texture image
Textureimage [0]: = nil; // release the image structure
Result: = status; // return status
End;

 

<P style = line-Height: 150%> {
Add the following two lines to the glinit () code segment.
The first line draws the object in full brightness and performs a 50% Alpha mixture (translucent) on it ).
When the hybrid option is enabled, this object will produce a 50% transparent effect.
Set the hybrid type in the second row.

 

<P style = line-Height: 150%> supplement to Rui Martins:
The alpha channel value is 0.0, which means that the object material is completely transparent.
1.0 means that it is completely opaque.
}

 

<P style = line-Height: 150%> procedure glinit (); // All OpenGL settings are started here.
Begin
If (not loadtexture) Then // call the texture loading subroutine
Exit; // exit if loading fails.

 

<P style = line-Height: 150%> glable (gl_texture_2d); // enable texture ing
Glshademodel (gl_smooth); // enables shadow smoothing.
Glclearcolor (0.0, 0.0, 0.0, 0.0); // black background
Glcleardepth (1.0); // sets the depth cache.
Glable (gl_depth_test); // enable deep Test
Gldepthfunc (gl_less); // type of the deep Test
Glhint (gl_perspective_correction_hint, gl_nicest); // highly optimized Perspective Projection computing
Gllightfv (gl_light1, gl_ambient, @ lightambient [0]); // sets the ambient light.
Gllightfv (gl_light1, gl_diffuse, @ lightdiffuse [0]); // sets the diffuse light.
Gllightfv (gl_light1, gl_position, @ lightposition); // The Position of the light source.
Glable (gl_light1); // enable light source 1

 

<P style = line-Height: 150%> glcolor4f (1.0, 1.0, 1.0, 0.5); // brightness, 50% Alpha mixture (new)
Glblendfunc (gl_src_alpha, gl_one); // translucent hybrid function based on the Alpha Channel value of the source pixel (new)

 

<P style = line-Height: 150%> end;

 

<P style = line-Height: 150%> {find the following code snippet near the end of Lesson 7.
If keys [vk_left] Then // is the left direction key pressed?
Yspeed: = yspeed-0.01; // If yes, yspeed is reduced.
Next, we add the following code.
These lines monitor whether keys B are pressed.
If yes, check whether the mixed options are enabled.
Set it to the opposite state.
}
If (Keys [ord (B)] and not bp) Then // B Jian pressed and BP is false?
Begin
BP: = true; // If yes, BP is set to true.
Blend: = not blend; // switch between the true and false mixed options.
If (blend) Then // is it enabled together?
Begin
Glable (gl_blend); // open the hybrid
Gldisable (gl_depth_test); // disable the deep test.
End
Else // otherwise
Begin
Gldisable (gl_blend); // disable Hybrid
Glable (gl_depth_test); // enable the deep test.
End;
End;
If (not keys [ord (B)]) Then // is the key B released?
Begin
BP: = false; // If yes, BP is set to false.
End;
{
But how can we specify the color of the mixture when texture textures are used? Very simple,
When adjusting the texture mode, the color of each pixel point of the texture is determined by the alpha channel parameter.
Obtained by multiplying the color of the current geographic pixel.
For example, if the color is (0.5, 0.6, 0.4 ),
We will multiply the colors (0.5, 0.6, 0.4, 0.2)
(If the Alpha parameter is not specified, the default value is 0 ).
That's it! It is really easy to implement Alpha mixing Using OpenGL!
}
{
Original article note (11/13/99)
I (nehe) mixed color code was modified to make the displayed object look more realistic.
At the same time, the Alpha parameter is used to mix the source pixel and the target pixel, which will make the artificial trace of the object very obvious.
It will make the back of the object look darker along the side.
Basically, objects look weird.
The color mixing method I used may not be the best, but it does work.
After the light source is enabled, the object looks lifelike.
Thanks to the original code provided by Tom, the mixed color method is correct,
But the object does not look as attractive as expected :)
The code is modified again because the gldepthmask () function has addressing problems on some graphics cards.
This command does not seem very effective when enabling or disabling deep buffer tests on some cards,
So I have converted the code that enables or disables the deep buffer test into the old-fashioned glenable and gldisable.

 

<P style = line-Height: 150%> alpha mixing of texture textures
The Alpha parameter used for texture maps can be read from the problem maps like the color.
The method is as follows. You need to obtain the Alpha parameter while loading the required material.
Then, the gl_rgba color format is used when glteximage2d () is called.
}

 

<P style = line-Height: 150%>

 

<P style = line-Height: 150%>

 

<P style = line-Height: 150%> // run the command to check the effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.