我正在尝试在 x64 平台上创建一个 OpenGL 窗口。我的初始化代码适用于 x86/Win32,但在“wglMakeCurrent”处对 x64 失败。我猜问题出在像素格式的设置或获取DC(getDC())中。我已经为我的 PIXELFORMATDESCRIPTOR 尝试了不同的设置(假设 OpenGL 的 x64 实现不支持我正在使用的那个),但在那里没有成功。调试器表明 hdc 可能已损坏 - (类似于 0xfffffffff10102e0) - 但是 wglCreateContext 然后返回一个有效的外观 hglrc (即 0x0000000000010000)。但即使我将值从 hdc on-the-fly 更改为 0x0000000010102e0 (使用调试器,在调用 wglCreateContext 之前) wglMakeCurrent 仍然失败。
我在带有 Visual Studio 12 RC 1 的 Windows 8 上。有什么想法我在这里做错了吗?还是 x64 OpenGL 实现可能存在一些限制?
#include <windows.h>
#pragma comment(lib, "OpenGL32.lib")
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch (uMsg){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WindowProc;
wc.hInstance = nullptr;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClassTest";
RegisterClassEx(&wc);
DWORD dwStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
RECT WindowRect;
WindowRect.left = (long)0;
WindowRect.right = (long)640;
WindowRect.top = (long)0;
WindowRect.bottom = (long)480;
AdjustWindowRect(&WindowRect, dwStyle, FALSE);
HWND hWnd = CreateWindowEx(0,
"WindowClassTest",
"WindowTitle",
dwStyle,
0, 0,
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top,
nullptr,
nullptr,
wc.hInstance,
(LPVOID) nullptr);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
ShowWindow(hWnd, SW_SHOW);
SetFocus(hWnd);
HDC hdc = GetDC(hWnd);
int pf;
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pf = ChoosePixelFormat(hdc, &pfd);
if (pf == 0) {
MessageBox(NULL, "ChoosePixelFormat() failed: "
"Cannot find a suitable pixel format.", "Error", MB_OK);
}
if (SetPixelFormat(hdc, pf, &pfd) == FALSE) {
MessageBox(NULL, "SetPixelFormat() failed: "
"Cannot set format specified.", "Error", MB_OK);
}
DescribePixelFormat(hdc, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
HGLRC hglrc = wglCreateContext(hdc);
if (!wglMakeCurrent(hdc, hglrc)){
MessageBox(NULL, "wglMakeCurrent() failed: "
"Cannot make context current.", "Error", MB_OK);
}
/* left out other unnecessary code here*/
return 0;
}