《基于MFC的OpenGL编程》Part 11 Blending, Antialiasing and Fog

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
并在构造函数中对其初始化:

复制代码
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,加入控制上述三种效果的菜单项及其事件处理函数

复制代码
void COpenGLView::OnEffectsBlending() 
{
    // TODO: Add your command handler code here
    m_blend = !m_blend;
    if(m_blend)
        glEnable(GL_BLEND);
    else
        glDisable(GL_BLEND);
    InvalidateRect(NULL,FALSE);
}
void COpenGLView::OnUpdateEffectsBlending(CCmdUI* pCmdUI) 
{
    // TODO: Add your command update UI handler code here
    pCmdUI->SetCheck(m_blend);
}
void COpenGLView::OnEffectsAntialiasing() 
{
    // TODO: Add your command handler code here
    m_antialias = !m_antialias;
    if(m_antialias)
        glEnable(GL_POLYGON_SMOOTH);
    else
        glDisable(GL_POLYGON_SMOOTH);
    InvalidateRect(NULL,FALSE);
}
void COpenGLView::OnUpdateEffectsAntialiasing(CCmdUI* pCmdUI) 
{
    // TODO: Add your command update UI handler code here
    pCmdUI->SetCheck(m_antialias);    
}
void COpenGLView::OnEffectsFog() 
{
    // TODO: Add your command handler code here
    m_fog = !m_fog;
    if(m_fog)
        glEnable(GL_FOG);
    else
        glDisable(GL_FOG);
    InvalidateRect(NULL,FALSE);
}
void COpenGLView::OnUpdateEffectsFog(CCmdUI* pCmdUI) 
{
    // TODO: Add your command update UI handler code here
    pCmdUI->SetCheck(m_fog);
}
复制代码
3,在InitializeOpenGL函数中对雾效果进行设置。

复制代码
BOOL CCY457OpenGLView::InitializeOpenGL()
{
    //Get a DC for the Client Area
    m_pDC = new CClientDC(this);
    //Failure to Get DC
    if(m_pDC == NULL)
    {
        MessageBox("Error Obtaining DC");
        return FALSE;
    }
    //Failure to set the pixel format
    if(!SetupPixelFormat())
    {
        return FALSE;
    }
    //Create Rendering Context
    m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());
    //Failure to Create Rendering Context
    if(m_hRC == 0)
    {
        MessageBox("Error Creating RC");
        return FALSE;
    }
    //Make the RC Current
    if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE)
    {
        MessageBox("Error making RC Current");
        return FALSE;
    }
    //Specify Black as the clear color
    ::glClearColor(0.0f,0.0f,0.0f,0.0f);
    //Specify the back of the buffer as clear depth
    ::glClearDepth(1.0f);
    //Enable Depth Testing
    ::glEnable(GL_DEPTH_TEST);
    ::glShadeModel(GL_SMOOTH);
    //设置雾
    ::glFogi(GL_FOG_MODE, GL_EXP);
    GLfloat fog_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);
    return TRUE;
}
复制代码

本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/06/1327948.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
D3D11是Direct3D 11的简称,是一种用于绘制图形的API。Alpha blending是一种用于混合处理图形的技术,主要用于实现透明效果。 一个经典的D3D11 alpha blending的例子是在绘制两个具有透明度的图形时,根据其透明度值来混合两个图形的颜色。例如,假设我们有一个红色的正方形和一个蓝色的圆形,它们的透明度分别为0.5和0.2。 首先,我们需要在D3D11中创建两个纹理对象,分别对应红色正方形和蓝色圆形的图像。然后,我们将这两个纹理对象绑定到对应的渲染目标上。 接下来,我们需要在渲染管线中启用alpha blending功能,通过设置混合状态对象来实现。混合状态对象可以定义混合操作,例如使用源颜色和目标颜色的比例。 然后,在绘制图形之前,我们要设置两个图形的透明度值。我们可以使用顶点着色器或者像素着色器来设置透明度。 最后,在绘制图形时,渲染管线将根据透明度值和混合状态对象来执行alpha blending的操作,将两个图形的颜色按透明度进行混合。例如,红色正方形的颜色将以0.5的透明度与蓝色圆形的颜色以0.2的透明度进行混合,最终呈现出一种融合红色和蓝色的颜色。 这只是一个简单的例子,实际应用中,D3D11 alpha blending可以用于创建各种透明效果,例如烟雾、水波纹等。通过合理设置透明度和混合操作,我们可以实现更加复杂的图形效果和混合效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值