2016-01-20 102 views
0

我想在win32 API的OpenGL上下文中設置MSAA。一切工作正常,但MSAA只是不想激活。這裏是我的構建上下文代碼:,沒有的WndProc在WM_CREATE情況在OpenGL Win32上激活多重採樣

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後叫...什麼可能是錯誤的?

+0

你可以試試'glGetIntegerv(GL_SAMPLES,...)'仔細檢查你實際上得到了多采樣framebuffer?此外,你可以嘗試一個更普遍支持的值,如4樣本數。 「WGL_COLOR_BITS_ARB」的值應該是24(它只計算RGB位),但這可能沒有任何傷害。 –

+0

它返回一個值爲0的整數。 –

+0

我確定我沒有創建上下文,但是我找不到用wgl屬性創建上下文的示例。 –

回答

3

我看到你要求16個樣本的部分。但我沒有看到你啓用的部分 GL_MULTISAMPLE。如果沒有這些,渲染到多采樣緩衝區的行爲與渲染到單個採樣緩衝區的行爲沒有區別。

另外,我建議你使用framebuffer object作爲多重採樣渲染目標,而不是默認幀緩衝。是的,很高興默認的幀緩衝區可以通過窗口調整大小。但通過使用framebuffer對象,您可以控制何時multisampling is resolved

此外,它可以讓你保持駕駛員的討厭的控制面板選項與您的樣品數搞亂;)

+0

感謝您回答如此之快......我在最後的wglMakeCurrent之後添加了glEnable(GL_MULTISAMPLE)並且沒有任何更改... –

+0

GL_MULTISAMPLE'默認啓用。因此,除非它在早些時候被明確禁用,否則調用'glEnable(GL_MULTISAMPLE)'是一個無操作。 –