Win32 OpenGL programming (12) mixing and translucent Effect

Source: Internet
Author: User
Tags mercurial

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie


Discuss newsgroups and documents

Technorati label: Hybrid
, Blend
, Translucent
, OpenGL
, 3D
, Graphic
, Programming

All the power of mankind is a mixture of patience and time. --Balzac

The hybrid power is very powerful. As Balzac said, many interesting and practical effects of graphic processing can be achieved by using a mixture in OpenGL, for example, the most commonly used semi-transparent effect, which has been used to the extreme in the 3D desktop Effect of compiz, is impressive, even if you never use Linux, you can also watch a video showing the effects of Linux Desktop by many people. You Need To Know Where Vista is when the results come out. Haha, unfortunately, the performance of a desktop system is not just a comparison between others .....

 

Concept

In OpenGL, what is hybrid? Just like the general meaning of mixing, the combination in OpenGL refers to the combination of two graphics, but this combination can be defined in many ways. Mixing is a simple concept, and there are not too many related interfaces. However, because there are many customizable content, parameters are complex and the meaning of each parameter is simple, but you need to know when to use what kind of parameter is not a simple concept, like lighting.

The most common example in real life is to see something through glass, the image you see is actually an image formed in the eyes after the light reflected by the glass and the light reflected by the object after the glass passes through the glass, it is described in OpenGL as an image produced after the mixture of the light reflected by the object after the glass and the light reflected by the glass.

Alpha, a value that we have never used before, plays a key role in mixing. We generally call it transparency, but in fact, this value can be used for more purposes in mixing, of course, it is no problem to use it only as transparency.

Out of interest in mind map, let's make a picture:

 

 

 

Use

In OpenGL, the new fragment becomes the source and the existing image becomes the target. In use, use glblendfunc {separate} to specify how to calculate the mixed factor of the source and target, then, use glblendequation {separate} to specify how to mix (that is, to specify the mixed mode). The procedure is quite simple. In addition to using gl_blend to enable it, follow the conventions, the second step is added only in OpenGL 1.2. Previously, only the Add mode is used. Now, when we ignore the second step, the Add mode is used by default. The additional separate function specifies the RGB and Alpha values respectively.

OpenGL programming guide
":

Glblendfunc-specify pixel Arithmetic
C Specification
Void glblendfunc (glenum sfactor,
Glenum dfactor );
Parameters

Sfactor

Specifies how the red, green, blue,
And Alpha source blending factors are computed.
The following symbolic constants are accepted:
Gl_zero,
Gl_one,
Gl_src_color,
Gl_one_minus_src_color,
Gl_dst_color,
Gl_one_minus_dst_color,
Gl_src_alpha,
Gl_one_minus_src_alpha,
Gl_dst_alpha,
Gl_one_minus_dst_alpha,
Gl_constant_color,
Gl_one_minus_constant_color,
Gl_constant_alpha,
Gl_one_minus_constant_alpha, and
Gl_src_alpha_saturate.
The initial value is gl_one.
Dfactor

Specifies how the red, green, blue,
And Alpha destination blending factors are computed.
The following symbolic constants are accepted:
Gl_zero,
Gl_one,
Gl_src_color,
Gl_one_minus_src_color,
Gl_dst_color,
Gl_one_minus_dst_color,
Gl_src_alpha,
Gl_one_minus_src_alpha,
Gl_dst_alpha,
Gl_one_minus_dst_alpha.
Gl_constant_color,
Gl_one_minus_constant_color,
Gl_constant_alpha, and
Gl_one_minus_constant_alpha.
The initial value is gl_zero.

Glblendequation-specify the equation used for both the RGB blend equation and the Alpha blend Equation
C Specification
Void glblendequation (glenum mode );
Parameters

Mode

Specifies how source and destination colors are combined.
It must be gl_func_add, gl_func_subtract,
Gl_func_reverse_subtract, gl_min, gl_max.

Indeed, simple use is really very simple (although there can be many combinations of parameters). In fact, as shown in the following example, just a few lines of code can achieve a translucent effect.

 
// Starts OpenGL initialization.
Void sceneinit (int w, int H)
{
Glenum err = glewinit ();
If (Err! = Glew_ OK)
{
MessageBox (null, _ T ("error"), _ T ("glew init failed."), mb_ OK );
Exit (-1 );
}

Glclearcolor (0.0, 0.0, 0.0, 0.0 );

Glable (gl_cull_face );
Glcullface (gl_back );

Glable (gl_blend );
Glblendfunc (gl_src_alpha, gl_dst_color );
}

