windows/mfc程序中使用OpenGL的多重采样功能

2022-08-26 12:44:02 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

windows/mfc程序中使用OpenGL的多重采样功能

学过opengl基础的人都知道,打开混合功能并调用glEnable(GL_POINT_SMOOTH)/glEnable(GL_LINE_SMOOTH)可以实现模型点/线的反走样功能,非常简单且效果不错。

但是对于实心多边形的支持却没有那么简单,单纯的调用 glEnable(GL_POLYGON_SMOOTH)发现并没有效果。

OpenGL对于多边形的反走样有另一个多重采样的特性支持——glEnable(GL_MULTISAMPLE) 但是当调用后发现还是不起作用,查阅资料后才发现在Windows程序中要想使用OpenGL的多重采样,需要额外的一些操作。

一般情况下MFC程序创建OpenGL环境

普通的Windows OpenGL程序在OnCreat()函数中完成环境初始化比较简单

代码语言:javascript复制
int OnCreate(LPCREATESTRUCT lpCreateStruct)
{ 
   
	CWnd::OnCreate(lpCreateStruct);
    
	PIXELFORMATDESCRIPTOR pfd;
	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL
		| PFD_DOUBLEBUFFER/* PFD_SUPPORT_GDI*/ | PFD_STEREO_DONTCARE;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;
	pfd.cRedBits = 8;
	pfd.cRedShift = 16;
	pfd.cGreenBits = 8;
	pfd.cGreenShift = 8;
	pfd.cBlueBits = 8;
	pfd.cBlueShift = 0;
	pfd.cAlphaBits = 0;
	pfd.cAlphaShift = 0;
	pfd.cAccumBits = 64;
	pfd.cAccumRedBits = 16;
	pfd.cAccumGreenBits = 16;
	pfd.cAccumBlueBits = 16;
	pfd.cAccumAlphaBits = 0;
	pfd.cDepthBits = 32;
	pfd.cStencilBits = 8;
	pfd.cAuxBuffers = 0;
	pfd.iLayerType = PFD_MAIN_PLANE;
	pfd.bReserved = 0;
	pfd.dwLayerMask = 0;
	pfd.dwVisibleMask = 0;
	pfd.dwDamageMask = 0;

	CDC* wnd_dc = GetDC();
	if (!wnd_dc) return -1;

	HDC dc_handle = wnd_dc->GetSafeHdc();
	int choosed_pixel_format = ChoosePixelFormat(dc_handle, &pfd);
	if (0 == choosed_pixel_format)
	{ 
   
		choosed_pixel_format = 1;
		if (0 == DescribePixelFormat(dc_handle, choosed_pixel_format, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
		{ 
   
			ReleaseDC(wnd_dc);
			return -1;
		}
	}

	if (FALSE == SetPixelFormat(dc_handle, choosed_pixel_format, &pfd))
	{ 
   
		ReleaseDC(wnd_dc);
		return -1;
	}

	HGLRC render_context = wglCreateContext(dc_handle);
	if (dc_handle && render_context && TRUE == wglMakeCurrent(dc_handle, render_context))
	{ 
   
		GLenum err = glewInit();
	}
	return 0;
}

如何使用OpenGL的多重采样

上面方式最主要的问题是PIXELFORMATDESCRIPTOR的数据结构是固定的,没有对多重采样的支持,要想使用此特性可以使用wglew库的wglChoosePixelFormatARB函数替换ChoosePixelFormat,但是使用wglChoosePixelFormatARB函数必须先调用glewInit()来初始化glew库,要初始化glew库则必须先得到窗口的渲染上下文,于是就必须先有一个临时窗口,但不能是真正的窗口。具体过程如下:

代码语言:javascript复制
int OnCreate(LPCREATESTRUCT lpCreateStruct)
{ 
   
	CWnd::OnCreate(lpCreateStruct);

    CWnd tempWnd;    
    tempWnd.Create(_T("STATIC"), _T("TMP"), WS_CHILD | WS_VISIBLE, CRect(), AfxGetMainWnd(), NULL);
    HDC tempHdc = tempWnd.GetDC()->GetSafeHdc();
    PIXELFORMATDESCRIPTOR pfd;
    if (!SetPixelFormat(tempHdc, 1, &pfd))		//每个窗口只能设置一次 
        return -1;
    HGLRC temphRC = wglCreateContext(tempHdc);  //创建一个临时的环境为了初始化glew
    wglMakeCurrent(tempHdc, temphRC);      
    GLenum err = glewInit();                    //初始化glew库,一个程序中初始化一次就可以,不需要每个opengl环境都初始化
    if (GLEW_OK != err) return -1;

    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(temphRC);
    tempWnd.DestroyWindow();    

    float fPixAttribs[] = { 
    0, 0 };
    int iPixAttribs[] = { 
    WGL_SUPPORT_OPENGL_ARB, GL_TRUE, // Must support OGL rendering
        WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, // pf that can run a window 
        WGL_ACCELERATION_ARB,
        WGL_FULL_ACCELERATION_ARB, // must be HW accelerated
        WGL_COLOR_BITS_ARB, 32, // 8 bits of each R, G and B
        WGL_DEPTH_BITS_ARB, 24, // 24 bits of depth precision for window
        WGL_DOUBLE_BUFFER_ARB, GL_TRUE, // Double buffered context
        WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, // pf should be RGBA type
        WGL_STENCIL_BITS_ARB, 8,//开启模板缓冲区,模板缓冲区位数=8
        WGL_SAMPLE_BUFFERS_ARB, GL_TRUE, // MSAA on,开启多重采样
        WGL_SAMPLES_ARB, 4, // 4x MSAA ,多重采样样本数量为4
        0 }; // NULL termination 

    CDC* wnd_dc = GetDC();
    if (!wnd_dc) return -1;

    HDC dc_handle = wnd_dc->GetSafeHdc();
    if (!wnd_dc) return -1;

    int nPixelFormat = -1;
    int nPixCount = 0;
    wglChoosePixelFormatARB(dc_handle, iPixAttribs, fPixAttribs, 1, &nPixelFormat, (UINT*)&nPixCount);//新的查询像素格式的函数 
    if (nPixelFormat == -1)    //多重采样时,如果硬件不支持就使用下面的代码关闭多重采样 
    { 
    // Try again without MSAA 
        iPixAttribs[19] = 1;
        wglChoosePixelFormatARB(dc_handle, iPixAttribs, fPixAttribs, 1, &nPixelFormat, (UINT*)&nPixCount);
    } // Got a format, now set it as the current one

    PIXELFORMATDESCRIPTOR pfd;
    if (!SetPixelFormat(dc_handle, nPixelFormat, &pfd))
        return -1;

    GLint attribs[] = { 
    WGL_CONTEXT_MAJOR_VERSION_ARB, 4,//主版本4
        WGL_CONTEXT_MINOR_VERSION_ARB, 3,//次版本号3
        WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
        0 };
    render_context = wglCreateContextAttribsARB(dc_handle, 0, attribs);
    if (m_gl_rendering_context == NULL)
    { 
   
        TRACE0("!!! Could not create an OpenGL 4.3 context.n");
        attribs[1] = 1;
        attribs[3] = 3;
        render_context = wglCreateContextAttribsARB(dc_handle, 0, attribs);
        if (render_context == NULL)
        { 
   
            TRACE0("!!! Could not create an OpenGL 1.3 context.n");
            return -1;
        }
    }

    if(!wglMakeCurrent(dc_handle, render_context))
        return -1;
}

如此初始化后,再调用 glEnable(GL_MULTISAMPLE)多重采样就起作用啦,效果非常明显:

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143672.html原文链接:https://javaforall.cn

0 人点赞