Blending and Transparency
Blending in OpenGL provides pixel-level control of RGBA color storage in the color buffer. To enable blending we must first call glEnable(GL_BLEND). We have to set up the blending function glBlendFunc with two arguments: the source and the destination colors. By default these are GL_ONE and GL_ZERO respectively, which is equivalent to glDisable(GL_BLEND). The blend functions are applied to the source color set by glColor and destination color in the color buffer. The results of the blending functions are added together to generate the new color value which is put onscreen.
Transparency is perhaps the most typical use of blending. In order to produce transparency we should set up the blending function as follows - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). This combination takes the source color, scales it based on the alpha component and then adds the destination pixel color scaled by 1 minus the alpha value. It basically takes a fraction of the current drawing color and overlays it on the pixel on the screen. The alpha component can be from 0 (completely transparent) to 1 (completely opaque).
Anti-Aliasing
You might have noticed in some of your OpenGL pictures that lines, especially nearly horizontal or nearly vertical ones, appear jagged. These jaggies appear because the ideal line is approximated by a series of pixels that must lie on the pixel grid. The jaggedness is called aliasing. Antialiasing can be enabled by calling the function - glEnable(GL_POLYGON_SMOOTH) for polygons.
Fog
An entire image can be made to appear more natural by adding fog, which makes objects fade into the distance. Fog is a general term that describes similar forms of atmospheric effects; it can be used to simulate haze, mist, smoke, or pollution. Fog is essential in visual-simulation applications, where limited visibility needs to be approximated.
When fog is enabled, objects that are farther from the viewpoint begin to fade into the fog color. You can control the density of the fog, which determines the rate at which objects fade as the distance increases, as well as the fog's color. Since fog is applied after matrix transformations, lighting, and texturing are performed, it affects transformed, lit, and textured objects.
Using fog is easy. Enable it by passing GL_FOG to glEnable(), and you choose the color and the equation that controls the density with glFog*().
1,在CCY457OpenGLView类中加入下列布尔变量,分别代表是否开启混合,雾和反锯齿效果。
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->//Effects
BOOLm_blend;
BOOLm_fog;
BOOLm_antialias;
并在构造函数中对其初始化:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->CCY457OpenGLView::CCY457OpenGLView()
{
m_xRot=0.0f;
m_yRot=0.0f;
m_texWrap=GL_CLAMP;
m_texMode=GL_DECAL;
m_texFilter=GL_NEAREST;
m_blend=FALSE;
m_fog=FALSE;
m_antialias=FALSE;
}
2,加入控制上述三种效果的菜单项及其事件处理函数
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCOpenGLView::OnEffectsBlending()
{
//TODO:Addyourcommandhandlercodehere
m_blend=!m_blend;
if(m_blend)
glEnable(GL_BLEND);
else
glDisable(GL_BLEND);
InvalidateRect(NULL,FALSE);
}
voidCOpenGLView::OnUpdateEffectsBlending(CCmdUI*pCmdUI)
{
//TODO:AddyourcommandupdateUIhandlercodehere
pCmdUI->SetCheck(m_blend);
}
voidCOpenGLView::OnEffectsAntialiasing()
{
//TODO:Addyourcommandhandlercodehere
m_antialias=!m_antialias;
if(m_antialias)
glEnable(GL_POLYGON_SMOOTH);
else
glDisable(GL_POLYGON_SMOOTH);
InvalidateRect(NULL,FALSE);
}
voidCOpenGLView::OnUpdateEffectsAntialiasing(CCmdUI*pCmdUI)
{
//TODO:AddyourcommandupdateUIhandlercodehere
pCmdUI->SetCheck(m_antialias);
}
voidCOpenGLView::OnEffectsFog()
{
//TODO:Addyourcommandhandlercodehere
m_fog=!m_fog;
if(m_fog)
glEnable(GL_FOG);
else
glDisable(GL_FOG);
InvalidateRect(NULL,FALSE);
}
voidCOpenGLView::OnUpdateEffectsFog(CCmdUI*pCmdUI)
{
//TODO:AddyourcommandupdateUIhandlercodehere
pCmdUI->SetCheck(m_fog);
}
3,在InitializeOpenGL函数中对雾效果进行设置。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->BOOLCCY457OpenGLView::InitializeOpenGL()
{
//GetaDCfortheClientArea
m_pDC=newCClientDC(this);
//FailuretoGetDC
if(m_pDC==NULL)
{
MessageBox("ErrorObtainingDC");
returnFALSE;
}
//Failuretosetthepixelformat
if(!SetupPixelFormat())
{
returnFALSE;
}
//CreateRenderingContext
m_hRC=::wglCreateContext(m_pDC->GetSafeHdc());
//FailuretoCreateRenderingContext
if(m_hRC==0)
{
MessageBox("ErrorCreatingRC");
returnFALSE;
}
//MaketheRCCurrent
if(::wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC)==FALSE)
{
MessageBox("ErrormakingRCCurrent");
returnFALSE;
}
//SpecifyBlackastheclearcolor
::glClearColor(0.0f,0.0f,0.0f,0.0f);
//Specifythebackofthebufferascleardepth
::glClearDepth(1.0f);
//EnableDepthTesting
::glEnable(GL_DEPTH_TEST);
::glShadeModel(GL_SMOOTH);
//设置雾
::glFogi(GL_FOG_MODE,GL_EXP);
GLfloatfog_color[4]={0.2f,0.2f,0.2f,0.0f};
::glFogfv(GL_FOG_COLOR,fog_color);
::glFogf(GL_FOG_DENSITY,0.25);
//加载纹理
LoadGLTextures();
//设置灯光
SetupLighting();
::glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
returnTRUE;
}
分享到:
相关推荐
《基于MFC的OpenGL编程》Part 11 Blending, Antialiasing and Fog
9. Antialiasing 10. Lighting Techniques 11. Scene Realism 12. Transparency 13. Image Processing 14. Special Effects 15. Illustration and Artistic Techniques 16. Scientific Visualization Techniques 17....
OpenGL® ES™ is the industry’s leading software interface and graphics library for rendering sophisticated 3D graphics on handheld and embedded devices. The newest version, OpenGL ES 3.0, makes it ...
在Visual Studio 2015上基于OpenGL实现混合(透明物体处理)。 首先介绍一下OpenGL中混合的基本概念。混合是实现物体透明的一种技术。当一个物体时透明的,我们看到的颜色是物体本身的颜色和它背后其它物体的颜色的...
OpenGL 中的很多特效都是通过混合(Blending) 来完成的, 混合就是把屏幕上已有的颜色与新的颜色进行合成。 至于合成的方法, 取决于颜色的Alpha值, 还有/或所用的混合函数。 Alpha值是颜色的第4个分量, 过去你使用...
OpenGL ES 2.0 is the industry's leading software interface and graphics library for rendering sophisticated 3D graphics on handheld and embedded devices. With OpenGL ES 2.0, the full programmability ...
The functions in the OpenGL library enable programmers to build geometric models, view models interactively in 3D space, control color and lighting, manipulate pixels, and perform such tasks as alpha ...
On the Blending of the Landsat and MODIS Surface Reflectance: Predicting Daily Landsat Surface Reflectance; Data fusion, image enhancement, image processing, Landsat, Moderate Resolution Imaging ...
Antialiasing Procedural Textures Section 17.1. Sources of Aliasing Section 17.2. Avoiding Aliasing Section 17.3. Increasing Resolution Section 17.4. Antialiased Stripe Example Section 17.5. Frequency...
Quadtree Displacement Mapping with Height Blending Michal Drobot Overview Introduction Overview of Ray-Tracing Algorithms Quadtree Displacement Mapping Self-Shadowing Ambient Occlusion Surface ...
In Pro OpenGL ES for Android, you'll find out how to harness the full power of OpenGL ES, and design your own 3D applications by building a fully-functional 3D solar system model using Open GL ES!...
In Pro OpenGL ES for Android, you'll find out how to harness the full power of OpenGL ES, and design your own 3D applications by building a fully-functional 3D solar system model using Open GL ES!...
图像融合算法介绍课件
It includes new Direct3D 11 features such as hardware tessellation and the compute shader, and covers advanced rendering techniques such as ambient occlusion, normal and displacement mapping, shadow ...
自做的opengl半透明效果,其中还包含obj模型加载,mipmap纹理。因为要有一个场景演示半透明效果;半透明效果给出了两种不同的混合因子的效果。
It includes new Direct3D 11 features such as hardware tessellation and the compute shader, and covers advanced rendering techniques such as ambient occlusion, normal and displacement mapping, shadow ...
一个演示使用D3D11实现透明效果的示例程序,包括主程序和一个程序框架,附带可执行程序。如有问题,欢迎交流~
This book is based on modern OpenGL v3.3 and above. It covers a myriad of topics of interest ranging from basic camera models and view frustum culling to advanced topics, such as dual quaternion ...
基于MATLAB的 拉普拉斯金字塔图像融合,Laplacian-Pyramid-Blending
vc(opengl)一个能自动转动的立方体,可能不是很好.