我已經能夠做到這一點,我不知道兩週前和最後一次的Windows更新之間有什麼變化,但由於某種原因,SetPixelFormat
沒有創建一個alpha通道。SetPixelFormat沒有爲OpenGL創建Alpha通道
gDebugger顯示窗口的後臺緩衝區只有3個通道。
白+ 0 alpha渲染爲白色。
因此,我正在做的事情或者更新破壞了內容。
下面的代碼應該粘貼到一個空的VS項目中。
#include <Windows.h>
#include <dwmapi.h>
#include <gl/GL.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"dwmapi.lib")
HWND hWnd = 0;
HDC hDC = 0;
HGLRC hRC = 0;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = WndProc;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.lpszClassName = TEXT("why_class");
RegisterClassEx(&wcex);
// no errors
hWnd = CreateWindowEx(
NULL,
TEXT("why_class"),
TEXT("why_window"),
WS_OVERLAPPEDWINDOW,
128,128,
256,256,
NULL,
NULL,
hInstance,
NULL
);
// no errors
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER|
PFD_SUPPORT_COMPOSITION;
pfd.cColorBits = 32;
pfd.cAlphaBits = 8; // need an alpha channel
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
hDC = GetDC(hWnd);
int i = ChoosePixelFormat(hDC,&pfd);
SetPixelFormat(hDC,i,&pfd);
// no errors
hRC = wglCreateContext(hDC);
// no errors
wglMakeCurrent(hDC,hRC);
// no errors
// EDIT: Turn on alpha testing (which actually won't
// fix the clear color problem below)
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
// EDIT: Regardless of whether or not GL_BLEND is enabled,
// a clear color with an alpha of 0 should (or did at one time)
// make this window transparent
glClearColor(
0,0,0, // if this is (1,1,1), the window renders
// solid white regardless of the alpha
0 // changing the alpha here has some effect
);
DWM_BLURBEHIND bb = {0};
bb.dwFlags = DWM_BB_ENABLE|DWM_BB_TRANSITIONONMAXIMIZED;
bb.fEnable = TRUE;
bb.fTransitionOnMaximized = TRUE;
DwmEnableBlurBehindWindow(hWnd,&bb);
// no errors
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
// no errors
MSG msg = {0};
while(true){
GetMessage(&msg,NULL,NULL,NULL);
if(msg.message == WM_QUIT){
return (int)msg.wParam;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
// this vertex should be transparent,
// as it was when I last built this test
//
// it renders as white
glColor4f(1,1,1,0);
glVertex2f(0,0);
glColor4f(0,1,1,1);
glVertex2f(1,0);
glColor4f(1,0,1,1);
glVertex2f(0,1);
glEnd();
SwapBuffers(hDC);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
}return 0;
default:
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
「白色+ 0 alpha呈現爲白色。」當然它呈現白色;你沒有打開混合。阿爾法並不意味着「透明」。這意味着任何你想要的意思。所以,如果你想alpha意味着「透明」,你必須_make_它意味着「透明」。一般通過使用混合模式。 –
我真的沒有注意到alpha測試不在這裏...我希望我的截圖能夠在之前工作,但我想我正在處理一個允許它以這種方式直接渲染到framebuffer的bug。我啓用了alpha測試,看看它是否可以工作,但是它並沒有影響頂點(它仍然呈現白色,現在只是略微透明)。然而,我的問題是爲什麼我的窗口突然沒有alpha通道 - 或者爲什麼我渲染的所有東西看起來好像windows正期待預乘alpha。 – defube
@NicolBolas:我認爲OP意味着用於窗口組成的alpha通道,即透明窗口。 (PFD_SUPPORT_COMPOSITION標誌設置)。對合成器如何處理alpha通道幾乎沒有影響,但通常認爲它是不透明的。一些合成器假設預倍頻阿爾法,其他則不。有關X11/GLX的等效示例,請參閱https://github.com/datenwolf/codesamples/tree/master/samples/OpenGL/x11argb_opengl_glsl。 – datenwolf