《基于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
//Effects
BOOL m_blend;
BOOL m_fog;
BOOL m_antialias;

并在构造函数中对其初始化:

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;
}

时间: 2024-10-30 07:10:08

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

《基于MFC的OpenGL编程》Part 19 Creating a Virtual Reality Walkthrough…

<基于MFC的OpenGL编程>Part 19 Creating a Virtual Reality Walkthrough Application 本文是整个系列文章的最后一篇,将创建一个完整的虚拟office应用程序(如图所示)来做为ending. 1,在CCY457OpenGLView类中加入下述变量,用来保存office内各个物体的显示列表 //Display List Names GLuint m_SceneList; GLuint m_ComputerList; GLuint m_

《基于MFC的OpenGL编程》Part 5 Transformations

<基于MFC的OpenGL编程>Part 5 Transformations - Rotations,Translations and Scaling Transformations - Translation, Rotation and Scaling Translation is nothing but moving along an arbitrary axis. Rotation is spinning about an arbitrary axis. Scaling is incre

《基于MFC的OpenGL编程》Part 2 Setting up OpenGL on Windows

WGL – Windows的 OpenGL扩展层 The WGL extension consists of a set of functions (wglCreateContext, wglDeleteContext etc.) and structures (such as PIXELFORMATDESCRIPTOR, GLYPHMETRICSFLOAT) etc. Thus every OpenGL implementation has a platform-specific portio

《基于MFC的OpenGL编程》Part 14 Quadrics

本文在第11篇文章的基础上,为其加入显示各种二次曲面的代码: Quadrics Every quadric has a few settings associated with it. We have to create a quadric first and then customize its settings to render the shape we want. The gluNewQuadric function creates a state variable that descr

《基于MFC的OpenGL编程》Part 12 Creating and Using Display Lists

本文对第11篇文章进行修改,使用显示列表来存储渲染命令. 显示列表 OpenGL provides a facility to create a preprocessed set of OpenGL commands called a display list. Creating a display list is a straight forward process. We just have to delimit the display list code with glNewList an

《基于MFC的OpenGL编程》Part 1 A Primer

3D图形学基本概念 Perspective Perspective refers to the angles between the lines that lend the illusion of three dimensions. Colors and Shading Moving beyond line drawing, we need to add color to create a solid object. Shading refers to the way the color is

《基于MFC的OpenGL编程》Part 18 Reading objects from the OBJ File Format

本文将介绍如何从Obj文件格式中创建3D对象,我们使用的是Nate Miller的obj格式加载类. This would be very useful to create large Virtual Reality applications as we could make use of the readily available 3D model files or make use of modeling tools to create these models and load them

《基于MFC的OpenGL编程》Part 17 Shadows

Shadows Conceptually drawing a shadow is quite simple. A shadow is produced when an object keeps light from a source from striking some object or surface behind the object, casting the shadow. The area on the shadowed object's surface outlined by the

《基于MFC的OpenGL编程》Part 15 Selection

Selection Selection is a powerful feature of OpenGL that allows you click at some position of the OpenGL window using the mouse and determine which of your objects lie beneath it. The act of selecting a specific object is called Picking. With OpenGL'