Void drawsmoothcolorpyramid (glfloat adsize)
{
Static glfloat fpyramiddatas [] = {0.0, 1.0, 0.0, // Vertex on the triangle cone
-1.0, 0.0, 1.0, // left front vertex of the bottom
1.0, 0.0, 1.0, // bottom right bottom Vertex
0.0, 0.0,-1.0}; // bottom vertex of the bottom surface

Glfloat fpyramidsizedatas [sizeof (fpyramiddatas)/sizeof (glfloat)] = {0 };

// Calculate the size
For (INT I = 0; I <12; ++ I)
{
Fpyramidsizedatas [I] = fpyramiddatas [I] * adsize;
}

Static glfloat fpyramidcolors [] = {0.0, 0.0, 0.0, 0.7,
1.0, 0.0, 0.0, 0.7,
0.0, 1.0, 0.0, 0.7,
0.0, 0.0, 1.0, 0.7 };

Static glubyte ubyindices [] = {0, 1, 2, // front
0, 3, 1, // left side
0, 2, 3, // right side
1, 3, 2}; // bottom

Glenableclientstate (gl_vertex_array );
Glableclientstate (gl_color_array );

Glvertexpointer (3, gl_float, 0, fpyramidsizedatas );
Glcolorpointer (4, gl_float, 0, fpyramidcolors );

For (INT I = 0; I <4; ++ I)
{
Gldrawelements (gl_triangles, 3, gl_unsigned_byte, ubyindices + I * 3 );
}
}

// Do all the plotting work here
Void sceneshow (glvoid)
{
Glclear (gl_color_buffer_bit );
Glcolor3f (1.0, 0.0, 0.0 );


Glpushmatrix ();
Drawsmoothcolorpyramid (0.5 );
Drawsmoothcolorpyramid (1 );
Glpopmatrix ();

Glloadidentity ();
Glulookat (gviewposx, gviewposy, gviewposz, gviewdirx, gviewdiry, gviewdirz, gviewupdirx, gviewupdiry, gviewupdirz );

Glflush ();
}

This example was changed from 2009-10-25/glcullface. The basic idea is to draw a large triangle cone out of a small triangle cone, that is, the following sentence:

glPushMatrix();DrawSmoothColorPyramid(0.5);DrawSmoothColorPyramid(1);glPopMatrix();
In addition, the new key code is
glEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA, GL_DST_COLOR);


In either case, enable mixing and set the mixing factor as the source mixing factor to use the RGB + Alpha value. The target mixing factor color is the original color, we use the default hybrid addition mode. Is it easy? There are only two pieces of code. In addition, I have added the Alpha parameter to the color array (not available in the past)

Static glfloat fpyramidcolors [] = {0.0, 0.0, 0.0, 0.7,

1.0, 0.0, 0.0, 0.7,

0.0, 1.0, 0.0, 0.7,

0.0, 0.0, 1.0, 0.7 };

This indicates that the color Alpha value of the triangle cone is 0.7. Let's see what the translucent effect is.

 

 

 

 

 

 

 

 

 

First look at the left side, there is no small triangle cone at all, the reason is obvious, because the outside triangle cone is drawn after, it will be completely covered by the small triangle cone first drawn inside, but we can clearly see the small triangle cone on the right side, huh, because we have enabled the semi-transparent effect (implemented using a mixture.

To save space, only key clips are posted. For the complete source code, see the 2009-11-11-11/glhalftrans/directory of the source code of my blog. For details about how to obtain the complete source code of the blog, see the article.

Since I decided to explain and demonstrate the concept, this series of articles tries to demonstrate the concepts and effects of all parameters as they used, here, the use of hybrid operations can be said to be the simplest. I didn't even call the glblendequation {separate} function, but the actual use can be quite complicated, for more information about the functions of each parameter, see OpenGL programming guide.
Oracle :)

For other articles in this series, see the OpenGL topic "Win32 OpenGL series topics ".
"

References

1. OpenGL Reference Manual
, OpenGL Reference Manual

2. OpenGL
OpenGL programming guide
), Dave shreiner, Mason Woo, Jackie neider, Tom Davis
Xu Bo, Mechanical Industry Press

3. nehe OpenGL tutorials, In the http://nehe.gamedev.net/
You can find the tutorials and related code to download. (The PDF version of the tutorials is available) nehe also developed an object-oriented framework. As a demo program, this framework is very suitable. There are also Chinese Versions
Take all the necessary information.

4. OpenGL getting started, by eastcowboy, this is a good tutorial I have found on the Internet. It is quite complete and popular. This is the first address: http://bbs.pfan.cn/post-184355.html

Complete source code retrieval instructions

Due to space limitations, this article generally only posts the main focus of the Code, the full version of the Code with a project (or makefile) (if any) can be downloaded in Google code using mercurial. The article is stored in different directories on the date published by the blog post. Use mercurial to clone the following database:

Https://blog-sample-code.jtianling.googlecode.com/hg/

For how to use mercurial, see Introduction and brief introduction to the distributed and next-generation version control system mercurial.
"

If you only want to browse all the code, you can go to Google Code to view it. the following URL:

Http://code.google.com/p/jtianling/source/browse? Repo = blog-Sample-code

 

The author of the original article retains the copyright reprinted. Please indicate the original author and give a link

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

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.