NeHe OpenGL Lesson46 - Fullscreen AntiAliasing

lessson46_screenshot

This sample shows us how to switch on the MSAA (Multiple sampling Anti-Aliasing) on windows. I remember some material that I read before. The MSAA just sample several pixels instead of only one pixel at each position. Then an average pixel will be calculated out. To do that we could render the whole scene into a big picture with a increased size back buffer, then resolve such back buffer into a normal size one. The normal size one picture will be very smooth; The other method will not increase the back buffer size, just calculate those neigh pixels at the rendering time, then an average one calculated out and filled into the back buffer.

Full-screen Anti-Aliasing is a very good way to improve the visual quality. But it may be the performance hit. You could check what I said with a very simple test: comparing the FPS between the small windowed mode and full screen windowed mode. Because you increase the size of the back buffer by switch on multiple sampling, more pixels or fragments  should be shaded with color, or the primitive take more fragments, so more time we need to take care of rendering.

 

MSAA Render Buffers

As MSAA feature switch on, you also need to increase the size of the depth & stencil buffer at the same time. This may cause some errors easily with D3D, we need to provide an render target with a proper size depth & stencil buffer at the same time. And when you resolve a MSAA back buffer to a texture, it will take a bit longer time. Here is the code to calculate the total video memory size:

Vid_mem = sizeof(Front_buffer) + sizeof(Back_buffer) + num_samples
    * (sizeof(Front_buffer) +sizeof(ZS_buffer))

 

FXAA & MSAA

FXAA (Fast Approximate Anti-Aliasing) is another solution for Anti-Aliasing. It does not increase the back buffer size, and just apply some image smooth method during post-process. NVIDIA official website already provide some FXAA demo and source code. 

 

Use OpenGL MSAA on Windows

Here is the main workflow;

1) Create our window as Normal;

2) Check the “WGL_ARB_multisample”extensions & Query the possible Multiple-sampling values;

// See If The String Exists In WGL!
if (!WGLisExtensionSupported("WGL_ARB_multisample"))
{
    arbMultisampleSupported=false;
    return false;
}

// Get Our Pixel Format
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB =
    (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");

// ...    
    
int iAttributes[] = { WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
    WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
    WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
    WGL_COLOR_BITS_ARB,24,
    WGL_ALPHA_BITS_ARB,8,
    WGL_DEPTH_BITS_ARB,16,
    WGL_STENCIL_BITS_ARB,0,
    WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
    WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
    WGL_SAMPLES_ARB, 4 ,                        // Check For 4x Multisampling
    0,0};

// First We Check To See If We Can Get A Pixel Format For 4 Samples
valid = wglChoosePixelFormatARB(hDC,iAttributes,fAttributes,1,&pixelFormat,&numFormats);

// if We Returned True, And Our Format Count Is Greater Than 1
if (valid && numFormats >= 1)
{
    arbMultisampleSupported = true;
    arbMultisampleFormat    = pixelFormat; 
    return arbMultisampleSupported;
}

3) If Multiple-Sampling support, then destroy this window and create it with this New PixelFormat;

4) For parts we want to antialias, simply call glEnable(GL_ARB_MULTISAMPLE);

glEnable(GL_MULTISAMPLE_ARB);
 
// Render The Scene
 
glDisable(GL_MULTISAMPLE_ARB);

 

The full source code could be found here.

转载于:https://www.cnblogs.com/open-coder/archive/2012/08/21/2648483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值