opengl绘制三维多边形如何反走样
在程序中加入了多重采样的代码,但是完全没有效果。需要设置显卡吗?
// 判断是否支持这个扩展
bool MultiSample::WGLisExtensionSupported(const char *extension)
{
const size_t extlen = strlen(extension);
const char *supported = NULL;
// 返回在WGL的扩展中查找是否支持特定的扩展
PROC wglGetExtString = wglGetProcAddress("wglGetExtensionsStringARB");
if (wglGetExtString)
supported = ((char*(__stdcall*)(HDC))wglGetExtString)(wglGetCurrentDC());
//在OpenGL的扩展中查找是否支持特定的扩展
if (supported == NULL)
supported = (char*)glGetString(GL_EXTENSIONS);
if (supported == NULL)
return false;
// 查找是否包含需要的扩展名
for (const char* p = supported; ; p++)
{
p = strstr(p, extension);
if (p == NULL)
return false;// No Match
if ((p==supported || p[-1]==' ') && (p[extlen]=='\0' || p[extlen]==' '))
return true;// Match
}
}
// 初始化多重渲染
bool MultiSample::InitMultiSample(CDC* cdc)
{
// 检测是否支持多重渲染
multisampleSupportted = WGLisExtensionSupported("WGL_ARB_multisample");
if(!multisampleSupportted)
return false;
// 返回wglChoosePixelFormatARB函数的入口
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
HDC hdc = cdc->GetSafeHdc();
int pixelFormat;
int valid;
UINT numFormats;
float fAttributes[] = {0,0};
//设置多重采样的像素格式
int iAtributes[] =
{
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, 8,
0,0
};
// 测试是否支持4个采样点的多重采样
valid = wglChoosePixelFormatARB(hdc, iAtributes, fAttributes, 1, &pixelFormat, &numFormats);
// 如果返回true并且numformats大于1,则表示成功,那么起用多重采样
if (valid && numFormats >= 1)
{
multisampleSupportted = true;
MultiSampleFormat = pixelFormat;
return multisampleSupportted;
}
// 测试是否支持2个采样点的多重采样
iAtributes[19] = 2;
// 如果返回true并且numformats大于1,则表示成功,那么起用多重采样
valid = wglChoosePixelFormatARB(hdc,iAtributes,fAttributes,1,&pixelFormat,&numFormats);
if (valid && numFormats >= 1)
{
multisampleSupportted = true;
MultiSampleFormat = pixelFormat;
return multisampleSupportted;
}
// 返回支持多重采样
return multisampleSupportted;
}
static PIXELFORMATDESCRIPTOR pfd=OpenGL 3D
{//像素点格式信息描述结构体
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |//支持Windows与OpenGL二环境绘图
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,//使用双缓存交换显示图形
PFD_TYPE_RGBA, // RGBA type
24,// 24-bit color depth
0, 0, 0, 0, 0, 0,// color bits ignored
0,// no alpha buffer
0,// shift bit ignored
0,// no accumulation buffer
0, 0, 0, 0,// accum bits ignored
16,// 32-bit z-buffer
0,// no stencil buffer
0,// no auxiliary buffer
PFD_MAIN_PLANE,// main layer
0,// reserved
0, 0, 0// layer masks ignored
};
MultiSample Multisam;
Multisam.InitMultiSample(pDC);
GLuint PixelFormat;
Multisam.GetMultiSampleFormat();
if(!Multisam.multisampleSupportted)
{
PixelFormat = ChoosePixelFormat (pDC->GetSafeHdc(), &pfd);
if (PixelFormat == 0)
{
return FALSE;
}
}
else
{
PixelFormat = Multisam.MultiSampleFormat;
}
if (SetPixelFormat (pDC->GetSafeHdc(), PixelFormat, &pfd) == FALSE)
{
return FALSE;
}
hglrc = wglCreateContext (pDC->GetSafeHdc());
if (hglrc == 0)
{
return FALSE;
}
if (wglMakeCurrent (pDC->GetSafeHdc(), hglrc) == FALSE)
{
return FALSE;
}