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.

posted @   opencoder  阅读(594)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示