簡體   English   中英

在 OpenGL Win32 上激活多重采樣

[英]Activating Multisample on OpenGL Win32

我想在 win32 API 中的 OpenGL 上下文上設置 MSAA。 一切正常,但 MSAA 只是不想激活。 這是我用於構建上下文的代碼:

void Display::CreateGLContext(HWND hWND) {
mHDC = GetDC(hWND);  //get current windows device context

int nPixelFormat;

PIXELFORMATDESCRIPTOR pfd; // Create a new PIXELFORMATDESCRIPTOR (PFD)  
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); // Clear our  PFD  
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); // Set the size of the PFD to the size of the class  
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; // Enable double buffering, opengl support and drawing to a window  
pfd.iPixelType = PFD_TYPE_RGBA; // Set our application to use RGBA pixels  
pfd.cColorBits = 32; // Give us 32 bits of color information (the higher, the more colors)  
pfd.cDepthBits = 16; // Give us 32 bits of depth information (the higher, the more depth levels)  
pfd.iLayerType = PFD_MAIN_PLANE; // Set the layer of the PFD

/*      Choose best matching format*/
nPixelFormat = ChoosePixelFormat(mHDC, &pfd);

/*      Set the pixel format to the device context*/
SetPixelFormat(mHDC, nPixelFormat, &pfd);

HGLRC tempRC = wglCreateContext(mHDC);
wglMakeCurrent(mHDC, tempRC);

if (glewInit() != GLEW_OK) {
    MessageBox(mHWND, "Eroare", "glew", MB_OK);
}
int nPixelFormat2;

BOOL bValidPixFormat;
UINT nMaxFormats = 1;
UINT nNumFormats;
float pfAttribFList[] = { 0, 0 };
int piAttribIList[] = { 
    WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
    WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
    WGL_COLOR_BITS_ARB, 32,
    WGL_RED_BITS_ARB, 8,
    WGL_GREEN_BITS_ARB, 8,
    WGL_BLUE_BITS_ARB, 8,
    WGL_ALPHA_BITS_ARB, 8,
    WGL_DEPTH_BITS_ARB, 16,
    WGL_STENCIL_BITS_ARB, 0,
    WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
    WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
    WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
    WGL_SAMPLES_ARB, 16,
    0, 0 };

bValidPixFormat = wglChoosePixelFormatARB(mHDC, piAttribIList, pfAttribFList, nMaxFormats, &nPixelFormat2, &nNumFormats);

if (!bValidPixFormat)
{
    MessageBox(NULL, "Invalid Pixel Format", "Error! (SetupWGLPixelFormat)", MB_OK);
}

SetPixelFormat(mHDC, nPixelFormat2, &pfd);


mGLRenderContext = wglCreateContext(mHDC);

wglMakeCurrent(mHDC, NULL);
wglDeleteContext(tempRC);
wglMakeCurrent(mHDC, mGLRenderContext);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
}

代碼工作正常,它是在主類中創建 hWnd 之后調用的,而不是在 WM_CREATE 情況下的 WndProc 中調用......有什么問題?

我看到您要求提供 16 個樣品的部分。 但我沒有看到您啟用GL_MULTISAMPLE的部分。 沒有它,渲染到多采樣緩沖區與渲染到單個采樣緩沖區沒有什么不同。

另外,我建議您為多重采樣渲染目標使用幀緩沖區對象,而不是默認的幀緩沖區。 是的,默認幀緩沖區可以由窗口調整大小很好。 但是通過使用幀緩沖對象,您可以控制何時解析多重采樣

此外,它還允許您防止驅動程序討厭的控制面板選項干擾您的樣本計數;)

我找到了問題所在。 基本上你不能在本文提到的同一窗口上調用 SetPixelFormat: https ://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_(WGL)#Proper_Context_Creation

解決方案基本上是創建一個虛擬窗口(不可見)啟用opengl並刪除它。 我已經從這里復制了代碼,它對我有用 使用 WGL 創建現代 OpenGL 上下文?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM